import json from logging import getLogger from django.core.management import BaseCommand from books.models import Chapter from books.models import ContentBlock from books.models import Module logger = getLogger(__name__) 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() createed_content_blocks = 0 failed_modules = [] for module in Module.objects.all(): try: chapter = create_chapter_from_objective_group(module) for objective_group in module.objective_groups.all(): default_content_block = create_default_content(objective_group, chapter) # Create "Benutzerdefinierte Lernziele" custom_objectives_by_owner = get_objectives_by_owner(objective_group) for owner, owner_objectives in custom_objectives_by_owner.items(): print(f" Owner: {owner}") print(f" Objectives: ") default_objectives = list(objective_group.objectives.filter(owner__isnull=True)) visible_default_objectives_by_class = remove_hidden_objectives(default_objectives, owner) contentblocks_by_merged_objectives_ids = {} for school_class in visible_default_objectives_by_class.keys(): print(f" School class: {school_class}") merged_objectives = visible_default_objectives_by_class[school_class] + owner_objectives merged_objectives_ids = tuple(objective.id for objective in merged_objectives) # Create content block if that set of objectives has not been created yet if merged_objectives_ids not in contentblocks_by_merged_objectives_ids: for objective in merged_objectives: print(f" Objective: {objective} {objective.owner}") pass custom_content_block = create_content_block_from_objective(objective_group, chapter, owner=owner) contentblocks_by_merged_objectives_ids[merged_objectives_ids] = custom_content_block create_text_in_content_block(merged_objectives, custom_content_block) createed_content_blocks += 1 else: print(f" Objective: Reuse content block") pass # set visibility current_content_block = contentblocks_by_merged_objectives_ids[merged_objectives_ids] current_content_block.visible_for.add(school_class) # hide default objectives content if custom content exists if custom_content_block: default_content_block.hidden_for.add(school_class) except Exception as e: print(e) print(f"Error with module {module}") logger.error(e) failed_modules.append(module) print(f"Created {createed_content_blocks} content blocks") print(f"Failed modules: {len(failed_modules)}") for module in failed_modules: print(f"Faile module: {module}") def create_default_content(objective_group, chapter): print(f" Objective group: {objective_group}") print(" Default objectives:") # create "Verlagsinhalt Lernziele" default_objectives = objective_group.objectives.filter(owner__isnull=True).order_by('id') default_content_block = create_content_block_from_objective(objective_group, chapter) create_text_in_content_block(default_objectives, default_content_block) for objective in default_objectives: print(f" Objective: {objective} {objective.owner}") pass return default_content_block def remove_hidden_objectives(objectives, user): school_classes = user.school_classes.all() visible_objectives = {} for school_class in school_classes: if school_class not in visible_objectives: visible_objectives[school_class] = [] objectives_vis = [objective for objective in objectives if not objective.is_hidden_for_class(school_class)] visible_objectives[school_class].extend(objectives_vis) return visible_objectives def get_objectives_by_owner(objective_group): custom_objectives = objective_group.objectives.filter(owner__isnull=False) custom_objectives_by_owner = {} for objective in custom_objectives: owner = objective.owner if owner not in custom_objectives_by_owner: custom_objectives_by_owner[owner] = [] custom_objectives_by_owner[owner].append(objective) return custom_objectives_by_owner def create_chapter_from_objective_group(module): chapter = Chapter(title=f"XXX Lernziele") 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, owner=None): content_block = ContentBlock( title=f"XXX {objective_group.get_title_display()}", type="normal", owner=owner, user_created=owner is not None, ) chapter.add_child(instance=content_block) return content_block def create_text_in_content_block(objectives, content_block): print(content_block.contents) objective_li = [f"