From fa68f68a6ea87c977eed5d762d7982b5f8380e6c Mon Sep 17 00:00:00 2001 From: Lorenz Padberg Date: Wed, 7 Feb 2024 11:15:34 +0100 Subject: [PATCH] Resolve comments from pull request --- .../commands/migrate_objective_snapshots.py | 57 ++++++------------- .../commands/migrate_objectives_to_content.py | 26 ++++----- 2 files changed, 31 insertions(+), 52 deletions(-) diff --git a/server/books/management/commands/migrate_objective_snapshots.py b/server/books/management/commands/migrate_objective_snapshots.py index 9049466c..5f053bd5 100644 --- a/server/books/management/commands/migrate_objective_snapshots.py +++ b/server/books/management/commands/migrate_objective_snapshots.py @@ -3,9 +3,9 @@ from logging import getLogger from django.core.management import BaseCommand -from books.management.commands.migrate_objectives_to_content import create_chapter_from_objective_group, \ - create_content_block_from_objective, create_text_in_content_block, create_content_block_snapshot_from_objective, \ - create_chapter_snapshot_from_objective_group, create_content_block_contents +from books.management.commands.migrate_objectives_to_content import create_text_in_content_block, \ + create_content_block_snapshot_from_objective, \ + create_content_block_contents from books.models import Chapter, ObjectiveGroupSnapshot, ContentBlockSnapshot, Snapshot, ChapterSnapshot from books.models import ContentBlock from books.models import Module @@ -103,15 +103,9 @@ def migrate_snapshots(): if hidden_default_objectives or visible_custom_objectives: print(header + f"Case 3 - {info} create custom content blocks") case3_count += 1 - # Verlags Lernziele - default_objectives = Objective.objects.filter(group=objective_group, - group__module=module, - owner__isnull=True, - objectivesnapshot__isnull=True) - visible_default_objectives = [objective for objective in default_objectives if - objective.id not in hidden_default_objectives.values_list("id", - flat=True)] + # Verlags Lernziele + visible_default_objectives = get_visible_default_objectives(objective_group, module, snapshot) # Benutzerdefinierte Lernziele visible_custom_objectives = list(snapshot.custom_objectives.filter(hidden=False)) @@ -145,13 +139,12 @@ def migrate_snapshots(): chapter = module.get_first_child() if "Lernziele" not in chapter.title: raise Exception(f"Chapter does not contain 'Lernziele' first title is {chapter.title}") - # + # Owner des custom blocks festlegen custom_content_block_snapshot = create_content_block_snapshot_from_objective( objective_group, chapter, snapshot, - owner=snapshot.creator, - prefix="SNAP ") - # + owner=snapshot.creator) + # Hide default content block for this objective group, since custom content block is created default_content_block = get_default_content_block(objective_group_snapshot, module) if default_content_block: @@ -204,30 +197,16 @@ def get_default_content_block(objective_group_snapshot, module): raise Exception("Content block does not exist ") -def get_visible_default_objectives(): - # TODO: Objective Group geht so nicth - default_objectives = Objective.objects.filter(group=ObjectiveGroup, group__module=module, owner__isnull=True) - visible_objectives = list(default_objectives) - visible_objectives_ids = [objective.id for objective in visible_objectives] - - # Verlags Lernziele - for hidden_objective in snapshot.hidden_objectives.all(): - visible_objectives = [objective for objective in visible_objectives if - hidden_objective.id not in visible_objectives_ids] - return visible_objectives - - -def get_content_block_by_objectives(objectives, module, user_created=False, user=None): - contents = create_content_block_contents(objectives) - - content_block_qs = ContentBlock.objects.filter( - owner=user, - user_created=user_created, - contents=contents) - if content_block_qs.exists(): - return content_block_qs.first() - else: - raise Exception("Content block does not exist ") +def get_visible_default_objectives(objective_group, module, snapshot): + default_objectives = Objective.objects.filter(group=objective_group, + group__module=module, + owner__isnull=True, + objectivesnapshot__isnull=True) + hidden_default_objectives = snapshot.hidden_objectives.filter(group=objective_group) + visible_default_objectives = [objective for objective in default_objectives if + objective.id not in hidden_default_objectives.values_list("id", + flat=True)] + return visible_default_objectives def analyze(): diff --git a/server/books/management/commands/migrate_objectives_to_content.py b/server/books/management/commands/migrate_objectives_to_content.py index 0b828fd1..9f5df387 100644 --- a/server/books/management/commands/migrate_objectives_to_content.py +++ b/server/books/management/commands/migrate_objectives_to_content.py @@ -59,24 +59,25 @@ def migrate_objectives_to_content(): print(f"Owner: {owner}") print(f" Objectives: ") - visible_default_objectives_by_class = remove_hidden_objectives(default_objectives, owner) + visible_default_objectives_by_class = filter_visible_objectives_by_class(default_objectives, owner) - for school_class in visible_default_objectives_by_class.keys(): + for school_class, default_objectives_for_class in visible_default_objectives_by_class.items(): custom_content_block = None print(f" School class: {school_class}") # merge "Verlagsinhalte" and "benutzerdefinierte Inhalte" visible_owner_objectives = [objective for objective in owner_objectives if not objective.is_hidden_for_class(school_class)] - merged_objectives = visible_default_objectives_by_class[school_class] + visible_owner_objectives + merged_objectives = default_objectives_for_class + visible_owner_objectives merged_objectives_ids = tuple(objective.id for objective in merged_objectives) + is_default_content = merged_objectives_ids == default_objectives_ids - if merged_objectives_ids == default_objectives_ids: + if is_default_content: print(f" Objective: Reuse default content block") # custom_content_block = default_content_block # Create content block if that set of objectives has not been created yet - if merged_objectives_ids not in contentblocks_by_merged_objectives_ids and merged_objectives_ids != default_objectives_ids: + if merged_objectives_ids not in contentblocks_by_merged_objectives_ids and not is_default_content: for objective in merged_objectives: print(f" Objective: {objective} {objective.owner}") @@ -84,13 +85,13 @@ def migrate_objectives_to_content(): custom_content_block = create_content_block_from_objective(objective_group, chapter, owner=owner, - prefix=f"CUSTOM {owner.username} ") + ) contentblocks_by_merged_objectives_ids[ merged_objectives_ids] = custom_content_block create_text_in_content_block(merged_objectives, custom_content_block) created_content_blocks += 1 else: - if merged_objectives_ids != default_objectives_ids: + if not is_default_content: print(f" Objective: Reuse content block") custom_content_block = contentblocks_by_merged_objectives_ids[ merged_objectives_ids] @@ -134,7 +135,7 @@ def create_default_content(objective_group, chapter): default_objectives = list( objective_group.objectives.filter(owner__isnull=True, objectivesnapshot__isnull=True).order_by('id')) - default_content_block = create_content_block_from_objective(objective_group, chapter, prefix="XXX YYY ") + 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: @@ -143,7 +144,7 @@ def create_default_content(objective_group, chapter): return default_content_block -def remove_hidden_objectives(objectives, user): +def filter_visible_objectives_by_class(objectives, user): school_classes = user.school_classes.all() visible_objectives = {} for school_class in school_classes: @@ -155,8 +156,7 @@ def remove_hidden_objectives(objectives, user): def get_objectives_by_owner(objective_group, exclude_snapshots=True): - custom_objectives = objective_group.objectives.filter(owner__isnull=False).exclude( - objectivesnapshot__isnull=False).order_by('order') + custom_objectives = objective_group.objectives.filter(owner__isnull=False, objectivesnapshot__isnull=True).order_by('order') custom_objectives_by_owner = {} for objective in custom_objectives: @@ -202,7 +202,7 @@ def create_chapter_snapshot_from_objective_group(module, snapshot, prefix="XXX " return chapter -def create_content_block_from_objective(objective_group, chapter, owner=None, prefix="XXX "): +def create_content_block_from_objective(objective_group, chapter, owner=None): content_block = ContentBlock( title=f"{objective_group.get_title_display()}", type="normal", @@ -214,7 +214,7 @@ def create_content_block_from_objective(objective_group, chapter, owner=None, pr return content_block -def create_content_block_snapshot_from_objective(objective_group, chapter, snapshot, owner=None, prefix="XXX "): +def create_content_block_snapshot_from_objective(objective_group, chapter, snapshot, owner=None): content_block_snapshot = ContentBlockSnapshot( title=f"{objective_group.get_title_display()}", type="normal",