Add tests for objectives migration
This commit is contained in:
parent
106bad4c52
commit
0631b42867
|
|
@ -9,8 +9,6 @@
|
||||||
<section class="snapshot-header__section">
|
<section class="snapshot-header__section">
|
||||||
<h2 class="snapshot-header__subtitle">In diesem Snapshot sind {{ changesCount }} Anpassungen gespeichert:</h2>
|
<h2 class="snapshot-header__subtitle">In diesem Snapshot sind {{ changesCount }} Anpassungen gespeichert:</h2>
|
||||||
<ul class="snapshot-header__list">
|
<ul class="snapshot-header__list">
|
||||||
<li class="snapshot-header__list-item">{{ hiddenObjectives }} Lernziele wurden ausgeblendet</li>
|
|
||||||
<li class="snapshot-header__list-item">{{ newObjectives }} Lernziele wurde erfasst</li>
|
|
||||||
<li class="snapshot-header__list-item">{{ hiddenContentBlocks }} Inhaltsblöcke wurden ausgeblendet</li>
|
<li class="snapshot-header__list-item">{{ hiddenContentBlocks }} Inhaltsblöcke wurden ausgeblendet</li>
|
||||||
<li class="snapshot-header__list-item">{{ newContentBlocks }} Inhaltsblock wurde erfasst</li>
|
<li class="snapshot-header__list-item">{{ newContentBlocks }} Inhaltsblock wurde erfasst</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
|
||||||
|
|
@ -49,112 +49,124 @@ class Command(BaseCommand):
|
||||||
ChapterSnapshot.objects.filter(chapter__title__startswith=prefix).delete()
|
ChapterSnapshot.objects.filter(chapter__title__startswith=prefix).delete()
|
||||||
|
|
||||||
analyze()
|
analyze()
|
||||||
count = 0
|
|
||||||
|
|
||||||
case1_count = 0
|
migrate_snapshots()
|
||||||
case2_count = 0
|
|
||||||
case3_count = 0
|
|
||||||
|
|
||||||
createed_content_blocks = 0
|
|
||||||
visible_objectives_by_ids = {}
|
|
||||||
snapshot_counter = 0
|
|
||||||
|
|
||||||
for module in Module.objects.all(): # .filter(title__contains="Politik mitbestimmen"):
|
def migrate_snapshots():
|
||||||
for snapshot in Snapshot.objects.filter(module=module):
|
count = 0
|
||||||
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(
|
case1_count = 0
|
||||||
objective_group__module=module, snapshot=snapshot):
|
case2_count = 0
|
||||||
header = f"{count} {module.title:50} {objective_group_snapshot.objective_group.get_title_display():25} {str(snapshot.creator):40} {objective_group_snapshot.hidden} "
|
case3_count = 0
|
||||||
count += 1
|
|
||||||
objective_group = objective_group_snapshot.objective_group
|
|
||||||
|
|
||||||
hidden_default_objectives = snapshot.hidden_objectives.filter(
|
createed_content_blocks = 0
|
||||||
group=objective_group)
|
visible_objectives_by_ids = {}
|
||||||
|
snapshot_counter = 0
|
||||||
|
|
||||||
visible_custom_objectives = snapshot.custom_objectives.filter(snapshot=snapshot, hidden=False,
|
for module in Module.objects.all():
|
||||||
group=objective_group_snapshot.objective_group)
|
for snapshot in Snapshot.objects.filter(module=module):
|
||||||
group_is_hidden = objective_group_snapshot.hidden
|
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
|
||||||
|
|
||||||
info = f"{hidden_default_objectives.count()} {visible_custom_objectives.count()}"
|
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} "
|
||||||
|
count += 1
|
||||||
|
objective_group = objective_group_snapshot.objective_group
|
||||||
|
|
||||||
if (not hidden_default_objectives and not visible_custom_objectives and not group_is_hidden):
|
hidden_default_objectives = snapshot.hidden_objectives.filter(
|
||||||
# print(f"{info} Case 1 - skip")
|
group=objective_group)
|
||||||
case1_count += 1
|
|
||||||
break
|
|
||||||
|
|
||||||
if not hidden_default_objectives and not visible_custom_objectives and group_is_hidden:
|
visible_custom_objectives = snapshot.custom_objectives.filter(snapshot=snapshot, hidden=False,
|
||||||
print(header + f"Case 2 - {info} hide default content group")
|
group=objective_group_snapshot.objective_group)
|
||||||
case2_count += 1
|
group_is_hidden = objective_group_snapshot.hidden
|
||||||
content_block = get_content_block_by_objective_group(objective_group_snapshot,
|
|
||||||
module, False, None)
|
|
||||||
snapshot.hidden_content_blocks.add(content_block.id)
|
|
||||||
snapshot.save()
|
|
||||||
|
|
||||||
if hidden_default_objectives or visible_custom_objectives:
|
info = f"{hidden_default_objectives.count()} {visible_custom_objectives.count()}"
|
||||||
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)]
|
|
||||||
|
|
||||||
# Benutzerdefinierte Lernziele
|
if (not hidden_default_objectives and not visible_custom_objectives and not group_is_hidden):
|
||||||
visible_custom_objectives = list(snapshot.custom_objectives.filter(hidden=False))
|
# print(f"{info} Case 1 - skip")
|
||||||
|
case1_count += 1
|
||||||
|
break
|
||||||
|
|
||||||
visible_objectives = visible_default_objectives + visible_custom_objectives
|
if not hidden_default_objectives and not visible_custom_objectives and group_is_hidden:
|
||||||
|
print(header + f"Case 2 - {info} hide default content group")
|
||||||
|
case2_count += 1
|
||||||
|
content_block = get_content_block_by_objective_group(objective_group_snapshot,
|
||||||
|
module, False, None)
|
||||||
|
snapshot.hidden_content_blocks.add(content_block.id)
|
||||||
|
snapshot.save()
|
||||||
|
|
||||||
# filter for unique texts in objectives
|
if hidden_default_objectives or visible_custom_objectives:
|
||||||
# TODO: I don't know why there are duplicated objectives
|
print(header + f"Case 3 - {info} create custom content blocks")
|
||||||
objectives_by_texts = {}
|
case3_count += 1
|
||||||
for objective in visible_objectives:
|
# Verlags Lernziele
|
||||||
if objective.text not in objectives_by_texts:
|
default_objectives = Objective.objects.filter(group=objective_group,
|
||||||
objectives_by_texts[objective.text] = objective
|
group__module=module,
|
||||||
visible_objectives = list(objectives_by_texts.values())
|
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)]
|
||||||
|
|
||||||
if visible_objectives:
|
# Benutzerdefinierte Lernziele
|
||||||
# make combinations of objectives unique by text, this prevents generation of many duplicated content blocks
|
visible_custom_objectives = list(snapshot.custom_objectives.filter(hidden=False))
|
||||||
visible_objectives_hash = hash(
|
|
||||||
[objective.text for objective in visible_objectives].__str__())
|
|
||||||
|
|
||||||
visible_objectives_by_ids[visible_objectives_hash] = visible_objectives
|
visible_objectives = visible_default_objectives + visible_custom_objectives
|
||||||
|
|
||||||
for objectives in visible_objectives_by_ids.values():
|
# filter for unique texts in objectives
|
||||||
print("")
|
# TODO: I don't know why there are duplicated objectives
|
||||||
for objective in objectives:
|
objectives_by_texts = {}
|
||||||
print(f" Objective: {objective.group} {objective} owner:{objective.owner}")
|
for objective in visible_objectives:
|
||||||
print("-")
|
if objective.text not in objectives_by_texts:
|
||||||
|
objectives_by_texts[objective.text] = objective
|
||||||
|
visible_objectives = list(objectives_by_texts.values())
|
||||||
|
|
||||||
print(f" visible_objectives_by_ids: {len(visible_objectives_by_ids.items())}")
|
if visible_objectives:
|
||||||
|
# make combinations of objectives unique by text, this prevents generation of many duplicated content blocks
|
||||||
|
visible_objectives_hash = hash(
|
||||||
|
[objective.text for objective in visible_objectives].__str__())
|
||||||
|
|
||||||
# create custom content blocks with the objectives
|
visible_objectives_by_ids[visible_objectives_hash] = visible_objectives
|
||||||
created_content_blocks = 0
|
|
||||||
chapter = module.get_first_child()
|
for objectives in visible_objectives_by_ids.values():
|
||||||
if "Lernziele" not in chapter.title:
|
print("")
|
||||||
raise Exception(f"Chapter does not contain 'Lernziele' first title is {chapter.title}")
|
for objective in objectives:
|
||||||
#
|
print(f" Objective: {objective.group} {objective} owner:{objective.owner}")
|
||||||
# Owner des custom blocks festlegen
|
print("-")
|
||||||
custom_content_block_snapshot = create_content_block_snapshot_from_objective(
|
|
||||||
objective_group, chapter, snapshot,
|
print(f" visible_objectives_by_ids: {len(visible_objectives_by_ids.items())}")
|
||||||
owner=snapshot.creator,
|
|
||||||
prefix="SNAP ")
|
# create custom content blocks with the objectives
|
||||||
#
|
created_content_blocks = 0
|
||||||
|
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 ")
|
||||||
|
#
|
||||||
|
# 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:
|
||||||
|
print(f"Default content block: {default_content_block.title}")
|
||||||
|
snapshot.hidden_content_blocks.add(default_content_block.id)
|
||||||
|
|
||||||
|
if list(visible_objectives_by_ids.values()):
|
||||||
objectives = list(visible_objectives_by_ids.values())[0]
|
objectives = list(visible_objectives_by_ids.values())[0]
|
||||||
create_text_in_content_block(objectives, custom_content_block_snapshot, get_or_create=True)
|
create_text_in_content_block(objectives, custom_content_block_snapshot, get_or_create=True)
|
||||||
created_content_blocks += 1
|
created_content_blocks += 1
|
||||||
snapshot.save()
|
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")
|
||||||
print(f"Created new content {case3_count} Case 3")
|
print(f"Created new content {case3_count} Case 3")
|
||||||
|
|
||||||
|
|
||||||
def get_content_block_by_objective_group(objective_group_snapshot, module, user_created: bool, user):
|
def get_content_block_by_objective_group(objective_group_snapshot, module, user_created: bool, user):
|
||||||
|
|
@ -168,7 +180,32 @@ def get_content_block_by_objective_group(objective_group_snapshot, module, user_
|
||||||
return content_block.first()
|
return content_block.first()
|
||||||
|
|
||||||
|
|
||||||
|
def get_default_content_block(objective_group_snapshot, module):
|
||||||
|
default_objectives = Objective.objects.filter(group=objective_group_snapshot.objective_group,
|
||||||
|
group__module=module,
|
||||||
|
owner__isnull=True,
|
||||||
|
objectivesnapshot__isnull=True)
|
||||||
|
|
||||||
|
default_content_block_contents = create_content_block_contents(default_objectives)
|
||||||
|
|
||||||
|
contents = json.loads(default_content_block_contents)
|
||||||
|
contents[0].get("value").get("text")
|
||||||
|
chapter = Chapter.objects.filter(title__contains="Lernziele").descendant_of(module)
|
||||||
|
text_contents = contents[0].get("value").get("text")
|
||||||
|
group_title = objective_group_snapshot.objective_group.get_title_display()
|
||||||
|
content_blocks = ContentBlock.objects.filter(user_created=False).descendant_of(chapter.first())
|
||||||
|
|
||||||
|
for content_block in content_blocks:
|
||||||
|
print(content_block.title, group_title)
|
||||||
|
if group_title in content_block.title:
|
||||||
|
return content_block
|
||||||
|
|
||||||
|
if not content_block.exists():
|
||||||
|
raise Exception("Content block does not exist ")
|
||||||
|
|
||||||
|
|
||||||
def get_visible_default_objectives():
|
def get_visible_default_objectives():
|
||||||
|
# TODO: Objective Group geht so nicth
|
||||||
default_objectives = Objective.objects.filter(group=ObjectiveGroup, group__module=module, owner__isnull=True)
|
default_objectives = Objective.objects.filter(group=ObjectiveGroup, group__module=module, owner__isnull=True)
|
||||||
visible_objectives = list(default_objectives)
|
visible_objectives = list(default_objectives)
|
||||||
visible_objectives_ids = [objective.id for objective in visible_objectives]
|
visible_objectives_ids = [objective.id for objective in visible_objectives]
|
||||||
|
|
@ -193,11 +230,6 @@ def get_content_block_by_objectives(objectives, module, user_created=False, user
|
||||||
raise Exception("Content block does not exist ")
|
raise Exception("Content block does not exist ")
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
|
|
||||||
# print(f"created_content_blocks: {createed_content_blocks}")
|
|
||||||
|
|
||||||
|
|
||||||
def analyze():
|
def analyze():
|
||||||
print(f"""
|
print(f"""
|
||||||
OjectiveGroups: {ObjectiveGroup.objects.count()}
|
OjectiveGroups: {ObjectiveGroup.objects.count()}
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,9 @@ class Command(BaseCommand):
|
||||||
ContentBlock.objects.filter(title__startswith="XXX").delete()
|
ContentBlock.objects.filter(title__startswith="XXX").delete()
|
||||||
Chapter.objects.filter(title__startswith="XXX").delete()
|
Chapter.objects.filter(title__startswith="XXX").delete()
|
||||||
|
|
||||||
|
migrate_objectives_to_content()
|
||||||
|
|
||||||
|
def migrate_objectives_to_content():
|
||||||
created_content_blocks = 0
|
created_content_blocks = 0
|
||||||
|
|
||||||
failed_modules = []
|
failed_modules = []
|
||||||
|
|
@ -63,7 +66,9 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
print(f" School class: {school_class}")
|
print(f" School class: {school_class}")
|
||||||
# merge "Verlagsinhalte" and "benutzerdefinierte Inhalte"
|
# merge "Verlagsinhalte" and "benutzerdefinierte Inhalte"
|
||||||
merged_objectives = visible_default_objectives_by_class[school_class] + owner_objectives
|
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_ids = tuple(objective.id for objective in merged_objectives)
|
merged_objectives_ids = tuple(objective.id for objective in merged_objectives)
|
||||||
|
|
||||||
if merged_objectives_ids == default_objectives_ids:
|
if merged_objectives_ids == default_objectives_ids:
|
||||||
|
|
@ -73,7 +78,7 @@ class Command(BaseCommand):
|
||||||
# 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 merged_objectives_ids != default_objectives_ids:
|
||||||
for objective in merged_objectives:
|
for objective in merged_objectives:
|
||||||
print(f" Objective: {objective} {objective.owner}")
|
print(f" Objective: {objective} {objective.owner}")
|
||||||
|
|
||||||
if merged_objectives_ids:
|
if merged_objectives_ids:
|
||||||
custom_content_block = create_content_block_from_objective(objective_group,
|
custom_content_block = create_content_block_from_objective(objective_group,
|
||||||
|
|
@ -165,6 +170,7 @@ def get_objectives_by_owner(objective_group, exclude_snapshots=True):
|
||||||
# add owners with hidden default objectives to custom objectives, needed for further processing
|
# add owners with hidden default objectives to custom objectives, needed for further processing
|
||||||
hidden_default_objectives = list(
|
hidden_default_objectives = list(
|
||||||
objective_group.objectives.filter(owner__isnull=True, objectivesnapshot__isnull=True, hidden_for__isnull=False))
|
objective_group.objectives.filter(owner__isnull=True, objectivesnapshot__isnull=True, hidden_for__isnull=False))
|
||||||
|
|
||||||
for hidden_default_objective in hidden_default_objectives:
|
for hidden_default_objective in hidden_default_objectives:
|
||||||
for school_class in hidden_default_objective.hidden_for.all():
|
for school_class in hidden_default_objective.hidden_for.all():
|
||||||
for teacher in school_class.get_teachers():
|
for teacher in school_class.get_teachers():
|
||||||
|
|
@ -175,7 +181,7 @@ def get_objectives_by_owner(objective_group, exclude_snapshots=True):
|
||||||
|
|
||||||
|
|
||||||
def create_chapter_from_objective_group(module, prefix="XXX "):
|
def create_chapter_from_objective_group(module, prefix="XXX "):
|
||||||
chapter = Chapter(title=f"{prefix}Lernziele")
|
chapter = Chapter(title=f"Lernziele")
|
||||||
|
|
||||||
first_sibling = module.get_first_child()
|
first_sibling = module.get_first_child()
|
||||||
if first_sibling is not None:
|
if first_sibling is not None:
|
||||||
|
|
@ -187,8 +193,8 @@ def create_chapter_from_objective_group(module, prefix="XXX "):
|
||||||
|
|
||||||
|
|
||||||
def create_chapter_snapshot_from_objective_group(module, snapshot, prefix="XXX "):
|
def create_chapter_snapshot_from_objective_group(module, snapshot, prefix="XXX "):
|
||||||
chapter = Chapter.objects.filter(parent=module, title=f"{prefix}Lernziele").first()
|
chapter = Chapter.objects.filter(parent=module, title=f"Lernziele").first()
|
||||||
chapter_snapshot = ChapterSnapshot(title=f"{prefix}Lernziele", snapshot=snapshot)
|
chapter_snapshot = ChapterSnapshot(title=f"Lernziele", snapshot=snapshot)
|
||||||
|
|
||||||
first_sibling = module.get_first_child()
|
first_sibling = module.get_first_child()
|
||||||
if first_sibling is not None:
|
if first_sibling is not None:
|
||||||
|
|
@ -198,7 +204,7 @@ def create_chapter_snapshot_from_objective_group(module, snapshot, prefix="XXX "
|
||||||
|
|
||||||
def create_content_block_from_objective(objective_group, chapter, owner=None, prefix="XXX "):
|
def create_content_block_from_objective(objective_group, chapter, owner=None, prefix="XXX "):
|
||||||
content_block = ContentBlock(
|
content_block = ContentBlock(
|
||||||
title=f"{prefix}{objective_group.get_title_display()}",
|
title=f"{objective_group.get_title_display()}",
|
||||||
type="normal",
|
type="normal",
|
||||||
owner=owner,
|
owner=owner,
|
||||||
user_created=owner is not None,
|
user_created=owner is not None,
|
||||||
|
|
@ -210,7 +216,7 @@ def create_content_block_from_objective(objective_group, chapter, owner=None, pr
|
||||||
|
|
||||||
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, prefix="XXX "):
|
||||||
content_block_snapshot = ContentBlockSnapshot(
|
content_block_snapshot = ContentBlockSnapshot(
|
||||||
title=f"{prefix}{objective_group.get_title_display()}",
|
title=f"{objective_group.get_title_display()}",
|
||||||
type="normal",
|
type="normal",
|
||||||
owner=owner,
|
owner=owner,
|
||||||
user_created=owner is not None,
|
user_created=owner is not None,
|
||||||
|
|
@ -223,13 +229,6 @@ def create_content_block_snapshot_from_objective(objective_group, chapter, snaps
|
||||||
|
|
||||||
def create_text_in_content_block(objectives, content_block, get_or_create=False):
|
def create_text_in_content_block(objectives, content_block, get_or_create=False):
|
||||||
objectives = list(objectives)
|
objectives = list(objectives)
|
||||||
objective_li = [f"<li>{objective.text} -{objective.owner}- </li>" for objective in objectives if objective.text]
|
|
||||||
|
|
||||||
texts = [{'type': 'text_block',
|
|
||||||
'value': {
|
|
||||||
'text': f"<ul> {''.join(str(i) for i in objective_li)} </ul>"
|
|
||||||
}}]
|
|
||||||
|
|
||||||
content_block.contents = create_content_block_contents(objectives)
|
content_block.contents = create_content_block_contents(objectives)
|
||||||
|
|
||||||
if get_or_create:
|
if get_or_create:
|
||||||
|
|
@ -245,11 +244,11 @@ def create_text_in_content_block(objectives, content_block, get_or_create=False)
|
||||||
|
|
||||||
def create_content_block_contents(objectives):
|
def create_content_block_contents(objectives):
|
||||||
objectives = list(objectives)
|
objectives = list(objectives)
|
||||||
objective_li = [f"<li>{objective.text} -{objective.owner}- </li>" for objective in objectives if objective.text]
|
objective_li = [f"<li>{objective.text}</li>" for objective in objectives if objective.text]
|
||||||
|
|
||||||
texts = [{'type': 'text_block',
|
texts = [{'type': 'text_block',
|
||||||
'value': {
|
'value': {
|
||||||
'text': f"<ul> {''.join(str(i) for i in objective_li)} </ul>"
|
'text': f"<ul>{''.join(str(i) for i in objective_li)}</ul>"
|
||||||
}}]
|
}}]
|
||||||
|
|
||||||
contents = json.dumps(texts)
|
contents = json.dumps(texts)
|
||||||
|
|
|
||||||
|
|
@ -156,11 +156,3 @@ class Snapshot(models.Model):
|
||||||
chapter.title_hidden_for.add(selected_class)
|
chapter.title_hidden_for.add(selected_class)
|
||||||
if chapter_snapshot.description_hidden:
|
if chapter_snapshot.description_hidden:
|
||||||
chapter.description_hidden_for.add(selected_class)
|
chapter.description_hidden_for.add(selected_class)
|
||||||
# hier objective snapshots entfernt werden. die werden nicht mehr gebraucht
|
|
||||||
# for objective_group_snapshot in self.objective_groups.through.objects.all():
|
|
||||||
# if objective_group_snapshot.hidden:
|
|
||||||
# objective_group_snapshot.objective_group.hidden_for.add(selected_class)
|
|
||||||
# for objective in self.hidden_objectives.all():
|
|
||||||
# objective.hidden_for.add(selected_class)
|
|
||||||
# for custom_objective in self.custom_objectives.all():
|
|
||||||
# custom_objective.to_regular_objective(owner=user, school_class=selected_class)
|
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ query ModulesQuery($slug: String, $id: ID) {
|
||||||
contentBlocks {
|
contentBlocks {
|
||||||
id
|
id
|
||||||
title
|
title
|
||||||
|
userCreated
|
||||||
originalCreator {
|
originalCreator {
|
||||||
id
|
id
|
||||||
fullName
|
fullName
|
||||||
|
|
@ -32,7 +33,8 @@ query ModulesQuery($slug: String, $id: ID) {
|
||||||
}
|
}
|
||||||
hiddenFor {
|
hiddenFor {
|
||||||
name
|
name
|
||||||
}
|
}
|
||||||
|
contents
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,111 @@
|
||||||
|
from django.test import RequestFactory
|
||||||
|
from graphene.test import Client
|
||||||
|
from graphql_relay import to_global_id, from_global_id
|
||||||
|
|
||||||
|
from api.schema import schema
|
||||||
|
from api.utils import get_object
|
||||||
|
from books.factories import ModuleFactory, ChapterFactory, ContentBlockFactory
|
||||||
|
from books.management.commands.migrate_objective_snapshots import migrate_snapshots
|
||||||
|
from books.management.commands.migrate_objectives_to_content import migrate_objectives_to_content
|
||||||
|
from books.models import Snapshot, ChapterSnapshot
|
||||||
|
from books.tests.queries import MODULE_QUERY, SNAPSHOT_MODULE_QUERY, CREATE_SNAPSHOT_MUTATION, APPLY_SNAPSHOT_MUTATION, \
|
||||||
|
MODULE_SNAPSHOTS_QUERY, SHARE_SNAPSHOT_MUTATION, UPDATE_SNAPSHOT_MUTATION, DELETE_SNAPSHOT_MUTATION
|
||||||
|
from core.tests.base_test import SkillboxTestCase
|
||||||
|
from objectives.factories import ObjectiveGroupFactory, ObjectiveFactory
|
||||||
|
from users.factories import SchoolClassFactory
|
||||||
|
from users.models import User, SchoolClass
|
||||||
|
|
||||||
|
|
||||||
|
class TestObjectivesMigration(SkillboxTestCase):
|
||||||
|
@property
|
||||||
|
def graphene_client(self):
|
||||||
|
return self.get_client()
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.createDefault()
|
||||||
|
self.client = self.get_client()
|
||||||
|
# teacher will create snapshot
|
||||||
|
self.slug = 'some-module'
|
||||||
|
|
||||||
|
self.module = ModuleFactory(slug=self.slug)
|
||||||
|
self.skillbox_class = SchoolClass.objects.get(name='skillbox')
|
||||||
|
self.teacher2 = User.objects.get(username='teacher2')
|
||||||
|
self.second_class_name = 'second_class'
|
||||||
|
self.second_class = SchoolClass.objects.get(name=self.second_class_name)
|
||||||
|
self.admin = User.objects.get(username='admin')
|
||||||
|
|
||||||
|
# we make a snapshot S of the module M
|
||||||
|
# snapshot S looks like module M for school class X
|
||||||
|
# module M has a chapter
|
||||||
|
self.chapter = ChapterFactory(parent=self.module, slug='some-chapter', owner=self.admin)
|
||||||
|
ChapterFactory(parent=self.module, slug='some-other-chapter', owner=self.admin)
|
||||||
|
|
||||||
|
objective_group = ObjectiveGroupFactory(module=self.module, title='Gesellschaft')
|
||||||
|
second_objective_group = ObjectiveGroupFactory(module=self.module, title='Sprache & Kommunikation')
|
||||||
|
|
||||||
|
self.visible_objective = ObjectiveFactory(text='visible-objective', group=objective_group)
|
||||||
|
self.hidden_objective = ObjectiveFactory(text='hidden-objective', group=objective_group)
|
||||||
|
self.custom_objective = ObjectiveFactory(text='custom-objective', group=objective_group, owner=self.teacher)
|
||||||
|
self.custom_hidden_objective = ObjectiveFactory(text='custom-hidden-objective', group=objective_group,
|
||||||
|
owner=self.teacher)
|
||||||
|
|
||||||
|
self.visible_objective = ObjectiveFactory(text='objective1', group=second_objective_group)
|
||||||
|
|
||||||
|
self.hidden_objective.hidden_for.add(self.skillbox_class)
|
||||||
|
self.hidden_objective.save()
|
||||||
|
|
||||||
|
self.custom_objective.visible_for.add(self.skillbox_class)
|
||||||
|
self.custom_objective.save()
|
||||||
|
|
||||||
|
second_objective_group.hidden_for.add(self.skillbox_class)
|
||||||
|
second_objective_group.save()
|
||||||
|
|
||||||
|
self.custom_hidden_objective.visible_for.remove(self.skillbox_class)
|
||||||
|
|
||||||
|
migrate_objectives_to_content()
|
||||||
|
|
||||||
|
def test_objectives_migration_hidden_default_content(self):
|
||||||
|
result = self.client.execute(MODULE_QUERY, variables={
|
||||||
|
'slug': self.module.slug
|
||||||
|
})
|
||||||
|
module = result.data['module']
|
||||||
|
chapter1 = module['chapters'][0]
|
||||||
|
default_content, _, _ = chapter1['contentBlocks']
|
||||||
|
|
||||||
|
# default content block (Verlagsinhalte) exists but is hidden (since one objective is hidden for this class)
|
||||||
|
self.assertEqual(default_content['title'], 'Gesellschaft')
|
||||||
|
self.assertEqual(default_content['originalCreator'], None)
|
||||||
|
self.assertEqual(default_content['contents'][0]['value']['text'],
|
||||||
|
'<ul><li>visible-objective</li><li>hidden-objective</li></ul>')
|
||||||
|
self.assertEqual(default_content['hiddenFor'], [{'name': 'skillbox'}])
|
||||||
|
|
||||||
|
def test_objectives_migration_hidden_default_content_creates_custom_content(self):
|
||||||
|
result = self.client.execute(MODULE_QUERY, variables={
|
||||||
|
'slug': self.module.slug
|
||||||
|
})
|
||||||
|
module = result.data['module']
|
||||||
|
chapter1 = module['chapters'][0]
|
||||||
|
_, custom, _ = chapter1['contentBlocks']
|
||||||
|
|
||||||
|
# default content block (Verlagsinhalte) exists but is hidden (since one objective is hidden for this class)
|
||||||
|
self.assertEqual(custom['title'], 'Gesellschaft')
|
||||||
|
self.assertTrue(custom['originalCreator'] is not None)
|
||||||
|
self.assertEqual(custom['hiddenFor'], [])
|
||||||
|
self.assertEqual(custom['visibleFor'], [{'name': 'skillbox'}])
|
||||||
|
self.assertEqual(custom['contents'][0]['value']['text'],
|
||||||
|
'<ul><li>visible-objective</li><li>custom-objective</li></ul>')
|
||||||
|
|
||||||
|
def test_objectives_migration_hidden_default_content_creates_hidden_content_block(self):
|
||||||
|
result = self.client.execute(MODULE_QUERY, variables={
|
||||||
|
'slug': self.module.slug
|
||||||
|
})
|
||||||
|
module = result.data['module']
|
||||||
|
chapter1 = module['chapters'][0]
|
||||||
|
_, _, hidden_custom = chapter1['contentBlocks']
|
||||||
|
|
||||||
|
# default content block (Verlagsinhalte) exists but is hidden (since one objective is hidden for this class)
|
||||||
|
self.assertEqual(hidden_custom['title'], 'Sprache & Kommunikation')
|
||||||
|
self.assertTrue(hidden_custom['originalCreator'] is None)
|
||||||
|
self.assertEqual(hidden_custom['hiddenFor'], [])
|
||||||
|
self.assertEqual(hidden_custom['visibleFor'], [])
|
||||||
|
self.assertEqual(hidden_custom['contents'][0]['value']['text'], '<ul><li>objective1</li></ul>')
|
||||||
|
|
@ -0,0 +1,195 @@
|
||||||
|
from django.test import RequestFactory
|
||||||
|
from graphene.test import Client
|
||||||
|
from graphql_relay import to_global_id, from_global_id
|
||||||
|
|
||||||
|
from api.schema import schema
|
||||||
|
from api.utils import get_object
|
||||||
|
from books.factories import ModuleFactory, ChapterFactory, ContentBlockFactory
|
||||||
|
from books.management.commands.migrate_objective_snapshots import migrate_snapshots
|
||||||
|
from books.management.commands.migrate_objectives_to_content import migrate_objectives_to_content
|
||||||
|
from books.models import Snapshot, ChapterSnapshot, ContentBlock
|
||||||
|
from books.tests.queries import MODULE_QUERY, SNAPSHOT_MODULE_QUERY, CREATE_SNAPSHOT_MUTATION, APPLY_SNAPSHOT_MUTATION, \
|
||||||
|
MODULE_SNAPSHOTS_QUERY, SHARE_SNAPSHOT_MUTATION, UPDATE_SNAPSHOT_MUTATION, DELETE_SNAPSHOT_MUTATION
|
||||||
|
from core.tests.base_test import SkillboxTestCase
|
||||||
|
from objectives.factories import ObjectiveGroupFactory, ObjectiveFactory
|
||||||
|
from users.factories import SchoolClassFactory
|
||||||
|
from users.models import User, SchoolClass
|
||||||
|
|
||||||
|
|
||||||
|
class TestSnapshotMigration(SkillboxTestCase):
|
||||||
|
@property
|
||||||
|
def graphene_client(self):
|
||||||
|
return self.get_client()
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.createDefault()
|
||||||
|
self.client = self.get_client()
|
||||||
|
# teacher will create snapshot
|
||||||
|
self.slug = 'some-module'
|
||||||
|
|
||||||
|
self.module = ModuleFactory(slug=self.slug)
|
||||||
|
self.skillbox_class = SchoolClass.objects.get(name='skillbox')
|
||||||
|
self.teacher2 = User.objects.get(username='teacher2')
|
||||||
|
self.second_class_name = 'second_class'
|
||||||
|
self.second_class = SchoolClass.objects.get(name=self.second_class_name)
|
||||||
|
self.admin = User.objects.get(username='admin')
|
||||||
|
|
||||||
|
# we make a snapshot S of the module M
|
||||||
|
# snapshot S looks like module M for school class X
|
||||||
|
# module M has a chapter
|
||||||
|
self.chapter = ChapterFactory(parent=self.module, slug='some-chapter', owner=self.admin)
|
||||||
|
ChapterFactory(parent=self.module, slug='some-other-chapter', owner=self.admin)
|
||||||
|
|
||||||
|
objective_group = ObjectiveGroupFactory(module=self.module, title='Gesellschaft')
|
||||||
|
second_objective_group = ObjectiveGroupFactory(module=self.module, title='Sprache & Kommunikation')
|
||||||
|
|
||||||
|
self.visible_objective = ObjectiveFactory(text='visible-objective', group=objective_group)
|
||||||
|
self.visible_objective_2 = ObjectiveFactory(text='hidden-objective', group=objective_group)
|
||||||
|
self.custom_objective = ObjectiveFactory(text='custom-objective', group=objective_group, owner=self.teacher)
|
||||||
|
self.custom_hidden_objective = ObjectiveFactory(text='custom-hidden-objective', group=objective_group,
|
||||||
|
owner=self.teacher)
|
||||||
|
|
||||||
|
self.visible_objective = ObjectiveFactory(text='objective1', group=second_objective_group)
|
||||||
|
|
||||||
|
self.custom_objective.visible_for.add(self.skillbox_class)
|
||||||
|
self.custom_objective.save()
|
||||||
|
|
||||||
|
second_objective_group.hidden_for.add(self.skillbox_class)
|
||||||
|
second_objective_group.save()
|
||||||
|
|
||||||
|
self.custom_hidden_objective.visible_for.remove(self.skillbox_class)
|
||||||
|
|
||||||
|
self.snapshot1 = Snapshot.objects.create_snapshot(self.module, self.skillbox_class, self.teacher)
|
||||||
|
|
||||||
|
migrate_objectives_to_content()
|
||||||
|
|
||||||
|
migrate_snapshots()
|
||||||
|
|
||||||
|
# Change visibility of objectives resp. content blocks, hide all
|
||||||
|
|
||||||
|
for content_block in ContentBlock.objects.all().descendant_of(self.chapter):
|
||||||
|
if content_block.owner is None:
|
||||||
|
content_block.hidden_for.add(self.skillbox_class)
|
||||||
|
else:
|
||||||
|
content_block.visible_for.remove(self.skillbox_class)
|
||||||
|
content_block.save()
|
||||||
|
|
||||||
|
def test_snapshot_migration_dfault_content_pre_apply(self):
|
||||||
|
result = self.client.execute(MODULE_QUERY, variables={
|
||||||
|
'slug': self.module.slug
|
||||||
|
})
|
||||||
|
module = result.data['module']
|
||||||
|
chapter1 = module['chapters'][0]
|
||||||
|
default_content, _, _ = chapter1['contentBlocks']
|
||||||
|
|
||||||
|
# default content block (Verlagsinhalte) exists but is hidden (since one objective is hidden for this class)
|
||||||
|
self.assertEqual(default_content['title'], 'Gesellschaft')
|
||||||
|
self.assertEqual(default_content['originalCreator'], None)
|
||||||
|
self.assertEqual(default_content['contents'][0]['value']['text'],
|
||||||
|
'<ul><li>visible-objective</li><li>hidden-objective</li></ul>')
|
||||||
|
self.assertEqual(default_content['hiddenFor'], [{'name': 'skillbox'}])
|
||||||
|
|
||||||
|
def test_snapshot_migration_hidden_default_content_creates_custom_content_pre_apply(self):
|
||||||
|
result = self.client.execute(MODULE_QUERY, variables={
|
||||||
|
'slug': self.module.slug
|
||||||
|
})
|
||||||
|
module = result.data['module']
|
||||||
|
chapter1 = module['chapters'][0]
|
||||||
|
_, custom, _ = chapter1['contentBlocks']
|
||||||
|
|
||||||
|
self.assertEqual(custom['title'], 'Gesellschaft')
|
||||||
|
self.assertTrue(custom['originalCreator'] is not None)
|
||||||
|
self.assertEqual(custom['hiddenFor'], [])
|
||||||
|
self.assertEqual(custom['visibleFor'], [])
|
||||||
|
self.assertEqual(custom['contents'][0]['value']['text'],
|
||||||
|
'<ul><li>visible-objective</li><li>hidden-objective</li><li>custom-objective</li></ul>')
|
||||||
|
|
||||||
|
def test_snapshot_migration_hidden_content_block_pre_apply(self):
|
||||||
|
result = self.client.execute(MODULE_QUERY, variables={
|
||||||
|
'slug': self.module.slug
|
||||||
|
})
|
||||||
|
module = result.data['module']
|
||||||
|
chapter1 = module['chapters'][0]
|
||||||
|
_, _, hidden_custom = chapter1['contentBlocks']
|
||||||
|
|
||||||
|
# default content block (Verlagsinhalte) exists but is hidden (since one objective is hidden for this class)
|
||||||
|
self.assertEqual(hidden_custom['title'], 'Sprache & Kommunikation')
|
||||||
|
self.assertTrue(hidden_custom['originalCreator'] is None)
|
||||||
|
self.assertEqual(hidden_custom['hiddenFor'], [{'name': 'skillbox'}])
|
||||||
|
self.assertEqual(hidden_custom['visibleFor'], [])
|
||||||
|
self.assertEqual(hidden_custom['contents'][0]['value']['text'], '<ul><li>objective1</li></ul>')
|
||||||
|
|
||||||
|
def test_snapshot_migration_default_apply_snapshot(self):
|
||||||
|
# apply snapshot
|
||||||
|
self.snapshot1.apply(self.teacher, self.school_class)
|
||||||
|
result = self.client.execute(MODULE_QUERY, variables={
|
||||||
|
'slug': self.module.slug
|
||||||
|
})
|
||||||
|
module = result.data['module']
|
||||||
|
chapter1 = module['chapters'][0]
|
||||||
|
default_content, _, _, _ = chapter1['contentBlocks']
|
||||||
|
|
||||||
|
# default content block (Verlagsinhalte) exists but is hidden (since one objective is hidden for this class)
|
||||||
|
self.assertEqual(default_content['title'], 'Gesellschaft')
|
||||||
|
self.assertFalse(default_content['userCreated'])
|
||||||
|
self.assertEqual(default_content['originalCreator'], None)
|
||||||
|
self.assertEqual(default_content['contents'][0]['value']['text'],
|
||||||
|
'<ul><li>visible-objective</li><li>hidden-objective</li></ul>')
|
||||||
|
self.assertEqual(default_content['hiddenFor'], [{'name': 'skillbox'}])
|
||||||
|
|
||||||
|
def test_snapshot_migration_hidden_custom_content_apply_snapshot(self):
|
||||||
|
# custom content from bevore the snapshot must be hidden (visible for nobody)
|
||||||
|
self.snapshot1.apply(self.teacher, self.school_class)
|
||||||
|
|
||||||
|
result = self.client.execute(MODULE_QUERY, variables={
|
||||||
|
'slug': self.module.slug
|
||||||
|
})
|
||||||
|
module = result.data['module']
|
||||||
|
chapter1 = module['chapters'][0]
|
||||||
|
_, custom, _, _ = chapter1['contentBlocks']
|
||||||
|
|
||||||
|
self.assertEqual(custom['title'], 'Gesellschaft')
|
||||||
|
self.assertTrue(custom['userCreated'])
|
||||||
|
self.assertTrue(custom['originalCreator'] is not None)
|
||||||
|
self.assertEqual(custom['hiddenFor'], [])
|
||||||
|
self.assertEqual(custom['visibleFor'], [])
|
||||||
|
self.assertEqual(custom['contents'][0]['value']['text'],
|
||||||
|
'<ul><li>visible-objective</li><li>hidden-objective</li><li>custom-objective</li></ul>')
|
||||||
|
|
||||||
|
def test_snapshot_migration_hidden_content_block_apply_snapshot_2(self):
|
||||||
|
# custom content from bevore the snapshot must be hidden (visible for nobody)
|
||||||
|
self.snapshot1.apply(self.teacher, self.school_class)
|
||||||
|
|
||||||
|
result = self.client.execute(MODULE_QUERY, variables={
|
||||||
|
'slug': self.module.slug
|
||||||
|
})
|
||||||
|
module = result.data['module']
|
||||||
|
chapter1 = module['chapters'][0]
|
||||||
|
_, _, hidden_custom, _ = chapter1['contentBlocks']
|
||||||
|
|
||||||
|
# default content block (Verlagsinhalte) exists but is hidden (since one objective is hidden for this class)
|
||||||
|
self.assertEqual(hidden_custom['title'], 'Sprache & Kommunikation')
|
||||||
|
self.assertFalse(hidden_custom['userCreated'])
|
||||||
|
self.assertTrue(hidden_custom['originalCreator'] is None)
|
||||||
|
self.assertEqual(hidden_custom['hiddenFor'], [{'name': 'skillbox'}])
|
||||||
|
self.assertEqual(hidden_custom['visibleFor'], [])
|
||||||
|
self.assertEqual(hidden_custom['contents'][0]['value']['text'], '<ul><li>objective1</li></ul>')
|
||||||
|
|
||||||
|
def test_snapshot_migration_visible_content_snapshot_new_content(self):
|
||||||
|
# the applicaiton of a snapshot hides old custom content, and creates new custom content for the visible stuff
|
||||||
|
self.snapshot1.apply(self.teacher, self.school_class)
|
||||||
|
|
||||||
|
result = self.client.execute(MODULE_QUERY, variables={
|
||||||
|
'slug': self.module.slug
|
||||||
|
})
|
||||||
|
module = result.data['module']
|
||||||
|
chapter1 = module['chapters'][0]
|
||||||
|
_, _, _, new_content_block = chapter1['contentBlocks']
|
||||||
|
|
||||||
|
self.assertEqual(new_content_block['title'], 'Gesellschaft')
|
||||||
|
self.assertTrue(new_content_block['userCreated'])
|
||||||
|
self.assertTrue(new_content_block['originalCreator'] is None)
|
||||||
|
self.assertEqual(new_content_block['hiddenFor'], [])
|
||||||
|
self.assertEqual(new_content_block['visibleFor'], [{'name': 'skillbox'}])
|
||||||
|
self.assertEqual(new_content_block['contents'][0]['value']['text'],
|
||||||
|
'<ul><li>visible-objective</li><li>hidden-objective</li><li>custom-objective</li></ul>')
|
||||||
Loading…
Reference in New Issue