Fix bug in content creation

This commit is contained in:
Lorenz Padberg 2024-01-29 16:31:38 +01:00
parent 1908f11370
commit 9f16573b92
4 changed files with 176 additions and 60 deletions

View File

@ -6,8 +6,9 @@ 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
from books.models import Chapter, ObjectiveGroupSnapshot, ContentBlockSnapshot, Snapshot
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
@ -18,6 +19,8 @@ class Command(BaseCommand):
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()
@ -78,11 +81,16 @@ class Command(BaseCommand):
# 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
chapter = create_chapter_from_objective_group(module, prefix="SNAP ")
# 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,
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

View File

@ -2,72 +2,116 @@ import json
from logging import getLogger
from django.core.management import BaseCommand
from django.core.exceptions import ValidationError
from books.models import Chapter
from books.models import Chapter, ObjectiveGroupSnapshot, Snapshot, ContentBlockSnapshot, ChapterSnapshot
from books.models import ContentBlock
from books.models import Module
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()
ContentBlock.objects.filter(title__startswith="CUSTOM").delete()
Chapter.objects.filter(title__startswith="TESTOBJECTIVE").delete()
ContentBlock.objects.filter(title__startswith="XXX").delete()
Chapter.objects.filter(title__startswith="XXX").delete()
createed_content_blocks = 0
created_content_blocks = 0
failed_modules = []
for module in Module.objects.all():
# This dict stores all content blocks that have been created for a set of objectives
# In this way we can reuse content blocks for the same set of objectives
created_default_content_blocks = {}
for module in Module.objects.filter(title="Politik mitbestimmen"):
try:
chapter = create_chapter_from_objective_group(module)
for objective_group in module.objective_groups.all():
default_content_block = create_default_content(objective_group, chapter)
default_objectives = list(objective_group.objectives.filter(owner__isnull=True, )
.exclude(objectivesnapshot__isnull=False).order_by('order'))
default_objectives_ids = tuple(objective.id for objective in default_objectives)
# Create "Verlagsinhalte" content block if it does not exist yet
if default_objectives_ids not in created_default_content_blocks:
default_content_block = create_default_content(objective_group, chapter)
created_default_content_blocks[default_objectives_ids] = default_content_block
else:
default_content_block = created_default_content_blocks[default_objectives_ids]
# Create "Benutzerdefinierte Lernziele"
custom_objectives_by_owner = get_objectives_by_owner(objective_group)
for owner, owner_objectives in custom_objectives_by_owner.items():
print(f" Owner: {owner}")
print(f" Objectives: ")
default_objectives = list(objective_group.objectives.filter(owner__isnull=True))
visible_default_objectives_by_class = remove_hidden_objectives(default_objectives, owner)
if default_objectives or custom_objectives_by_owner:
contentblocks_by_merged_objectives_ids = {}
for school_class in visible_default_objectives_by_class.keys():
print(f" School class: {school_class}")
merged_objectives = visible_default_objectives_by_class[school_class] + owner_objectives
merged_objectives_ids = tuple(objective.id for objective in merged_objectives)
for owner, owner_objectives in custom_objectives_by_owner.items():
print(f"Owner: {owner}")
print(f" Objectives: ")
# Create content block if that set of objectives has not been created yet
if merged_objectives_ids not in contentblocks_by_merged_objectives_ids:
for objective in merged_objectives:
print(f" Objective: {objective} {objective.owner}")
visible_default_objectives_by_class = remove_hidden_objectives(default_objectives, owner)
for school_class in visible_default_objectives_by_class.keys():
custom_content_block = None
print(f" School class: {school_class}")
# merge "Verlagsinhalte" and "benutzerdefinierte Inhalte"
merged_objectives = visible_default_objectives_by_class[school_class] + owner_objectives
merged_objectives_ids = tuple(objective.id for objective in merged_objectives)
# Create content block if that set of objectives has not been created yet
if merged_objectives_ids not in contentblocks_by_merged_objectives_ids:
for objective in merged_objectives:
print(f" Objective: {objective} {objective.owner}")
if merged_objectives_ids:
custom_content_block = create_content_block_from_objective(objective_group, chapter,
owner=owner, prefix=f"CUSTOM {owner.username} ")
contentblocks_by_merged_objectives_ids[merged_objectives_ids] = custom_content_block
create_text_in_content_block(merged_objectives, custom_content_block)
created_content_blocks += 1
else:
print(f" Objective: Reuse content block")
custom_content_block = contentblocks_by_merged_objectives_ids[merged_objectives_ids]
# set visibility
# hide default objectives if custom objectives exist
current_block = contentblocks_by_merged_objectives_ids.get(merged_objectives_ids)
if current_block:
# default_content_block.hidden_for.add(school_class)
# default_content_block.save_revision().publish()
# default_content_block.save()
# created_default_content_blocks[default_objectives_ids] = default_content_block
pass
custom_content_block = create_content_block_from_objective(objective_group, chapter,
owner=owner)
contentblocks_by_merged_objectives_ids[merged_objectives_ids] = custom_content_block
create_text_in_content_block(merged_objectives, custom_content_block)
createed_content_blocks += 1
else:
print(f" Objective: Reuse content block")
pass
current_block.visible_for.add(school_class)
current_block.save_revision().publish()
current_block.save()
# set visibility
current_content_block = contentblocks_by_merged_objectives_ids[merged_objectives_ids]
current_content_block.visible_for.add(school_class)
# hide default objectives content if custom content exists
if custom_content_block:
default_content_block.hidden_for.add(school_class)
except Exception as e:
print(e)
if custom_content_block:
pass
# default_content_block.hidden_for.add(school_class)
# default_content_block.save()
#
# custom_content_block.visible_for.add(school_class)
# custom_content_block.save()
except ValidationError as e:
print(f"Error with module {module}")
logger.error(e)
failed_modules.append(module)
print(f"Created {createed_content_blocks} content blocks")
print(f"Created {created_content_blocks} content blocks")
print(f"Failed modules: {len(failed_modules)}")
for module in failed_modules:
@ -75,19 +119,17 @@ class Command(BaseCommand):
def create_default_content(objective_group, chapter):
"""Create Verlagsinhalt Lernziele"""
print(f" Objective group: {objective_group}")
print(" Default objectives:")
# create "Verlagsinhalt Lernziele"
default_objectives = list(objective_group.objectives.filter(owner__isnull=True, objectivesnapshot__isnull=True).order_by('id'))
default_objectives = objective_group.objectives.filter(owner__isnull=True).order_by('id')
default_content_block = create_content_block_from_objective(objective_group, chapter)
default_content_block = create_content_block_from_objective(objective_group, chapter, prefix="XXX YYY ")
create_text_in_content_block(default_objectives, default_content_block)
for objective in default_objectives:
print(f" Objective: {objective} {objective.owner}")
pass
return default_content_block
@ -103,8 +145,9 @@ def remove_hidden_objectives(objectives, user):
return visible_objectives
def get_objectives_by_owner(objective_group):
custom_objectives = objective_group.objectives.filter(owner__isnull=False)
def get_objectives_by_owner(objective_group, exclude_snapshots=True):
custom_objectives = objective_group.objectives.filter(owner__isnull=False).exclude(
objectivesnapshot__isnull=False).order_by('order')
custom_objectives_by_owner = {}
for objective in custom_objectives:
@ -115,11 +158,20 @@ def get_objectives_by_owner(objective_group):
custom_objectives_by_owner[owner].append(objective)
# add owners with hidden default objectives to custom objectives, needed for further processing
hidden_default_objectives = list(
objective_group.objectives.filter(owner__isnull=True, objectivesnapshot__isnull=True, hidden_for__isnull=False))
for hidden_default_objective in hidden_default_objectives:
for school_class in hidden_default_objective.hidden_for.all():
for teacher in school_class.get_teachers():
if teacher not in custom_objectives_by_owner:
custom_objectives_by_owner[teacher] = []
return custom_objectives_by_owner
def create_chapter_from_objective_group(module):
chapter = Chapter(title=f"XXX Lernziele")
def create_chapter_from_objective_group(module, prefix="XXX "):
chapter = Chapter(title=f"{prefix}Lernziele")
first_sibling = module.get_first_child()
if first_sibling is not None:
@ -130,26 +182,77 @@ def create_chapter_from_objective_group(module):
return chapter
def create_content_block_from_objective(objective_group, chapter, owner=None):
def create_chapter_snapshot_from_objective_group(module, snapshot, prefix="XXX "):
chapter = Chapter.objects.filter(parent=module, title=f"{prefix}Lernziele").first()
chapter_snapshot = ChapterSnapshot(title=f"{prefix}Lernziele", snapshot=snapshot)
first_sibling = module.get_first_child()
if first_sibling is not None:
first_sibling.add_sibling(instance=chapter_snapshot, pos='left')
return chapter
def create_content_block_from_objective(objective_group, chapter, owner=None, prefix="XXX "):
content_block = ContentBlock(
title=f"XXX {objective_group.get_title_display()}",
title=f"{prefix}{objective_group.get_title_display()}",
type="normal",
owner=owner,
user_created=owner is not None,
original_creator=owner
)
chapter.add_child(instance=content_block)
return content_block
def create_content_block_snapshot_from_objective(objective_group, chapter, snapshot, owner=None, prefix="XXX "):
content_block_snapshot = ContentBlockSnapshot(
title=f"{prefix}{objective_group.get_title_display()}",
type="normal",
owner=owner,
user_created=owner is not None,
snapshot=snapshot
)
chapter.add_child(instance=content_block_snapshot)
return content_block_snapshot
def create_text_in_content_block(objectives, content_block):
print(content_block.contents)
objectives = list(objectives)
objective_texts = set([objective.text for objective in objectives])
objective_li = [f"<li>{objective.text} -{objective.owner}- </li>" for objective in objectives if objective.text]
texts = [{'type': 'text_block',
'value': {
'text': f"<ul> {''.join(str(i) for i in objective_li)} </ul>"
}}]
content_block.contents = json.dumps(texts)
content_block.save_revision().publish()
return content_block
def analyze():
print(f"""
OjectiveGroups: {ObjectiveGroup.objects.count()}
Objectives: {Objective.objects.count()}
ObjectiveGroupSnapshots: {ObjectiveGroupSnapshot.objects.count()}
ObjectivesSnapshots: {ObjectiveSnapshot.objects.filter(hidden=True).count()}
ObjectiveGroups: {ObjectiveGroup.objects.filter(objectivegroupsnapshot__isnull=True).count()}
Objectives: {Objective.objects.filter(objectivesnapshot__isnull=True).count()}
Snapshot: {Snapshot.objects.count()}
""")
def clean_snapshots():
emails = ["mia.teacher", "simone.gerber", "pascal.sigg", "dario.aebersold", "steph-teacher"]
for snapshot in Snapshot.objects.all():
for email in emails:
if email in snapshot.creator.email:
print(f"Deleting snapshot {email}")
snapshot.delete()

