Convert existing method to django command

Also rename it
This commit is contained in:
Ramon Wenger 2023-08-29 15:11:21 +02:00
parent 9837686319
commit 4011c016dc
2 changed files with 51 additions and 4 deletions

View File

@ -11,7 +11,7 @@ logger = get_logger(__name__)
# todo: make this a django command # todo: make this a django command
# todo: add language to translate to as an argument # todo: add language to translate to as an argument
# todo: write a test maybe # todo: write a test maybe
def migrate_i18n(slug: str, original_slug: str): def convert_page_to_translation(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.
@ -19,9 +19,13 @@ def migrate_i18n(slug: str, original_slug: str):
@param slug: the slug of the module to migrate @param slug: the slug of the module to migrate
@param original_slug: the slug of the module in the original locale @param original_slug: the slug of the module in the original locale
""" """
module = Module.objects.get(slug=slug) try:
original = Module.objects.get(slug=original_slug) module = Module.objects.get(slug=slug)
en = Locale.objects.get(language_code="en") original = Module.objects.get(slug=original_slug)
except Module.DoesNotExist:
logger.warning("Module does not exist")
return
en = Locale.objects.get(language_code=language_code)
logger.debug("starting this") logger.debug("starting this")
topic: Topic = module.get_parent() topic: Topic = module.get_parent()
try: try:

View File

@ -0,0 +1,43 @@
import csv
import os
from django.core import management
from django.core.management import BaseCommand
from basicknowledge.models import BasicKnowledge
from datetime import date
from core.convert_page_translation import convert_page_to_translation
EN = "en"
FR = "fr"
class Command(BaseCommand):
"""
usage: `python manage.py migrate_translation --slug slug --original original_slug --language 'en'`
"""
def add_arguments(self, parser):
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)
def handle(self, *_, **options):
slug = options.get("slug")
original_slug = options.get("original")
language_code = options.get("language")
self.stdout.write(f"args {slug} {original_slug} {language_code}")
convert_page_to_translation(
slug=slug, original_slug=original_slug, language_code=language_code
)
# with open(filename) as f:
# reader = csv.reader(f)
# for name, slugs in reader:
# self.stdout.write(name)
# self.stdout.write(slugs)
# cwd = os.getcwd()
# self.stdout.write(cwd)
# with open(f"{cwd}/exports/{name}.html", "w") as dest:
# management.call_command(
# "export_instruments", slugs=slugs, stdout=dest
# )