Create Custom order for objective groups
This commit is contained in:
parent
ea1816e3cc
commit
16da66da31
|
|
@ -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,6 +23,7 @@ class Command(BaseCommand):
|
|||
|
||||
migrate_objectives_to_content()
|
||||
|
||||
|
||||
def migrate_objectives_to_content():
|
||||
created_content_blocks = 0
|
||||
|
||||
|
|
@ -34,7 +37,7 @@ def migrate_objectives_to_content():
|
|||
try:
|
||||
chapter = create_chapter_from_objective_group(module)
|
||||
|
||||
for objective_group in module.objective_groups.all().order_by('title'):
|
||||
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'))
|
||||
|
||||
|
|
@ -59,14 +62,16 @@ def migrate_objectives_to_content():
|
|||
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
|
||||
|
||||
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)]
|
||||
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)
|
||||
|
|
@ -127,6 +132,24 @@ def migrate_objectives_to_content():
|
|||
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):
|
||||
"""Create Verlagsinhalt Lernziele"""
|
||||
print(f" Objective group: {objective_group}")
|
||||
|
|
@ -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:
|
||||
|
|
@ -242,6 +266,7 @@ def create_text_in_content_block(objectives, content_block, get_or_create=False)
|
|||
content_block.save_revision().publish()
|
||||
return content_block
|
||||
|
||||
|
||||
def create_content_block_contents(objectives):
|
||||
objectives = list(objectives)
|
||||
objective_li = [f"<li>{objective.text}</li>" 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()}
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
|
|
|
|||
Loading…
Reference in New Issue