2017-08-13 13:44:48 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2022-08-25 08:15:09 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
|
|
|
|
from rest_framework import viewsets, views
|
2022-01-10 14:44:48 +00:00
|
|
|
from rest_framework.decorators import action
|
|
|
|
from rest_framework.response import Response
|
2022-09-05 21:00:03 +00:00
|
|
|
from rest_framework.schemas.openapi import AutoSchema
|
2017-08-13 13:44:48 +00:00
|
|
|
|
2017-11-10 02:15:09 +00:00
|
|
|
from core import models
|
2022-08-25 08:15:09 +00:00
|
|
|
from babybuddy import models as babybuddy_models
|
2017-08-16 22:33:02 +00:00
|
|
|
|
2021-10-31 13:13:38 +00:00
|
|
|
from . import serializers, filters
|
2017-08-13 14:48:16 +00:00
|
|
|
|
2022-02-27 19:36:31 +00:00
|
|
|
|
2022-05-27 22:39:25 +00:00
|
|
|
class BMIViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = models.BMI.objects.all()
|
|
|
|
serializer_class = serializers.BMISerializer
|
|
|
|
filterset_fields = ("child", "date")
|
2023-02-11 02:51:04 +00:00
|
|
|
ordering_fields = ("child", "date")
|
|
|
|
ordering = "-date"
|
2022-05-27 22:39:25 +00:00
|
|
|
|
|
|
|
def get_view_name(self):
|
|
|
|
"""
|
|
|
|
Gets the view name without changing the case of the model verbose name.
|
|
|
|
"""
|
|
|
|
name = models.BMI._meta.verbose_name
|
|
|
|
if self.suffix:
|
2022-05-27 23:17:53 +00:00
|
|
|
name += " " + self.suffix
|
2022-05-27 22:39:25 +00:00
|
|
|
return name
|
|
|
|
|
|
|
|
|
2017-08-16 12:49:58 +00:00
|
|
|
class ChildViewSet(viewsets.ModelViewSet):
|
2017-11-10 02:15:09 +00:00
|
|
|
queryset = models.Child.objects.all()
|
|
|
|
serializer_class = serializers.ChildSerializer
|
2022-02-10 00:00:30 +00:00
|
|
|
lookup_field = "slug"
|
|
|
|
filterset_fields = ("first_name", "last_name", "slug", "birth_date")
|
2023-02-11 02:51:04 +00:00
|
|
|
ordering_fields = ("birth_date", "first_name", "last_name", "slug")
|
|
|
|
ordering = "-birth_date"
|
2017-08-13 20:15:57 +00:00
|
|
|
|
2017-08-13 14:48:16 +00:00
|
|
|
|
2017-08-13 19:51:25 +00:00
|
|
|
class DiaperChangeViewSet(viewsets.ModelViewSet):
|
2017-11-10 02:15:09 +00:00
|
|
|
queryset = models.DiaperChange.objects.all()
|
|
|
|
serializer_class = serializers.DiaperChangeSerializer
|
2021-10-31 13:13:38 +00:00
|
|
|
filterset_class = filters.DiaperChangeFilter
|
2023-02-11 02:51:04 +00:00
|
|
|
ordering_fields = ("amount", "time")
|
|
|
|
ordering = "-time"
|
2017-08-13 20:15:57 +00:00
|
|
|
|
2017-08-13 19:51:25 +00:00
|
|
|
|
2022-06-06 13:47:57 +00:00
|
|
|
class FeedingViewSet(viewsets.ModelViewSet):
|
2017-11-10 02:15:09 +00:00
|
|
|
queryset = models.Feeding.objects.all()
|
|
|
|
serializer_class = serializers.FeedingSerializer
|
2021-10-31 13:34:45 +00:00
|
|
|
filterset_class = filters.FeedingFilter
|
2023-02-11 02:51:04 +00:00
|
|
|
ordering_fields = ("amount", "duration", "end", "start")
|
|
|
|
ordering = "-end"
|
2017-08-13 20:15:57 +00:00
|
|
|
|
2017-08-13 15:59:14 +00:00
|
|
|
|
2022-05-27 22:39:25 +00:00
|
|
|
class HeadCircumferenceViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = models.HeadCircumference.objects.all()
|
|
|
|
serializer_class = serializers.HeadCircumferenceSerializer
|
|
|
|
filterset_fields = ("child", "date")
|
2023-02-11 02:51:04 +00:00
|
|
|
ordering_fields = ("date", "head_circumference")
|
|
|
|
ordering = "-date"
|
2022-05-27 22:39:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HeightViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = models.Height.objects.all()
|
|
|
|
serializer_class = serializers.HeightSerializer
|
|
|
|
filterset_fields = ("child", "date")
|
2023-02-11 02:51:04 +00:00
|
|
|
ordering_fields = ("date", "height")
|
|
|
|
ordering = "-date"
|
2022-05-27 22:39:25 +00:00
|
|
|
|
|
|
|
|
2017-08-13 20:48:16 +00:00
|
|
|
class NoteViewSet(viewsets.ModelViewSet):
|
2017-11-10 02:15:09 +00:00
|
|
|
queryset = models.Note.objects.all()
|
|
|
|
serializer_class = serializers.NoteSerializer
|
2021-10-31 13:13:38 +00:00
|
|
|
filterset_class = filters.NoteFilter
|
2023-02-11 02:51:04 +00:00
|
|
|
ordering_fields = "time"
|
|
|
|
ordering = "-time"
|
2017-08-13 20:48:16 +00:00
|
|
|
|
|
|
|
|
2022-05-27 22:39:25 +00:00
|
|
|
class PumpingViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = models.Pumping.objects.all()
|
|
|
|
serializer_class = serializers.PumpingSerializer
|
|
|
|
filterset_class = filters.PumpingFilter
|
2023-02-11 02:51:04 +00:00
|
|
|
ordering_fields = ("amount", "time")
|
|
|
|
ordering = "-time"
|
2022-05-27 22:39:25 +00:00
|
|
|
|
|
|
|
|
2022-06-06 13:47:57 +00:00
|
|
|
class SleepViewSet(viewsets.ModelViewSet):
|
2017-11-10 02:15:09 +00:00
|
|
|
queryset = models.Sleep.objects.all()
|
|
|
|
serializer_class = serializers.SleepSerializer
|
2021-10-31 13:34:45 +00:00
|
|
|
filterset_class = filters.SleepFilter
|
2023-02-11 02:51:04 +00:00
|
|
|
ordering_fields = ("duration", "end", "start")
|
|
|
|
ordering = "-end"
|
2017-08-13 20:15:57 +00:00
|
|
|
|
2017-08-13 15:59:14 +00:00
|
|
|
|
2022-05-27 22:39:25 +00:00
|
|
|
class TagViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = models.Tag.objects.all()
|
|
|
|
serializer_class = serializers.TagSerializer
|
|
|
|
lookup_field = "slug"
|
|
|
|
filterset_fields = ("last_used", "name")
|
2023-02-11 02:51:04 +00:00
|
|
|
ordering_fields = ("last_used", "name", "slug")
|
|
|
|
ordering = "name"
|
2022-05-27 22:39:25 +00:00
|
|
|
|
|
|
|
|
2019-05-17 05:13:14 +00:00
|
|
|
class TemperatureViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = models.Temperature.objects.all()
|
|
|
|
serializer_class = serializers.TemperatureSerializer
|
2021-10-31 13:13:38 +00:00
|
|
|
filterset_class = filters.TemperatureFilter
|
2023-02-11 02:51:04 +00:00
|
|
|
ordering_fields = ("temperature", "time")
|
|
|
|
ordering = "-time"
|
2019-05-17 05:13:14 +00:00
|
|
|
|
|
|
|
|
2017-08-16 22:33:02 +00:00
|
|
|
class TimerViewSet(viewsets.ModelViewSet):
|
2017-11-10 02:15:09 +00:00
|
|
|
queryset = models.Timer.objects.all()
|
|
|
|
serializer_class = serializers.TimerSerializer
|
2021-10-31 13:34:45 +00:00
|
|
|
filterset_class = filters.TimerFilter
|
2023-02-11 02:51:04 +00:00
|
|
|
ordering_fields = ("duration", "end", "start")
|
|
|
|
ordering = "-start"
|
2017-08-16 22:33:02 +00:00
|
|
|
|
2022-02-10 00:00:30 +00:00
|
|
|
@action(detail=True, methods=["patch"])
|
2022-01-10 14:44:48 +00:00
|
|
|
def stop(self, request, pk=None):
|
2022-01-11 23:25:22 +00:00
|
|
|
timer = self.get_object()
|
|
|
|
timer.stop()
|
|
|
|
return Response(self.serializer_class(timer).data)
|
2022-01-10 14:44:48 +00:00
|
|
|
|
2022-02-10 00:00:30 +00:00
|
|
|
@action(detail=True, methods=["patch"])
|
2022-01-10 14:44:48 +00:00
|
|
|
def restart(self, request, pk=None):
|
2022-01-11 23:25:22 +00:00
|
|
|
timer = self.get_object()
|
|
|
|
timer.restart()
|
|
|
|
return Response(self.serializer_class(timer).data)
|
2022-01-10 14:44:48 +00:00
|
|
|
|
2017-08-16 22:33:02 +00:00
|
|
|
|
2022-06-06 13:47:57 +00:00
|
|
|
class TummyTimeViewSet(viewsets.ModelViewSet):
|
2017-11-10 02:15:09 +00:00
|
|
|
queryset = models.TummyTime.objects.all()
|
|
|
|
serializer_class = serializers.TummyTimeSerializer
|
2021-10-31 13:34:45 +00:00
|
|
|
filterset_class = filters.TummyTimeFilter
|
2023-02-11 02:51:04 +00:00
|
|
|
ordering_fields = ("duration", "end", "start")
|
|
|
|
ordering = "-start"
|
2017-11-10 02:15:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WeightViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = models.Weight.objects.all()
|
|
|
|
serializer_class = serializers.WeightSerializer
|
2022-02-10 00:00:30 +00:00
|
|
|
filterset_fields = ("child", "date")
|
2023-02-11 02:51:04 +00:00
|
|
|
ordering_fields = ("date", "weight")
|
|
|
|
ordering = "-date"
|
2022-08-25 08:15:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProfileView(views.APIView):
|
2022-09-05 21:00:03 +00:00
|
|
|
schema = AutoSchema(operation_id_base="CurrentProfile")
|
|
|
|
|
|
|
|
action = "get"
|
2022-08-25 08:15:09 +00:00
|
|
|
basename = "profile"
|
2022-09-05 21:00:03 +00:00
|
|
|
|
2022-08-25 08:15:09 +00:00
|
|
|
queryset = babybuddy_models.Settings.objects.all()
|
|
|
|
serializer_class = serializers.ProfileSerializer
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
settings = get_object_or_404(
|
2022-08-26 13:42:30 +00:00
|
|
|
babybuddy_models.Settings.objects, user=request.user
|
2022-08-25 08:15:09 +00:00
|
|
|
)
|
|
|
|
serializer = self.serializer_class(settings)
|
2022-08-26 13:42:30 +00:00
|
|
|
return Response(serializer.data)
|