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
from logging import getLogger
from django.core.management import BaseCommand
from books.blocks import TextBlock
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 Chapter
from books.models import ContentBlock
from users.models import SchoolClass
from books.models import Module
logger = getLogger(__name__)
class Command(BaseCommand):
def handle(self, *args, **options):
ContentBlock.objects.filter(title__startswith="TESTOBJECTIVE").delete()
@ -22,8 +17,10 @@ class Command(BaseCommand):
createed_content_blocks = 0
for module in Module.objects.filter(title="Politische Streitfragen"):
print(f"Module: {module}")
failed_modules = []
for module in Module.objects.all():
try:
chapter = create_chapter_from_objective_group(module)
for objective_group in module.objective_groups.all():
@ -49,6 +46,7 @@ class Command(BaseCommand):
if merged_objectives_ids not in contentblocks_by_merged_objectives_ids:
for objective in merged_objectives:
print(f" Objective: {objective} {objective.owner}")
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
@ -56,6 +54,7 @@ class Command(BaseCommand):
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]
@ -63,8 +62,16 @@ class Command(BaseCommand):
# 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"Failed modules: {len(failed_modules)}")
for module in failed_modules:
print(f"Faile module: {module}")
def create_default_content(objective_group, chapter):
@ -73,13 +80,14 @@ def create_default_content(objective_group, chapter):
# 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)
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
@ -136,7 +144,7 @@ def create_content_block_from_objective(objective_group, chapter, owner=None):
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]
objective_li = [f"<li>{objective.text} -{objective.owner}- </li>" for objective in objectives if objective.text]
texts = [{'type': 'text_block',
'value': {