Reverse order of days in fake command and prevent cross over of sleep entries.

This commit is contained in:
Christopher Charbonneau Wells 2017-10-21 20:00:05 -04:00
parent 347727e725
commit 3c01a8710f
1 changed files with 10 additions and 3 deletions

View File

@ -49,7 +49,7 @@ class Command(BaseCommand):
) )
child.save() child.save()
for j in range(0, days): for j in range(days - 1, -1, -1):
date = (timezone.localtime() - timedelta(days=j)).replace( date = (timezone.localtime() - timedelta(days=j)).replace(
hour=0, minute=0, second=0) hour=0, minute=0, second=0)
self._add_child_data(child, date) self._add_child_data(child, date)
@ -107,7 +107,14 @@ class Command(BaseCommand):
last_end = end last_end = end
last_end = date last_end = date
# TODO: Update last_end if a previous day's end overlapped midnight.
# Adjust last_end if the last sleep entry crossed in to date.
last_entry = Sleep.objects.filter(child=child).order_by('end').last()
if last_entry:
last_entry_end = timezone.localtime(last_entry.end)
if last_entry_end > last_end:
last_end = last_entry_end
while last_end < date + timedelta(days=1): while last_end < date + timedelta(days=1):
start = last_end + timedelta(minutes=randint(0, 60 * 2)) start = last_end + timedelta(minutes=randint(0, 60 * 2))
if start.date() != date.date(): if start.date() != date.date():
@ -127,7 +134,7 @@ class Command(BaseCommand):
else: else:
milestone = '' milestone = ''
start = last_end + timedelta(minutes=randint(0, 60 * 2)) start = last_end + timedelta(minutes=randint(0, 60 * 5))
end = start + timedelta(minutes=randint(1, 10)) end = start + timedelta(minutes=randint(1, 10))
if end > now: if end > now:
break break