From ea1816e3cc7e0a90906322d358452b39f0984dfb Mon Sep 17 00:00:00 2001 From: Lorenz Padberg Date: Fri, 16 Feb 2024 12:31:45 +0100 Subject: [PATCH 1/9] Add test to replicate order bug --- .../books/tests/test_objectives_migration.py | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/server/books/tests/test_objectives_migration.py b/server/books/tests/test_objectives_migration.py index 785d15bc..2acf1270 100644 --- a/server/books/tests/test_objectives_migration.py +++ b/server/books/tests/test_objectives_migration.py @@ -1,18 +1,8 @@ -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.factories import ModuleFactory, ChapterFactory 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 books.tests.queries import MODULE_QUERY from core.tests.base_test import SkillboxTestCase from objectives.factories import ObjectiveGroupFactory, ObjectiveFactory -from users.factories import SchoolClassFactory from users.models import User, SchoolClass @@ -42,6 +32,7 @@ class TestObjectivesMigration(SkillboxTestCase): objective_group = ObjectiveGroupFactory(module=self.module, title='Gesellschaft') second_objective_group = ObjectiveGroupFactory(module=self.module, title='Sprache & Kommunikation') + third_objective_group = ObjectiveGroupFactory(module=self.module, title='Übergeordnete Lernziele') self.visible_objective = ObjectiveFactory(text='visible-objective', group=objective_group) self.hidden_objective = ObjectiveFactory(text='hidden-objective', group=objective_group) @@ -109,3 +100,21 @@ class TestObjectivesMigration(SkillboxTestCase): self.assertEqual(hidden_custom['hiddenFor'], []) self.assertEqual(hidden_custom['visibleFor'], []) self.assertEqual(hidden_custom['contents'][0]['value']['text'], '') + + def test_objectives_order(self): + """The correct oder of the objectives is: + - Sprache & Kommunikation + - Gesellschaft + - Übergeordnete Lernziele + """ + + result = self.client.execute(MODULE_QUERY, variables={ + 'slug': self.module.slug + }) + module = result.data['module'] + chapter1 = module['chapters'][0] + titles = [content['title'] for content in chapter1['contentBlocks']] + self.assertEqual(titles, ['Sprache & Kommunikation', 'Gesellschaft','Gesellschaft','Übergeordnete Lernziele']) + + + From 16da66da311f4a6aacb48889c6994073899766cb Mon Sep 17 00:00:00 2001 From: Lorenz Padberg Date: Fri, 16 Feb 2024 12:55:42 +0100 Subject: [PATCH 2/9] Create Custom order for objective groups --- .../commands/migrate_objectives_to_content.py | 192 ++++++++++-------- .../books/tests/test_objectives_migration.py | 6 +- .../books/tests/test_snapshots_migration.py | 12 +- 3 files changed, 118 insertions(+), 92 deletions(-) diff --git a/server/books/management/commands/migrate_objectives_to_content.py b/server/books/management/commands/migrate_objectives_to_content.py index 9f5df387..48211b5f 100644 --- a/server/books/management/commands/migrate_objectives_to_content.py +++ b/server/books/management/commands/migrate_objectives_to_content.py @@ -3,6 +3,7 @@ from logging import getLogger from django.core.management import BaseCommand from django.core.exceptions import ValidationError +from django.db import models from books.models import Chapter, ObjectiveGroupSnapshot, Snapshot, ContentBlockSnapshot, ChapterSnapshot from books.models import ContentBlock @@ -11,6 +12,7 @@ from objectives.models import ObjectiveSnapshot, Objective, ObjectiveGroup logger = getLogger(__name__) + class Command(BaseCommand): def handle(self, *args, **options): ContentBlock.objects.filter(title__startswith="TESTOBJECTIVE").delete() @@ -21,110 +23,131 @@ class Command(BaseCommand): migrate_objectives_to_content() + def migrate_objectives_to_content(): - created_content_blocks = 0 + created_content_blocks = 0 - failed_modules = [] + failed_modules = [] - # This dict stores all content blocks that have been created for a set of objectives - # In this way we can reuse content blocks for the same set of objectives - created_default_content_blocks = {} + # This dict stores all content blocks that have been created for a set of objectives + # In this way we can reuse content blocks for the same set of objectives + created_default_content_blocks = {} - for module in Module.objects.all(): - try: - chapter = create_chapter_from_objective_group(module) + for module in Module.objects.all(): + try: + chapter = create_chapter_from_objective_group(module) - for objective_group in module.objective_groups.all().order_by('title'): - default_objectives = list(objective_group.objectives.filter(owner__isnull=True, ) - .exclude(objectivesnapshot__isnull=False).order_by('order')) + for objective_group in get_objectives_groups_in_specific_order(module): + default_objectives = list(objective_group.objectives.filter(owner__isnull=True, ) + .exclude(objectivesnapshot__isnull=False).order_by('order')) - default_objectives_ids = tuple(objective.id for objective in default_objectives) + default_objectives_ids = tuple(objective.id for objective in default_objectives) - # Create "Verlagsinhalte" content block if it does not exist yet - if default_objectives_ids not in created_default_content_blocks: - default_content_block = create_default_content(objective_group, chapter) - created_default_content_blocks[default_objectives_ids] = default_content_block - else: - default_content_block = created_default_content_blocks[default_objectives_ids] + # Create "Verlagsinhalte" content block if it does not exist yet + if default_objectives_ids not in created_default_content_blocks: + default_content_block = create_default_content(objective_group, chapter) + created_default_content_blocks[default_objectives_ids] = default_content_block + else: + default_content_block = created_default_content_blocks[default_objectives_ids] - custom_objectives_by_owner = get_objectives_by_owner(objective_group) + custom_objectives_by_owner = get_objectives_by_owner(objective_group) - if default_objectives or custom_objectives_by_owner: - contentblocks_by_merged_objectives_ids = {} + if default_objectives or custom_objectives_by_owner: + contentblocks_by_merged_objectives_ids = {} - # cor custom objectives iterate over owners, - # - one ownsers custom objectives must not be changed by another owner - # - visibility is set per class - for owner, owner_objectives in custom_objectives_by_owner.items(): - print(f"Owner: {owner}") - print(f" Objectives: ") + # cor custom objectives iterate over owners, + # - one ownsers custom objectives must not be changed by another owner + # - visibility is set per class + for owner, owner_objectives in custom_objectives_by_owner.items(): + print(f"Owner: {owner}") + print(f" Objectives: ") - visible_default_objectives_by_class = filter_visible_objectives_by_class(default_objectives, owner) + visible_default_objectives_by_class = filter_visible_objectives_by_class(default_objectives, + owner) - for school_class, default_objectives_for_class in visible_default_objectives_by_class.items(): - custom_content_block = None + 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)] + 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 = 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 + 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 is_default_content: - print(f" Objective: Reuse default content block") - # custom_content_block = default_content_block + 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 not is_default_content: - for objective in merged_objectives: - print(f" Objective: {objective} {objective.owner}") + # 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 not is_default_content: + for objective in merged_objectives: + print(f" Objective: {objective} {objective.owner}") - if merged_objectives_ids: - custom_content_block = create_content_block_from_objective(objective_group, - chapter, - owner=owner, - ) - 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 + if merged_objectives_ids: + custom_content_block = create_content_block_from_objective(objective_group, + chapter, + owner=owner, + ) + 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 not is_default_content: + print(f" Objective: Reuse content block") + custom_content_block = contentblocks_by_merged_objectives_ids[ + merged_objectives_ids] else: - if not is_default_content: - print(f" Objective: Reuse content block") - custom_content_block = contentblocks_by_merged_objectives_ids[ - merged_objectives_ids] - else: - print(f" Objective: Reuse default content block") + print(f" Objective: Reuse default content block") - # set visibility - # hide default objectives if custom objectives exist - if custom_content_block: - default_content_block.hidden_for.add(school_class) - default_content_block.save_revision().publish() - default_content_block.save() + # set visibility + # hide default objectives if custom objectives exist + if custom_content_block: + default_content_block.hidden_for.add(school_class) + default_content_block.save_revision().publish() + default_content_block.save() - custom_content_block.visible_for.add(school_class) - custom_content_block.save_revision().publish() - custom_content_block.save() + custom_content_block.visible_for.add(school_class) + custom_content_block.save_revision().publish() + custom_content_block.save() - if objective_group.hidden_for.filter(id=school_class.id).exists(): - default_content_block.hidden_for.add(school_class) - default_content_block.save_revision().publish() - default_content_block.save() + if objective_group.hidden_for.filter(id=school_class.id).exists(): + default_content_block.hidden_for.add(school_class) + default_content_block.save_revision().publish() + default_content_block.save() - except ValidationError as e: - print(f"Error with module {module}") - logger.error(e) - failed_modules.append(module) + except ValidationError as e: + print(f"Error with module {module}") + logger.error(e) + failed_modules.append(module) - print(f"Created {created_content_blocks} content blocks") - print(f"Failed modules: {len(failed_modules)}") + print(f"Created {created_content_blocks} content blocks") + print(f"Failed modules: {len(failed_modules)}") - for module in failed_modules: - print(f"Faile module: {module}") + for module in failed_modules: + print(f"Faile module: {module}") + + +def get_objectives_groups_in_specific_order(module): + # Create a specific order for the objective groups + # https://stackoverflow.com/questions/5966462/sort-queryset-by-values-in-list + # https://docs.djangoproject.com/en/5.0/ref/models/conditional-expressions/ + order_of_objective_groups = ["Sprache & Kommunikation", "Gesellschaft", "Übergeordnete Lernziele"] + _whens = [] + for sort_index, value in enumerate(order_of_objective_groups): + _whens.append( + models.When(title=value, then=sort_index) + ) + + qs = module.objective_groups.all().annotate(_sort_index=models.Case(*_whens, + output_field=models.IntegerField() + ) + ).order_by('_sort_index') + return qs def create_default_content(objective_group, chapter): @@ -156,7 +179,8 @@ def filter_visible_objectives_by_class(objectives, user): def get_objectives_by_owner(objective_group, exclude_snapshots=True): - custom_objectives = objective_group.objectives.filter(owner__isnull=False, objectivesnapshot__isnull=True).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: @@ -233,15 +257,16 @@ def create_text_in_content_block(objectives, content_block, get_or_create=False) if get_or_create: content_block_qs = ContentBlock.objects.filter(title=content_block.title, - owner=content_block.owner, - user_created=content_block.user_created, - contents=content_block.contents) + owner=content_block.owner, + user_created=content_block.user_created, + contents=content_block.contents) if content_block_qs.exists(): content_block = content_block_qs.first() content_block.save_revision().publish() return content_block + def create_content_block_contents(objectives): objectives = list(objectives) objective_li = [f"
  • {objective.text}
  • " for objective in objectives if objective.text] @@ -254,6 +279,7 @@ def create_content_block_contents(objectives): contents = json.dumps(texts) return contents + def analyze(): print(f""" OjectiveGroups: {ObjectiveGroup.objects.count()} diff --git a/server/books/tests/test_objectives_migration.py b/server/books/tests/test_objectives_migration.py index 2acf1270..bfc5120f 100644 --- a/server/books/tests/test_objectives_migration.py +++ b/server/books/tests/test_objectives_migration.py @@ -61,7 +61,7 @@ class TestObjectivesMigration(SkillboxTestCase): }) module = result.data['module'] chapter1 = module['chapters'][0] - default_content, _, _ = chapter1['contentBlocks'] + _, 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') @@ -76,7 +76,7 @@ class TestObjectivesMigration(SkillboxTestCase): }) module = result.data['module'] chapter1 = module['chapters'][0] - _, custom, _ = chapter1['contentBlocks'] + _,_, custom, _ = chapter1['contentBlocks'] # default content block (Verlagsinhalte) exists but is hidden (since one objective is hidden for this class) self.assertEqual(custom['title'], 'Gesellschaft') @@ -92,7 +92,7 @@ class TestObjectivesMigration(SkillboxTestCase): }) module = result.data['module'] chapter1 = module['chapters'][0] - _, _, hidden_custom = chapter1['contentBlocks'] + 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') diff --git a/server/books/tests/test_snapshots_migration.py b/server/books/tests/test_snapshots_migration.py index 492920a0..5ae3ea8e 100644 --- a/server/books/tests/test_snapshots_migration.py +++ b/server/books/tests/test_snapshots_migration.py @@ -80,7 +80,7 @@ class TestSnapshotMigration(SkillboxTestCase): }) module = result.data['module'] chapter1 = module['chapters'][0] - default_content, _, _ = chapter1['contentBlocks'] + _, 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') @@ -95,7 +95,7 @@ class TestSnapshotMigration(SkillboxTestCase): }) module = result.data['module'] chapter1 = module['chapters'][0] - _, custom, _ = chapter1['contentBlocks'] + _, _, custom = chapter1['contentBlocks'] self.assertEqual(custom['title'], 'Gesellschaft') self.assertTrue(custom['originalCreator'] is not None) @@ -110,7 +110,7 @@ class TestSnapshotMigration(SkillboxTestCase): }) module = result.data['module'] chapter1 = module['chapters'][0] - _, _, hidden_custom = chapter1['contentBlocks'] + 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') @@ -127,7 +127,7 @@ class TestSnapshotMigration(SkillboxTestCase): }) module = result.data['module'] chapter1 = module['chapters'][0] - default_content, _, _, _ = chapter1['contentBlocks'] + _, 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') @@ -146,7 +146,7 @@ class TestSnapshotMigration(SkillboxTestCase): }) module = result.data['module'] chapter1 = module['chapters'][0] - _, custom, _, _ = chapter1['contentBlocks'] + _, _, custom, _ = chapter1['contentBlocks'] self.assertEqual(custom['title'], 'Gesellschaft') self.assertTrue(custom['userCreated']) @@ -165,7 +165,7 @@ class TestSnapshotMigration(SkillboxTestCase): }) module = result.data['module'] chapter1 = module['chapters'][0] - _, _, hidden_custom, _ = chapter1['contentBlocks'] + 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') From 55fc5da9a7f8793159bce46b8cc49a75a25d67fe Mon Sep 17 00:00:00 2001 From: Lorenz Padberg Date: Fri, 16 Feb 2024 13:02:17 +0100 Subject: [PATCH 3/9] Add test for hidden default content bug --- server/books/tests/test_objectives_migration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/books/tests/test_objectives_migration.py b/server/books/tests/test_objectives_migration.py index bfc5120f..8fe1f6b6 100644 --- a/server/books/tests/test_objectives_migration.py +++ b/server/books/tests/test_objectives_migration.py @@ -97,7 +97,7 @@ class TestObjectivesMigration(SkillboxTestCase): # 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['hiddenFor'], [{'name': 'skillbox'}]) self.assertEqual(hidden_custom['visibleFor'], []) self.assertEqual(hidden_custom['contents'][0]['value']['text'], '
    • objective1
    ') From 3e86d9a62097b5fb972e42b7571879e5cea7db1e Mon Sep 17 00:00:00 2001 From: Lorenz Padberg Date: Mon, 26 Feb 2024 11:34:19 +0100 Subject: [PATCH 4/9] Fix hidden default group bug --- .../commands/migrate_objectives_to_content.py | 6 ++++++ server/books/tests/test_objectives_migration.py | 12 ++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/server/books/management/commands/migrate_objectives_to_content.py b/server/books/management/commands/migrate_objectives_to_content.py index 48211b5f..4bc7f8a5 100644 --- a/server/books/management/commands/migrate_objectives_to_content.py +++ b/server/books/management/commands/migrate_objectives_to_content.py @@ -50,6 +50,12 @@ def migrate_objectives_to_content(): else: default_content_block = created_default_content_blocks[default_objectives_ids] + # set visibility for objective_group + if objective_group.hidden_for.exists(): + default_content_block.hidden_for.add(*objective_group.hidden_for.all()) + default_content_block.save_revision().publish() + default_content_block.save() + custom_objectives_by_owner = get_objectives_by_owner(objective_group) if default_objectives or custom_objectives_by_owner: diff --git a/server/books/tests/test_objectives_migration.py b/server/books/tests/test_objectives_migration.py index 8fe1f6b6..b7d0573a 100644 --- a/server/books/tests/test_objectives_migration.py +++ b/server/books/tests/test_objectives_migration.py @@ -92,14 +92,14 @@ class TestObjectivesMigration(SkillboxTestCase): }) module = result.data['module'] chapter1 = module['chapters'][0] - hidden_custom, _, _, _ = chapter1['contentBlocks'] + hidden_default, _, _, _ = 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'], '
    • objective1
    ') + self.assertEqual(hidden_default['title'], 'Sprache & Kommunikation') + self.assertTrue(hidden_default['originalCreator'] is None) + self.assertEqual(hidden_default['hiddenFor'], [{'name': 'skillbox'}]) + self.assertEqual(hidden_default['visibleFor'], []) + self.assertEqual(hidden_default['contents'][0]['value']['text'], '
    • objective1
    ') def test_objectives_order(self): """The correct oder of the objectives is: From 817284ab4cd42ca914c2448d5694753e8a280130 Mon Sep 17 00:00:00 2001 From: Lorenz Padberg Date: Mon, 26 Feb 2024 11:36:09 +0100 Subject: [PATCH 5/9] Set environment to skillbox --- server/lorenz.env | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/server/lorenz.env b/server/lorenz.env index 1a183ea9..75517a14 100644 --- a/server/lorenz.env +++ b/server/lorenz.env @@ -8,6 +8,5 @@ export USE_AWS=False export WAGTAILADMIN_BASE_URL=/ export ALLOW_BETA_LOGIN=True -export THEME=my-kv -export APP_FLAVOR=my-kv - +#export THEME=my-kv +#export APP_FLAVOR=my-kv From 4e7443906de1c324c2ca48aa199fe872c2508813 Mon Sep 17 00:00:00 2001 From: Lorenz Padberg Date: Mon, 26 Feb 2024 11:50:36 +0100 Subject: [PATCH 6/9] Remove unused code --- .../commands/migrate_objectives_to_content.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/server/books/management/commands/migrate_objectives_to_content.py b/server/books/management/commands/migrate_objectives_to_content.py index 4bc7f8a5..53997929 100644 --- a/server/books/management/commands/migrate_objectives_to_content.py +++ b/server/books/management/commands/migrate_objectives_to_content.py @@ -15,12 +15,6 @@ logger = getLogger(__name__) class Command(BaseCommand): def handle(self, *args, **options): - ContentBlock.objects.filter(title__startswith="TESTOBJECTIVE").delete() - ContentBlock.objects.filter(title__startswith="CUSTOM").delete() - Chapter.objects.filter(title__startswith="TESTOBJECTIVE").delete() - ContentBlock.objects.filter(title__startswith="XXX").delete() - Chapter.objects.filter(title__startswith="XXX").delete() - migrate_objectives_to_content() @@ -61,7 +55,7 @@ def migrate_objectives_to_content(): if default_objectives or custom_objectives_by_owner: contentblocks_by_merged_objectives_ids = {} - # cor custom objectives iterate over owners, + # for custom objectives iterate over owners, # - one ownsers custom objectives must not be changed by another owner # - visibility is set per class for owner, owner_objectives in custom_objectives_by_owner.items(): @@ -126,6 +120,9 @@ def migrate_objectives_to_content(): default_content_block.save() + + + except ValidationError as e: print(f"Error with module {module}") logger.error(e) From 18c6e0ac7268f5ea1b31442776b6838fdbef6e9b Mon Sep 17 00:00:00 2001 From: Lorenz Padberg Date: Mon, 26 Feb 2024 15:57:07 +0100 Subject: [PATCH 7/9] Fix bug Custom objectives in hidden group --- .../commands/migrate_objectives_to_content.py | 18 +++---- .../books/tests/test_objectives_migration.py | 49 +++++++++++++------ 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/server/books/management/commands/migrate_objectives_to_content.py b/server/books/management/commands/migrate_objectives_to_content.py index 53997929..ed5e0f77 100644 --- a/server/books/management/commands/migrate_objectives_to_content.py +++ b/server/books/management/commands/migrate_objectives_to_content.py @@ -44,7 +44,7 @@ def migrate_objectives_to_content(): else: default_content_block = created_default_content_blocks[default_objectives_ids] - # set visibility for objective_group + # set visibility for objective_group for default content (Verlagsinhalte) if objective_group.hidden_for.exists(): default_content_block.hidden_for.add(*objective_group.hidden_for.all()) default_content_block.save_revision().publish() @@ -110,19 +110,15 @@ def migrate_objectives_to_content(): default_content_block.save_revision().publish() default_content_block.save() - custom_content_block.visible_for.add(school_class) + # make custom content block visible for school class if it is not in hidden list + if not objective_group.hidden_for.filter(id=school_class.id).exists(): + custom_content_block.visible_for.add(school_class) + else: + custom_content_block.hidden_for.add(school_class) + custom_content_block.save_revision().publish() custom_content_block.save() - if objective_group.hidden_for.filter(id=school_class.id).exists(): - default_content_block.hidden_for.add(school_class) - default_content_block.save_revision().publish() - default_content_block.save() - - - - - except ValidationError as e: print(f"Error with module {module}") logger.error(e) diff --git a/server/books/tests/test_objectives_migration.py b/server/books/tests/test_objectives_migration.py index b7d0573a..ecf557fa 100644 --- a/server/books/tests/test_objectives_migration.py +++ b/server/books/tests/test_objectives_migration.py @@ -31,27 +31,32 @@ class TestObjectivesMigration(SkillboxTestCase): 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') - third_objective_group = ObjectiveGroupFactory(module=self.module, title='Übergeordnete Lernziele') 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 = ObjectiveFactory(text='custom-objective', group=objective_group, owner=self.teacher) self.custom_objective.visible_for.add(self.skillbox_class) self.custom_objective.save() + self.custom_hidden_objective = ObjectiveFactory(text='custom-hidden-objective', group=objective_group, + owner=self.teacher) + self.custom_hidden_objective.visible_for.remove(self.skillbox_class) + second_objective_group = ObjectiveGroupFactory(module=self.module, title='Sprache & Kommunikation') + self.visible_objective = ObjectiveFactory(text='objective1', group=second_objective_group) second_objective_group.hidden_for.add(self.skillbox_class) second_objective_group.save() - self.custom_hidden_objective.visible_for.remove(self.skillbox_class) + third_objective_group = ObjectiveGroupFactory(module=self.module, title='Übergeordnete Lernziele') + self.visible_objective_hidden_group_3 = ObjectiveFactory(text='objective1', group=third_objective_group) + self.hidden_objective_hidden_group_3 = ObjectiveFactory(text='objective2', group=third_objective_group, + owner=self.teacher) + self.hidden_objective_hidden_group_3.visible_for.add(self.skillbox_class) + self.hidden_objective_hidden_group_3.save() + + third_objective_group.hidden_for.add(self.skillbox_class) + third_objective_group.save() migrate_objectives_to_content() @@ -61,7 +66,7 @@ class TestObjectivesMigration(SkillboxTestCase): }) module = result.data['module'] chapter1 = module['chapters'][0] - _, default_content, _,_ = chapter1['contentBlocks'] + _, 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') @@ -76,7 +81,7 @@ class TestObjectivesMigration(SkillboxTestCase): }) module = result.data['module'] chapter1 = module['chapters'][0] - _,_, custom, _ = chapter1['contentBlocks'] + _, _, custom, _, _ = chapter1['contentBlocks'] # default content block (Verlagsinhalte) exists but is hidden (since one objective is hidden for this class) self.assertEqual(custom['title'], 'Gesellschaft') @@ -92,7 +97,7 @@ class TestObjectivesMigration(SkillboxTestCase): }) module = result.data['module'] chapter1 = module['chapters'][0] - hidden_default, _, _, _ = chapter1['contentBlocks'] + hidden_default, _, _, _, _ = chapter1['contentBlocks'] # default content block (Verlagsinhalte) exists but is hidden (since one objective is hidden for this class) self.assertEqual(hidden_default['title'], 'Sprache & Kommunikation') @@ -114,7 +119,21 @@ class TestObjectivesMigration(SkillboxTestCase): module = result.data['module'] chapter1 = module['chapters'][0] titles = [content['title'] for content in chapter1['contentBlocks']] - self.assertEqual(titles, ['Sprache & Kommunikation', 'Gesellschaft','Gesellschaft','Übergeordnete Lernziele']) - + self.assertEqual(titles, ['Sprache & Kommunikation', 'Gesellschaft', 'Gesellschaft', 'Übergeordnete Lernziele', + 'Übergeordnete Lernziele']) + def test_objectives_migration_hidden_group_custom_content(self): + result = self.client.execute(MODULE_QUERY, variables={ + 'slug': self.module.slug + }) + module = result.data['module'] + chapter1 = module['chapters'][0] + _, _, _, _, hidden_custom_group = chapter1['contentBlocks'] + # default content block (Verlagsinhalte) exists but is hidden (since one objective is hidden for this class) + self.assertEqual(hidden_custom_group['title'], 'Übergeordnete Lernziele') + self.assertTrue(hidden_custom_group['originalCreator'] is not None) + self.assertEqual(hidden_custom_group['hiddenFor'], [{'name': 'skillbox'}]) + self.assertEqual(hidden_custom_group['visibleFor'], []) + self.assertEqual(hidden_custom_group['contents'][0]['value']['text'], + '
    • objective1
    • objective2
    ') From db6c2c4f8bba081c740b590aa666019b942bedc8 Mon Sep 17 00:00:00 2001 From: Lorenz Padberg Date: Mon, 26 Feb 2024 17:25:01 +0100 Subject: [PATCH 8/9] Fix CMS sorting bug --- .../management/commands/migrate_objectives_to_content.py | 2 +- server/core/views.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/server/books/management/commands/migrate_objectives_to_content.py b/server/books/management/commands/migrate_objectives_to_content.py index ed5e0f77..4c3df4e4 100644 --- a/server/books/management/commands/migrate_objectives_to_content.py +++ b/server/books/management/commands/migrate_objectives_to_content.py @@ -135,7 +135,7 @@ def get_objectives_groups_in_specific_order(module): # Create a specific order for the objective groups # https://stackoverflow.com/questions/5966462/sort-queryset-by-values-in-list # https://docs.djangoproject.com/en/5.0/ref/models/conditional-expressions/ - order_of_objective_groups = ["Sprache & Kommunikation", "Gesellschaft", "Übergeordnete Lernziele"] + order_of_objective_groups = ["language_communication", "society", "interdisciplinary"] _whens = [] for sort_index, value in enumerate(order_of_objective_groups): _whens.append( diff --git a/server/core/views.py b/server/core/views.py index 16b65e54..e808a13f 100644 --- a/server/core/views.py +++ b/server/core/views.py @@ -9,7 +9,7 @@ from django.views.decorators.csrf import ensure_csrf_cookie from graphene_django.views import GraphQLView from graphql import get_operation_ast, parse from sentry_sdk.api import start_transaction -from wagtail.admin.views.pages.listing import IndexView +from wagtail.admin.views.pages import listing logger = get_logger(__name__) @@ -67,4 +67,4 @@ def override_wagtailadmin_explore_default_ordering(request, parent_page_id): # Display reordering handles by default for children of all Page types. return HttpResponseRedirect(request.path_info + "?ordering=ord") - return IndexView.as_view(request=request, parent_page_id=parent_page_id) + return listing.IndexView.as_view()(request, parent_page_id) From c418a2bbd3fb5a3bddefc500fa46f1f68bc9a9ee Mon Sep 17 00:00:00 2001 From: Lorenz Padberg Date: Tue, 27 Feb 2024 11:39:37 +0100 Subject: [PATCH 9/9] Add new sorting to Snapshot migration --- .../commands/migrate_objective_snapshots.py | 21 +++++++++++++++++-- .../books/tests/test_objectives_migration.py | 12 +++++------ .../books/tests/test_snapshots_migration.py | 4 ++-- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/server/books/management/commands/migrate_objective_snapshots.py b/server/books/management/commands/migrate_objective_snapshots.py index ad3e3f97..624cb734 100644 --- a/server/books/management/commands/migrate_objective_snapshots.py +++ b/server/books/management/commands/migrate_objective_snapshots.py @@ -2,6 +2,8 @@ import json from logging import getLogger from django.core.management import BaseCommand +from django.db import models +from django.db.models import Case, IntegerField from books.management.commands.migrate_objectives_to_content import create_text_in_content_block, \ create_content_block_snapshot_from_objective, \ @@ -72,8 +74,7 @@ def migrate_snapshots(): 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): + for objective_group_snapshot in get_objectives_group_snapshots_in_specific_order(module, 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 @@ -197,6 +198,22 @@ def get_default_content_block(objective_group_snapshot, module): raise Exception("Content block does not exist ") +def get_objectives_group_snapshots_in_specific_order(module: Module, snapshot: Snapshot): + # Create a specific order for the objective groups + # https://stackoverflow.com/questions/5966462/sort-queryset-by-values-in-list + # https://docs.djangoproject.com/en/5.0/ref/models/conditional-expressions/ + order_of_objective_groups = ["language_communication", "society", "interdisciplinary"] + _whens = [models.When(objective_group__title=value, then=sort_index) for sort_index, value in enumerate(order_of_objective_groups)] + + qs = snapshot.objective_groups.through.objects.filter( + objective_group__module=module, + snapshot=snapshot + ).annotate( + _sort_index=Case(*_whens, default=models.Value(len(order_of_objective_groups)), output_field=IntegerField()) + ).order_by('_sort_index') + return qs + + def get_visible_default_objectives(objective_group, module, snapshot): default_objectives = Objective.objects.filter(group=objective_group, group__module=module, diff --git a/server/books/tests/test_objectives_migration.py b/server/books/tests/test_objectives_migration.py index ecf557fa..d8b8e674 100644 --- a/server/books/tests/test_objectives_migration.py +++ b/server/books/tests/test_objectives_migration.py @@ -30,7 +30,7 @@ class TestObjectivesMigration(SkillboxTestCase): 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') + objective_group = ObjectiveGroupFactory(module=self.module, title='society') self.visible_objective = ObjectiveFactory(text='visible-objective', group=objective_group) self.hidden_objective = ObjectiveFactory(text='hidden-objective', group=objective_group) @@ -43,12 +43,12 @@ class TestObjectivesMigration(SkillboxTestCase): owner=self.teacher) self.custom_hidden_objective.visible_for.remove(self.skillbox_class) - second_objective_group = ObjectiveGroupFactory(module=self.module, title='Sprache & Kommunikation') + second_objective_group = ObjectiveGroupFactory(module=self.module, title='language_communication') self.visible_objective = ObjectiveFactory(text='objective1', group=second_objective_group) second_objective_group.hidden_for.add(self.skillbox_class) second_objective_group.save() - third_objective_group = ObjectiveGroupFactory(module=self.module, title='Übergeordnete Lernziele') + third_objective_group = ObjectiveGroupFactory(module=self.module, title='interdisciplinary') self.visible_objective_hidden_group_3 = ObjectiveFactory(text='objective1', group=third_objective_group) self.hidden_objective_hidden_group_3 = ObjectiveFactory(text='objective2', group=third_objective_group, owner=self.teacher) @@ -119,8 +119,8 @@ class TestObjectivesMigration(SkillboxTestCase): module = result.data['module'] chapter1 = module['chapters'][0] titles = [content['title'] for content in chapter1['contentBlocks']] - self.assertEqual(titles, ['Sprache & Kommunikation', 'Gesellschaft', 'Gesellschaft', 'Übergeordnete Lernziele', - 'Übergeordnete Lernziele']) + self.assertEqual(titles, ['Sprache & Kommunikation', 'Gesellschaft', 'Gesellschaft', 'Überfachliche Lernziele', + 'Überfachliche Lernziele']) def test_objectives_migration_hidden_group_custom_content(self): result = self.client.execute(MODULE_QUERY, variables={ @@ -131,7 +131,7 @@ class TestObjectivesMigration(SkillboxTestCase): _, _, _, _, hidden_custom_group = chapter1['contentBlocks'] # default content block (Verlagsinhalte) exists but is hidden (since one objective is hidden for this class) - self.assertEqual(hidden_custom_group['title'], 'Übergeordnete Lernziele') + self.assertEqual(hidden_custom_group['title'], 'Überfachliche Lernziele') self.assertTrue(hidden_custom_group['originalCreator'] is not None) self.assertEqual(hidden_custom_group['hiddenFor'], [{'name': 'skillbox'}]) self.assertEqual(hidden_custom_group['visibleFor'], []) diff --git a/server/books/tests/test_snapshots_migration.py b/server/books/tests/test_snapshots_migration.py index 5ae3ea8e..e95aaada 100644 --- a/server/books/tests/test_snapshots_migration.py +++ b/server/books/tests/test_snapshots_migration.py @@ -40,8 +40,8 @@ class TestSnapshotMigration(SkillboxTestCase): 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') + objective_group = ObjectiveGroupFactory(module=self.module, title='society') + second_objective_group = ObjectiveGroupFactory(module=self.module, title='language_communication') self.visible_objective = ObjectiveFactory(text='visible-objective', group=objective_group) self.visible_objective_2 = ObjectiveFactory(text='hidden-objective', group=objective_group)