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/management/commands/migrate_objectives_to_content.py b/server/books/management/commands/migrate_objectives_to_content.py index 9f5df387..4c3df4e4 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,120 +12,141 @@ 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() - 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() + 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) + # 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() + default_content_block.save() - if default_objectives or custom_objectives_by_owner: - contentblocks_by_merged_objectives_ids = {} + custom_objectives_by_owner = get_objectives_by_owner(objective_group) - # 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: ") + if default_objectives or custom_objectives_by_owner: + contentblocks_by_merged_objectives_ids = {} - visible_default_objectives_by_class = filter_visible_objectives_by_class(default_objectives, owner) + # 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(): + print(f"Owner: {owner}") + print(f" Objectives: ") - for school_class, default_objectives_for_class in visible_default_objectives_by_class.items(): - custom_content_block = None + visible_default_objectives_by_class = filter_visible_objectives_by_class(default_objectives, + owner) - 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)] + for school_class, default_objectives_for_class in visible_default_objectives_by_class.items(): + custom_content_block = None - 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 + 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)] - if is_default_content: - print(f" Objective: Reuse default content block") - # custom_content_block = default_content_block + 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 - # 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 is_default_content: + print(f" Objective: Reuse default content block") + # custom_content_block = default_content_block - 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 + # 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 + 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() + # 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) - custom_content_block.save_revision().publish() - custom_content_block.save() + else: + custom_content_block.hidden_for.add(school_class) - 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() + custom_content_block.save_revision().publish() + custom_content_block.save() + + 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)}") + + for module in failed_modules: + print(f"Faile module: {module}") - except ValidationError as e: - print(f"Error with module {module}") - logger.error(e) - failed_modules.append(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 = ["language_communication", "society", "interdisciplinary"] + _whens = [] + for sort_index, value in enumerate(order_of_objective_groups): + _whens.append( + models.When(title=value, then=sort_index) + ) - 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}") + 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 +178,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 +256,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 +278,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 785d15bc..d8b8e674 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 @@ -40,27 +30,33 @@ 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') - second_objective_group = ObjectiveGroupFactory(module=self.module, title='Sprache & Kommunikation') + 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) - 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='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() - self.custom_hidden_objective.visible_for.remove(self.skillbox_class) + 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) + 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() @@ -70,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') @@ -85,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') @@ -101,11 +97,43 @@ 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'], []) - self.assertEqual(hidden_custom['visibleFor'], []) - self.assertEqual(hidden_custom['contents'][0]['value']['text'], '') + 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'], '') + + 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', 'Überfachliche Lernziele', + 'Überfachliche 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'], 'Ü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'], []) + self.assertEqual(hidden_custom_group['contents'][0]['value']['text'], + '') diff --git a/server/books/tests/test_snapshots_migration.py b/server/books/tests/test_snapshots_migration.py index 492920a0..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) @@ -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') 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) 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