diff --git a/server/books/management/commands/migrate_objectives_to_content.py b/server/books/management/commands/migrate_objectives_to_content.py index 22d3a505..f721e39b 100644 --- a/server/books/management/commands/migrate_objectives_to_content.py +++ b/server/books/management/commands/migrate_objectives_to_content.py @@ -1,18 +1,13 @@ import json +from logging import getLogger 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 django.db.models import Avg, Count, Min, Sum +from books.models import Chapter from books.models import ContentBlock -from users.models import SchoolClass - +from books.models import Module +logger = getLogger(__name__) class Command(BaseCommand): def handle(self, *args, **options): ContentBlock.objects.filter(title__startswith="TESTOBJECTIVE").delete() @@ -22,49 +17,61 @@ class Command(BaseCommand): createed_content_blocks = 0 - for module in Module.objects.filter(title="Politische Streitfragen"): - print(f"Module: {module}") - chapter = create_chapter_from_objective_group(module) + failed_modules = [] - for objective_group in module.objective_groups.all(): - default_content_block = create_default_content(objective_group, chapter) + for module in Module.objects.all(): + try: + chapter = create_chapter_from_objective_group(module) - # Create "Benutzerdefinierte Lernziele" - custom_objectives_by_owner = get_objectives_by_owner(objective_group) + for objective_group in module.objective_groups.all(): + default_content_block = create_default_content(objective_group, chapter) - for owner, owner_objectives in custom_objectives_by_owner.items(): - print(f" Owner: {owner}") - print(f" Objectives: ") + # Create "Benutzerdefinierte Lernziele" + custom_objectives_by_owner = get_objectives_by_owner(objective_group) - 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 owner, owner_objectives in custom_objectives_by_owner.items(): + print(f" Owner: {owner}") + print(f" Objectives: ") - 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) + 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 = {} - # 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}") - 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") + 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) - # 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) + # 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): @@ -73,13 +80,14 @@ def create_default_content(objective_group, chapter): # create "Verlagsinhalt Lernziele" - default_objectives = objective_group.objectives.filter(owner__isnull=True) + 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 @@ -136,7 +144,7 @@ def create_content_block_from_objective(objective_group, chapter, owner=None): def create_text_in_content_block(objectives, content_block): print(content_block.contents) - objective_li = [f"