Add new sorting to Snapshot migration

This commit is contained in:
Lorenz Padberg 2024-02-27 11:39:37 +01:00
parent db6c2c4f8b
commit c418a2bbd3
3 changed files with 27 additions and 10 deletions

View File

@ -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,

View File

@ -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'], [])

View File

@ -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)