From 8caa0d86ca167bb3a9b62e5321264f3052f8f622 Mon Sep 17 00:00:00 2001 From: gustavomm19 Date: Wed, 8 Jan 2025 19:04:00 +0000 Subject: [PATCH] add commands to fix out dated assets slugs in syllabus and tasks --- .../fix_assets_slug_in_syllabus_json.py | 58 +++++++++++++++++++ .../commands/correct_tasks_alias_slug.py | 25 ++++++++ 2 files changed, 83 insertions(+) create mode 100644 breathecode/admissions/management/commands/fix_assets_slug_in_syllabus_json.py create mode 100644 breathecode/assignments/management/commands/correct_tasks_alias_slug.py diff --git a/breathecode/admissions/management/commands/fix_assets_slug_in_syllabus_json.py b/breathecode/admissions/management/commands/fix_assets_slug_in_syllabus_json.py new file mode 100644 index 000000000..ebecc6ca9 --- /dev/null +++ b/breathecode/admissions/management/commands/fix_assets_slug_in_syllabus_json.py @@ -0,0 +1,58 @@ +import json +from django.core.management.base import BaseCommand +from .models import SyllabusVersion +from breathecode.registry.models import Asset + + +class Command(BaseCommand): + help = "Replace asset aliases in the syllabus with the current slugs" + + def handle(self, *args, **options): + try: + from breathecode.certificate.actions import syllabus_weeks_to_days + + syllabus_list = SyllabusVersion.objects.all() + key_map = { + "QUIZ": "quizzes", + "LESSON": "lessons", + "EXERCISE": "replits", + "PROJECT": "assignments", + } + + for s in syllabus_list: + if isinstance(s.json, str): + s.json = json.loads(s.json) + + # in case the json contains "weeks" instead of "days" + s.json = syllabus_weeks_to_days(s.json) + + for module_index, day in enumerate(s.json["days"]): + + for asset_type in key_map: + if key_map[asset_type] not in day: + continue + + for asset_index, assignment in enumerate(day[key_map[asset_type]]): + assignment_slug = assignment["slug"] if isinstance(assignment, dict) else assignment + asset = Asset.get_by_slug(assignment_slug) + + if asset is not None and (asset.lang not in ["us", "en"] or asset.slug != assignment_slug): + updated_slug = assignment_slug + + if asset.lang not in ["us", "en"]: + english_translation = asset.all_translations.filter(lang__in=["en", "us"]).first() + updated_slug = english_translation.slug + elif asset.slug != assignment_slug: + updated_slug = asset.slug + + if isinstance(assignment, dict): + s.json["days"][module_index][key_map[asset_type]][asset_index][ + "slug" + ] = updated_slug + else: + s.json["days"][module_index][key_map[asset_type]][asset_index] = updated_slug + + s.save() + + except Exception: + self.stderr.write("Failed to fix assets slugs in all syllabus") diff --git a/breathecode/assignments/management/commands/correct_tasks_alias_slug.py b/breathecode/assignments/management/commands/correct_tasks_alias_slug.py new file mode 100644 index 000000000..5a21d5657 --- /dev/null +++ b/breathecode/assignments/management/commands/correct_tasks_alias_slug.py @@ -0,0 +1,25 @@ +from django.core.management.base import BaseCommand +from breathecode.registry.models import Asset +from ...models import Task + + +class Command(BaseCommand): + help = "Replace asset aliases with the current slugs" + + def handle(self, *args, **options): + + tasks = Task.objects.all() + for task in tasks: + associated_slug = task.associated_slug + asset = Asset.get_by_slug(associated_slug) + if asset is not None: + if asset.lang not in ["us", "en"]: + english_translation = asset.all_translations.filter(lang__in=["en", "us"]).first() + english_slug = english_translation.slug + task.associated_slug = english_slug + task.save() + + # if the slug si different than the stored associated_slug it means that it is an alias + elif asset.slug != associated_slug: + task.associated_slug = asset.slug + task.save()