Add Timer model.

This will used for creating timers in the app and (eventually) applying them to entries during create operations.
This commit is contained in:
Christopher Charbonneau Wells 2017-08-16 18:33:02 -04:00
parent 79472dcdbe
commit 6b2de93736
6 changed files with 59 additions and 7 deletions

View File

@ -3,7 +3,8 @@ from __future__ import unicode_literals
from rest_framework import serializers
from core.models import Child, DiaperChange, Feeding, Note, Sleep, TummyTime
from core.models import (Child, DiaperChange, Feeding, Note, Sleep, Timer,
TummyTime)
class ChildSerializer(serializers.HyperlinkedModelSerializer):
@ -37,6 +38,12 @@ class SleepSerializer(serializers.HyperlinkedModelSerializer):
fields = ('baby', 'start', 'end', 'duration')
class TimerSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Timer
fields = ('name', 'start', 'end', 'duration', 'active')
class TummyTimeSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = TummyTime

View File

@ -5,7 +5,7 @@ from django.conf.urls import url, include
from rest_framework import routers
from .views import (ChildViewSet, DiaperChangeViewSet, FeedingViewSet,
NoteViewSet, SleepViewSet, TummyTimeViewSet)
NoteViewSet, SleepViewSet, TimerViewSet, TummyTimeViewSet)
router = routers.DefaultRouter()
router.register(r'children', ChildViewSet)
@ -13,6 +13,7 @@ router.register(r'diaper-changes', DiaperChangeViewSet)
router.register(r'feedings', FeedingViewSet)
router.register(r'notes', NoteViewSet)
router.register(r'sleep', SleepViewSet)
router.register(r'timers', TimerViewSet)
router.register(r'tummy-times', TummyTimeViewSet)
urlpatterns = [

View File

@ -3,11 +3,13 @@ from __future__ import unicode_literals
from rest_framework import viewsets
from core.models import Child, DiaperChange, Feeding, Note, Sleep, TummyTime
from core.models import (Child, DiaperChange, Feeding, Note, Sleep, Timer,
TummyTime)
from core.utils import filter_by_params
from .serializers import (ChildSerializer, DiaperChangeSerializer,
FeedingSerializer, NoteSerializer, SleepSerializer,
TummyTimeSerializer,)
TimerSerializer, TummyTimeSerializer,)
class ChildViewSet(viewsets.ModelViewSet):
@ -55,6 +57,15 @@ class SleepViewSet(viewsets.ModelViewSet):
return filter_by_params(self.request, Sleep, params)
class TimerViewSet(viewsets.ModelViewSet):
queryset = Timer.objects.all()
serializer_class = TimerSerializer
def get_queryset(self):
params = ['name', 'active']
return filter_by_params(self.request, Timer, params)
class TummyTimeViewSet(viewsets.ModelViewSet):
queryset = TummyTime.objects.all()
serializer_class = TummyTimeSerializer

View File

@ -3,7 +3,8 @@ from __future__ import unicode_literals
from django.contrib import admin
from .models import Child, DiaperChange, Feeding, Note, Sleep, TummyTime
from .models import (Child, DiaperChange, Feeding, Note, Sleep, Timer,
TummyTime)
@admin.register(Child)
@ -43,6 +44,13 @@ class SleepAdmin(admin.ModelAdmin):
search_fields = ('child__first_name', 'child__last_name',)
@admin.register(Timer)
class TimerAdmin(admin.ModelAdmin):
list_display = ('name', 'start', 'end', 'duration', 'active',)
list_filter = ('active',)
search_fields = ('name',)
@admin.register(TummyTime)
class TummyTimeAdmin(admin.ModelAdmin):
list_display = ('start', 'end', 'duration', 'child', 'milestone',)

View File

@ -2,6 +2,7 @@
from __future__ import unicode_literals
from django.db import models
from django.utils import timezone
from .utils import duration_string
@ -41,7 +42,8 @@ class DiaperChange(models.Model):
ordering = ['-time']
def __str__(self):
return 'Diaper change for {} on {}'.format(self.child, self.time.date())
return 'Diaper change for {} on {}'.format(
self.child, self.time.date())
class Feeding(models.Model):
@ -108,6 +110,29 @@ class Sleep(models.Model):
return duration_string(self.start, self.end)
class Timer(models.Model):
name = models.CharField(max_length=255, blank=True)
start = models.DateTimeField(auto_now=True)
end = models.DateTimeField(blank=True, null=True, editable=False)
active = models.BooleanField(default=True, editable=False)
objects = models.Manager()
class Meta:
default_permissions = ('view', 'add', 'change', 'delete')
ordering = ['active', '-start']
def __str__(self):
return 'Timer ({})'.format(self.name)
def duration(self):
return duration_string(self.start, self.end or timezone.now())
def save(self, *args, **kwargs):
self.active = self.end is None
super(Timer, self).save(*args, **kwargs)
class TummyTime(models.Model):
child = models.ForeignKey('Child', related_name='tummy_time')
start = models.DateTimeField(blank=False, null=False)

View File

@ -19,7 +19,7 @@
</head>
<body style="padding-top: 5rem;">
<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
<a class="navbar-brand" href="#"><span class="text-info">Baby</span> Blotter</a>
<a class="navbar-brand" href="/"><span class="text-info">Baby</span> Blotter</a>
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarsExampleDefault"
aria-controls="navbarsExampleDefault" aria-expanded="false"