Add User created content

This commit is contained in:
Lorenz Padberg 2023-12-18 15:34:56 +01:00
parent 48d3c34fbe
commit 568564a64c
1 changed files with 51 additions and 19 deletions

View File

@ -8,8 +8,9 @@ from books.categorize_modules import categorize_modules, delete_unused_levels, u
from books.categorize_modules import create_default_levels_and_categories
from books.models import Module
from books.models import ContentBlock, Chapter
from django.db.models import Avg, Count, Min, Sum
from books.models import ContentBlock
from users.models import SchoolClass
class Command(BaseCommand):
@ -26,28 +27,56 @@ class Command(BaseCommand):
for objective_group in module.objective_groups.all():
print(f" Objective group: {objective_group}")
print(" Default objectives:")
content_block = create_content_block_from_objective(objective_group, chapter)
# create "Verlagsinhalt Lernziele"
default_objectives = objective_group.objectives.filter(owner__isnull=True)
content_block = create_text_in_content_block(default_objectives, content_block)
default_content_block = create_content_block_from_objective(objective_group, chapter)
create_text_in_content_block(default_objectives, default_content_block)
for objective in default_objectives:
print(f" Objective: {objective} {objective.owner}")
# Create "Benutzerdefinierte Lernziele"
custom_objectives = objective_group.objectives.filter(owner__isnull=False)
create_text_in_content_block(custom_objectives, content_block)
print(" Custom objectives:")
custom_objectives_by_owner = {}
for objective in custom_objectives:
owner = objective.owner
owners_school_classes = owner.school_classes.all()
if owner not in custom_objectives_by_owner:
custom_objectives_by_owner[owner] = []
custom_objectives_by_owner[owner].append(objective)
for owner, owner_objectives in custom_objectives_by_owner.items():
print(f" Owner: {owner}")
print(f" Objectives: ")
default_objectives = objective_group.objectives.filter(owner__isnull=True)
owner_objectives.extend(default_objectives)
for objective in owner_objectives:
print(f" Objective: {objective} {objective.owner}")
#
custom_content_block = create_content_block_from_objective(objective_group, chapter, owner=owner)
create_text_in_content_block(owner_objectives, custom_content_block)
print("")
owners_school_classes = owner.school_classes.all()
for school_class in owners_school_classes:
custom_content_block.visible_for.add(school_class.id)
if objective.is_hidden_for_class(school_class):
custom_content_block.hidden_for.add(school_class.id)
custom_content_block.save_revision().publish()
def create_chapter_from_objective_group(module):
chapter = Chapter(title=f"XXX Leistungsziele")
#
chapter = Chapter(title=f"XXX Lernziele")
first_sibling = module.get_first_child()
if first_sibling is not None:
first_sibling.add_sibling(instance=chapter, pos='left')
@ -56,24 +85,27 @@ def create_chapter_from_objective_group(module):
chapter.save()
return chapter
def create_content_block_from_objective(objective_group, chapter):
def create_content_block_from_objective(objective_group, chapter, owner=None):
content_block = ContentBlock(
title=f"XXX {objective_group.title}",
type="normal"
title=f"XXX {objective_group.get_title_display()}",
type="normal",
owner=owner,
user_created=owner is not None,
)
chapter.add_child(instance=content_block)
chapter.add_child(instance=content_block)
return content_block
def create_text_in_content_block( objectives, content_block):
def create_text_in_content_block(objectives, content_block):
print(content_block.contents)
objective_li = [f"<li>---{objective.owner}--- {objective.text}</li>" for objective in objectives if objective.text]
texts = [{'type': 'text_block',
'value': {
'text': f"- {objective.text}"
}} for objective in objectives]
'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