Insert content block in correct location when migrating snapshots

This commit is contained in:
Lorenz Padberg 2024-03-04 14:06:11 +01:00
parent 9c4249de01
commit 6019677890
5 changed files with 64 additions and 22 deletions

View File

@ -24,11 +24,8 @@ class Command(BaseCommand):
- Verlagsinhalte - deafult content blocks are referced in the snapshot by foreign key - Verlagsinhalte - deafult content blocks are referced in the snapshot by foreign key
Man muss unterscheiden zwischen, snapshots die nur Verlagslernziele sichtbar und unsichtbar machen. Man muss unterscheiden zwischen, snapshots die nur Verlagslernziele sichtbar und unsichtbar machen.
Und solchen die auch benutzerdefinierte Lernziele sichtbar und unsichtbar machen. Und solchen die auch benutzerdefinierte Lernziele sichtbar und unsichtbar machen.
Es gibt keine custom objective groups! Es gibt keine custom objective groups!
Es gibt keine hidden custom objective_groups Es gibt keine hidden custom objective_groups
Case1: Case1:
@ -113,7 +110,6 @@ def migrate_snapshots():
visible_objectives = visible_default_objectives + visible_custom_objectives visible_objectives = visible_default_objectives + visible_custom_objectives
# filter for unique texts in objectives # filter for unique texts in objectives
# TODO: I don't know why there are duplicated objectives
objectives_by_texts = {} objectives_by_texts = {}
for objective in visible_objectives: for objective in visible_objectives:
if objective.text not in objectives_by_texts: 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://stackoverflow.com/questions/5966462/sort-queryset-by-values-in-list
# https://docs.djangoproject.com/en/5.0/ref/models/conditional-expressions/ # https://docs.djangoproject.com/en/5.0/ref/models/conditional-expressions/
order_of_objective_groups = ["language_communication", "society", "interdisciplinary"] 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( qs = snapshot.objective_groups.through.objects.filter(
objective_group__module=module, objective_group__module=module,

View File

@ -237,16 +237,34 @@ def create_content_block_from_objective(objective_group, chapter, owner=None):
return content_block 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): def create_content_block_snapshot_from_objective(objective_group, chapter, snapshot, owner=None):
content_block_snapshot = ContentBlockSnapshot( content_block_snapshot = ContentBlockSnapshot(
title=f"{objective_group.get_title_display()}", title=f"{objective_group.get_title_display()}",
type="normal", type="normal",
owner=owner, owner=owner,
#original_creator=owner,
user_created=owner is not None, user_created=owner is not None,
snapshot=snapshot snapshot=snapshot
) )
chapter.add_child(instance=content_block_snapshot) insert_content_block(chapter, content_block_snapshot)
return content_block_snapshot return content_block_snapshot

View File

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

View File

@ -41,24 +41,23 @@ class TestSnapshotMigration(SkillboxTestCase):
ChapterFactory(parent=self.module, slug='some-other-chapter', owner=self.admin) ChapterFactory(parent=self.module, slug='some-other-chapter', owner=self.admin)
objective_group = ObjectiveGroupFactory(module=self.module, title='society') 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 = ObjectiveFactory(text='visible-objective', group=objective_group)
self.visible_objective_2 = ObjectiveFactory(text='hidden-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_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.visible_for.add(self.skillbox_class)
self.custom_objective.save() 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.hidden_for.add(self.skillbox_class)
second_objective_group.save() 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) self.snapshot1 = Snapshot.objects.create_snapshot(self.module, self.skillbox_class, self.teacher)
migrate_objectives_to_content() migrate_objectives_to_content()
@ -67,10 +66,13 @@ class TestSnapshotMigration(SkillboxTestCase):
# Change visibility of objectives resp. content blocks, hide all # 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: if content_block.owner is None:
print(f"Titel verlagsinhalt {content_block.title}")
content_block.hidden_for.add(self.skillbox_class) content_block.hidden_for.add(self.skillbox_class)
else: else:
print(f"Titel Benutzerinhal {content_block.title}")
content_block.visible_for.remove(self.skillbox_class) content_block.visible_for.remove(self.skillbox_class)
content_block.save() content_block.save()
@ -138,7 +140,6 @@ class TestSnapshotMigration(SkillboxTestCase):
self.assertEqual(default_content['hiddenFor'], [{'name': 'skillbox'}]) self.assertEqual(default_content['hiddenFor'], [{'name': 'skillbox'}])
def test_snapshot_migration_hidden_custom_content_apply_snapshot(self): 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) self.snapshot1.apply(self.teacher, self.school_class)
result = self.client.execute(MODULE_QUERY, variables={ result = self.client.execute(MODULE_QUERY, variables={
@ -150,14 +151,13 @@ class TestSnapshotMigration(SkillboxTestCase):
self.assertEqual(custom['title'], 'Gesellschaft') self.assertEqual(custom['title'], 'Gesellschaft')
self.assertTrue(custom['userCreated']) self.assertTrue(custom['userCreated'])
self.assertTrue(custom['originalCreator'] is not None) self.assertTrue(custom['originalCreator'] is None)
self.assertEqual(custom['hiddenFor'], []) self.assertEqual(custom['hiddenFor'], [])
self.assertEqual(custom['visibleFor'], []) self.assertEqual(custom['visibleFor'], [{'name': 'skillbox'}])
self.assertEqual(custom['contents'][0]['value']['text'], self.assertEqual(custom['contents'][0]['value']['text'],
'<ul><li>visible-objective</li><li>hidden-objective</li><li>custom-objective</li></ul>') '<ul><li>visible-objective</li><li>hidden-objective</li><li>custom-objective</li></ul>')
def test_snapshot_migration_hidden_content_block_apply_snapshot_2(self): 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) self.snapshot1.apply(self.teacher, self.school_class)
result = self.client.execute(MODULE_QUERY, variables={ result = self.client.execute(MODULE_QUERY, variables={
@ -184,7 +184,7 @@ class TestSnapshotMigration(SkillboxTestCase):
}) })
module = result.data['module'] module = result.data['module']
chapter1 = module['chapters'][0] chapter1 = module['chapters'][0]
_, _, _, new_content_block = chapter1['contentBlocks'] _, _, new_content_block, _ = chapter1['contentBlocks']
self.assertEqual(new_content_block['title'], 'Gesellschaft') self.assertEqual(new_content_block['title'], 'Gesellschaft')
self.assertTrue(new_content_block['userCreated']) self.assertTrue(new_content_block['userCreated'])

View File

@ -7,6 +7,5 @@ pg_dump -Fc --no-acl -h localhost -U skillbox skillbox > latest-migrated-objecti
python manage.py migrate_objective_snapshots python manage.py migrate_objective_snapshots
pg_dump -Fc --no-acl -h localhost -U skillbox skillbox > latest-migrated-objectives-and-snapshots.dump pg_dump -Fc --no-acl -h localhost -U skillbox skillbox > latest-migrated-objectives-and-snapshots.dump
#Use this command to restore the database from the dump: #Use this command to restore the database from the dump:
#pg_restore --verbose --clean --no-acl --no-owner -h localhost -U skillbox -d skillbox latest-migrated-objectives.dump #pg_restore --verbose --clean --no-acl --no-owner -h localhost -U skillbox -d skillbox latest-migrated-objectives.dump