mirror of https://github.com/snachodog/mybuddy.git
Split get_objects to short functions
This commit is contained in:
parent
ee5f7a682a
commit
ce507761cd
135
core/timeline.py
135
core/timeline.py
|
@ -17,27 +17,69 @@ def get_objects(child, date):
|
||||||
max_date = date.replace(hour=23, minute=59, second=59)
|
max_date = date.replace(hour=23, minute=59, second=59)
|
||||||
events = []
|
events = []
|
||||||
|
|
||||||
instances = DiaperChange.objects.filter(child=child).filter(
|
_add_diaper_changes(child, min_date, max_date, events)
|
||||||
time__range=(min_date, max_date)).order_by('-time')
|
_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)
|
||||||
|
|
||||||
|
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(
|
||||||
|
start__range=(min_date, max_date)).order_by('-start')
|
||||||
for instance in instances:
|
for instance in instances:
|
||||||
contents = []
|
edit_link = reverse('core:tummytime-update', args=[instance.id])
|
||||||
if instance.wet:
|
|
||||||
contents.append('💧wet')
|
|
||||||
if instance.solid:
|
|
||||||
contents.append('💩solid')
|
|
||||||
events.append({
|
events.append({
|
||||||
'time': timezone.localtime(instance.time),
|
'time': timezone.localtime(instance.start),
|
||||||
'event': _('%(child)s had a diaper change.') % {
|
'event': _('%(child)s started tummy time!') % {
|
||||||
'child': child.first_name
|
'child': instance.child.first_name
|
||||||
},
|
},
|
||||||
'details': _('Contents: %(contents)s') % {
|
'edit_link': edit_link,
|
||||||
'contents': ', '.join(contents),
|
'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
|
||||||
},
|
},
|
||||||
'edit_link': reverse('core:diaperchange-update',
|
'edit_link': edit_link,
|
||||||
args=[instance.id]),
|
'duration': timesince.timesince(instance.start, now=instance.end),
|
||||||
'model_name': instance.model_name
|
'model_name': instance.model_name,
|
||||||
|
'type': 'end'
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
def _add_sleeps(child, min_date, max_date, events):
|
||||||
|
instances = Sleep.objects.filter(child=child).filter(
|
||||||
|
start__range=(min_date, max_date)).order_by('-start')
|
||||||
|
for instance in instances:
|
||||||
|
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
|
||||||
|
},
|
||||||
|
'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
|
||||||
|
},
|
||||||
|
'edit_link': edit_link,
|
||||||
|
'duration': timesince.timesince(instance.start, now=instance.end),
|
||||||
|
'model_name': instance.model_name,
|
||||||
|
'type': 'end'
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
def _add_feedings(child, min_date, max_date, events):
|
||||||
instances = Feeding.objects.filter(child=child).filter(
|
instances = Feeding.objects.filter(child=child).filter(
|
||||||
start__range=(min_date, max_date)).order_by('-start')
|
start__range=(min_date, max_date)).order_by('-start')
|
||||||
for instance in instances:
|
for instance in instances:
|
||||||
|
@ -69,54 +111,25 @@ def get_objects(child, date):
|
||||||
'type': 'end'
|
'type': 'end'
|
||||||
})
|
})
|
||||||
|
|
||||||
instances = Sleep.objects.filter(child=child).filter(
|
|
||||||
start__range=(min_date, max_date)).order_by('-start')
|
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:
|
for instance in instances:
|
||||||
edit_link = reverse('core:sleep-update', args=[instance.id])
|
contents = []
|
||||||
|
if instance.wet:
|
||||||
|
contents.append('💧wet')
|
||||||
|
if instance.solid:
|
||||||
|
contents.append('💩solid')
|
||||||
events.append({
|
events.append({
|
||||||
'time': timezone.localtime(instance.start),
|
'time': timezone.localtime(instance.time),
|
||||||
'event': _('%(child)s fell asleep.') % {
|
'event': _('%(child)s had a diaper change.') % {
|
||||||
'child': instance.child.first_name
|
'child': child.first_name
|
||||||
},
|
},
|
||||||
'edit_link': edit_link,
|
'details': _('Contents: %(contents)s') % {
|
||||||
'model_name': instance.model_name,
|
'contents': ', '.join(contents),
|
||||||
'type': 'start'
|
|
||||||
})
|
|
||||||
events.append({
|
|
||||||
'time': timezone.localtime(instance.end),
|
|
||||||
'event': _('%(child)s woke up.') % {
|
|
||||||
'child': instance.child.first_name
|
|
||||||
},
|
},
|
||||||
'edit_link': edit_link,
|
'edit_link': reverse('core:diaperchange-update',
|
||||||
'duration': timesince.timesince(instance.start, now=instance.end),
|
args=[instance.id]),
|
||||||
'model_name': instance.model_name,
|
'model_name': instance.model_name
|
||||||
'type': 'end'
|
|
||||||
})
|
})
|
||||||
|
|
||||||
instances = TummyTime.objects.filter(child=child).filter(
|
|
||||||
start__range=(min_date, max_date)).order_by('-start')
|
|
||||||
for instance in instances:
|
|
||||||
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
|
|
||||||
},
|
|
||||||
'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
|
|
||||||
},
|
|
||||||
'edit_link': edit_link,
|
|
||||||
'duration': timesince.timesince(instance.start, now=instance.end),
|
|
||||||
'model_name': instance.model_name,
|
|
||||||
'type': 'end'
|
|
||||||
})
|
|
||||||
|
|
||||||
events.sort(key=lambda x: x['time'], reverse=True)
|
|
||||||
|
|
||||||
return events
|
|
||||||
|
|
Loading…
Reference in New Issue