Add "timer" field information to OPTIONS (#131)

This commit is contained in:
Christopher C. Wells 2020-03-14 13:15:14 -07:00 committed by Christopher Charbonneau Wells
parent 3071050881
commit 7276dc331a
2 changed files with 31 additions and 3 deletions

27
api/mixins.py Normal file
View File

@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
from collections import OrderedDict
from rest_framework.response import Response
class TimerFieldSupportMixin:
def options(self, request, *args, **kwargs):
"""
Add information about the optional "timer" field.
"""
meta = self.metadata_class()
data = meta.determine_metadata(request, self)
post = data.get('actions').get('POST') # type: OrderedDict
post['timer'] = OrderedDict({
"type": "integer",
"required": False,
"read_only": False,
"label": "Timer",
"details": "ID for an existing Timer, may be used in place of the "
"`start`, `end`, and/or `child` fields. "
})
details = "Required unless a value is provided in the `timer` field."
post['child']['details'] = details
post['start']['details'] = details
post['end']['details'] = details
return Response(data)

View File

@ -4,6 +4,7 @@ from rest_framework import viewsets
from core import models
from . import serializers
from .mixins import TimerFieldSupportMixin
class ChildViewSet(viewsets.ModelViewSet):
@ -19,7 +20,7 @@ class DiaperChangeViewSet(viewsets.ModelViewSet):
filterset_fields = ('child', 'wet', 'solid', 'color', 'amount')
class FeedingViewSet(viewsets.ModelViewSet):
class FeedingViewSet(TimerFieldSupportMixin, viewsets.ModelViewSet):
queryset = models.Feeding.objects.all()
serializer_class = serializers.FeedingSerializer
filterset_fields = ('child', 'type', 'method')
@ -31,7 +32,7 @@ class NoteViewSet(viewsets.ModelViewSet):
filterset_fields = ('child',)
class SleepViewSet(viewsets.ModelViewSet):
class SleepViewSet(TimerFieldSupportMixin, viewsets.ModelViewSet):
queryset = models.Sleep.objects.all()
serializer_class = serializers.SleepSerializer
filterset_fields = ('child',)
@ -49,7 +50,7 @@ class TimerViewSet(viewsets.ModelViewSet):
filterset_fields = ('child', 'active', 'user')
class TummyTimeViewSet(viewsets.ModelViewSet):
class TummyTimeViewSet(TimerFieldSupportMixin, viewsets.ModelViewSet):
queryset = models.TummyTime.objects.all()
serializer_class = serializers.TummyTimeSerializer
filterset_fields = ('child',)