mirror of https://github.com/snachodog/mybuddy.git
Way simpler implementation of the new endpoints
Signed-off-by: Paul Konstantin Gerke <paulkgerke@craftware.info>
This commit is contained in:
parent
53d4759add
commit
c1a0abd1f7
32
api/views.py
32
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 = self.get_object()
|
||||
timer.stop()
|
||||
return Response({"detail": "timer stopped"})
|
||||
return self.__timer_operation(pk, do_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 = self.get_object()
|
||||
timer.restart()
|
||||
return Response({"detail": "timer restarted"})
|
||||
return self.__timer_operation(pk, do_restart)
|
||||
return Response(self.serializer_class(timer).data)
|
||||
|
||||
|
||||
class TummyTimeViewSet(TimerFieldSupportMixin, viewsets.ModelViewSet):
|
||||
|
|
Loading…
Reference in New Issue