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
|
|
|
|
2021-11-20 17:41:15 +00:00
|
|
|
from core.models import DiaperChange, Feeding, Note, Sleep, TummyTime
|
2021-08-05 21:13:21 +00:00
|
|
|
from datetime import timedelta
|
2017-11-04 03:03:24 +00:00
|
|
|
|
|
|
|
|
2021-08-29 20:50:22 +00:00
|
|
|
def get_objects(date, child=None):
|
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 date: a DateTime instance for the day to be summarized.
|
2021-08-29 20:50:22 +00:00
|
|
|
:param child: Child instance to filter results for (no filter if `None`).
|
2017-11-04 03:03:24 +00:00
|
|
|
:returns: a list of the day's events.
|
|
|
|
"""
|
|
|
|
min_date = date
|
|
|
|
max_date = date.replace(hour=23, minute=59, second=59)
|
|
|
|
events = []
|
|
|
|
|
2021-08-29 20:50:22 +00:00
|
|
|
_add_diaper_changes(min_date, max_date, events, child)
|
|
|
|
_add_feedings(min_date, max_date, events, child)
|
|
|
|
_add_sleeps(min_date, max_date, events, child)
|
|
|
|
_add_tummy_times(min_date, max_date, events, child)
|
2021-11-20 17:41:15 +00:00
|
|
|
_add_notes(min_date, max_date, events, child)
|
2017-11-04 03:03:24 +00:00
|
|
|
|
2022-02-10 00:00:30 +00:00
|
|
|
explicit_type_ordering = {"start": 0, "end": 1}
|
2022-01-17 14:58:25 +00:00
|
|
|
events.sort(
|
2022-01-17 15:03:15 +00:00
|
|
|
key=lambda x: (
|
2022-02-10 00:00:30 +00:00
|
|
|
x["time"],
|
|
|
|
explicit_type_ordering.get(x.get("type"), -1),
|
2022-01-17 15:03:15 +00:00
|
|
|
),
|
2022-01-17 14:58:25 +00:00
|
|
|
reverse=True,
|
|
|
|
)
|
2021-08-05 21:06:23 +00:00
|
|
|
|
|
|
|
return events
|
|
|
|
|
|
|
|
|
2021-08-29 20:50:22 +00:00
|
|
|
def _add_tummy_times(min_date, max_date, events, child=None):
|
2022-02-10 00:00:30 +00:00
|
|
|
instances = TummyTime.objects.filter(start__range=(min_date, max_date)).order_by(
|
|
|
|
"-start"
|
|
|
|
)
|
2021-08-29 20:50:22 +00:00
|
|
|
if child:
|
|
|
|
instances = instances.filter(child=child)
|
2017-11-04 03:03:24 +00:00
|
|
|
for instance in instances:
|
2021-08-10 21:13:53 +00:00
|
|
|
details = []
|
|
|
|
if instance.milestone:
|
|
|
|
details.append(instance.milestone)
|
2022-02-10 00:00:30 +00:00
|
|
|
edit_link = reverse("core:tummytime-update", args=[instance.id])
|
|
|
|
events.append(
|
|
|
|
{
|
|
|
|
"time": timezone.localtime(instance.start),
|
|
|
|
"event": _("%(child)s started tummy time!")
|
|
|
|
% {"child": instance.child.first_name},
|
|
|
|
"details": details,
|
|
|
|
"edit_link": edit_link,
|
|
|
|
"model_name": instance.model_name,
|
|
|
|
"type": "start",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
events.append(
|
|
|
|
{
|
|
|
|
"time": timezone.localtime(instance.end),
|
|
|
|
"event": _("%(child)s finished tummy time.")
|
|
|
|
% {"child": instance.child.first_name},
|
|
|
|
"details": details,
|
|
|
|
"edit_link": edit_link,
|
|
|
|
"duration": timesince.timesince(instance.start, now=instance.end),
|
|
|
|
"model_name": instance.model_name,
|
|
|
|
"type": "end",
|
|
|
|
}
|
|
|
|
)
|
2017-11-04 03:03:24 +00:00
|
|
|
|
2021-08-05 21:06:23 +00:00
|
|
|
|
2021-08-29 20:50:22 +00:00
|
|
|
def _add_sleeps(min_date, max_date, events, child=None):
|
2022-02-10 00:00:30 +00:00
|
|
|
instances = Sleep.objects.filter(start__range=(min_date, max_date)).order_by(
|
|
|
|
"-start"
|
|
|
|
)
|
2021-08-29 20:50:22 +00:00
|
|
|
if child:
|
|
|
|
instances = instances.filter(child=child)
|
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)
|
2022-02-10 00:00:30 +00:00
|
|
|
edit_link = reverse("core:sleep-update", args=[instance.id])
|
|
|
|
events.append(
|
|
|
|
{
|
|
|
|
"time": timezone.localtime(instance.start),
|
|
|
|
"event": _("%(child)s fell asleep.")
|
|
|
|
% {"child": instance.child.first_name},
|
|
|
|
"details": details,
|
|
|
|
"edit_link": edit_link,
|
|
|
|
"model_name": instance.model_name,
|
|
|
|
"type": "start",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
events.append(
|
|
|
|
{
|
|
|
|
"time": timezone.localtime(instance.end),
|
|
|
|
"event": _("%(child)s woke up.") % {"child": instance.child.first_name},
|
|
|
|
"details": details,
|
|
|
|
"edit_link": edit_link,
|
|
|
|
"duration": timesince.timesince(instance.start, now=instance.end),
|
|
|
|
"model_name": instance.model_name,
|
|
|
|
"type": "end",
|
|
|
|
}
|
|
|
|
)
|
2017-11-04 03:03:24 +00:00
|
|
|
|
2021-08-05 21:06:23 +00:00
|
|
|
|
2021-08-29 20:50:22 +00:00
|
|
|
def _add_feedings(min_date, max_date, events, child=None):
|
|
|
|
# Ensure first feeding has a previous.
|
|
|
|
yesterday = min_date - timedelta(days=1)
|
2021-08-05 21:13:21 +00:00
|
|
|
prev_start = None
|
|
|
|
|
2022-02-10 00:00:30 +00:00
|
|
|
instances = Feeding.objects.filter(start__range=(yesterday, max_date)).order_by(
|
|
|
|
"start"
|
|
|
|
)
|
2021-08-29 20:50:22 +00:00
|
|
|
if child:
|
|
|
|
instances = instances.filter(child=child)
|
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:
|
2022-02-10 00:00:30 +00:00
|
|
|
time_since_prev = timesince.timesince(prev_start, now=instance.start)
|
2021-08-05 21:13:21 +00:00
|
|
|
prev_start = instance.start
|
|
|
|
if instance.start < min_date:
|
|
|
|
continue
|
2022-02-10 00:00:30 +00:00
|
|
|
edit_link = reverse("core:feeding-update", args=[instance.id])
|
2021-08-05 21:06:23 +00:00
|
|
|
if instance.amount:
|
2022-02-10 00:00:30 +00:00
|
|
|
details.append(
|
|
|
|
_("Amount: %(amount).0f")
|
|
|
|
% {
|
|
|
|
"amount": instance.amount,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
events.append(
|
|
|
|
{
|
|
|
|
"time": timezone.localtime(instance.start),
|
|
|
|
"event": _("%(child)s started feeding.")
|
|
|
|
% {"child": instance.child.first_name},
|
|
|
|
"details": details,
|
|
|
|
"edit_link": edit_link,
|
|
|
|
"time_since_prev": time_since_prev,
|
|
|
|
"model_name": instance.model_name,
|
|
|
|
"type": "start",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
events.append(
|
|
|
|
{
|
|
|
|
"time": timezone.localtime(instance.end),
|
|
|
|
"event": _("%(child)s finished feeding.")
|
|
|
|
% {"child": instance.child.first_name},
|
|
|
|
"details": details,
|
|
|
|
"edit_link": edit_link,
|
|
|
|
"duration": timesince.timesince(instance.start, now=instance.end),
|
|
|
|
"model_name": instance.model_name,
|
|
|
|
"type": "end",
|
|
|
|
}
|
|
|
|
)
|
2017-11-04 03:03:24 +00:00
|
|
|
|
|
|
|
|
2021-08-29 20:50:22 +00:00
|
|
|
def _add_diaper_changes(min_date, max_date, events, child):
|
2022-02-10 00:00:30 +00:00
|
|
|
instances = DiaperChange.objects.filter(time__range=(min_date, max_date)).order_by(
|
|
|
|
"-time"
|
|
|
|
)
|
2021-08-29 20:50:22 +00:00
|
|
|
if child:
|
|
|
|
instances = instances.filter(child=child)
|
2021-08-05 21:06:23 +00:00
|
|
|
for instance in instances:
|
|
|
|
contents = []
|
|
|
|
if instance.wet:
|
2022-02-10 00:00:30 +00:00
|
|
|
contents.append("💧")
|
2021-08-05 21:06:23 +00:00
|
|
|
if instance.solid:
|
2022-02-10 00:00:30 +00:00
|
|
|
contents.append("💩")
|
|
|
|
events.append(
|
|
|
|
{
|
|
|
|
"time": timezone.localtime(instance.time),
|
|
|
|
"event": _("%(child)s had a %(type)s diaper change.")
|
|
|
|
% {
|
|
|
|
"child": instance.child.first_name,
|
|
|
|
"type": "".join(contents),
|
|
|
|
},
|
|
|
|
"edit_link": reverse("core:diaperchange-update", args=[instance.id]),
|
|
|
|
"model_name": instance.model_name,
|
|
|
|
}
|
|
|
|
)
|
2021-11-20 17:41:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _add_notes(min_date, max_date, events, child):
|
2022-02-10 00:00:30 +00:00
|
|
|
instances = Note.objects.filter(time__range=(min_date, max_date)).order_by("-time")
|
2021-11-20 17:41:15 +00:00
|
|
|
if child:
|
|
|
|
instances = instances.filter(child=child)
|
|
|
|
for instance in instances:
|
2022-02-10 00:00:30 +00:00
|
|
|
events.append(
|
|
|
|
{
|
|
|
|
"time": timezone.localtime(instance.time),
|
|
|
|
"details": [instance.note],
|
|
|
|
"edit_link": reverse("core:note-update", args=[instance.id]),
|
|
|
|
"model_name": instance.model_name,
|
|
|
|
}
|
|
|
|
)
|