117 lines
5.0 KiB
Python
117 lines
5.0 KiB
Python
|
|
|
|
import json
|
|
from logging import getLogger
|
|
|
|
from django.core.management import BaseCommand
|
|
|
|
from books.management.commands.migrate_objectives_to_content import create_chapter_from_objective_group, \
|
|
create_content_block_from_objective, create_text_in_content_block, create_content_block_snapshot_from_objective, \
|
|
create_chapter_snapshot_from_objective_group
|
|
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):
|
|
prefix = "SNAP "
|
|
ContentBlock.objects.filter(title__startswith=prefix).delete()
|
|
Chapter.objects.filter(title__startswith=prefix).delete()
|
|
ContentBlockSnapshot.objects.filter(title__startswith=prefix).delete()
|
|
ChapterSnapshot.objects.filter(chapter__title__startswith=prefix).delete()
|
|
|
|
|
|
|
|
analyze()
|
|
|
|
createed_content_blocks = 0
|
|
|
|
failed_modules = []
|
|
|
|
visible_objectives_by_ids = {}
|
|
|
|
for module in Module.objects.filter():
|
|
# try:
|
|
|
|
snapshots = Snapshot.objects.filter(module=module)
|
|
module_snapshot_by_id = {}
|
|
#chapter = create_chapter_from_objective_group(module)
|
|
|
|
|
|
for snapshot in snapshots:
|
|
# hier objective snapshots entfernt werden. die werden nicht mehr gebraucht
|
|
# for objective_group_snapshot in self.objective_groups.through.objects.all():
|
|
# if objective_group_snapshot.hidden:
|
|
# objective_group_snapshot.objective_group.hidden_for.add(selected_class)
|
|
default_objectives = Objective.objects.filter(group__module=module, owner__isnull=True)
|
|
visible_objectives = list(default_objectives)
|
|
visible_objectives_ids = [objective.id for objective in visible_objectives]
|
|
|
|
for hidden_objective in snapshot.hidden_objectives.all():
|
|
visible_objectives = [objective for objective in visible_objectives if hidden_objective.id not in visible_objectives_ids]
|
|
for custom_objective_snapshot in snapshot.custom_objectives.all():
|
|
visible_objectives.append(custom_objective_snapshot)
|
|
|
|
# filter for unique texts in objectives
|
|
# TODO: I don't know why this is necessary
|
|
objectives_by_texts = {}
|
|
for objective in visible_objectives:
|
|
if objective.text not in objectives_by_texts:
|
|
objectives_by_texts[objective.text] = objective
|
|
visible_objectives = list(objectives_by_texts.values())
|
|
|
|
if visible_objectives:
|
|
# make comvinations of objectives unique, this prevents generatino of many duplicated content blocks
|
|
visible_objectives_hash = hash([objective.text for objective in visible_objectives].__str__())
|
|
|
|
visible_objectives_by_ids[visible_objectives_hash] = visible_objectives
|
|
|
|
|
|
for objectives in visible_objectives_by_ids.values():
|
|
print("")
|
|
for objective in objectives:
|
|
print(f" Objective: {objective.group} {objective} {objective.owner}")
|
|
print("-")
|
|
|
|
print(f" visible_objectives_by_ids: {len(visible_objectives_by_ids.items())}")
|
|
|
|
|
|
# create custom content blocks with the objectives
|
|
createed_content_blocks = 0
|
|
for objectives in visible_objectives_by_ids.values():
|
|
snapshot = objectives[0].group.module.snapshots.first()
|
|
module = objectives[0].group.module
|
|
# does that work?
|
|
chapter = module.get_first_child()
|
|
if "Lernziele" not in chapter.title:
|
|
raise Exception("Chapter does not contain 'Lernziele'")
|
|
#chapter = create_chapter_snapshot_from_objective_group(module, snapshot, prefix="SNAP ")
|
|
|
|
# Owner des custom blocks festlegen
|
|
custom_content_block = create_content_block_snapshot_from_objective(objectives[0].group, chapter, snapshot,
|
|
owner=None, prefix="SNAP ")
|
|
create_text_in_content_block(objectives, custom_content_block)
|
|
createed_content_blocks += 1
|
|
|
|
print(f"created_content_blocks: {createed_content_blocks}")
|
|
|
|
|
|
def analyze():
|
|
print(f"""
|
|
OjectiveGroups: {ObjectiveGroup.objects.count()}
|
|
Objectives: {Objective.objects.count()}
|
|
|
|
ObjectiveGroupSnapshots: {ObjectiveGroupSnapshot.objects.count()}
|
|
ObjectivesSnapshots: {ObjectiveSnapshot.objects.count()}
|
|
|
|
|
|
ObjectiveGroups: {ObjectiveGroup.objects.filter(objectivegroupsnapshot__isnull=True).count()}
|
|
Objectives: {Objective.objects.filter(objectivesnapshot__isnull=True).count()}
|
|
|
|
Snapshot: {Snapshot.objects.count()}
|
|
""")
|
|
|
|
|