2017-08-20 13:44:44 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-08-22 13:32:29 +00:00
|
|
|
from os import path
|
|
|
|
|
2017-08-20 13:44:44 +00:00
|
|
|
from django.apps import apps
|
|
|
|
from django.contrib.auth import get_user_model
|
2020-01-29 05:55:54 +00:00
|
|
|
from django.core.cache import cache
|
2017-08-20 13:44:44 +00:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
|
|
from django.core.management.commands.flush import Command as Flush
|
|
|
|
|
|
|
|
from .fake import Command as Fake
|
2020-01-29 04:45:37 +00:00
|
|
|
from .migrate import Command as Migrate
|
2017-08-20 13:44:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = 'Reapplies core migrations and generates fake data.'
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(Command, self).__init__(*args, **kwargs)
|
|
|
|
self.UserModel = get_user_model()
|
|
|
|
self.username_field = self.UserModel._meta.get_field(
|
|
|
|
self.UserModel.USERNAME_FIELD)
|
|
|
|
|
2020-08-13 02:58:53 +00:00
|
|
|
# Disable system checks for reset.
|
|
|
|
self.requires_system_checks = False
|
|
|
|
|
2017-08-20 13:44:44 +00:00
|
|
|
def add_arguments(self, parser):
|
|
|
|
Migrate().add_arguments(parser)
|
|
|
|
Fake().add_arguments(parser)
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
verbosity = options['verbosity']
|
|
|
|
|
2020-01-29 05:12:26 +00:00
|
|
|
# Flush all existing database records.
|
2017-08-20 13:44:44 +00:00
|
|
|
flush = Flush()
|
|
|
|
flush.handle(**options)
|
|
|
|
if verbosity > 0:
|
|
|
|
self.stdout.write(self.style.SUCCESS('Database flushed.'))
|
|
|
|
|
2020-01-29 05:12:26 +00:00
|
|
|
# Run migrations for all Baby Buddy apps.
|
2017-08-20 13:44:44 +00:00
|
|
|
for config in apps.app_configs.values():
|
2017-10-22 18:00:42 +00:00
|
|
|
if path.split(path.split(config.path)[0])[1] == 'babybuddy':
|
2017-08-20 13:44:44 +00:00
|
|
|
migrate = Migrate()
|
|
|
|
options['app_label'] = config.name
|
|
|
|
options['migration_name'] = 'zero'
|
|
|
|
|
|
|
|
try:
|
|
|
|
migrate.handle(*args, **options)
|
|
|
|
except CommandError:
|
|
|
|
# Ignore apps without migrations.
|
|
|
|
pass
|
|
|
|
|
2020-01-29 05:12:26 +00:00
|
|
|
# Run other migrations.
|
2017-08-20 13:44:44 +00:00
|
|
|
migrate = Migrate()
|
|
|
|
options['app_label'] = None
|
|
|
|
options['migration_name'] = None
|
|
|
|
migrate.handle(*args, **options)
|
|
|
|
|
2020-01-29 05:55:54 +00:00
|
|
|
# Clear cache.
|
|
|
|
cache.clear()
|
|
|
|
if verbosity > 0:
|
|
|
|
self.stdout.write(self.style.SUCCESS('Cache cleared.'))
|
|
|
|
|
2020-01-29 04:45:37 +00:00
|
|
|
# Populate database with fake data.
|
2017-08-20 13:44:44 +00:00
|
|
|
fake = Fake()
|
|
|
|
fake.handle(*args, **options)
|
|
|
|
|
|
|
|
if verbosity > 0:
|
|
|
|
self.stdout.write(self.style.SUCCESS('Database reset complete.'))
|