-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1528 from gustavomm19/syllabus-assets
Add commands to fix out dated assets slugs in syllabus and tasks
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
breathecode/admissions/management/commands/fix_assets_slug_in_syllabus_json.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") |
25 changes: 25 additions & 0 deletions
25
breathecode/assignments/management/commands/correct_tasks_alias_slug.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |