Prevent date intersections in the "fake" management command.

This commit is contained in:
Christopher Charbonneau Wells 2017-08-22 09:35:01 -04:00
parent f5768959d8
commit 10e87c27b5
2 changed files with 41 additions and 25 deletions

View File

@ -30,14 +30,14 @@ class Command(BaseCommand):
parser.add_argument( parser.add_argument(
'--days', '--days',
dest='days', dest='days',
default=5, default=7,
help='How many days of fake entries to create.' help='How many days of fake entries to create.'
) )
def handle(self, *args, **kwargs): def handle(self, *args, **kwargs):
verbosity = int(kwargs['verbosity']) or 1 verbosity = int(kwargs['verbosity']) or 1
children = int(kwargs['children']) or 1 children = int(kwargs['children']) or 1
days = int(kwargs['days']) or 5 days = int(kwargs['days']) or 7
for i in range(0, children): for i in range(0, children):
child = Child.objects.create( child = Child.objects.create(
@ -58,7 +58,8 @@ class Command(BaseCommand):
) )
def _add_child_data(self, child, date): def _add_child_data(self, child, date):
"""TODO: Make sure the dates do not intersect (by advancing date?).""" now = timezone.now()
for i in (range(0, randint(5, 20))): for i in (range(0, randint(5, 20))):
solid = choice([True, False]) solid = choice([True, False])
if solid: if solid:
@ -69,54 +70,68 @@ class Command(BaseCommand):
wet = True wet = True
color = '' color = ''
time = date + timedelta(minutes=randint(0, 60 * 24))
if time < now:
DiaperChange.objects.create( DiaperChange.objects.create(
child=child, child=child,
time=date + timedelta(seconds=randint(0, 86400)), time=time,
wet=wet, wet=wet,
solid=solid, solid=solid,
color=color color=color
).save() ).save()
for i in (range(0, randint(5, 20))): start = date
start = date + timedelta(seconds=randint(0, 86400)) while start < date + timedelta(days=1):
method = choice(Feeding._meta.get_field('method').choices)[0] method = choice(Feeding._meta.get_field('method').choices)[0]
if method is 'bottle': if method is 'bottle':
amount = Decimal('%d.%d' % (randint(0, 6), randint(0, 9))) amount = Decimal('%d.%d' % (randint(0, 6), randint(0, 9)))
else: else:
amount = None amount = None
start += timedelta(minutes=randint(0, 60 * 2))
end = start + timedelta(minutes=randint(5, 20))
if end > now:
break
Feeding.objects.create( Feeding.objects.create(
child=child, child=child,
start=start, start=start,
end=start + timedelta(minutes=randint(5, 60)), end=end,
type=choice(Feeding._meta.get_field('type').choices)[0], type=choice(Feeding._meta.get_field('type').choices)[0],
method=method, method=method,
amount=amount amount=amount
).save() ).save()
start = end
for i in (range(0, randint(2, 10))): start = date
start = date + timedelta(seconds=randint(0, 86400)) while start < date + timedelta(days=1):
start += timedelta(minutes=randint(0, 60 * 2))
end = start + timedelta(minutes=randint(10, 60 * 3))
if end > now:
break
Sleep.objects.create( Sleep.objects.create(child=child, start=start, end=end).save()
child=child, start = end
start=start,
end=start + timedelta(minutes=randint(5, 120))
).save()
for i in (range(0, randint(2, 10))):
start = date + timedelta(seconds=randint(0, 86400))
start = date
while start < date + timedelta(days=1):
if choice([True, False]): if choice([True, False]):
milestone = self.faker.sentence() milestone = self.faker.sentence()
else: else:
milestone = '' milestone = ''
start += timedelta(minutes=randint(0, 60 * 2))
end = start + timedelta(minutes=randint(1, 10))
if end > now:
break
TummyTime.objects.create( TummyTime.objects.create(
child=child, child=child,
start=start, start=start,
end=start + timedelta(minutes=randint(1, 10)), end=end,
milestone=milestone milestone=milestone
).save() ).save()
start = end

View File

@ -8,6 +8,7 @@ from core.models import Child
class SleepReport(PermissionRequiredMixin, DetailView): class SleepReport(PermissionRequiredMixin, DetailView):
"""TODO: Account for sleep sessions crossing midnight."""
model = Child model = Child
permission_required = ('core.view_child',) permission_required = ('core.view_child',)
template_name = 'reports/sleep.html' template_name = 'reports/sleep.html'