2017-11-04 03:03:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2021-07-08 03:09:22 +00:00
|
|
|
from django.urls import reverse
|
2021-07-09 20:45:07 +00:00
|
|
|
from django.utils import timezone, timesince
|
2019-04-17 03:33:49 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2017-11-04 03:03:24 +00:00
|
|
|
|
|
|
|
from core.models import DiaperChange, Feeding, Sleep, TummyTime
|
2021-08-05 21:13:21 +00:00
|
|
|
from datetime import timedelta
|
2017-11-04 03:03:24 +00:00
|
|
|
|
|
|
|
|
2017-11-07 18:15:48 +00:00
|
|
|
def get_objects(child, date):
|
2017-11-04 03:03:24 +00:00
|
|
|
"""
|
2017-11-07 18:15:48 +00:00
|
|
|
Create a time-sorted dictionary of all events for a child.
|
2017-11-04 03:03:24 +00:00
|
|
|
:param child: an instance of a Child.
|
|
|
|
:param date: a DateTime instance for the day to be summarized.
|
|
|
|
:returns: a list of the day's events.
|
|
|
|
"""
|
|
|
|
min_date = date
|
|
|
|
max_date = date.replace(hour=23, minute=59, second=59)
|
|
|
|
events = []
|
|
|
|
|
2021-08-05 21:06:23 +00:00
|
|
|
_add_diaper_changes(child, min_date, max_date, events)
|
|
|
|
_add_feedings(child, min_date, max_date, events)
|
|
|
|
_add_sleeps(child, min_date, max_date, events)
|
|
|
|
_add_tummy_times(child, min_date, max_date, events)
|
2017-11-04 03:03:24 +00:00
|
|
|
|
2021-08-05 21:06:23 +00:00
|
|
|
events.sort(key=lambda x: x['time'], reverse=True)
|
|
|
|
|
|
|
|
return events
|
|
|
|
|
|
|
|
|
|
|
|
def _add_tummy_times(child, min_date, max_date, events):
|
|
|
|
instances = TummyTime.objects.filter(child=child).filter(
|
2017-11-04 03:03:24 +00:00
|
|
|
start__range=(min_date, max_date)).order_by('-start')
|
|
|
|
for instance in instances:
|
2021-08-10 21:13:53 +00:00
|
|
|
details = []
|
|
|
|
if instance.milestone:
|
|
|
|
details.append(instance.milestone)
|
2021-08-05 21:06:23 +00:00
|
|
|
edit_link = reverse('core:tummytime-update', args=[instance.id])
|
2017-11-04 03:03:24 +00:00
|
|
|
events.append({
|
|
|
|
'time': timezone.localtime(instance.start),
|
2021-08-05 21:06:23 +00:00
|
|
|
'event': _('%(child)s started tummy time!') % {
|
2019-04-17 03:33:49 +00:00
|
|
|
'child': instance.child.first_name
|
|
|
|
},
|
2021-08-10 21:13:53 +00:00
|
|
|
'details': details,
|
2021-07-08 03:09:22 +00:00
|
|
|
'edit_link': edit_link,
|
2017-11-04 03:03:24 +00:00
|
|
|
'model_name': instance.model_name,
|
|
|
|
'type': 'start'
|
|
|
|
})
|
|
|
|
events.append({
|
|
|
|
'time': timezone.localtime(instance.end),
|
2021-08-05 21:06:23 +00:00
|
|
|
'event': _('%(child)s finished tummy time.') % {
|
2019-04-17 03:33:49 +00:00
|
|
|
'child': instance.child.first_name
|
|
|
|
},
|
2021-08-10 21:13:53 +00:00
|
|
|
'details': details,
|
2021-07-08 03:09:22 +00:00
|
|
|
'edit_link': edit_link,
|
2021-07-09 20:45:07 +00:00
|
|
|
'duration': timesince.timesince(instance.start, now=instance.end),
|
2017-11-04 03:03:24 +00:00
|
|
|
'model_name': instance.model_name,
|
|
|
|
'type': 'end'
|
|
|
|
})
|
|
|
|
|
2021-08-05 21:06:23 +00:00
|
|
|
|
|
|
|
def _add_sleeps(child, min_date, max_date, events):
|
2017-11-04 03:03:24 +00:00
|
|
|
instances = Sleep.objects.filter(child=child).filter(
|
|
|
|
start__range=(min_date, max_date)).order_by('-start')
|
|
|
|
for instance in instances:
|
2021-08-10 21:13:53 +00:00
|
|
|
details = []
|
|
|
|
if instance.notes:
|
|
|
|
details.append(instance.notes)
|
2021-07-08 03:09:22 +00:00
|
|
|
edit_link = reverse('core:sleep-update', args=[instance.id])
|
2017-11-04 03:03:24 +00:00
|
|
|
events.append({
|
|
|
|
'time': timezone.localtime(instance.start),
|
2019-04-17 03:33:49 +00:00
|
|
|
'event': _('%(child)s fell asleep.') % {
|
|
|
|
'child': instance.child.first_name
|
|
|
|
},
|
2021-08-10 21:13:53 +00:00
|
|
|
'details': details,
|
2021-07-08 03:09:22 +00:00
|
|
|
'edit_link': edit_link,
|
2017-11-04 03:03:24 +00:00
|
|
|
'model_name': instance.model_name,
|
|
|
|
'type': 'start'
|
|
|
|
})
|
|
|
|
events.append({
|
|
|
|
'time': timezone.localtime(instance.end),
|
2019-04-17 03:33:49 +00:00
|
|
|
'event': _('%(child)s woke up.') % {
|
|
|
|
'child': instance.child.first_name
|
|
|
|
},
|
2021-08-10 21:13:53 +00:00
|
|
|
'details': details,
|
2021-07-08 03:09:22 +00:00
|
|
|
'edit_link': edit_link,
|
2021-07-09 20:45:07 +00:00
|
|
|
'duration': timesince.timesince(instance.start, now=instance.end),
|
2017-11-04 03:03:24 +00:00
|
|
|
'model_name': instance.model_name,
|
|
|
|
'type': 'end'
|
|
|
|
})
|
|
|
|
|
2021-08-05 21:06:23 +00:00
|
|
|
|
|
|
|
def _add_feedings(child, min_date, max_date, events):
|
2021-08-05 21:13:21 +00:00
|
|
|
yesterday = min_date - timedelta(days=1) # So first feeding has a previous
|
|
|
|
prev_start = None
|
|
|
|
|
2021-08-05 21:06:23 +00:00
|
|
|
instances = Feeding.objects.filter(child=child).filter(
|
2021-08-05 21:13:21 +00:00
|
|
|
start__range=(yesterday, max_date)).order_by('start')
|
2017-11-04 03:03:24 +00:00
|
|
|
for instance in instances:
|
2021-08-10 21:13:53 +00:00
|
|
|
details = []
|
|
|
|
if instance.notes:
|
|
|
|
details.append(instance.notes)
|
2021-08-05 21:13:21 +00:00
|
|
|
time_since_prev = None
|
|
|
|
if prev_start:
|
|
|
|
time_since_prev = \
|
|
|
|
timesince.timesince(prev_start, now=instance.start)
|
|
|
|
prev_start = instance.start
|
|
|
|
if instance.start < min_date:
|
|
|
|
continue
|
2021-08-05 21:06:23 +00:00
|
|
|
edit_link = reverse('core:feeding-update', args=[instance.id])
|
|
|
|
if instance.amount:
|
2021-08-10 21:13:53 +00:00
|
|
|
details.append(_('Amount: %(amount).0f') % {
|
2021-08-05 21:06:23 +00:00
|
|
|
'amount': instance.amount,
|
2021-08-10 21:13:53 +00:00
|
|
|
})
|
2017-11-04 03:03:24 +00:00
|
|
|
events.append({
|
|
|
|
'time': timezone.localtime(instance.start),
|
2021-08-05 21:06:23 +00:00
|
|
|
'event': _('%(child)s started feeding.') % {
|
2019-04-17 03:33:49 +00:00
|
|
|
'child': instance.child.first_name
|
|
|
|
},
|
2021-08-05 21:06:23 +00:00
|
|
|
'details': details,
|
2021-07-08 03:09:22 +00:00
|
|
|
'edit_link': edit_link,
|
2021-08-05 21:13:21 +00:00
|
|
|
'time_since_prev': time_since_prev,
|
2017-11-04 03:03:24 +00:00
|
|
|
'model_name': instance.model_name,
|
|
|
|
'type': 'start'
|
|
|
|
})
|
|
|
|
events.append({
|
|
|
|
'time': timezone.localtime(instance.end),
|
2021-08-05 21:06:23 +00:00
|
|
|
'event': _('%(child)s finished feeding.') % {
|
2019-04-17 03:33:49 +00:00
|
|
|
'child': instance.child.first_name
|
|
|
|
},
|
2021-08-05 21:06:23 +00:00
|
|
|
'details': details,
|
2021-07-08 03:09:22 +00:00
|
|
|
'edit_link': edit_link,
|
2021-07-09 20:45:07 +00:00
|
|
|
'duration': timesince.timesince(instance.start, now=instance.end),
|
2017-11-04 03:03:24 +00:00
|
|
|
'model_name': instance.model_name,
|
|
|
|
'type': 'end'
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2021-08-05 21:06:23 +00:00
|
|
|
def _add_diaper_changes(child, min_date, max_date, events):
|
|
|
|
instances = DiaperChange.objects.filter(child=child).filter(
|
|
|
|
time__range=(min_date, max_date)).order_by('-time')
|
|
|
|
for instance in instances:
|
|
|
|
contents = []
|
|
|
|
if instance.wet:
|
|
|
|
contents.append('💧wet')
|
|
|
|
if instance.solid:
|
|
|
|
contents.append('💩solid')
|
2021-08-10 21:13:53 +00:00
|
|
|
details = [_('Contents: %(contents)s') % {
|
|
|
|
'contents': ', '.join(contents),
|
|
|
|
}]
|
|
|
|
if instance.notes:
|
|
|
|
details.append(instance.notes)
|
2021-08-05 21:06:23 +00:00
|
|
|
events.append({
|
|
|
|
'time': timezone.localtime(instance.time),
|
|
|
|
'event': _('%(child)s had a diaper change.') % {
|
|
|
|
'child': child.first_name
|
|
|
|
},
|
2021-08-10 21:13:53 +00:00
|
|
|
'details': details,
|
2021-08-05 21:06:23 +00:00
|
|
|
'edit_link': reverse('core:diaperchange-update',
|
|
|
|
args=[instance.id]),
|
|
|
|
'model_name': instance.model_name
|
|
|
|
})
|