diff --git a/server/books/management/commands/migrate_objective_snapshots.py b/server/books/management/commands/migrate_objective_snapshots.py index 624cb734..b908e42c 100644 --- a/server/books/management/commands/migrate_objective_snapshots.py +++ b/server/books/management/commands/migrate_objective_snapshots.py @@ -24,11 +24,8 @@ class Command(BaseCommand): - Verlagsinhalte - deafult content blocks are referced in the snapshot by foreign key Man muss unterscheiden zwischen, snapshots die nur Verlagslernziele sichtbar und unsichtbar machen. - Und solchen die auch benutzerdefinierte Lernziele sichtbar und unsichtbar machen. - Es gibt keine custom objective groups! - Es gibt keine hidden custom objective_groups Case1: @@ -113,7 +110,6 @@ def migrate_snapshots(): visible_objectives = visible_default_objectives + visible_custom_objectives # 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: @@ -203,7 +199,8 @@ def get_objectives_group_snapshots_in_specific_order(module: Module, snapshot: S # 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)] + _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, diff --git a/server/books/management/commands/migrate_objectives_to_content.py b/server/books/management/commands/migrate_objectives_to_content.py index 4c3df4e4..2f6165ae 100644 --- a/server/books/management/commands/migrate_objectives_to_content.py +++ b/server/books/management/commands/migrate_objectives_to_content.py @@ -237,16 +237,34 @@ def create_content_block_from_objective(objective_group, chapter, owner=None): return content_block +def insert_content_block(chapter, content_block): + "" "Inserts the content block into the chapter.""" + if chapter.title != "Lernziele": + return chapter + + siblings = chapter.get_children() + siblings_titles = [sibling.title for sibling in siblings] + + # get the index of the last occurrence of the title + target_position = len(siblings_titles) - 1 - siblings_titles[::-1].index(content_block.title) + + target_child = siblings[target_position] + target_child.add_sibling(instance=content_block, pos="left") + return chapter + + def create_content_block_snapshot_from_objective(objective_group, chapter, snapshot, owner=None): content_block_snapshot = ContentBlockSnapshot( title=f"{objective_group.get_title_display()}", type="normal", owner=owner, + #original_creator=owner, user_created=owner is not None, snapshot=snapshot ) - chapter.add_child(instance=content_block_snapshot) + insert_content_block(chapter, content_block_snapshot) + return content_block_snapshot diff --git a/server/books/management/commands/print_lernziele_order.py b/server/books/management/commands/print_lernziele_order.py new file mode 100644 index 00000000..b17add05 --- /dev/null +++ b/server/books/management/commands/print_lernziele_order.py @@ -0,0 +1,28 @@ +import json +from logging import getLogger + +from django.core.management import BaseCommand + +from books.management.commands.migrate_objectives_to_content import create_text_in_content_block, \ + create_content_block_snapshot_from_objective, \ + create_content_block_contents +from books.models import Chapter, ObjectiveGroupSnapshot, ContentBlockSnapshot, Snapshot, ChapterSnapshot +from books.models import ContentBlock +from books.models import Module +from objectives.models import Objective, ObjectiveSnapshot, ObjectiveGroup + +logger = getLogger(__name__) + + +class Command(BaseCommand): + def handle(self, *args, **options): + + analyze() + + + +def analyze(): + for chapter in Chapter.objects.filter(title="Lernziele"): + print(f"Chapter: {chapter.get_parent().title} {[cb.title for cb in ContentBlock.get_by_parent(chapter)]}") + # [cb.delete() for cb in ContentBlock.get_by_parent(chapter)] + # chapter.delete() diff --git a/server/books/tests/test_snapshots_migration.py b/server/books/tests/test_snapshots_migration.py index e95aaada..ad6b2ef1 100644 --- a/server/books/tests/test_snapshots_migration.py +++ b/server/books/tests/test_snapshots_migration.py @@ -41,24 +41,23 @@ class TestSnapshotMigration(SkillboxTestCase): ChapterFactory(parent=self.module, slug='some-other-chapter', owner=self.admin) 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) 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() + 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) + self.custom_hidden_objective.save() + + + 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) - self.snapshot1 = Snapshot.objects.create_snapshot(self.module, self.skillbox_class, self.teacher) migrate_objectives_to_content() @@ -67,10 +66,13 @@ class TestSnapshotMigration(SkillboxTestCase): # Change visibility of objectives resp. content blocks, hide all - for content_block in ContentBlock.objects.all().descendant_of(self.chapter): + for content_block in ContentBlock.objects.all().descendant_of(self.module): if content_block.owner is None: + print(f"Titel verlagsinhalt {content_block.title}") content_block.hidden_for.add(self.skillbox_class) + else: + print(f"Titel Benutzerinhal {content_block.title}") content_block.visible_for.remove(self.skillbox_class) content_block.save() @@ -138,7 +140,6 @@ class TestSnapshotMigration(SkillboxTestCase): 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={ @@ -150,14 +151,13 @@ class TestSnapshotMigration(SkillboxTestCase): self.assertEqual(custom['title'], 'Gesellschaft') self.assertTrue(custom['userCreated']) - self.assertTrue(custom['originalCreator'] is not None) + self.assertTrue(custom['originalCreator'] is None) self.assertEqual(custom['hiddenFor'], []) - self.assertEqual(custom['visibleFor'], []) + self.assertEqual(custom['visibleFor'], [{'name': 'skillbox'}]) self.assertEqual(custom['contents'][0]['value']['text'], '