mybuddy/core/models.py

238 lines
7.2 KiB
Python
Raw Normal View History

2017-08-13 15:20:09 +00:00
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
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, timesince
2017-08-11 18:32:02 +00:00
2017-08-13 14:48:16 +00:00
2017-08-16 12:49:58 +00:00
class Child(models.Model):
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-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)
def name(self, reverse=False):
if reverse:
return '{}, {}'.format(self.last_name, self.first_name)
return '{} {}'.format(self.first_name, self.last_name)
2017-08-13 14:48:16 +00:00
2017-08-13 19:51:25 +00:00
class DiaperChange(models.Model):
model_name = 'diaperchange'
child = models.ForeignKey('Child', related_name='diaper_change')
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-09-15 15:55:30 +00:00
if self.wet and self.solid:
name = 'Wet, solid ({}) diaper change'.format(self.color)
elif self.wet:
name = 'Wet diaper change'
elif self.solid:
name = self.solid = 'Solid ({}) diaper change'.format(self.color)
else:
name = 'Diaper change'
return name
2017-08-13 19:51:25 +00:00
def since(self, time=timezone.now()):
return timesince.timesince(self.time, time)
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-08-13 19:51:25 +00:00
2017-08-13 15:59:14 +00:00
class Feeding(models.Model):
model_name = 'feeding'
child = models.ForeignKey('Child', related_name='feeding')
2017-08-13 15:59:14 +00:00
start = models.DateTimeField(blank=False, null=False)
end = models.DateTimeField(blank=False, null=False)
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'),
])
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-09-15 15:55:30 +00:00
name = '{} ({}) feeding'.format(self.method, self.type)
if self.amount:
name += ' ({} oz.)'.format(self.amount)
return name.capitalize()
2017-08-13 15:59:14 +00:00
def since(self, time=timezone.now()):
return timesince.timesince(self.end, time)
def save(self, *args, **kwargs):
if self.start and self.end:
self.duration = self.end - self.start
super(Feeding, self).save(*args, **kwargs)
2017-08-13 15:59:14 +00:00
2017-08-13 20:48:16 +00:00
class Note(models.Model):
model_name = 'note'
child = models.ForeignKey('Child', related_name='note')
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-09-15 15:55:30 +00:00
return 'Note about {}'.format(self.child)
2017-08-13 20:48:16 +00:00
def since(self, time=timezone.now()):
return timesince.timesince(self.time, time)
2017-08-13 20:48:16 +00:00
2017-08-13 14:48:16 +00:00
class Sleep(models.Model):
model_name = 'sleep'
child = models.ForeignKey('Child', related_name='sleep')
2017-08-13 14:48:16 +00:00
start = models.DateTimeField(blank=False, null=False)
end = models.DateTimeField(blank=False, null=False)
duration = models.DurationField(null=True, editable=False)
2017-08-13 14:48:16 +00:00
objects = models.Manager()
class Meta:
default_permissions = ('view', 'add', 'change', 'delete')
ordering = ['-start']
verbose_name_plural = 'Sleep'
def __str__(self):
2017-09-15 15:55:30 +00:00
return 'Sleep ({})'.format(self.duration)
2017-08-13 19:05:44 +00:00
def since(self, time=timezone.now()):
return timesince.timesince(self.end, time)
def save(self, *args, **kwargs):
if self.start and self.end:
self.duration = self.end - self.start
super(Sleep, self).save(*args, **kwargs)
2017-08-13 19:05:44 +00:00
class Timer(models.Model):
model_name = 'timer'
name = models.CharField(max_length=255, null=True, blank=True)
2017-08-18 05:01:37 +00:00
start = models.DateTimeField(auto_now_add=True)
end = models.DateTimeField(blank=True, null=True, editable=False)
duration = models.DurationField(null=True, editable=False)
active = models.BooleanField(default=True, editable=False)
user = models.ForeignKey('auth.User', related_name='timers')
objects = models.Manager()
class Meta:
default_permissions = ('view', 'add', 'change', 'delete')
2017-08-18 05:56:23 +00:00
ordering = ['-active', '-start', '-end']
def __str__(self):
return self.name or 'Timer #{}'.format(self.id)
def current_duration(self):
if self.duration:
return self.duration
else:
return timezone.now() - self.start
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:
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
super(Timer, self).save(*args, **kwargs)
2017-08-13 19:05:44 +00:00
class TummyTime(models.Model):
model_name = 'tummytime'
child = models.ForeignKey('Child', related_name='tummy_time')
2017-08-13 19:05:44 +00:00
start = models.DateTimeField(blank=False, null=False)
end = models.DateTimeField(blank=False, null=False)
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-09-15 15:55:30 +00:00
return 'Tummy time ({})'.format(self.duration)
2017-08-13 19:05:44 +00:00
2017-08-18 18:58:10 +00:00
def duration_td(self):
return self.end - self.start
def since(self, time=timezone.now()):
return timesince.timesince(self.end, time)
def save(self, *args, **kwargs):
if self.start and self.end:
self.duration = self.end - self.start
super(TummyTime, self).save(*args, **kwargs)