From 784f6d89732ab6d54c4e891c1f1f6e0f29839313 Mon Sep 17 00:00:00 2001 From: Lorenz Padberg Date: Tue, 12 Sep 2023 16:17:37 +0200 Subject: [PATCH] Reafctor for instrument and module models --- server/core/convert_page_translation.py | 38 +++++++++---------- ...slation.py => migrate_page_translation.py} | 30 +++++++++------ 2 files changed, 37 insertions(+), 31 deletions(-) rename server/core/management/commands/{migrate_translation.py => migrate_page_translation.py} (52%) diff --git a/server/core/convert_page_translation.py b/server/core/convert_page_translation.py index 11a2d75e..6981fbe8 100644 --- a/server/core/convert_page_translation.py +++ b/server/core/convert_page_translation.py @@ -13,7 +13,7 @@ class AlreadyTranslatedException(Exception): pass -class ModuleDoesNotExistException(Exception): +class PageDoesNotExistException(Exception): pass @@ -25,10 +25,8 @@ def translate_page(page: Page, locale: Locale): return translated_page -# todo: make this a django command -# todo: add language to translate to as an argument # 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 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 """ try: - module = Module.objects.get(slug=slug) - except Module.DoesNotExist: - raise ModuleDoesNotExistException(slug) + page = page_model.objects.get(slug=slug) + except page_model.DoesNotExist: + raise PageDoesNotExistException(slug) try: - original = Module.objects.get(slug=original_slug) - except Module.DoesNotExist: - raise ModuleDoesNotExistException(original_slug) - if original.translation_key == module.translation_key: + original = page_model.objects.get(slug=original_slug) + except page_model.DoesNotExist: + raise PageDoesNotExistException(original_slug) + if original.translation_key == page.translation_key: raise AlreadyTranslatedException locale, _ = Locale.objects.get_or_create(language_code=language_code) logger.debug("Starting conversion") - topic: Topic = module.get_parent() + topic: Topic = page.get_parent() try: logger.debug("Getting existing topic translation") 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) translated_topic = translate_page(topic, locale) - module.locale = locale + page.locale = locale if original is not None: translation_key = original.translation_key - module.translation_key = translation_key - module.save() - chapters = module.get_children() + page.translation_key = translation_key + page.save() + chapters = page.get_children() for chapter in chapters: chapter.locale = locale chapter.save() content_blocks = chapter.get_children() for content_block in content_blocks: content_block.locale = locale - content_block.save() + #content_block.save() - logger.debug(f"moving {module} to {translated_topic}") - module.move(translated_topic, "last-child") + logger.debug(f"moving {page} to {translated_topic}") + page.move(translated_topic, "last-child") return f"Converted page {slug} to be the {language_code} translation of {original_slug}" + + diff --git a/server/core/management/commands/migrate_translation.py b/server/core/management/commands/migrate_page_translation.py similarity index 52% rename from server/core/management/commands/migrate_translation.py rename to server/core/management/commands/migrate_page_translation.py index 13d21b1b..7de582dd 100644 --- a/server/core/management/commands/migrate_translation.py +++ b/server/core/management/commands/migrate_page_translation.py @@ -1,14 +1,10 @@ -import csv -import os -from django.core import management from django.core.management import BaseCommand, CommandError from basicknowledge.models import BasicKnowledge -from datetime import date - +from books.models import Module from core.convert_page_translation import ( AlreadyTranslatedException, - ModuleDoesNotExistException, + PageDoesNotExistException, convert_page_to_translation, ) @@ -25,19 +21,29 @@ class Command(BaseCommand): parser.add_argument("--slug", 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("--model", type=str, choices=["instrument", "module"], default="instrument") def handle(self, *_, **options): slug = options.get("slug") original_slug = options.get("original") 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: - result = convert_page_to_translation( - slug=slug, original_slug=original_slug, language_code=language_code - ) + result = convert_page_to_translation(page_model, + slug=slug, original_slug=original_slug, language_code=language_code + ) self.stdout.write(result) - except ModuleDoesNotExistException as e: - raise CommandError(f"Module does not exist: {e}") + except PageDoesNotExistException as e: + raise CommandError(f"Instrument does not exist: {e}") except AlreadyTranslatedException: 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" )