2
0
Fork 0

move html import into subcommand

This commit is contained in:
Andreas (@xAndy) Zimmermann 2022-04-29 00:20:50 +02:00
parent 2b5ed73f9b
commit a047862be5
5 changed files with 27 additions and 22 deletions

View File

@ -9,6 +9,7 @@ case "$ROLE" in
rm -rf static/* rm -rf static/*
python manage.py collectstatic python manage.py collectstatic
python manage.py migrate python manage.py migrate
python manage.py importhtml
gunicorn shiftregister.wsgi -b 0.0.0.0:8000 "$@" gunicorn shiftregister.wsgi -b 0.0.0.0:8000 "$@"
;; ;;
worker) worker)

View File

@ -1,29 +1,7 @@
from django.apps import AppConfig from django.apps import AppConfig
from pathlib import Path 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): class PagesConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField" default_auto_field = "django.db.models.BigAutoField"
name = "shiftregister.pages" 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)

View File

@ -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}")