mirror of https://github.com/snachodog/mybuddy.git
Add Feeding model.
This commit is contained in:
parent
7e0b81960f
commit
481ada74f8
|
@ -4,13 +4,7 @@ from __future__ import unicode_literals
|
|||
from django.contrib.auth.models import User
|
||||
from rest_framework import serializers
|
||||
|
||||
from core.models import Baby, Sleep
|
||||
|
||||
|
||||
class UserSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ('url', 'username', 'email', 'is_staff')
|
||||
from core.models import Baby, Feeding, Sleep
|
||||
|
||||
|
||||
class BabySerializer(serializers.HyperlinkedModelSerializer):
|
||||
|
@ -19,7 +13,19 @@ class BabySerializer(serializers.HyperlinkedModelSerializer):
|
|||
fields = ('first_name', 'last_name', 'birth_date')
|
||||
|
||||
|
||||
class FeedingSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = Feeding
|
||||
fields = ('baby', 'start', 'end', 'duration', 'type', 'method')
|
||||
|
||||
|
||||
class SleepSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = Sleep
|
||||
fields = ('baby', 'start', 'end', 'duration')
|
||||
|
||||
|
||||
class UserSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ('url', 'username', 'email', 'is_staff')
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
from django.conf.urls import url, include
|
||||
from rest_framework import routers
|
||||
|
||||
from .views import UserViewSet, BabyViewSet, SleepViewSet
|
||||
from .views import BabyViewSet, FeedingViewSet, SleepViewSet, UserViewSet
|
||||
|
||||
# Routers provide an easy way of automatically determining the URL conf.
|
||||
router = routers.DefaultRouter()
|
||||
router.register(r'users', UserViewSet)
|
||||
router.register(r'babies', BabyViewSet)
|
||||
router.register(r'feedings', FeedingViewSet)
|
||||
router.register(r'sleep', SleepViewSet)
|
||||
router.register(r'users', UserViewSet)
|
||||
|
||||
# Wire up our API using automatic URL routing.
|
||||
# Additionally, we include login URLs for the browsable API.
|
||||
|
|
20
api/views.py
20
api/views.py
|
@ -4,13 +4,9 @@ from __future__ import unicode_literals
|
|||
from django.contrib.auth.models import User
|
||||
from rest_framework import viewsets
|
||||
|
||||
from core.models import Baby, Sleep
|
||||
from .serializers import (UserSerializer, BabySerializer, SleepSerializer)
|
||||
|
||||
|
||||
class UserViewSet(viewsets.ModelViewSet):
|
||||
queryset = User.objects.all()
|
||||
serializer_class = UserSerializer
|
||||
from core.models import Baby, Feeding, Sleep
|
||||
from .serializers import (BabySerializer, FeedingSerializer, SleepSerializer,
|
||||
UserSerializer,)
|
||||
|
||||
|
||||
class BabyViewSet(viewsets.ModelViewSet):
|
||||
|
@ -18,6 +14,16 @@ class BabyViewSet(viewsets.ModelViewSet):
|
|||
serializer_class = BabySerializer
|
||||
|
||||
|
||||
class FeedingViewSet(viewsets.ModelViewSet):
|
||||
queryset = Feeding.objects.all()
|
||||
serializer_class = FeedingSerializer
|
||||
|
||||
|
||||
class SleepViewSet(viewsets.ModelViewSet):
|
||||
queryset = Sleep.objects.all()
|
||||
serializer_class = SleepSerializer
|
||||
|
||||
|
||||
class UserViewSet(viewsets.ModelViewSet):
|
||||
queryset = User.objects.all()
|
||||
serializer_class = UserSerializer
|
||||
|
|
|
@ -3,7 +3,7 @@ from __future__ import unicode_literals
|
|||
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import Baby, Sleep
|
||||
from .models import Baby, Feeding, Sleep
|
||||
|
||||
|
||||
@admin.register(Baby)
|
||||
|
@ -12,6 +12,12 @@ class BabyAdmin(admin.ModelAdmin):
|
|||
search_fields = ('first_name', 'last_name', 'birth_date',)
|
||||
|
||||
|
||||
@admin.register(Feeding)
|
||||
class FeedingAdmin(admin.ModelAdmin):
|
||||
list_display = ('baby', 'start', 'end', 'duration', 'type', 'method',)
|
||||
search_fields = ('baby__first_name', 'baby__last_name', 'type', 'method')
|
||||
|
||||
|
||||
@admin.register(Sleep)
|
||||
class SleepAdmin(admin.ModelAdmin):
|
||||
list_display = ('baby', 'start', 'end', 'duration',)
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from datetime import datetime
|
||||
from math import floor
|
||||
|
||||
from django.db import models
|
||||
|
||||
from .utils import duration_string
|
||||
|
||||
|
||||
class Baby(models.Model):
|
||||
first_name = models.CharField(max_length=255)
|
||||
|
@ -23,6 +22,33 @@ class Baby(models.Model):
|
|||
return '{} {}'.format(self.first_name, self.last_name)
|
||||
|
||||
|
||||
class Feeding(models.Model):
|
||||
baby = models.ForeignKey('Baby', related_name='feeding')
|
||||
start = models.DateTimeField(blank=False, null=False)
|
||||
end = models.DateTimeField(blank=False, null=False)
|
||||
type = models.CharField(max_length=255, choices=[
|
||||
('breast milk', 'Breast milk'),
|
||||
('formula', 'Formula'),
|
||||
])
|
||||
method = models.CharField(max_length=255, choices=[
|
||||
('bottle', 'Bottle'),
|
||||
('left breast', 'Left breast'),
|
||||
('right breast', 'Right breast'),
|
||||
])
|
||||
|
||||
objects = models.Manager()
|
||||
|
||||
class Meta:
|
||||
default_permissions = ('view', 'add', 'change', 'delete')
|
||||
ordering = ['-start']
|
||||
|
||||
def __str__(self):
|
||||
return '{} fed for {}'.format(self.baby, self.duration())
|
||||
|
||||
def duration(self):
|
||||
return duration_string(self.start, self.end)
|
||||
|
||||
|
||||
class Sleep(models.Model):
|
||||
baby = models.ForeignKey('Baby', related_name='sleep')
|
||||
start = models.DateTimeField(blank=False, null=False)
|
||||
|
@ -36,32 +62,7 @@ class Sleep(models.Model):
|
|||
verbose_name_plural = 'Sleep'
|
||||
|
||||
def __str__(self):
|
||||
return '{} slept for {}'.format(
|
||||
self.baby,
|
||||
self.duration()
|
||||
)
|
||||
return '{} slept for {}'.format(self.baby, self.duration())
|
||||
|
||||
def duration(self):
|
||||
diff = self.end - self.start
|
||||
if diff.seconds < 60:
|
||||
duration = '{} second{}'.format(
|
||||
diff.seconds,
|
||||
's' if diff.seconds > 1 else ''
|
||||
)
|
||||
elif diff.seconds < 3600:
|
||||
duration = '{} minute{}, {} second{}'.format(
|
||||
floor(diff.seconds / 60),
|
||||
's' if floor(diff.seconds / 60) > 1 else '',
|
||||
diff.seconds % 60,
|
||||
's' if diff.seconds % 60 > 1 else ''
|
||||
)
|
||||
else:
|
||||
duration = '{} hour{}, {} minute{}, {} second{}'.format(
|
||||
floor(diff.seconds / 3600),
|
||||
's' if floor(diff.seconds / 3600) > 1 else '',
|
||||
floor((diff.seconds - 3600) / 60),
|
||||
's' if floor((diff.seconds - 3600) / 60) > 1 else '',
|
||||
diff.seconds % 60,
|
||||
's' if diff.seconds % 60 > 1 else ''
|
||||
)
|
||||
return duration
|
||||
return duration_string(self.start, self.end)
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from math import floor
|
||||
|
||||
|
||||
def duration_string(start, end):
|
||||
diff = end - start
|
||||
if diff.seconds < 60:
|
||||
duration = '{} second{}'.format(
|
||||
diff.seconds,
|
||||
's' if diff.seconds > 1 else ''
|
||||
)
|
||||
elif diff.seconds < 3600:
|
||||
duration = '{} minute{}, {} second{}'.format(
|
||||
floor(diff.seconds / 60),
|
||||
's' if floor(diff.seconds / 60) > 1 else '',
|
||||
diff.seconds % 60,
|
||||
's' if diff.seconds % 60 > 1 else ''
|
||||
)
|
||||
else:
|
||||
duration = '{} hour{}, {} minute{}, {} second{}'.format(
|
||||
floor(diff.seconds / 3600),
|
||||
's' if floor(diff.seconds / 3600) > 1 else '',
|
||||
floor((diff.seconds - 3600) / 60),
|
||||
's' if floor((diff.seconds - 3600) / 60) > 1 else '',
|
||||
diff.seconds % 60,
|
||||
's' if diff.seconds % 60 > 1 else ''
|
||||
)
|
||||
return duration
|
BIN
db.sqlite3
BIN
db.sqlite3
Binary file not shown.
Loading…
Reference in New Issue