View File

@ -54,9 +54,11 @@ class SnapshotManager(models.Manager):
description_hidden=chapter.description_hidden_for.filter(id=school_class.id).exists()
)
base_qs = ContentBlock.get_by_parent(chapter).filter(contentblocksnapshot__isnull=True)
# Verlagsinhalte
for content_block in base_qs.filter(user_created=False):
if content_block.hidden_for.filter(id=school_class.id).exists():
snapshot.hidden_content_blocks.add(content_block)
# Benutzerdefinierte Inhalte
for content_block in base_qs.filter(Q(user_created=True) & Q(owner=user)):
new_content_block = ContentBlockSnapshot(
hidden=content_block.is_hidden_for_class(school_class),
@ -78,9 +80,11 @@ class SnapshotManager(models.Manager):
hidden=objective_group.hidden_for.filter(id=school_class.id).exists(),
)
base_qs = objective_group.objectives.filter(objectivesnapshot__isnull=True)
# Verlagslernziele
for objective in base_qs.filter(owner__isnull=True):
if objective.hidden_for.filter(id=school_class.id).exists():
snapshot.hidden_objectives.add(objective)
# Benutzerdefinierte Lernziele
for objective in base_qs.filter(owner=user):
ObjectiveSnapshot.objects.create(
hidden=objective.is_hidden_for_class(school_class=school_class),
@ -152,10 +156,11 @@ class Snapshot(models.Model):
chapter.title_hidden_for.add(selected_class)
if chapter_snapshot.description_hidden:
chapter.description_hidden_for.add(selected_class)
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)
for objective in self.hidden_objectives.all():
objective.hidden_for.add(selected_class)
for custom_objective in self.custom_objectives.all():
custom_objective.to_regular_objective(owner=user, school_class=selected_class)
# 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)
# for objective in self.hidden_objectives.all():
# objective.hidden_for.add(selected_class)
# for custom_objective in self.custom_objectives.all():
# custom_objective.to_regular_objective(owner=user, school_class=selected_class)

View File