Add an actual duration field to relevant models.

This commit is contained in:
Christopher Charbonneau Wells 2017-08-19 16:16:42 -04:00
parent 22fbdf2868
commit c5fc35b177
2 changed files with 30 additions and 20 deletions

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-08-18 18:58
# Generated by Django 1.11.1 on 2017-08-19 20:13
from __future__ import unicode_literals
from django.conf import settings
@ -26,9 +26,9 @@ class Migration(migrations.Migration):
('slug', models.SlugField(editable=False, max_length=100, unique=True)),
],
options={
'verbose_name_plural': 'Children',
'ordering': ['last_name', 'first_name'],
'default_permissions': ('view', 'add', 'change', 'delete'),
'verbose_name_plural': 'Children',
},
),
migrations.CreateModel(
@ -52,6 +52,7 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('start', models.DateTimeField()),
('end', models.DateTimeField()),
('duration', models.DurationField(editable=False, null=True)),
('type', models.CharField(choices=[('breast milk', 'Breast milk'), ('formula', 'Formula')], max_length=255)),
('method', models.CharField(choices=[('bottle', 'Bottle'), ('left breast', 'Left breast'), ('right breast', 'Right breast')], max_length=255)),
('amount', models.FloatField(blank=True, null=True)),
@ -81,12 +82,13 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('start', models.DateTimeField()),
('end', models.DateTimeField()),
('duration', models.DurationField(editable=False, null=True)),
('child', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='sleep', to='core.Child')),
],
options={
'verbose_name_plural': 'Sleep',
'ordering': ['-start'],
'default_permissions': ('view', 'add', 'change', 'delete'),
'verbose_name_plural': 'Sleep',
},
),
migrations.CreateModel(
@ -96,6 +98,7 @@ class Migration(migrations.Migration):
('name', models.CharField(blank=True, max_length=255, null=True)),
('start', models.DateTimeField(auto_now_add=True)),
('end', models.DateTimeField(blank=True, editable=False, null=True)),
('duration', models.DurationField(editable=False, null=True)),
('active', models.BooleanField(default=True, editable=False)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='timers', to=settings.AUTH_USER_MODEL)),
],
@ -110,6 +113,7 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('start', models.DateTimeField()),
('end', models.DateTimeField()),
('duration', models.DurationField(editable=False, null=True)),
('milestone', models.CharField(blank=True, max_length=255)),
('child', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='tummy_time', to='core.Child')),
],

View File

@ -5,8 +5,6 @@ from django.db import models
from django.template.defaultfilters import slugify
from django.utils import timezone, timesince
from .utils import duration_string
class Child(models.Model):
first_name = models.CharField(max_length=255)
@ -69,6 +67,7 @@ class Feeding(models.Model):
child = models.ForeignKey('Child', related_name='feeding')
start = models.DateTimeField(blank=False, null=False)
end = models.DateTimeField(blank=False, null=False)
duration = models.DurationField(null=True, editable=False)
type = models.CharField(max_length=255, choices=[
('breast milk', 'Breast milk'),
('formula', 'Formula'),
@ -88,14 +87,16 @@ class Feeding(models.Model):
def __str__(self):
return 'Feeding for {} on {} ({})'.format(
self.child, self.end.date(), self.duration())
def duration(self):
return duration_string(self.start, self.end)
self.child, self.end.date(), self.duration)
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)
class Note(models.Model):
child = models.ForeignKey('Child', related_name='note')
@ -119,6 +120,7 @@ class Sleep(models.Model):
child = models.ForeignKey('Child', related_name='sleep')
start = models.DateTimeField(blank=False, null=False)
end = models.DateTimeField(blank=False, null=False)
duration = models.DurationField(null=True, editable=False)
objects = models.Manager()
@ -129,19 +131,22 @@ class Sleep(models.Model):
def __str__(self):
return 'Sleep for {} on {} ({})'.format(
self.child, self.end.date(), self.duration())
def duration(self):
return duration_string(self.start, self.end)
self.child, self.end.date(), self.duration)
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)
class Timer(models.Model):
name = models.CharField(max_length=255, null=True, blank=True)
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')
@ -154,13 +159,11 @@ class Timer(models.Model):
def __str__(self):
return self.name or 'Timer #{}'.format(self.id)
def duration(self):
return duration_string(self.start, self.end or timezone.now(),
short=True)
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)
@ -168,6 +171,7 @@ class TummyTime(models.Model):
child = models.ForeignKey('Child', related_name='tummy_time')
start = models.DateTimeField(blank=False, null=False)
end = models.DateTimeField(blank=False, null=False)
duration = models.DurationField(null=True, editable=False)
milestone = models.CharField(max_length=255, blank=True)
objects = models.Manager()
@ -180,11 +184,13 @@ class TummyTime(models.Model):
return 'Tummy time for {} on {} ({})'.format(
self.child, self.end.date(), self.duration())
def duration(self):
return duration_string(self.start, self.end)
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)