2017-08-19 18:48:13 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from random import randint, choice
|
|
|
|
from datetime import timedelta
|
|
|
|
from decimal import Decimal
|
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
from faker import Factory
|
|
|
|
|
2017-10-22 01:59:09 +00:00
|
|
|
from core.models import Child, DiaperChange, Feeding, Note, Sleep, TummyTime
|
2017-08-19 18:48:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = 'Generates fake children and related entries.'
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(Command, self).__init__(*args, **kwargs)
|
|
|
|
self.faker = Factory.create()
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
parser.add_argument(
|
|
|
|
'--children',
|
|
|
|
dest='children',
|
|
|
|
default=1,
|
|
|
|
help='The number of fake children to create.'
|
|
|
|
)
|
2017-08-21 23:34:06 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--days',
|
|
|
|
dest='days',
|
2017-10-21 22:54:24 +00:00
|
|
|
default=31,
|
2017-08-21 23:34:06 +00:00
|
|
|
help='How many days of fake entries to create.'
|
|
|
|
)
|
2017-08-19 18:48:13 +00:00
|
|
|
|
|
|
|
def handle(self, *args, **kwargs):
|
2017-10-22 01:59:09 +00:00
|
|
|
verbosity = int(kwargs['verbosity'])
|
2017-08-21 23:34:06 +00:00
|
|
|
children = int(kwargs['children']) or 1
|
2017-10-21 22:54:24 +00:00
|
|
|
days = int(kwargs['days']) or 31
|
2017-08-19 18:48:13 +00:00
|
|
|
|
2017-10-21 22:54:24 +00:00
|
|
|
# User first day of data that will created for birth date.
|
|
|
|
birth_date = (timezone.localtime() - timedelta(days=days))
|
2017-08-19 18:48:13 +00:00
|
|
|
for i in range(0, children):
|
|
|
|
child = Child.objects.create(
|
|
|
|
first_name=self.faker.first_name(),
|
|
|
|
last_name=self.faker.last_name(),
|
2017-10-21 22:54:24 +00:00
|
|
|
birth_date=birth_date
|
2017-08-19 18:48:13 +00:00
|
|
|
)
|
|
|
|
child.save()
|
|
|
|
|
2017-10-22 00:00:05 +00:00
|
|
|
for j in range(days - 1, -1, -1):
|
2017-08-23 01:08:31 +00:00
|
|
|
date = (timezone.localtime() - timedelta(days=j)).replace(
|
2017-08-21 23:34:06 +00:00
|
|
|
hour=0, minute=0, second=0)
|
2017-08-19 18:48:13 +00:00
|
|
|
self._add_child_data(child, date)
|
|
|
|
|
|
|
|
if verbosity > 0:
|
|
|
|
self.stdout.write(
|
|
|
|
self.style.SUCCESS('Successfully added fake data.')
|
|
|
|
)
|
|
|
|
|
|
|
|
def _add_child_data(self, child, date):
|
2017-08-23 01:08:31 +00:00
|
|
|
now = timezone.localtime()
|
2017-08-22 13:35:01 +00:00
|
|
|
|
2017-08-19 18:48:13 +00:00
|
|
|
for i in (range(0, randint(5, 20))):
|
|
|
|
solid = choice([True, False])
|
|
|
|
if solid:
|
|
|
|
wet = False
|
|
|
|
color = choice(
|
|
|
|
DiaperChange._meta.get_field('color').choices)[0]
|
|
|
|
else:
|
|
|
|
wet = True
|
|
|
|
color = ''
|
|
|
|
|
2017-08-22 13:35:01 +00:00
|
|
|
time = date + timedelta(minutes=randint(0, 60 * 24))
|
2017-08-19 18:48:13 +00:00
|
|
|
|
2017-08-22 13:35:01 +00:00
|
|
|
if time < now:
|
|
|
|
DiaperChange.objects.create(
|
|
|
|
child=child,
|
|
|
|
time=time,
|
|
|
|
wet=wet,
|
|
|
|
solid=solid,
|
|
|
|
color=color
|
|
|
|
).save()
|
2017-08-19 18:48:13 +00:00
|
|
|
|
2017-08-23 01:35:36 +00:00
|
|
|
last_end = date
|
|
|
|
while last_end < date + timedelta(days=1):
|
2017-08-22 13:35:01 +00:00
|
|
|
method = choice(Feeding._meta.get_field('method').choices)[0]
|
2017-08-19 18:48:13 +00:00
|
|
|
if method is 'bottle':
|
|
|
|
amount = Decimal('%d.%d' % (randint(0, 6), randint(0, 9)))
|
|
|
|
else:
|
|
|
|
amount = None
|
|
|
|
|
2017-08-23 01:35:36 +00:00
|
|
|
start = last_end + timedelta(minutes=randint(0, 60 * 2))
|
2017-08-22 13:35:01 +00:00
|
|
|
end = start + timedelta(minutes=randint(5, 20))
|
|
|
|
if end > now:
|
|
|
|
break
|
|
|
|
|
2017-08-19 18:48:13 +00:00
|
|
|
Feeding.objects.create(
|
|
|
|
child=child,
|
|
|
|
start=start,
|
2017-08-22 13:35:01 +00:00
|
|
|
end=end,
|
2017-08-19 18:48:13 +00:00
|
|
|
type=choice(Feeding._meta.get_field('type').choices)[0],
|
|
|
|
method=method,
|
|
|
|
amount=amount
|
|
|
|
).save()
|
2017-08-23 01:35:36 +00:00
|
|
|
last_end = end
|
2017-08-19 18:48:13 +00:00
|
|
|
|
2017-08-23 01:35:36 +00:00
|
|
|
last_end = date
|
2017-10-22 00:00:05 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2017-08-23 01:35:36 +00:00
|
|
|
while last_end < date + timedelta(days=1):
|
|
|
|
start = last_end + timedelta(minutes=randint(0, 60 * 2))
|
2017-10-21 22:54:24 +00:00
|
|
|
if start.date() != date.date():
|
|
|
|
break
|
|
|
|
|
2017-08-22 13:35:01 +00:00
|
|
|
end = start + timedelta(minutes=randint(10, 60 * 3))
|
|
|
|
if end > now:
|
|
|
|
break
|
2017-08-19 18:48:13 +00:00
|
|
|
|
2017-08-22 13:35:01 +00:00
|
|
|
Sleep.objects.create(child=child, start=start, end=end).save()
|
2017-08-23 01:35:36 +00:00
|
|
|
last_end = end
|
2017-08-19 18:48:13 +00:00
|
|
|
|
2017-08-23 01:35:36 +00:00
|
|
|
last_end = date
|
|
|
|
while last_end < date + timedelta(days=1):
|
2017-08-19 18:48:13 +00:00
|
|
|
if choice([True, False]):
|
|
|
|
milestone = self.faker.sentence()
|
|
|
|
else:
|
|
|
|
milestone = ''
|
|
|
|
|
2017-10-22 00:00:05 +00:00
|
|
|
start = last_end + timedelta(minutes=randint(0, 60 * 5))
|
2017-08-22 13:35:01 +00:00
|
|
|
end = start + timedelta(minutes=randint(1, 10))
|
|
|
|
if end > now:
|
|
|
|
break
|
|
|
|
|
2017-08-19 18:48:13 +00:00
|
|
|
TummyTime.objects.create(
|
|
|
|
child=child,
|
|
|
|
start=start,
|
2017-08-22 13:35:01 +00:00
|
|
|
end=end,
|
2017-08-19 18:48:13 +00:00
|
|
|
milestone=milestone
|
|
|
|
).save()
|
2017-08-23 01:35:36 +00:00
|
|
|
last_end = end
|
2017-10-22 01:59:09 +00:00
|
|
|
|
|
|
|
note = self.faker.sentence()
|
|
|
|
Note.objects.create(
|
|
|
|
child=child,
|
|
|
|
note=note,
|
|
|
|
time=date + timedelta(minutes=randint(0, 60 * 24))
|
|
|
|
).save()
|