2
0

reset_demo.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from django.conf import settings
  2. from django.core.cache import caches
  3. from django.core.files.storage import default_storage
  4. from django.core.management import call_command
  5. from django.core.management.base import BaseCommand, CommandError
  6. from django.db import DEFAULT_DB_ALIAS
  7. from django_extensions.settings import POSTGRESQL_ENGINES
  8. from wagtail.documents import get_document_model
  9. from wagtail.images import get_image_model
  10. class Command(BaseCommand):
  11. def handle(self, **options):
  12. if settings.DATABASES[DEFAULT_DB_ALIAS]["ENGINE"] not in POSTGRESQL_ENGINES:
  13. raise CommandError(
  14. "This command can be used only with PostgreSQL databases."
  15. )
  16. # 1. (optional) Remove all objects from S3
  17. if "s3" in settings.DEFAULT_FILE_STORAGE:
  18. self.stdout.write("Removing files from S3")
  19. default_storage.bucket.objects.all().delete()
  20. else:
  21. self.stdout.write("Removing images")
  22. get_image_model().objects.all().delete()
  23. self.stdout.write("Removing documents")
  24. get_document_model().objects.all().delete()
  25. # 2. Reset database to nothing
  26. self.stdout.write("Reset schema")
  27. call_command("reset_schema", interactive=False)
  28. # 3. Rebuild database
  29. call_command("migrate", interactive=False)
  30. # 4. Clear caches
  31. for cache in caches.all():
  32. cache.clear()
  33. # 5. Re-import data
  34. call_command("load_initial_data")
  35. # 6. Change the admin password (in case it's different in this environment)
  36. call_command("reset_admin_password")