diff --git a/api/views.py b/api/views.py index dcf05b2c..a7a47feb 100644 --- a/api/views.py +++ b/api/views.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from rest_framework import viewsets, status +from rest_framework import viewsets from rest_framework.decorators import action from rest_framework.response import Response @@ -51,39 +51,17 @@ class TimerViewSet(viewsets.ModelViewSet): serializer_class = serializers.TimerSerializer filterset_class = filters.TimerFilter - def __timer_operation(self, pk, func): - try: - timer = models.Timer.objects.get(pk=pk) - return func(timer) - except models.Timer.DoesNotExist: - return Response( - {"detail": "timer does not exist"}, - status=status.HTTP_404_NOT_FOUND - ) - @action(detail=True, methods=['patch']) def stop(self, request, pk=None): - def do_stop(timer): - if not timer.active: - return Response( - {"detail": "timer already stopped"}, - status=status.HTTP_412_PRECONDITION_FAILED - ) - timer.stop() - return Response({"detail": "timer stopped"}) - return self.__timer_operation(pk, do_stop) + timer = self.get_object() + timer.stop() + return Response(self.serializer_class(timer).data) @action(detail=True, methods=['patch']) def restart(self, request, pk=None): - def do_restart(timer): - if timer.active: - return Response( - {"detail": "timer already active"}, - status=status.HTTP_412_PRECONDITION_FAILED - ) - timer.restart() - return Response({"detail": "timer restarted"}) - return self.__timer_operation(pk, do_restart) + timer = self.get_object() + timer.restart() + return Response(self.serializer_class(timer).data) class TummyTimeViewSet(TimerFieldSupportMixin, viewsets.ModelViewSet):