Add Error Handling

This commit is contained in:
Lorenz Padberg 2023-12-19 10:25:05 +01:00
parent 6069c47c5e
commit c88447ebb6
1 changed files with 53 additions and 45 deletions

View File

@ -1,18 +1,13 @@
import json import json
from logging import getLogger
from django.core.management import BaseCommand from django.core.management import BaseCommand
from books.blocks import TextBlock from books.models import Chapter
from books.categorize_modules import categorize_modules, delete_unused_levels, uncategorize_modules, \
delete_unused_categories
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 books.models import ContentBlock
from users.models import SchoolClass from books.models import Module
logger = getLogger(__name__)
class Command(BaseCommand): class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
ContentBlock.objects.filter(title__startswith="TESTOBJECTIVE").delete() ContentBlock.objects.filter(title__startswith="TESTOBJECTIVE").delete()
@ -22,49 +17,61 @@ class Command(BaseCommand):
createed_content_blocks = 0 createed_content_blocks = 0
for module in Module.objects.filter(title="Politische Streitfragen"): failed_modules = []
print(f"Module: {module}")
chapter = create_chapter_from_objective_group(module)
for objective_group in module.objective_groups.all(): for module in Module.objects.all():
default_content_block = create_default_content(objective_group, chapter) try:
chapter = create_chapter_from_objective_group(module)
# Create "Benutzerdefinierte Lernziele" for objective_group in module.objective_groups.all():
custom_objectives_by_owner = get_objectives_by_owner(objective_group) default_content_block = create_default_content(objective_group, chapter)
for owner, owner_objectives in custom_objectives_by_owner.items(): # Create "Benutzerdefinierte Lernziele"
print(f" Owner: {owner}") custom_objectives_by_owner = get_objectives_by_owner(objective_group)
print(f" Objectives: ")
default_objectives = list(objective_group.objectives.filter(owner__isnull=True)) for owner, owner_objectives in custom_objectives_by_owner.items():
visible_default_objectives_by_class = remove_hidden_objectives(default_objectives, owner) print(f" Owner: {owner}")
contentblocks_by_merged_objectives_ids = {} print(f" Objectives: ")
for school_class in visible_default_objectives_by_class.keys(): default_objectives = list(objective_group.objectives.filter(owner__isnull=True))
print(f" School class: {school_class}") visible_default_objectives_by_class = remove_hidden_objectives(default_objectives, owner)
merged_objectives = visible_default_objectives_by_class[school_class] + owner_objectives contentblocks_by_merged_objectives_ids = {}
merged_objectives_ids = tuple(objective.id for objective in merged_objectives)
# Create content block if that set of objectives has not been created yet for school_class in visible_default_objectives_by_class.keys():
if merged_objectives_ids not in contentblocks_by_merged_objectives_ids: print(f" School class: {school_class}")
for objective in merged_objectives: merged_objectives = visible_default_objectives_by_class[school_class] + owner_objectives
print(f" Objective: {objective} {objective.owner}") merged_objectives_ids = tuple(objective.id for objective in merged_objectives)
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")
# set visibility # Create content block if that set of objectives has not been created yet
current_content_block = contentblocks_by_merged_objectives_ids[merged_objectives_ids] if merged_objectives_ids not in contentblocks_by_merged_objectives_ids:
current_content_block.visible_for.add(school_class) for objective in merged_objectives:
# hide default objectives content if custom content exists print(f" Objective: {objective} {objective.owner}")
if custom_content_block: pass
default_content_block.hidden_for.add(school_class) 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
# 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)
print(f"Error with module {module}")
logger.error(e)
failed_modules.append(module)
print(f"Created {createed_content_blocks} content blocks") print(f"Created {createed_content_blocks} content blocks")
print(f"Failed modules: {len(failed_modules)}")
for module in failed_modules:
print(f"Faile module: {module}")
def create_default_content(objective_group, chapter): def create_default_content(objective_group, chapter):
@ -73,13 +80,14 @@ def create_default_content(objective_group, chapter):
# create "Verlagsinhalt Lernziele" # create "Verlagsinhalt Lernziele"
default_objectives = objective_group.objectives.filter(owner__isnull=True) 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)
create_text_in_content_block(default_objectives, default_content_block) create_text_in_content_block(default_objectives, default_content_block)
for objective in default_objectives: for objective in default_objectives:
print(f" Objective: {objective} {objective.owner}") print(f" Objective: {objective} {objective.owner}")
pass
return default_content_block return default_content_block
@ -136,7 +144,7 @@ def create_content_block_from_objective(objective_group, chapter, owner=None):
def create_text_in_content_block(objectives, content_block): def create_text_in_content_block(objectives, content_block):
print(content_block.contents) print(content_block.contents)
objective_li = [f"<li>---{objective.owner}--- {objective.text}</li>" for objective in objectives if objective.text] objective_li = [f"<li>{objective.text} -{objective.owner}- </li>" for objective in objectives if objective.text]
texts = [{'type': 'text_block', texts = [{'type': 'text_block',
'value': { 'value': {