Skip to content

Commit a2bc6c7

Browse files
authored
Fix bad urls for edx video contentfiles (#2444)
1 parent 2b693ae commit a2bc6c7

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Generated manually
2+
3+
4+
from django.db import migrations
5+
from django.db.models import Value
6+
from django.db.models.functions import Replace
7+
8+
from learning_resources.utils import content_files_loaded_actions
9+
10+
11+
def update_contentfile_urls(apps, schema_editor):
12+
"""
13+
Update incorrect ContentFile URLs that use the "/jump_to/" format
14+
"""
15+
16+
ContentFile = apps.get_model("learning_resources", "ContentFile")
17+
LearningResourceRun = apps.get_model("learning_resources", "LearningResourceRun")
18+
content_files = ContentFile.objects.filter(url__contains="/jump_to/").only(
19+
"id", "url", "run__id"
20+
)
21+
# Get unique run IDs before updating
22+
run_ids = set(content_files.values_list("run__id", flat=True).distinct())
23+
24+
# Update all ContentFile URLs in a single database query
25+
content_files.update(url=Replace("url", Value("/jump_to/"), Value("/jump_to_id/")))
26+
27+
# Process the runs
28+
for run in LearningResourceRun.objects.filter(id__in=run_ids):
29+
content_files_loaded_actions(run)
30+
31+
32+
class Migration(migrations.Migration):
33+
dependencies = [
34+
(
35+
"data_fixtures",
36+
"0016_add_canvas_platform",
37+
),
38+
]
39+
40+
operations = [
41+
migrations.RunPython(
42+
update_contentfile_urls, reverse_code=migrations.RunPython.noop
43+
),
44+
]

learning_resources/etl/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,14 +463,15 @@ def get_url_from_module_id(
463463
if run.learning_resource.etl_source == ETLSource.oll.value
464464
else run.run_id
465465
)
466+
base_jump_url = f"{root_url}/courses/{run_id}/jump_to_id/"
466467
if module_id.startswith("asset"):
467468
video_meta = video_srt_metadata.get(module_id, {}) if video_srt_metadata else {}
468469
if video_meta:
469470
# Link to the parent video
470-
return f"{root_url}/courses/{run_id}/jump_to/{video_meta.split('@')[-1]}"
471+
return f"{base_jump_url}{video_meta.split('@')[-1]}"
471472
return f"{root_url}/{module_id}"
472473
elif module_id.startswith("block") and is_valid_uuid(module_id.split("@")[-1]):
473-
return f"{root_url}/courses/{run_id}/jump_to_id/{module_id.split('@')[-1]}"
474+
return f"{base_jump_url}{module_id.split('@')[-1]}"
474475
else:
475476
return None
476477

learning_resources/etl/utils_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ def test_get_video_metadata(mocker, tmp_path, video_dir_exists):
715715
"mit_edx",
716716
"asset-v1:test+type@[email protected]",
717717
True,
718-
"https://edx.org/courses/course-v1:test_run/jump_to/test_video",
718+
"https://edx.org/courses/course-v1:test_run/jump_to_id/test_video",
719719
),
720720
(
721721
"mit_edx",

0 commit comments

Comments
 (0)