mybuddy/core/models.py

569 lines
16 KiB
Python
Raw Normal View History

2017-08-13 15:20:09 +00:00
# -*- coding: utf-8 -*-
from datetime import timedelta
from django.conf import settings
from django.core.cache import cache
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
from django.utils import timezone
from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy as _
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.')},
2017-11-10 02:15:09 +00:00
code='date_invalid')
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.'),
2017-11-01 20:14:42 +00:00
code='end_before_start')
if model.end - model.start > max_duration:
raise ValidationError(_('Duration too long.'), code='max_duration')
2017-08-11 18:32:02 +00:00
2017-08-13 14:48:16 +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)
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')
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:
"""
if time and time > timezone.localtime():
raise ValidationError(
{field_name: _('Date/time can not be in the future.')},
code='time_invalid')
2017-08-16 12:49:58 +00:00
class Child(models.Model):
model_name = 'child'
first_name = models.CharField(max_length=255, verbose_name=_('First name'))
last_name = models.CharField(max_length=255, verbose_name=_('Last name'))
birth_date = models.DateField(
blank=False,
null=False,
verbose_name=_('Birth date')
)
slug = models.SlugField(
editable=False,
max_length=100,
unique=True,
verbose_name=_('Slug')
)
picture = models.ImageField(
blank=True,
null=True,
upload_to='child/picture/',
verbose_name=_('Picture')
)
2017-08-13 14:48:16 +00:00
objects = models.Manager()
cache_key_count = 'core.child.count'
2017-08-13 14:48:16 +00:00
class Meta:
default_permissions = ('view', 'add', 'change', 'delete')
ordering = ['last_name', 'first_name']
verbose_name = _('Child')
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)
cache.set(self.cache_key_count, Child.objects.count(), None)
def delete(self, using=None, keep_parents=False):
super(Child, self).delete(using, keep_parents)
cache.set(self.cache_key_count, Child.objects.count(), None)
2017-08-18 12:08:23 +00:00
def name(self, reverse=False):
if reverse:
return '{}, {}'.format(self.last_name, self.first_name)
return '{} {}'.format(self.first_name, self.last_name)
@classmethod
def count(cls):
""" Get a (cached) count of total number of Child instances. """
return cache.get_or_set(cls.cache_key_count, Child.objects.count, None)
2017-08-13 14:48:16 +00:00
2017-08-13 19:51:25 +00:00
class DiaperChange(models.Model):
model_name = 'diaperchange'
2017-12-03 21:52:27 +00:00
child = models.ForeignKey(
'Child',
on_delete=models.CASCADE,
related_name='diaper_change',
verbose_name=_('Child')
)
2019-04-19 02:55:55 +00:00
time = models.DateTimeField(
blank=False,
null=False,
verbose_name=_('Time')
)
wet = models.BooleanField(verbose_name=_('Wet'))
solid = models.BooleanField(verbose_name=_('Solid'))
color = models.CharField(
blank=True,
choices=[
('black', _('Black')),
('brown', _('Brown')),
('green', _('Green')),
('yellow', _('Yellow')),
],
max_length=255,
verbose_name=_('Color')
)
amount = models.FloatField(blank=True, null=True, verbose_name=_('Amount'))
2017-08-13 19:51:25 +00:00
objects = models.Manager()
class Meta:
default_permissions = ('view', 'add', 'change', 'delete')
ordering = ['-time']
verbose_name = _('Diaper Change')
verbose_name_plural = _('Diaper Changes')
2017-08-13 19:51:25 +00:00
def __str__(self):
return str(_('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(self._meta.get_field('wet').verbose_name)
2017-08-18 15:00:58 +00:00
if self.solid:
attributes.append(self._meta.get_field('solid').verbose_name)
2017-08-18 15:00:58 +00:00
if self.color:
attributes.append(self.get_color_display())
2017-08-18 15:00:58 +00:00
return attributes
2017-11-01 20:14:42 +00:00
def clean(self):
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')
2017-11-01 20:14:42 +00:00
2017-08-13 19:51:25 +00:00
2017-08-13 15:59:14 +00:00
class Feeding(models.Model):
model_name = 'feeding'
2017-12-03 21:52:27 +00:00
child = models.ForeignKey(
'Child',
on_delete=models.CASCADE,
related_name='feeding',
verbose_name=_('Child')
)
start = models.DateTimeField(
blank=False,
null=False,
verbose_name=_('Start time')
)
end = models.DateTimeField(
blank=False,
null=False,
verbose_name=_('End time')
)
duration = models.DurationField(
editable=False,
null=True,
verbose_name=_('Duration')
)
type = models.CharField(
choices=[
('breast milk', _('Breast milk')),
('formula', _('Formula')),
('fortified breast milk', _('Fortified breast milk')),
],
max_length=255,
verbose_name=_('Type')
)
method = models.CharField(
choices=[
('bottle', _('Bottle')),
('left breast', _('Left breast')),
('right breast', _('Right breast')),
('both breasts', _('Both breasts')),
],
max_length=255,
verbose_name=_('Method')
)
amount = models.FloatField(blank=True, null=True, verbose_name=_('Amount'))
2017-08-13 15:59:14 +00:00
objects = models.Manager()
class Meta:
default_permissions = ('view', 'add', 'change', 'delete')
ordering = ['-start']
verbose_name = _('Feeding')
verbose_name_plural = _('Feedings')
2017-08-13 15:59:14 +00:00
def __str__(self):
return str(_('Feeding'))
2017-08-13 15:59:14 +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)
def clean(self):
validate_time(self.start, 'start')
validate_time(self.end, 'end')
validate_duration(self)
validate_unique_period(Feeding.objects.filter(child=self.child), self)
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':
2017-11-01 20:14:42 +00:00
raise ValidationError(
{'method':
_('Only "Bottle" method is allowed with "Formula" type.')},
2017-11-01 20:14:42 +00:00
code='bottle_formula_mismatch')
2017-08-13 15:59:14 +00:00
2017-08-13 20:48:16 +00:00
class Note(models.Model):
model_name = 'note'
2017-12-03 21:52:27 +00:00
child = models.ForeignKey(
'Child',
on_delete=models.CASCADE,
related_name='note',
verbose_name=_('Child')
)
note = models.TextField(verbose_name=_('Note'))
time = models.DateTimeField(auto_now=True, verbose_name=_('Time'))
2017-08-13 20:48:16 +00:00
objects = models.Manager()
class Meta:
default_permissions = ('view', 'add', 'change', 'delete')
ordering = ['-time']
verbose_name = _('Note')
verbose_name_plural = _('Notes')
2017-08-13 20:48:16 +00:00
def __str__(self):
return str(_('Note'))
2017-08-13 20:48:16 +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):
model_name = 'sleep'
2017-12-03 21:52:27 +00:00
child = models.ForeignKey(
'Child',
on_delete=models.CASCADE,
related_name='sleep',
verbose_name=_('Child')
)
start = models.DateTimeField(
blank=False,
null=False,
verbose_name=_('Start time')
)
end = models.DateTimeField(
blank=False,
null=False,
verbose_name=_('End time')
)
duration = models.DurationField(
editable=False,
null=True,
verbose_name=_('Duration')
)
2017-08-13 14:48:16 +00:00
objects = models.Manager()
naps = NapsManager()
2017-08-13 14:48:16 +00:00
class Meta:
default_permissions = ('view', 'add', 'change', 'delete')
ordering = ['-start']
verbose_name = _('Sleep')
verbose_name_plural = _('Sleep')
2017-08-13 14:48:16 +00:00
def __str__(self):
return str(_('Sleep'))
2017-08-13 19:05:44 +00:00
@property
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
def save(self, *args, **kwargs):
if self.start and self.end:
self.duration = self.end - self.start
super(Sleep, self).save(*args, **kwargs)
def clean(self):
validate_time(self.start, 'start')
validate_time(self.end, 'end')
validate_duration(self)
validate_unique_period(Sleep.objects.filter(child=self.child), self)
2017-08-13 19:05:44 +00:00
2019-05-17 04:33:26 +00:00
class Temperature(models.Model):
model_name = 'temperature'
child = models.ForeignKey(
'Child',
on_delete=models.CASCADE,
related_name='temperature',
verbose_name=_('Child')
)
temperature = models.FloatField(
blank=False,
null=False,
verbose_name=_('Temperature')
)
time = models.DateTimeField(
blank=False,
null=False,
verbose_name=_('Time')
)
objects = models.Manager()
class Meta:
default_permissions = ('view', 'add', 'change', 'delete')
ordering = ['-time']
verbose_name = _('Temperature')
verbose_name_plural = _('Temperature')
def __str__(self):
return str(_('Temperature'))
def clean(self):
validate_time(self.time, 'time')
class Timer(models.Model):
model_name = 'timer'
2020-01-28 21:56:28 +00:00
child = models.ForeignKey(
'Child',
blank=True,
null=True,
on_delete=models.CASCADE,
related_name='timers',
verbose_name=_('Child')
)
name = models.CharField(
blank=True,
max_length=255,
null=True,
verbose_name=_('Name')
)
start = models.DateTimeField(
default=timezone.now,
blank=False,
verbose_name=_('Start time')
)
end = models.DateTimeField(
blank=True,
editable=False,
null=True,
verbose_name=_('End time')
)
duration = models.DurationField(
editable=False,
null=True,
verbose_name=_('Duration')
)
active = models.BooleanField(
default=True,
editable=False,
verbose_name=_('Active')
)
2017-12-03 21:52:27 +00:00
user = models.ForeignKey(
'auth.User',
on_delete=models.CASCADE,
related_name='timers',
verbose_name=_('User')
)
objects = models.Manager()
class Meta:
default_permissions = ('view', 'add', 'change', 'delete')
2017-08-18 05:56:23 +00:00
ordering = ['-active', '-start', '-end']
verbose_name = _('Timer')
verbose_name_plural = _('Timers')
def __str__(self):
return self.name or str(format_lazy(_('Timer #{id}'), id=self.id))
2020-01-28 21:56:28 +00:00
@property
def title_with_child(self):
""" Get Timer title with child name in parenthesis. """
title = str(self)
# Only actually add the name if there is more than one Child instance.
if title and self.child and Child.count() > 1:
title = format_lazy('{title} ({child})', title=title,
child=self.child)
return title
2020-01-28 21:56:28 +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
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 end:
end = timezone.now()
self.end = end
self.save()
def save(self, *args, **kwargs):
self.active = self.end is None
self.name = self.name or None
if self.start and self.end:
self.duration = self.end - self.start
else:
self.duration = None
super(Timer, self).save(*args, **kwargs)
def clean(self):
validate_time(self.start, 'start')
if self.end:
validate_time(self.end, 'end')
validate_duration(self)
2017-08-13 19:05:44 +00:00
class TummyTime(models.Model):
model_name = 'tummytime'
2017-12-03 21:52:27 +00:00
child = models.ForeignKey(
'Child',
on_delete=models.CASCADE,
related_name='tummy_time',
verbose_name=_('Child')
)
start = models.DateTimeField(
blank=False,
null=False,
verbose_name=_('Start time')
)
end = models.DateTimeField(
blank=False,
null=False,
verbose_name=_('End time')
)
duration = models.DurationField(
editable=False,
null=True,
verbose_name=_('Duration')
)
milestone = models.CharField(
blank=True,
max_length=255,
verbose_name=_('Milestone')
)
2017-08-13 19:05:44 +00:00
objects = models.Manager()
class Meta:
default_permissions = ('view', 'add', 'change', 'delete')
ordering = ['-start']
verbose_name = _('Tummy Time')
verbose_name_plural = _('Tummy Time')
2017-08-13 19:05:44 +00:00
def __str__(self):
return str(_('Tummy Time'))
2017-08-13 19:05:44 +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)
def clean(self):
validate_time(self.start, 'start')
validate_time(self.end, 'end')
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',
on_delete=models.CASCADE,
related_name='weight',
verbose_name=_('Child')
)
weight = models.FloatField(
blank=False,
null=False,
verbose_name=_('Weight')
)
date = models.DateField(
blank=False,
null=False,
verbose_name=_('Date')
)
2017-11-10 02:15:09 +00:00
objects = models.Manager()
class Meta:
default_permissions = ('view', 'add', 'change', 'delete')
ordering = ['-date']
verbose_name = _('Weight')
verbose_name_plural = _('Weight')
2017-11-10 02:15:09 +00:00
def __str__(self):
return str(_('Weight'))
2017-11-10 02:15:09 +00:00
def clean(self):
validate_date(self.date, 'date')