Add DiaperChange model.

This commit is contained in:
Christopher Charbonneau Wells 2017-08-13 15:51:25 -04:00
parent f5d91affcf
commit 105adec971
5 changed files with 48 additions and 7 deletions

View File

@ -4,7 +4,7 @@ from __future__ import unicode_literals
from django.contrib.auth.models import User from django.contrib.auth.models import User
from rest_framework import serializers from rest_framework import serializers
from core.models import Baby, Feeding, Sleep, TummyTime from core.models import Baby, DiaperChange, Feeding, Sleep, TummyTime
class BabySerializer(serializers.HyperlinkedModelSerializer): class BabySerializer(serializers.HyperlinkedModelSerializer):
@ -13,6 +13,12 @@ class BabySerializer(serializers.HyperlinkedModelSerializer):
fields = ('first_name', 'last_name', 'birth_date') fields = ('first_name', 'last_name', 'birth_date')
class DiaperChangeSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = DiaperChange
fields = ('baby', 'time', 'wet', 'solid', 'color')
class FeedingSerializer(serializers.HyperlinkedModelSerializer): class FeedingSerializer(serializers.HyperlinkedModelSerializer):
class Meta: class Meta:
model = Feeding model = Feeding

View File

@ -1,15 +1,16 @@
from django.conf.urls import url, include from django.conf.urls import url, include
from rest_framework import routers from rest_framework import routers
from .views import (BabyViewSet, FeedingViewSet, SleepViewSet, TummyTimeViewSet, from .views import (BabyViewSet, DiaperChangeViewSet, FeedingViewSet,
UserViewSet) SleepViewSet, TummyTimeViewSet, UserViewSet)
# Routers provide an easy way of automatically determining the URL conf. # Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r'babies', BabyViewSet) router.register(r'babies', BabyViewSet)
router.register(r'diaper-changes', DiaperChangeViewSet)
router.register(r'feedings', FeedingViewSet) router.register(r'feedings', FeedingViewSet)
router.register(r'sleep', SleepViewSet) router.register(r'sleep', SleepViewSet)
router.register(r'sleep', SleepViewSet) router.register(r'tummy-times', TummyTimeViewSet)
router.register(r'users', UserViewSet) router.register(r'users', UserViewSet)
# Wire up our API using automatic URL routing. # Wire up our API using automatic URL routing.

View File

@ -4,8 +4,9 @@ from __future__ import unicode_literals
from django.contrib.auth.models import User from django.contrib.auth.models import User
from rest_framework import viewsets from rest_framework import viewsets
from core.models import Baby, Feeding, Sleep, TummyTime from core.models import Baby, DiaperChange, Feeding, Sleep, TummyTime
from .serializers import (BabySerializer, FeedingSerializer, SleepSerializer, from .serializers import (BabySerializer, DiaperChangeSerializer,
FeedingSerializer, SleepSerializer,
TummyTimeSerializer, UserSerializer,) TummyTimeSerializer, UserSerializer,)
@ -14,6 +15,11 @@ class BabyViewSet(viewsets.ModelViewSet):
serializer_class = BabySerializer serializer_class = BabySerializer
class DiaperChangeViewSet(viewsets.ModelViewSet):
queryset = DiaperChange.objects.all()
serializer_class = DiaperChangeSerializer
class FeedingViewSet(viewsets.ModelViewSet): class FeedingViewSet(viewsets.ModelViewSet):
queryset = Feeding.objects.all() queryset = Feeding.objects.all()
serializer_class = FeedingSerializer serializer_class = FeedingSerializer

View File

@ -3,7 +3,7 @@ from __future__ import unicode_literals
from django.contrib import admin from django.contrib import admin
from .models import Baby, Feeding, Sleep, TummyTime from .models import Baby, DiaperChange, Feeding, Sleep, TummyTime
@admin.register(Baby) @admin.register(Baby)
@ -12,6 +12,12 @@ class BabyAdmin(admin.ModelAdmin):
search_fields = ('first_name', 'last_name', 'birth_date',) search_fields = ('first_name', 'last_name', 'birth_date',)
@admin.register(DiaperChange)
class DiaperChangeAdmin(admin.ModelAdmin):
list_display = ('baby', 'time', 'wet', 'solid', 'color')
search_fields = ('baby__first_name', 'baby__last_name',)
@admin.register(Feeding) @admin.register(Feeding)
class FeedingAdmin(admin.ModelAdmin): class FeedingAdmin(admin.ModelAdmin):
list_display = ('start', 'end', 'duration', 'baby', 'type', 'method',) list_display = ('start', 'end', 'duration', 'baby', 'type', 'method',)

View File

@ -22,6 +22,28 @@ class Baby(models.Model):
return '{} {}'.format(self.first_name, self.last_name) return '{} {}'.format(self.first_name, self.last_name)
class DiaperChange(models.Model):
baby = models.ForeignKey('Baby', related_name='diaper_change')
time = models.DateTimeField(blank=False, null=False)
wet = models.BooleanField()
solid = models.BooleanField()
color = models.CharField(max_length=255, blank=True, choices=[
('black', 'Black'),
('brown', 'Brown'),
('green', 'Green'),
('yellow', 'Yellow'),
])
objects = models.Manager()
class Meta:
default_permissions = ('view', 'add', 'change', 'delete')
ordering = ['-time']
def __str__(self):
return 'Diaper change for {} on {}'.format(self.baby, self.time.date())
class Feeding(models.Model): class Feeding(models.Model):
baby = models.ForeignKey('Baby', related_name='feeding') baby = models.ForeignKey('Baby', related_name='feeding')
start = models.DateTimeField(blank=False, null=False) start = models.DateTimeField(blank=False, null=False)