diff --git a/client/src/components/modules/SnapshotHeader.vue b/client/src/components/modules/SnapshotHeader.vue index ed5a0ca9..43e221cc 100644 --- a/client/src/components/modules/SnapshotHeader.vue +++ b/client/src/components/modules/SnapshotHeader.vue @@ -9,8 +9,6 @@

In diesem Snapshot sind {{ changesCount }} Anpassungen gespeichert:

diff --git a/server/books/management/commands/migrate_objective_snapshots.py b/server/books/management/commands/migrate_objective_snapshots.py index 46d3fb0f..9049466c 100644 --- a/server/books/management/commands/migrate_objective_snapshots.py +++ b/server/books/management/commands/migrate_objective_snapshots.py @@ -49,112 +49,124 @@ class Command(BaseCommand): ChapterSnapshot.objects.filter(chapter__title__startswith=prefix).delete() analyze() - count = 0 - case1_count = 0 - case2_count = 0 - case3_count = 0 + migrate_snapshots() - createed_content_blocks = 0 - visible_objectives_by_ids = {} - snapshot_counter = 0 - for module in Module.objects.all(): # .filter(title__contains="Politik mitbestimmen"): - for snapshot in Snapshot.objects.filter(module=module): - 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 +def migrate_snapshots(): + count = 0 - 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 + case1_count = 0 + case2_count = 0 + case3_count = 0 - hidden_default_objectives = snapshot.hidden_objectives.filter( - group=objective_group) + createed_content_blocks = 0 + visible_objectives_by_ids = {} + snapshot_counter = 0 - visible_custom_objectives = snapshot.custom_objectives.filter(snapshot=snapshot, hidden=False, - group=objective_group_snapshot.objective_group) - group_is_hidden = objective_group_snapshot.hidden + for module in Module.objects.all(): + for snapshot in Snapshot.objects.filter(module=module): + 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): - # print(f"{info} Case 1 - skip") - case1_count += 1 - break + hidden_default_objectives = snapshot.hidden_objectives.filter( + group=objective_group) - 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() + visible_custom_objectives = snapshot.custom_objectives.filter(snapshot=snapshot, hidden=False, + group=objective_group_snapshot.objective_group) + group_is_hidden = objective_group_snapshot.hidden - 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)] + info = f"{hidden_default_objectives.count()} {visible_custom_objectives.count()}" - # Benutzerdefinierte Lernziele - visible_custom_objectives = list(snapshot.custom_objectives.filter(hidden=False)) + if (not hidden_default_objectives and not visible_custom_objectives and not group_is_hidden): + # 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 - # TODO: I don't know why there are duplicated objectives - objectives_by_texts = {} - for objective in visible_objectives: - if objective.text not in objectives_by_texts: - objectives_by_texts[objective.text] = objective - visible_objectives = list(objectives_by_texts.values()) + 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)] - 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__()) + # Benutzerdefinierte Lernziele + visible_custom_objectives = list(snapshot.custom_objectives.filter(hidden=False)) - 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(): - print("") - for objective in objectives: - print(f" Objective: {objective.group} {objective} owner:{objective.owner}") - print("-") + # filter for unique texts in objectives + # TODO: I don't know why there are duplicated objectives + objectives_by_texts = {} + for objective in visible_objectives: + 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 - 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 ") - # + visible_objectives_by_ids[visible_objectives_hash] = visible_objectives + + for objectives in visible_objectives_by_ids.values(): + print("") + for objective in objectives: + print(f" Objective: {objective.group} {objective} owner:{objective.owner}") + print("-") + + print(f" visible_objectives_by_ids: {len(visible_objectives_by_ids.items())}") + + # 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] create_text_in_content_block(objectives, custom_content_block_snapshot, get_or_create=True) created_content_blocks += 1 - snapshot.save() - print() - print(f"Skipped {case1_count} Case 1") - print(f"Hidden default content groups {case2_count} Case 2") - print(f"Created new content {case3_count} Case 3") + snapshot.save() + print() + print(f"Skipped {case1_count} Case 1") + print(f"Hidden default content groups {case2_count} Case 2") + print(f"Created new content {case3_count} Case 3") 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() +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(): + # 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] @@ -193,11 +230,6 @@ def get_content_block_by_objectives(objectives, module, user_created=False, user raise Exception("Content block does not exist ") -# - -# print(f"created_content_blocks: {createed_content_blocks}") - - def analyze(): print(f""" OjectiveGroups: {ObjectiveGroup.objects.count()} diff --git a/server/books/management/commands/migrate_objectives_to_content.py b/server/books/management/commands/migrate_objectives_to_content.py index 89815d37..0b828fd1 100644 --- a/server/books/management/commands/migrate_objectives_to_content.py +++ b/server/books/management/commands/migrate_objectives_to_content.py @@ -19,6 +19,9 @@ class Command(BaseCommand): ContentBlock.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 failed_modules = [] @@ -63,7 +66,9 @@ class Command(BaseCommand): print(f" School class: {school_class}") # 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) 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 if merged_objectives_ids not in contentblocks_by_merged_objectives_ids and merged_objectives_ids != default_objectives_ids: for objective in merged_objectives: - print(f" Objective: {objective} {objective.owner}") + print(f" Objective: {objective} {objective.owner}") if merged_objectives_ids: 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 hidden_default_objectives = list( objective_group.objectives.filter(owner__isnull=True, objectivesnapshot__isnull=True, hidden_for__isnull=False)) + for hidden_default_objective in hidden_default_objectives: for school_class in hidden_default_objective.hidden_for.all(): 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 "): - chapter = Chapter(title=f"{prefix}Lernziele") + chapter = Chapter(title=f"Lernziele") first_sibling = module.get_first_child() 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 "): - chapter = Chapter.objects.filter(parent=module, title=f"{prefix}Lernziele").first() - chapter_snapshot = ChapterSnapshot(title=f"{prefix}Lernziele", snapshot=snapshot) + chapter = Chapter.objects.filter(parent=module, title=f"Lernziele").first() + chapter_snapshot = ChapterSnapshot(title=f"Lernziele", snapshot=snapshot) first_sibling = module.get_first_child() 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 "): content_block = ContentBlock( - title=f"{prefix}{objective_group.get_title_display()}", + title=f"{objective_group.get_title_display()}", type="normal", owner=owner, 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 "): content_block_snapshot = ContentBlockSnapshot( - title=f"{prefix}{objective_group.get_title_display()}", + title=f"{objective_group.get_title_display()}", type="normal", owner=owner, 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): objectives = list(objectives) - objective_li = [f"
  • {objective.text} -{objective.owner}-
  • " for objective in objectives if objective.text] - - texts = [{'type': 'text_block', - 'value': { - 'text': f"" - }}] - content_block.contents = create_content_block_contents(objectives) 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): objectives = list(objectives) - objective_li = [f"
  • {objective.text} -{objective.owner}-
  • " for objective in objectives if objective.text] + objective_li = [f"
  • {objective.text}
  • " for objective in objectives if objective.text] texts = [{'type': 'text_block', 'value': { - 'text': f"" + 'text': f"" }}] contents = json.dumps(texts) diff --git a/server/books/models/snapshot.py b/server/books/models/snapshot.py index ed851708..3d2adfa3 100644 --- a/server/books/models/snapshot.py +++ b/server/books/models/snapshot.py @@ -156,11 +156,3 @@ class Snapshot(models.Model): chapter.title_hidden_for.add(selected_class) if chapter_snapshot.description_hidden: 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) diff --git a/server/books/tests/queries.py b/server/books/tests/queries.py index ccfc63a3..e500bdd4 100644 --- a/server/books/tests/queries.py +++ b/server/books/tests/queries.py @@ -22,6 +22,7 @@ query ModulesQuery($slug: String, $id: ID) { contentBlocks { id title + userCreated originalCreator { id fullName @@ -32,7 +33,8 @@ query ModulesQuery($slug: String, $id: ID) { } hiddenFor { name - } + } + contents } } } diff --git a/server/books/tests/test_objectives_migration.py b/server/books/tests/test_objectives_migration.py new file mode 100644 index 00000000..785d15bc --- /dev/null +++ b/server/books/tests/test_objectives_migration.py @@ -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'], + '') + 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'], + '') + + 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'], '') diff --git a/server/books/tests/test_snapshots_migration.py b/server/books/tests/test_snapshots_migration.py new file mode 100644 index 00000000..492920a0 --- /dev/null +++ b/server/books/tests/test_snapshots_migration.py @@ -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'], + '') + 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'], + '') + + 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'], '') + + 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'], + '') + 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'], + '') + + 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'], '') + + 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'], + '')