From a047862be503b2f29a010841f218124e67d0aab1 Mon Sep 17 00:00:00 2001 From: "Andreas (@xAndy) Zimmermann" Date: Fri, 29 Apr 2022 00:20:50 +0200 Subject: [PATCH] move html import into subcommand --- entrypoint.sh | 1 + shiftregister/pages/apps.py | 22 ---------------- shiftregister/pages/management/__init__.py | 0 .../pages/management/commands/__init__.py | 0 .../pages/management/commands/importhtml.py | 26 +++++++++++++++++++ 5 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 shiftregister/pages/management/__init__.py create mode 100644 shiftregister/pages/management/commands/__init__.py create mode 100644 shiftregister/pages/management/commands/importhtml.py diff --git a/entrypoint.sh b/entrypoint.sh index c2e812f..8bfab4b 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -9,6 +9,7 @@ case "$ROLE" in rm -rf static/* python manage.py collectstatic python manage.py migrate + python manage.py importhtml gunicorn shiftregister.wsgi -b 0.0.0.0:8000 "$@" ;; worker) diff --git a/shiftregister/pages/apps.py b/shiftregister/pages/apps.py index d81fff3..b1e3e3d 100644 --- a/shiftregister/pages/apps.py +++ b/shiftregister/pages/apps.py @@ -1,29 +1,7 @@ from django.apps import AppConfig from pathlib import Path -from django.db.models.signals import post_migrate - - -def read_pages(sender, **kwargs): - from .models import Page - - print("pages init") - content_path = Path(__file__).resolve().parent / "default_content" - for file in content_path.iterdir(): - slug = file.stem - p, created = Page.objects.get_or_create(url=slug) - if not created: - continue - p.content = file.read_text() - p.title = slug - p.save() - print(f"created new page for slug {slug}") class PagesConfig(AppConfig): default_auto_field = "django.db.models.BigAutoField" name = "shiftregister.pages" - - def ready(self): - # we use thios signal so our code is not called before the schema is migrated - # and not during makemigrations/migrate - post_migrate.connect(read_pages, sender=self) diff --git a/shiftregister/pages/management/__init__.py b/shiftregister/pages/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/shiftregister/pages/management/commands/__init__.py b/shiftregister/pages/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/shiftregister/pages/management/commands/importhtml.py b/shiftregister/pages/management/commands/importhtml.py new file mode 100644 index 0000000..24dccf2 --- /dev/null +++ b/shiftregister/pages/management/commands/importhtml.py @@ -0,0 +1,26 @@ +from django.core.management.base import BaseCommand, CommandError +from ...models import Page +from pathlib import Path + + +class Command(BaseCommand): + help = "add missing cms files to database" + + def add_arguments(self, parser): + parser.add_argument( + "--force", + action="store_true", + help="Always overwrite content", + ) + + def handle(self, *args, **options): + content_path = Path(__file__).resolve().parent.parent.parent / "default_content" + for file in content_path.iterdir(): + slug = file.stem + p, created = Page.objects.get_or_create(url=slug) + if (not created) and (not options["force"]): + continue + p.content = file.read_text() + p.title = slug + p.save() + print(f"created new page for slug {slug}")