2017-08-13 15:20:09 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-11-01 16:44:07 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2017-11-04 11:59:28 +00:00
|
|
|
from django.conf import settings
|
2017-11-01 16:44:07 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2017-08-11 18:32:02 +00:00
|
|
|
from django.db import models
|
2017-08-18 12:08:23 +00:00
|
|
|
from django.template.defaultfilters import slugify
|
2017-10-25 13:19:14 +00:00
|
|
|
from django.utils import timezone
|
2017-11-01 16:44:07 +00:00
|
|
|
|
|
|
|
|
2017-11-10 02:15:09 +00:00
|
|
|
def validate_date(date, field_name):
|
|
|
|
"""
|
|
|
|
Confirm that a date is not in the future.
|
|
|
|
:param date: a timezone aware date instance.
|
|
|
|
:param field_name: the name of the field being checked.
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if date and date > timezone.localdate():
|
|
|
|
raise ValidationError(
|
|
|
|
{field_name: 'Date can not be in the future.'},
|
|
|
|
code='date_invalid')
|
|
|
|
|
|
|
|
|
2017-11-01 16:44:07 +00:00
|
|
|
def validate_duration(model, max_duration=timedelta(hours=24)):
|
|
|
|
"""
|
|
|
|
Basic sanity checks for models with a duration
|
|
|
|
:param model: a model instance with 'start' and 'end' attributes
|
|
|
|
:param max_duration: maximum allowed duration between start and end time
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if model.start and model.end:
|
|
|
|
if model.start > model.end:
|
2017-11-01 20:14:42 +00:00
|
|
|
raise ValidationError(
|
|
|
|
'Start time must come before end time.',
|
|
|
|
code='end_before_start')
|
2017-11-01 16:44:07 +00:00
|
|
|
if model.end - model.start > max_duration:
|
2017-11-04 03:29:55 +00:00
|
|
|
raise ValidationError('Duration too long.', code='max_duration')
|
2017-08-11 18:32:02 +00:00
|
|
|
|
2017-08-13 14:48:16 +00:00
|
|
|
|
2017-11-05 19:18:30 +00:00
|
|
|
def validate_unique_period(queryset, model):
|
|
|
|
"""
|
|
|
|
Confirm that model's start and end date do not intersect with other
|
|
|
|
instances.
|
|
|
|
:param queryset: a queryset of instances to check against.
|
|
|
|
:param model: a model instance with 'start' and 'end' attributes
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if model.id:
|
|
|
|
queryset = queryset.exclude(id=model.id)
|
2017-11-07 12:07:51 +00:00
|
|
|
if model.start and model.end:
|
|
|
|
if queryset.filter(start__lte=model.end, end__gte=model.start):
|
|
|
|
raise ValidationError(
|
|
|
|
'Another entry intersects the specified time period.',
|
|
|
|
code='period_intersection')
|
2017-11-05 19:18:30 +00:00
|
|
|
|
|
|
|
|
2017-11-04 01:30:40 +00:00
|
|
|
def validate_time(time, field_name):
|
|
|
|
"""
|
|
|
|
Confirm that a time is not in the future.
|
|
|
|
:param time: a timezone aware datetime instance.
|
|
|
|
:param field_name: the name of the field being checked.
|
|
|
|
:return:
|
|
|
|
"""
|
2017-11-07 12:07:51 +00:00
|
|
|
if time and time > timezone.localtime():
|
2017-11-04 01:30:40 +00:00
|
|
|
raise ValidationError(
|
2017-11-04 03:29:55 +00:00
|
|
|
{field_name: 'Date/time can not be in the future.'},
|
2017-11-04 01:30:40 +00:00
|
|
|
code='time_invalid')
|
|
|
|
|
|
|
|
|
2017-08-16 12:49:58 +00:00
|
|
|
class Child(models.Model):
|
2017-09-15 16:29:56 +00:00
|
|
|
model_name = 'child'
|
2017-08-13 14:48:16 +00:00
|
|
|
first_name = models.CharField(max_length=255)
|
|
|
|
last_name = models.CharField(max_length=255)
|
|
|
|
birth_date = models.DateField(blank=False, null=False)
|
2017-08-18 12:08:23 +00:00
|
|
|
slug = models.SlugField(max_length=100, unique=True, editable=False)
|
2017-11-18 09:22:12 +00:00
|
|
|
picture = models.ImageField(
|
|
|
|
upload_to='child/picture/',
|
|
|
|
blank=True,
|
|
|
|
null=True
|
|
|
|
)
|
2017-08-13 14:48:16 +00:00
|
|
|
|
|
|
|
objects = models.Manager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
default_permissions = ('view', 'add', 'change', 'delete')
|
|
|
|
ordering = ['last_name', 'first_name']
|
2017-08-16 12:49:58 +00:00
|
|
|
verbose_name_plural = 'Children'
|
2017-08-13 14:48:16 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{} {}'.format(self.first_name, self.last_name)
|
|
|
|
|
2017-08-18 12:08:23 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.slug = slugify(self)
|
|
|
|
super(Child, self).save(*args, **kwargs)
|
|
|
|
|
2017-08-24 17:09:41 +00:00
|
|
|
def name(self, reverse=False):
|
|
|
|
if reverse:
|
|
|
|
return '{}, {}'.format(self.last_name, self.first_name)
|
2017-08-24 18:54:01 +00:00
|
|
|
|
|
|
|
return '{} {}'.format(self.first_name, self.last_name)
|
2017-08-24 17:09:41 +00:00
|
|
|
|
2017-08-13 14:48:16 +00:00
|
|
|
|
2017-08-13 19:51:25 +00:00
|
|
|
class DiaperChange(models.Model):
|
2017-09-15 16:29:56 +00:00
|
|
|
model_name = 'diaperchange'
|
2017-12-03 21:52:27 +00:00
|
|
|
child = models.ForeignKey(
|
|
|
|
'Child', related_name='diaper_change', on_delete=models.CASCADE)
|
2017-08-13 19:51:25 +00:00
|
|
|
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):
|
2017-10-25 12:58:15 +00:00
|
|
|
return 'Diaper Change'
|
2017-08-13 19:51:25 +00:00
|
|
|
|
2017-08-18 15:00:58 +00:00
|
|
|
def attributes(self):
|
|
|
|
attributes = []
|
|
|
|
if self.wet:
|
|
|
|
attributes.append(DiaperChange._meta.get_field('wet').name)
|
|
|
|
if self.solid:
|
|
|
|
attributes.append(DiaperChange._meta.get_field('solid').name)
|
|
|
|
if self.color:
|
|
|
|
attributes.append(self.color)
|
|
|
|
return attributes
|
|
|
|
|
2017-11-01 20:14:42 +00:00
|
|
|
def clean(self):
|
2017-11-04 01:30:40 +00:00
|
|
|
validate_time(self.time, 'time')
|
|
|
|
|
2017-11-01 20:14:42 +00:00
|
|
|
# One or both of Wet and Solid is required.
|
|
|
|
if not self.wet and not self.solid:
|
|
|
|
raise ValidationError(
|
|
|
|
'Wet and/or solid is required.', code='wet_or_solid')
|
|
|
|
|
|
|
|
# Color is required when Solid is selected.
|
|
|
|
if self.solid and not self.color:
|
|
|
|
raise ValidationError(
|
|
|
|
{'color': 'Color is required for solid diaper changes.'},
|
|
|
|
code='solid_color_required')
|
|
|
|
|
2017-08-13 19:51:25 +00:00
|
|
|
|
2017-08-13 15:59:14 +00:00
|
|
|
class Feeding(models.Model):
|
2017-09-15 16:29:56 +00:00
|
|
|
model_name = 'feeding'
|
2017-12-03 21:52:27 +00:00
|
|
|
child = models.ForeignKey(
|
|
|
|
'Child', related_name='feeding', on_delete=models.CASCADE)
|
2017-08-13 15:59:14 +00:00
|
|
|
start = models.DateTimeField(blank=False, null=False)
|
|
|
|
end = models.DateTimeField(blank=False, null=False)
|
2017-08-19 20:16:42 +00:00
|
|
|
duration = models.DurationField(null=True, editable=False)
|
2017-08-13 15:59:14 +00:00
|
|
|
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'),
|
|
|
|
])
|
2017-08-16 22:07:25 +00:00
|
|
|
amount = models.FloatField(blank=True, null=True)
|
2017-08-13 15:59:14 +00:00
|
|
|
|
|
|
|
objects = models.Manager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
default_permissions = ('view', 'add', 'change', 'delete')
|
|
|
|
ordering = ['-start']
|
|
|
|
|
|
|
|
def __str__(self):
|
2017-10-25 12:58:15 +00:00
|
|
|
return 'Feeding'
|
2017-08-13 15:59:14 +00:00
|
|
|
|
2017-08-19 20:16:42 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if self.start and self.end:
|
|
|
|
self.duration = self.end - self.start
|
|
|
|
super(Feeding, self).save(*args, **kwargs)
|
|
|
|
|
2017-11-01 16:44:07 +00:00
|
|
|
def clean(self):
|
2017-11-04 01:30:40 +00:00
|
|
|
validate_time(self.start, 'start')
|
|
|
|
validate_time(self.end, 'end')
|
2017-11-01 16:44:07 +00:00
|
|
|
validate_duration(self)
|
2017-11-05 19:18:30 +00:00
|
|
|
validate_unique_period(Feeding.objects.filter(child=self.child), self)
|
2017-11-01 16:44:07 +00:00
|
|
|
|
2017-11-01 20:14:42 +00:00
|
|
|
# "Formula" Type may only be associated with "Bottle" Method.
|
|
|
|
if self.type == 'formula'and self.method != 'bottle':
|
|
|
|
raise ValidationError(
|
|
|
|
{'method':
|
|
|
|
'Only "Bottle" method is allowed with "Formula" type.'},
|
|
|
|
code='bottle_formula_mismatch')
|
|
|
|
|
2017-08-13 15:59:14 +00:00
|
|
|
|
2017-08-13 20:48:16 +00:00
|
|
|
class Note(models.Model):
|
2017-09-15 16:29:56 +00:00
|
|
|
model_name = 'note'
|
2017-12-03 21:52:27 +00:00
|
|
|
child = models.ForeignKey(
|
|
|
|
'Child', related_name='note', on_delete=models.CASCADE)
|
2017-08-13 20:48:16 +00:00
|
|
|
note = models.TextField()
|
|
|
|
time = models.DateTimeField(auto_now=True)
|
|
|
|
|
|
|
|
objects = models.Manager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
default_permissions = ('view', 'add', 'change', 'delete')
|
|
|
|
ordering = ['-time']
|
|
|
|
|
|
|
|
def __str__(self):
|
2017-10-25 12:58:15 +00:00
|
|
|
return 'Note'
|
2017-08-13 20:48:16 +00:00
|
|
|
|
|
|
|
|
2017-11-04 16:51:49 +00:00
|
|
|
class NapsManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
qs = super(NapsManager, self).get_queryset()
|
|
|
|
return qs.filter(id__in=[obj.id for obj in qs if obj.nap])
|
|
|
|
|
|
|
|
|
2017-08-13 14:48:16 +00:00
|
|
|
class Sleep(models.Model):
|
2017-09-15 16:29:56 +00:00
|
|
|
model_name = 'sleep'
|
2017-12-03 21:52:27 +00:00
|
|
|
child = models.ForeignKey(
|
|
|
|
'Child', related_name='sleep', on_delete=models.CASCADE)
|
2017-08-13 14:48:16 +00:00
|
|
|
start = models.DateTimeField(blank=False, null=False)
|
|
|
|
end = models.DateTimeField(blank=False, null=False)
|
2017-08-19 20:16:42 +00:00
|
|
|
duration = models.DurationField(null=True, editable=False)
|
2017-08-13 14:48:16 +00:00
|
|
|
|
|
|
|
objects = models.Manager()
|
2017-11-04 16:51:49 +00:00
|
|
|
naps = NapsManager()
|
2017-08-13 14:48:16 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
default_permissions = ('view', 'add', 'change', 'delete')
|
|
|
|
ordering = ['-start']
|
|
|
|
verbose_name_plural = 'Sleep'
|
|
|
|
|
|
|
|
def __str__(self):
|
2017-10-25 12:58:15 +00:00
|
|
|
return 'Sleep'
|
2017-08-13 19:05:44 +00:00
|
|
|
|
2017-11-04 16:51:49 +00:00
|
|
|
@property
|
2017-11-04 11:59:28 +00:00
|
|
|
def nap(self):
|
|
|
|
nap_start_min = timezone.datetime.strptime(
|
|
|
|
settings.BABY_BUDDY['NAP_START_MIN'], '%H:%M').time()
|
|
|
|
nap_start_max = timezone.datetime.strptime(
|
|
|
|
settings.BABY_BUDDY['NAP_START_MAX'], '%H:%M').time()
|
|
|
|
local_start_time = timezone.localtime(self.start).time()
|
|
|
|
return nap_start_min <= local_start_time <= nap_start_max
|
|
|
|
|
2017-08-19 20:16:42 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if self.start and self.end:
|
|
|
|
self.duration = self.end - self.start
|
|
|
|
super(Sleep, self).save(*args, **kwargs)
|
|
|
|
|
2017-11-01 16:44:07 +00:00
|
|
|
def clean(self):
|
2017-11-04 01:30:40 +00:00
|
|
|
validate_time(self.start, 'start')
|
|
|
|
validate_time(self.end, 'end')
|
2017-11-01 16:44:07 +00:00
|
|
|
validate_duration(self)
|
2017-11-05 19:18:30 +00:00
|
|
|
validate_unique_period(Sleep.objects.filter(child=self.child), self)
|
2017-11-01 16:44:07 +00:00
|
|
|
|
2017-08-13 19:05:44 +00:00
|
|
|
|
2017-08-16 22:33:02 +00:00
|
|
|
class Timer(models.Model):
|
2017-09-15 16:29:56 +00:00
|
|
|
model_name = 'timer'
|
2017-08-18 02:52:41 +00:00
|
|
|
name = models.CharField(max_length=255, null=True, blank=True)
|
2017-10-28 17:27:33 +00:00
|
|
|
start = models.DateTimeField(
|
|
|
|
default=timezone.now,
|
|
|
|
blank=False,
|
|
|
|
verbose_name='Start Time'
|
|
|
|
)
|
2017-08-16 22:33:02 +00:00
|
|
|
end = models.DateTimeField(blank=True, null=True, editable=False)
|
2017-08-19 20:16:42 +00:00
|
|
|
duration = models.DurationField(null=True, editable=False)
|
2017-08-16 22:33:02 +00:00
|
|
|
active = models.BooleanField(default=True, editable=False)
|
2017-12-03 21:52:27 +00:00
|
|
|
user = models.ForeignKey(
|
|
|
|
'auth.User', related_name='timers', on_delete=models.CASCADE)
|
2017-08-16 22:33:02 +00:00
|
|
|
|
|
|
|
objects = models.Manager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
default_permissions = ('view', 'add', 'change', 'delete')
|
2017-08-18 05:56:23 +00:00
|
|
|
ordering = ['-active', '-start', '-end']
|
2017-08-16 22:33:02 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2017-08-18 02:52:41 +00:00
|
|
|
return self.name or 'Timer #{}'.format(self.id)
|
2017-08-16 22:33:02 +00:00
|
|
|
|
2017-10-28 01:33:26 +00:00
|
|
|
@classmethod
|
|
|
|
def from_db(cls, db, field_names, values):
|
|
|
|
instance = super(Timer, cls).from_db(db, field_names, values)
|
|
|
|
if not instance.duration:
|
|
|
|
instance.duration = timezone.now() - instance.start
|
|
|
|
return instance
|
2017-08-19 20:46:50 +00:00
|
|
|
|
2017-09-09 16:47:41 +00:00
|
|
|
def restart(self):
|
|
|
|
"""Restart the timer."""
|
|
|
|
self.start = timezone.now()
|
|
|
|
self.end = None
|
|
|
|
self.duration = None
|
|
|
|
self.active = True
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
def stop(self, end=None):
|
|
|
|
"""Stop the timer."""
|
|
|
|
if not self.end:
|
2017-09-09 17:00:13 +00:00
|
|
|
end = timezone.now()
|
2017-09-09 16:47:41 +00:00
|
|
|
self.end = end
|
|
|
|
self.save()
|
|
|
|
|
2017-08-16 22:33:02 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.active = self.end is None
|
2017-08-18 02:52:41 +00:00
|
|
|
self.name = self.name or None
|
2017-08-19 20:16:42 +00:00
|
|
|
if self.start and self.end:
|
|
|
|
self.duration = self.end - self.start
|
2017-10-28 16:43:43 +00:00
|
|
|
else:
|
|
|
|
self.duration = None
|
2017-08-16 22:33:02 +00:00
|
|
|
super(Timer, self).save(*args, **kwargs)
|
|
|
|
|
2017-11-01 16:44:07 +00:00
|
|
|
def clean(self):
|
2017-11-04 01:30:40 +00:00
|
|
|
validate_time(self.start, 'start')
|
|
|
|
if self.end:
|
|
|
|
validate_time(self.end, 'end')
|
2017-11-01 16:44:07 +00:00
|
|
|
validate_duration(self)
|
|
|
|
|
2017-08-16 22:33:02 +00:00
|
|
|
|
2017-08-13 19:05:44 +00:00
|
|
|
class TummyTime(models.Model):
|
2017-09-15 16:29:56 +00:00
|
|
|
model_name = 'tummytime'
|
2017-12-03 21:52:27 +00:00
|
|
|
child = models.ForeignKey(
|
|
|
|
'Child', related_name='tummy_time', on_delete=models.CASCADE)
|
2017-08-13 19:05:44 +00:00
|
|
|
start = models.DateTimeField(blank=False, null=False)
|
|
|
|
end = models.DateTimeField(blank=False, null=False)
|
2017-08-19 20:16:42 +00:00
|
|
|
duration = models.DurationField(null=True, editable=False)
|
2017-08-13 19:05:44 +00:00
|
|
|
milestone = models.CharField(max_length=255, blank=True)
|
|
|
|
|
|
|
|
objects = models.Manager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
default_permissions = ('view', 'add', 'change', 'delete')
|
|
|
|
ordering = ['-start']
|
|
|
|
|
|
|
|
def __str__(self):
|
2017-10-25 12:58:15 +00:00
|
|
|
return 'Tummy Time'
|
2017-08-13 19:05:44 +00:00
|
|
|
|
2017-08-19 20:16:42 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if self.start and self.end:
|
|
|
|
self.duration = self.end - self.start
|
|
|
|
super(TummyTime, self).save(*args, **kwargs)
|
2017-11-01 16:44:07 +00:00
|
|
|
|
|
|
|
def clean(self):
|
2017-11-04 01:30:40 +00:00
|
|
|
validate_time(self.start, 'start')
|
|
|
|
validate_time(self.end, 'end')
|
2017-11-01 16:44:07 +00:00
|
|
|
validate_duration(self)
|
2017-11-05 20:02:22 +00:00
|
|
|
validate_unique_period(
|
|
|
|
TummyTime.objects.filter(child=self.child), self)
|
2017-11-10 02:15:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Weight(models.Model):
|
|
|
|
model_name = 'weight'
|
2017-12-03 21:52:27 +00:00
|
|
|
child = models.ForeignKey(
|
|
|
|
'Child', related_name='weight', on_delete=models.CASCADE)
|
2017-11-10 02:15:09 +00:00
|
|
|
weight = models.FloatField(blank=False, null=False)
|
|
|
|
date = models.DateField(blank=False, null=False)
|
|
|
|
|
|
|
|
objects = models.Manager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
default_permissions = ('view', 'add', 'change', 'delete')
|
|
|
|
ordering = ['-date']
|
|
|
|
verbose_name_plural = 'Weight'
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'Weight'
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
validate_date(self.date, 'date')
|