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
|
2022-10-30 02:39:18 +00:00
|
|
|
from django.core.management import call_command
|
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):
|
2022-02-10 00:00:30 +00:00
|
|
|
help = "Reapplies core migrations and generates fake data."
|
2017-08-20 13:44:44 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(Command, self).__init__(*args, **kwargs)
|
|
|
|
self.UserModel = get_user_model()
|
|
|
|
self.username_field = self.UserModel._meta.get_field(
|
2022-02-10 00:00:30 +00:00
|
|
|
self.UserModel.USERNAME_FIELD
|
|
|
|
)
|
2017-08-20 13:44:44 +00:00
|
|
|
|
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):
|
2022-02-10 00:00:30 +00:00
|
|
|
verbosity = options["verbosity"]
|
2017-08-20 13:44:44 +00:00
|
|
|
|
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:
|
2022-02-10 00:00:30 +00:00
|
|
|
self.stdout.write(self.style.SUCCESS("Database flushed."))
|
2017-08-20 13:44:44 +00:00
|
|
|
|
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():
|
2022-02-10 00:00:30 +00:00
|
|
|
if path.split(path.split(config.path)[0])[1] == "babybuddy":
|
2017-08-20 13:44:44 +00:00
|
|
|
migrate = Migrate()
|
2022-02-10 00:00:30 +00:00
|
|
|
options["app_label"] = config.name
|
|
|
|
options["migration_name"] = "zero"
|
2017-08-20 13:44:44 +00:00
|
|
|
|
|
|
|
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()
|
2022-02-10 00:00:30 +00:00
|
|
|
options["app_label"] = None
|
|
|
|
options["migration_name"] = None
|
2017-08-20 13:44:44 +00:00
|
|
|
migrate.handle(*args, **options)
|
|
|
|
|
2022-10-30 02:39:18 +00:00
|
|
|
# Create cache table.
|
|
|
|
call_command("createcachetable")
|
|
|
|
if verbosity > 0:
|
|
|
|
self.stdout.write(self.style.SUCCESS("Cache table created."))
|
|
|
|
|
2020-01-29 05:55:54 +00:00
|
|
|
# Clear cache.
|
|
|
|
cache.clear()
|
|
|
|
if verbosity > 0:
|
2022-02-10 00:00:30 +00:00
|
|
|
self.stdout.write(self.style.SUCCESS("Cache cleared."))
|
2020-01-29 05:55:54 +00:00
|
|
|
|
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:
|
2022-02-10 00:00:30 +00:00
|
|
|
self.stdout.write(self.style.SUCCESS("Database reset complete."))
|