Fix several bugs migrate_objective snapshots command

This commit is contained in:
Lorenz Padberg 2024-02-05 10:46:18 +01:00
parent e338f2e2ad
commit d3b51b1006
1 changed files with 30 additions and 19 deletions

View File

@ -57,10 +57,18 @@ class Command(BaseCommand):
createed_content_blocks = 0 createed_content_blocks = 0
visible_objectives_by_ids = {} visible_objectives_by_ids = {}
snapshot_counter = 0
for module in Module.objects.all(): # .filter(title__contains="Politik mitbestimmen"): for module in Module.objects.all(): # .filter(title__contains="Politik mitbestimmen"):
for snapshot in Snapshot.objects.filter(module=module): for snapshot in Snapshot.objects.filter(module=module):
for objective_group_snapshot in snapshot.objective_groups.through.objects.filter(objective_group__module=module,snapshot=snapshot): group_counter = snapshot.objective_groups.through.objects.filter(objective_group__module=module,
snapshot=snapshot).count()
print(
f"{snapshot_counter} Snapshot id: {snapshot.id} Module: {module.title} {group_counter} groups {snapshot.creator} {snapshot.title}")
snapshot_counter += 1
for objective_group_snapshot in snapshot.objective_groups.through.objects.filter(
objective_group__module=module, snapshot=snapshot):
header = f"{count} {module.title:50} {objective_group_snapshot.objective_group.get_title_display():25} {str(snapshot.creator):40} {objective_group_snapshot.hidden} " header = f"{count} {module.title:50} {objective_group_snapshot.objective_group.get_title_display():25} {str(snapshot.creator):40} {objective_group_snapshot.hidden} "
count += 1 count += 1
objective_group = objective_group_snapshot.objective_group objective_group = objective_group_snapshot.objective_group
@ -72,7 +80,6 @@ class Command(BaseCommand):
group=objective_group_snapshot.objective_group) group=objective_group_snapshot.objective_group)
group_is_hidden = objective_group_snapshot.hidden group_is_hidden = objective_group_snapshot.hidden
info = f"{hidden_default_objectives.count()} {visible_custom_objectives.count()}" info = f"{hidden_default_objectives.count()} {visible_custom_objectives.count()}"
if (not hidden_default_objectives and not visible_custom_objectives and not group_is_hidden): if (not hidden_default_objectives and not visible_custom_objectives and not group_is_hidden):
@ -91,11 +98,14 @@ class Command(BaseCommand):
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, default_objectives = Objective.objects.filter(group=objective_group,
group__module=module, group__module=module,
owner__isnull=True) owner__isnull=True,
visible_default_objectives = [objective for objective in default_objectives if objective.id not in hidden_default_objectives.values_list("id", flat=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)]
# Benutzerdefinierte Lernziele # Benutzerdefinierte Lernziele
visible_custom_objectives = list(snapshot.custom_objectives.filter(hidden=False)) visible_custom_objectives = list(snapshot.custom_objectives.filter(hidden=False))
@ -120,26 +130,27 @@ class Command(BaseCommand):
for objectives in visible_objectives_by_ids.values(): for objectives in visible_objectives_by_ids.values():
print("") print("")
for objective in objectives: for objective in objectives:
print(f" Objective: {objective.group} {objective} {objective.owner}") print(f" Objective: {objective.group} {objective} owner:{objective.owner}")
print("-") print("-")
print(f" visible_objectives_by_ids: {len(visible_objectives_by_ids.items())}") print(f" visible_objectives_by_ids: {len(visible_objectives_by_ids.items())}")
# create custom content blocks with the objectives # create custom content blocks with the objectives
created_content_blocks = 0 created_content_blocks = 0
for objectives in visible_objectives_by_ids.values(): 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,
objectives[0].group, chapter, snapshot, owner=snapshot.creator,
owner=snapshot.creator, prefix="SNAP ")
prefix="SNAP ") #
create_text_in_content_block(objectives, custom_content_block_snapshot, get_or_create=True) objectives = list(visible_objectives_by_ids.values())[0]
created_content_blocks += 1 create_text_in_content_block(objectives, custom_content_block_snapshot, get_or_create=True)
snapshot.save() created_content_blocks += 1
snapshot.save()
print() print()
print(f"Skipped {case1_count} Case 1") print(f"Skipped {case1_count} Case 1")
print(f"Hidden default content groups {case2_count} Case 2") print(f"Hidden default content groups {case2_count} Case 2")