Add a TummyTime model.

This commit is contained in:
Christopher Charbonneau Wells 2017-08-13 15:05:44 -04:00
parent 2b062050fc
commit 9422d73421
6 changed files with 45 additions and 6 deletions

View File

@ -4,7 +4,7 @@ from __future__ import unicode_literals
from django.contrib.auth.models import User
from rest_framework import serializers
from core.models import Baby, Feeding, Sleep
from core.models import Baby, Feeding, Sleep, TummyTime
class BabySerializer(serializers.HyperlinkedModelSerializer):
@ -25,6 +25,12 @@ class SleepSerializer(serializers.HyperlinkedModelSerializer):
fields = ('baby', 'start', 'end', 'duration')
class TummyTimeSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = TummyTime
fields = ('baby', 'start', 'end', 'duration', 'milestone')
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User

View File

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

View File

@ -4,9 +4,9 @@ from __future__ import unicode_literals
from django.contrib.auth.models import User
from rest_framework import viewsets
from core.models import Baby, Feeding, Sleep
from core.models import Baby, Feeding, Sleep, TummyTime
from .serializers import (BabySerializer, FeedingSerializer, SleepSerializer,
UserSerializer,)
TummyTimeSerializer, UserSerializer,)
class BabyViewSet(viewsets.ModelViewSet):
@ -24,6 +24,11 @@ class SleepViewSet(viewsets.ModelViewSet):
serializer_class = SleepSerializer
class TummyTimeViewSet(viewsets.ModelViewSet):
queryset = TummyTime.objects.all()
serializer_class = TummyTimeSerializer
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer

View File

@ -3,7 +3,7 @@ from __future__ import unicode_literals
from django.contrib import admin
from .models import Baby, Feeding, Sleep
from .models import Baby, Feeding, Sleep, TummyTime
@admin.register(Baby)
@ -15,10 +15,16 @@ class BabyAdmin(admin.ModelAdmin):
@admin.register(Feeding)
class FeedingAdmin(admin.ModelAdmin):
list_display = ('start', 'end', 'duration', 'baby', 'type', 'method',)
search_fields = ('baby__first_name', 'baby__last_name', 'type', 'method')
search_fields = ('baby__first_name', 'baby__last_name', 'type', 'method',)
@admin.register(Sleep)
class SleepAdmin(admin.ModelAdmin):
list_display = ('start', 'end', 'duration', 'baby',)
search_fields = ('baby__first_name', 'baby__last_name',)
@admin.register(TummyTime)
class TummyTimeAdmin(admin.ModelAdmin):
list_display = ('start', 'end', 'duration', 'baby', 'milestone',)
search_fields = ('baby__first_name', 'baby__last_name', 'milestone',)

View File

@ -68,3 +68,23 @@ class Sleep(models.Model):
def duration(self):
return duration_string(self.start, self.end)
class TummyTime(models.Model):
baby = models.ForeignKey('Baby', related_name='tummy_time')
start = models.DateTimeField(blank=False, null=False)
end = models.DateTimeField(blank=False, null=False)
milestone = models.CharField(max_length=255, blank=True)
objects = models.Manager()
class Meta:
default_permissions = ('view', 'add', 'change', 'delete')
ordering = ['-start']
def __str__(self):
return 'Tummy time for {} on {} ({})'.format(
self.baby, self.end.date(), self.duration())
def duration(self):
return duration_string(self.start, self.end)

Binary file not shown.