Create migration test command

This commit is contained in:
Lorenz Padberg 2023-12-15 23:17:10 +01:00
parent 992b9945e5
commit 48d3c34fbe
1 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,79 @@
import json
from django.core.management import BaseCommand
from books.blocks import TextBlock
from books.categorize_modules import categorize_modules, delete_unused_levels, uncategorize_modules, \
delete_unused_categories
from books.categorize_modules import create_default_levels_and_categories
from books.models import Module
from books.models import ContentBlock, Chapter
from books.models import ContentBlock
class Command(BaseCommand):
def handle(self, *args, **options):
ContentBlock.objects.filter(title__startswith="TESTOBJECTIVE").delete()
Chapter.objects.filter(title__startswith="TESTOBJECTIVE").delete()
ContentBlock.objects.filter(title__startswith="XXX").delete()
Chapter.objects.filter(title__startswith="XXX").delete()
for module in Module.objects.filter(title="Politische Streitfragen"):
print(f"Module: {module}")
chapter = create_chapter_from_objective_group(module)
for objective_group in module.objective_groups.all():
print(f" Objective group: {objective_group}")
print(" Default objectives:")
content_block = create_content_block_from_objective(objective_group, chapter)
default_objectives = objective_group.objectives.filter(owner__isnull=True)
content_block = create_text_in_content_block(default_objectives, content_block)
for objective in default_objectives:
print(f" Objective: {objective} {objective.owner}")
custom_objectives = objective_group.objectives.filter(owner__isnull=False)
create_text_in_content_block(custom_objectives, content_block)
print(" Custom objectives:")
for objective in custom_objectives:
print(f" Objective: {objective} {objective.owner}")
print("")
def create_chapter_from_objective_group(module):
chapter = Chapter(title=f"XXX Leistungsziele")
#
first_sibling = module.get_first_child()
if first_sibling is not None:
first_sibling.add_sibling(instance=chapter, pos='left')
chapter.save_revision().publish()
chapter.save()
return chapter
def create_content_block_from_objective(objective_group, chapter):
content_block = ContentBlock(
title=f"XXX {objective_group.title}",
type="normal"
)
chapter.add_child(instance=content_block)
return content_block
def create_text_in_content_block( objectives, content_block):
print(content_block.contents)
texts = [{'type': 'text_block',
'value': {
'text': f"- {objective.text}"
}} for objective in objectives]
content_block.contents = json.dumps(texts)
content_block.save_revision().publish()
return content_block