Reafctor for instrument and module models

This commit is contained in:
Lorenz Padberg 2023-09-12 16:17:37 +02:00
parent a0086a8010
commit 784f6d8973
2 changed files with 37 additions and 31 deletions

View File

@ -13,7 +13,7 @@ class AlreadyTranslatedException(Exception):
pass pass
class ModuleDoesNotExistException(Exception): class PageDoesNotExistException(Exception):
pass pass
@ -25,10 +25,8 @@ def translate_page(page: Page, locale: Locale):
return translated_page return translated_page
# todo: make this a django command
# todo: add language to translate to as an argument
# todo: write a test maybe # todo: write a test maybe
def convert_page_to_translation(slug: str, original_slug: str, language_code="en"): def convert_page_to_translation(page_model, slug: str, original_slug: str, language_code="en"):
""" """
Migrate a module and its children to a new locale and links it to Migrate a module and its children to a new locale and links it to
its translation in the original locale. its translation in the original locale.
@ -37,18 +35,18 @@ def convert_page_to_translation(slug: str, original_slug: str, language_code="en
@param original_slug: the slug of the module in the original locale @param original_slug: the slug of the module in the original locale
""" """
try: try:
module = Module.objects.get(slug=slug) page = page_model.objects.get(slug=slug)
except Module.DoesNotExist: except page_model.DoesNotExist:
raise ModuleDoesNotExistException(slug) raise PageDoesNotExistException(slug)
try: try:
original = Module.objects.get(slug=original_slug) original = page_model.objects.get(slug=original_slug)
except Module.DoesNotExist: except page_model.DoesNotExist:
raise ModuleDoesNotExistException(original_slug) raise PageDoesNotExistException(original_slug)
if original.translation_key == module.translation_key: if original.translation_key == page.translation_key:
raise AlreadyTranslatedException raise AlreadyTranslatedException
locale, _ = Locale.objects.get_or_create(language_code=language_code) locale, _ = Locale.objects.get_or_create(language_code=language_code)
logger.debug("Starting conversion") logger.debug("Starting conversion")
topic: Topic = module.get_parent() topic: Topic = page.get_parent()
try: try:
logger.debug("Getting existing topic translation") logger.debug("Getting existing topic translation")
translated_topic = topic.get_translation(locale) translated_topic = topic.get_translation(locale)
@ -61,21 +59,23 @@ def convert_page_to_translation(slug: str, original_slug: str, language_code="en
translate_page(topic.get_parent(), locale) translate_page(topic.get_parent(), locale)
translated_topic = translate_page(topic, locale) translated_topic = translate_page(topic, locale)
module.locale = locale page.locale = locale
if original is not None: if original is not None:
translation_key = original.translation_key translation_key = original.translation_key
module.translation_key = translation_key page.translation_key = translation_key
module.save() page.save()
chapters = module.get_children() chapters = page.get_children()
for chapter in chapters: for chapter in chapters:
chapter.locale = locale chapter.locale = locale
chapter.save() chapter.save()
content_blocks = chapter.get_children() content_blocks = chapter.get_children()
for content_block in content_blocks: for content_block in content_blocks:
content_block.locale = locale content_block.locale = locale
content_block.save() #content_block.save()
logger.debug(f"moving {module} to {translated_topic}") logger.debug(f"moving {page} to {translated_topic}")
module.move(translated_topic, "last-child") page.move(translated_topic, "last-child")
return f"Converted page {slug} to be the {language_code} translation of {original_slug}" return f"Converted page {slug} to be the {language_code} translation of {original_slug}"

View File

@ -1,14 +1,10 @@
import csv
import os
from django.core import management
from django.core.management import BaseCommand, CommandError from django.core.management import BaseCommand, CommandError
from basicknowledge.models import BasicKnowledge from basicknowledge.models import BasicKnowledge
from datetime import date from books.models import Module
from core.convert_page_translation import ( from core.convert_page_translation import (
AlreadyTranslatedException, AlreadyTranslatedException,
ModuleDoesNotExistException, PageDoesNotExistException,
convert_page_to_translation, convert_page_to_translation,
) )
@ -25,19 +21,29 @@ class Command(BaseCommand):
parser.add_argument("--slug", type=str, required=True) parser.add_argument("--slug", type=str, required=True)
parser.add_argument("--original", type=str, required=True) parser.add_argument("--original", type=str, required=True)
parser.add_argument("--language", type=str, choices=[EN, FR], default=EN) parser.add_argument("--language", type=str, choices=[EN, FR], default=EN)
parser.add_argument("--model", type=str, choices=["instrument", "module"], default="instrument")
def handle(self, *_, **options): def handle(self, *_, **options):
slug = options.get("slug") slug = options.get("slug")
original_slug = options.get("original") original_slug = options.get("original")
language_code = options.get("language") language_code = options.get("language")
model = options.get("model")
if model == "instrument":
page_model = BasicKnowledge
elif model == "module":
page_model = Module
else:
raise CommandError(f"Unknown model: {model}")
try: try:
result = convert_page_to_translation( result = convert_page_to_translation(page_model,
slug=slug, original_slug=original_slug, language_code=language_code slug=slug, original_slug=original_slug, language_code=language_code
) )
self.stdout.write(result) self.stdout.write(result)
except ModuleDoesNotExistException as e: except PageDoesNotExistException as e:
raise CommandError(f"Module does not exist: {e}") raise CommandError(f"Instrument does not exist: {e}")
except AlreadyTranslatedException: except AlreadyTranslatedException:
raise CommandError( raise CommandError(
f"Pages {slug} and {original_slug} already are translations of each other" f"Pages {model} {slug} and {original_slug} already are translations of each other"
) )