mybuddy/api/serializers.py

215 lines
6.4 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from copy import deepcopy
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from django.contrib.auth.models import User
from django.utils import timezone
2022-02-15 09:13:35 +00:00
from taggit.serializers import (
TagListSerializerField, TaggitSerializer
)
2017-11-10 02:15:09 +00:00
from core import models
2017-08-13 14:48:16 +00:00
class CoreModelSerializer(serializers.HyperlinkedModelSerializer):
"""
Provide the child link (used by most core models) and run model clean()
methods during POST operations.
"""
2022-02-10 00:00:30 +00:00
child = serializers.PrimaryKeyRelatedField(queryset=models.Child.objects.all())
def validate(self, attrs):
# Ensure that all instance data is available for partial updates to
# support clean methods that compare multiple fields.
if self.partial:
new_instance = deepcopy(self.instance)
for attr, value in attrs.items():
setattr(new_instance, attr, value)
else:
new_instance = self.Meta.model(**attrs)
new_instance.clean()
return attrs
class CoreModelWithDurationSerializer(CoreModelSerializer):
"""
Specific serializer base for models with a "start" and "end" field.
"""
2022-02-10 00:00:30 +00:00
child = serializers.PrimaryKeyRelatedField(
2022-02-10 00:00:30 +00:00
allow_null=True,
allow_empty=True,
queryset=models.Child.objects.all(),
required=False,
)
class Meta:
abstract = True
extra_kwargs = {
2022-02-10 00:00:30 +00:00
"start": {"required": False},
"end": {"required": False},
}
def validate(self, attrs):
# Check for a special "timer" data argument that can be used in place
# of "start" and "end" fields as well as "child" if it is set on the
# Timer entry.
timer = None
2022-02-10 00:00:30 +00:00
if "timer" in self.initial_data:
try:
2022-02-10 00:00:30 +00:00
timer = models.Timer.objects.get(pk=self.initial_data["timer"])
except models.Timer.DoesNotExist:
2022-02-10 00:00:30 +00:00
raise ValidationError({"timer": ["Timer does not exist."]})
if timer.end:
end = timer.end
else:
end = timezone.now()
if timer.child:
2022-02-10 00:00:30 +00:00
attrs["child"] = timer.child
# Overwrites values provided directly!
2022-02-10 00:00:30 +00:00
attrs["start"] = timer.start
attrs["end"] = end
# The "child", "start", and "end" field should all be set at this
# point. If one is not, model validation will fail because they are
# required fields at the model level.
if not self.partial:
errors = {}
2022-02-10 00:00:30 +00:00
for field in ["child", "start", "end"]:
if field not in attrs or not attrs[field]:
2022-02-10 00:00:30 +00:00
errors[field] = "This field is required."
if len(errors) > 0:
raise ValidationError(errors)
attrs = super().validate(attrs)
# Only actually stop the timer if all validation passed.
if timer:
2022-02-10 00:00:30 +00:00
timer.stop(attrs["end"])
return attrs
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
2022-02-10 00:00:30 +00:00
fields = ("id", "username")
2017-08-16 12:49:58 +00:00
class ChildSerializer(serializers.HyperlinkedModelSerializer):
2017-08-13 14:48:16 +00:00
class Meta:
2017-11-10 02:15:09 +00:00
model = models.Child
2022-02-10 00:00:30 +00:00
fields = ("id", "first_name", "last_name", "birth_date", "slug", "picture")
lookup_field = "slug"
2017-08-13 14:48:16 +00:00
class DiaperChangeSerializer(CoreModelSerializer):
2017-08-13 19:51:25 +00:00
class Meta:
2017-11-10 02:15:09 +00:00
model = models.DiaperChange
2022-02-10 00:00:30 +00:00
fields = ("id", "child", "time", "wet", "solid", "color", "amount", "notes")
2017-08-13 19:51:25 +00:00
class FeedingSerializer(CoreModelWithDurationSerializer):
class Meta(CoreModelWithDurationSerializer.Meta):
2017-11-10 02:15:09 +00:00
model = models.Feeding
2022-02-10 00:00:30 +00:00
fields = (
"id",
"child",
"start",
"end",
"duration",
"type",
"method",
"amount",
"notes",
)
2017-08-13 15:59:14 +00:00
2022-02-15 09:13:35 +00:00
class NoteSerializer(TaggitSerializer, CoreModelSerializer):
2017-08-13 20:48:16 +00:00
class Meta:
2017-11-10 02:15:09 +00:00
model = models.Note
2022-02-15 09:13:35 +00:00
fields = ("id", "child", "note", "time", "tags")
tags = TagListSerializerField()
2017-08-13 20:48:16 +00:00
class SleepSerializer(CoreModelWithDurationSerializer):
class Meta(CoreModelWithDurationSerializer.Meta):
2017-11-10 02:15:09 +00:00
model = models.Sleep
2022-02-10 00:00:30 +00:00
fields = ("id", "child", "start", "end", "duration", "nap", "notes")
2017-08-13 15:59:14 +00:00
2019-05-17 05:13:14 +00:00
class TemperatureSerializer(CoreModelSerializer):
class Meta:
model = models.Temperature
2022-02-10 00:00:30 +00:00
fields = ("id", "child", "temperature", "time", "notes")
2019-05-17 05:13:14 +00:00
class TimerSerializer(CoreModelSerializer):
child = serializers.PrimaryKeyRelatedField(
2022-02-10 00:00:30 +00:00
allow_null=True,
allow_empty=True,
queryset=models.Child.objects.all(),
required=False,
)
user = serializers.PrimaryKeyRelatedField(
2022-02-10 00:00:30 +00:00
allow_null=True, allow_empty=True, queryset=User.objects.all(), required=False
)
class Meta:
2017-11-10 02:15:09 +00:00
model = models.Timer
2022-02-10 00:00:30 +00:00
fields = ("id", "child", "name", "start", "end", "duration", "active", "user")
def validate(self, attrs):
attrs = super(TimerSerializer, self).validate(attrs)
# Set user to current user if no value is provided.
2022-02-10 00:00:30 +00:00
if "user" not in attrs or attrs["user"] is None:
attrs["user"] = self.context["request"].user
return attrs
class TummyTimeSerializer(CoreModelWithDurationSerializer):
class Meta(CoreModelWithDurationSerializer.Meta):
2017-11-10 02:15:09 +00:00
model = models.TummyTime
2022-02-10 00:00:30 +00:00
fields = ("id", "child", "start", "end", "duration", "milestone")
2017-11-10 02:15:09 +00:00
class WeightSerializer(CoreModelSerializer):
2017-11-10 02:15:09 +00:00
class Meta:
model = models.Weight
2022-02-10 00:00:30 +00:00
fields = ("id", "child", "weight", "date", "notes")
2021-12-30 03:32:55 +00:00
class HeightSerializer(CoreModelSerializer):
class Meta:
model = models.Height
2022-02-10 00:00:30 +00:00
fields = ("id", "child", "height", "date", "notes")
2021-12-30 03:32:55 +00:00
class HeadCircumferenceSerializer(CoreModelSerializer):
class Meta:
model = models.HeadCircumference
2022-02-10 00:00:30 +00:00
fields = ("id", "child", "head_circumference", "date", "notes")
2021-12-30 03:32:55 +00:00
class BMISerializer(CoreModelSerializer):
class Meta:
model = models.BMI
2022-02-10 00:00:30 +00:00
fields = ("id", "child", "bmi", "date", "notes")
2022-02-15 21:24:13 +00:00
class TagsSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.BabyBuddyTag
fields = ("slug", "name", "color", "last_used")
extra_kwargs = {
"slug": {"required": False, "read_only": True},
"color": {"required": False},
"last_used": {"required": False, "read_only": True},
}