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 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from rest_framework import viewsets, status
|
from rest_framework import viewsets
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
|
@ -51,39 +51,17 @@ class TimerViewSet(viewsets.ModelViewSet):
|
||||||
serializer_class = serializers.TimerSerializer
|
serializer_class = serializers.TimerSerializer
|
||||||
filterset_class = filters.TimerFilter
|
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'])
|
@action(detail=True, methods=['patch'])
|
||||||
def stop(self, request, pk=None):
|
def stop(self, request, pk=None):
|
||||||
def do_stop(timer):
|
timer = self.get_object()
|
||||||
if not timer.active:
|
|
||||||
return Response(
|
|
||||||
{"detail": "timer already stopped"},
|
|
||||||
status=status.HTTP_412_PRECONDITION_FAILED
|
|
||||||
)
|
|
||||||
timer.stop()
|
timer.stop()
|
||||||
return Response({"detail": "timer stopped"})
|
return Response(self.serializer_class(timer).data)
|
||||||
return self.__timer_operation(pk, do_stop)
|
|
||||||
|
|
||||||
@action(detail=True, methods=['patch'])
|
@action(detail=True, methods=['patch'])
|
||||||
def restart(self, request, pk=None):
|
def restart(self, request, pk=None):
|
||||||
def do_restart(timer):
|
timer = self.get_object()
|
||||||
if timer.active:
|
|
||||||
return Response(
|
|
||||||
{"detail": "timer already active"},
|
|
||||||
status=status.HTTP_412_PRECONDITION_FAILED
|
|
||||||
)
|
|
||||||
timer.restart()
|
timer.restart()
|
||||||
return Response({"detail": "timer restarted"})
|
return Response(self.serializer_class(timer).data)
|
||||||
return self.__timer_operation(pk, do_restart)
|
|
||||||
|
|
||||||
|
|
||||||
class TummyTimeViewSet(TimerFieldSupportMixin, viewsets.ModelViewSet):
|
class TummyTimeViewSet(TimerFieldSupportMixin, viewsets.ModelViewSet):
|
||||||
|
|
Loading…
Reference in New Issue