Resolve comments from pull request

This commit is contained in:
Lorenz Padberg 2024-02-07 11:15:34 +01:00
parent 0631b42867
commit fa68f68a6e
2 changed files with 31 additions and 52 deletions

View File

@ -3,9 +3,9 @@ from logging import getLogger
from django.core.management import BaseCommand from django.core.management import BaseCommand
from books.management.commands.migrate_objectives_to_content import create_chapter_from_objective_group, \ from books.management.commands.migrate_objectives_to_content import create_text_in_content_block, \
create_content_block_from_objective, create_text_in_content_block, create_content_block_snapshot_from_objective, \ create_content_block_snapshot_from_objective, \
create_chapter_snapshot_from_objective_group, create_content_block_contents create_content_block_contents
from books.models import Chapter, ObjectiveGroupSnapshot, ContentBlockSnapshot, Snapshot, ChapterSnapshot from books.models import Chapter, ObjectiveGroupSnapshot, ContentBlockSnapshot, Snapshot, ChapterSnapshot
from books.models import ContentBlock from books.models import ContentBlock
from books.models import Module from books.models import Module
@ -103,15 +103,9 @@ def migrate_snapshots():
if hidden_default_objectives or visible_custom_objectives: if hidden_default_objectives or visible_custom_objectives:
print(header + f"Case 3 - {info} create custom content blocks") print(header + f"Case 3 - {info} create custom content blocks")
case3_count += 1 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 # Benutzerdefinierte Lernziele
visible_custom_objectives = list(snapshot.custom_objectives.filter(hidden=False)) visible_custom_objectives = list(snapshot.custom_objectives.filter(hidden=False))
@ -145,13 +139,12 @@ def migrate_snapshots():
chapter = module.get_first_child() chapter = module.get_first_child()
if "Lernziele" not in chapter.title: if "Lernziele" not in chapter.title:
raise Exception(f"Chapter does not contain 'Lernziele' first title is {chapter.title}") raise Exception(f"Chapter does not contain 'Lernziele' first title is {chapter.title}")
#
# Owner des custom blocks festlegen # Owner des custom blocks festlegen
custom_content_block_snapshot = create_content_block_snapshot_from_objective( custom_content_block_snapshot = create_content_block_snapshot_from_objective(
objective_group, chapter, snapshot, objective_group, chapter, snapshot,
owner=snapshot.creator, owner=snapshot.creator)
prefix="SNAP ")
#
# Hide default content block for this objective group, since custom content block is created # 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) default_content_block = get_default_content_block(objective_group_snapshot, module)
if default_content_block: if default_content_block:
@ -204,30 +197,16 @@ def get_default_content_block(objective_group_snapshot, module):
raise Exception("Content block does not exist ") raise Exception("Content block does not exist ")
def get_visible_default_objectives(): def get_visible_default_objectives(objective_group, module, snapshot):
# TODO: Objective Group geht so nicth default_objectives = Objective.objects.filter(group=objective_group,
default_objectives = Objective.objects.filter(group=ObjectiveGroup, group__module=module, owner__isnull=True) group__module=module,
visible_objectives = list(default_objectives) owner__isnull=True,
visible_objectives_ids = [objective.id for objective in visible_objectives] objectivesnapshot__isnull=True)
hidden_default_objectives = snapshot.hidden_objectives.filter(group=objective_group)
# Verlags Lernziele visible_default_objectives = [objective for objective in default_objectives if
for hidden_objective in snapshot.hidden_objectives.all(): objective.id not in hidden_default_objectives.values_list("id",
visible_objectives = [objective for objective in visible_objectives if flat=True)]
hidden_objective.id not in visible_objectives_ids] return visible_default_objectives
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 analyze(): def analyze():

View File

@ -59,24 +59,25 @@ def migrate_objectives_to_content():
print(f"Owner: {owner}") print(f"Owner: {owner}")
print(f" Objectives: ") 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 custom_content_block = None
print(f" School class: {school_class}") print(f" School class: {school_class}")
# merge "Verlagsinhalte" and "benutzerdefinierte Inhalte" # merge "Verlagsinhalte" and "benutzerdefinierte Inhalte"
visible_owner_objectives = [objective for objective in owner_objectives if not objective.is_hidden_for_class(school_class)] 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) 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") print(f" Objective: Reuse default content block")
# custom_content_block = default_content_block # custom_content_block = default_content_block
# Create content block if that set of objectives has not been created yet # 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: for objective in merged_objectives:
print(f" Objective: {objective} {objective.owner}") 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, custom_content_block = create_content_block_from_objective(objective_group,
chapter, chapter,
owner=owner, owner=owner,
prefix=f"CUSTOM {owner.username} ") )
contentblocks_by_merged_objectives_ids[ contentblocks_by_merged_objectives_ids[
merged_objectives_ids] = custom_content_block merged_objectives_ids] = custom_content_block
create_text_in_content_block(merged_objectives, custom_content_block) create_text_in_content_block(merged_objectives, custom_content_block)
created_content_blocks += 1 created_content_blocks += 1
else: else:
if merged_objectives_ids != default_objectives_ids: if not is_default_content:
print(f" Objective: Reuse content block") print(f" Objective: Reuse content block")
custom_content_block = contentblocks_by_merged_objectives_ids[ custom_content_block = contentblocks_by_merged_objectives_ids[
merged_objectives_ids] merged_objectives_ids]
@ -134,7 +135,7 @@ def create_default_content(objective_group, chapter):
default_objectives = list( default_objectives = list(
objective_group.objectives.filter(owner__isnull=True, objectivesnapshot__isnull=True).order_by('id')) 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) create_text_in_content_block(default_objectives, default_content_block)
for objective in default_objectives: for objective in default_objectives:
@ -143,7 +144,7 @@ def create_default_content(objective_group, chapter):
return default_content_block return default_content_block
def remove_hidden_objectives(objectives, user): def filter_visible_objectives_by_class(objectives, user):
school_classes = user.school_classes.all() school_classes = user.school_classes.all()
visible_objectives = {} visible_objectives = {}
for school_class in school_classes: 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): def get_objectives_by_owner(objective_group, exclude_snapshots=True):
custom_objectives = objective_group.objectives.filter(owner__isnull=False).exclude( custom_objectives = objective_group.objectives.filter(owner__isnull=False, objectivesnapshot__isnull=True).order_by('order')
objectivesnapshot__isnull=False).order_by('order')
custom_objectives_by_owner = {} custom_objectives_by_owner = {}
for objective in custom_objectives: for objective in custom_objectives:
@ -202,7 +202,7 @@ def create_chapter_snapshot_from_objective_group(module, snapshot, prefix="XXX "
return chapter 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( content_block = ContentBlock(
title=f"{objective_group.get_title_display()}", title=f"{objective_group.get_title_display()}",
type="normal", type="normal",
@ -214,7 +214,7 @@ def create_content_block_from_objective(objective_group, chapter, owner=None, pr
return content_block 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( content_block_snapshot = ContentBlockSnapshot(
title=f"{objective_group.get_title_display()}", title=f"{objective_group.get_title_display()}",
type="normal", type="normal",