Skip to content

Commit 35299e2

Browse files
committed
fix videos
1 parent 917ccb0 commit 35299e2

2 files changed

Lines changed: 53 additions & 6 deletions

File tree

src/ol_openedx_uai_content_customization/ol_openedx_uai_content_customization/management/commands/generate_uai_courses.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
clone_course_in_modulestore,
6666
create_content_block,
6767
delete_course_sections,
68+
save_video_block_with_edx_video_id,
6869
)
6970

7071
log = logging.getLogger(__name__)
@@ -119,8 +120,6 @@ def handle(self, *args, **options): # noqa: ARG002, PLR0915
119120
except User.DoesNotExist:
120121
msg = f"No user found with username {username!r}."
121122
raise CommandError(msg) # noqa: B904
122-
user_id = user.id
123-
124123
customized_rows, customized_fieldnames = parse_csv(customized_csv)
125124
video_asset_rows, video_asset_fieldnames = parse_csv(video_assets_csv)
126125

@@ -182,7 +181,12 @@ def handle(self, *args, **options): # noqa: ARG002, PLR0915
182181

183182
try:
184183
self._create_course(
185-
source_key, new_key, display_name, videos, video_id_map, user_id
184+
source_key,
185+
new_key,
186+
display_name,
187+
videos,
188+
video_id_map,
189+
user,
186190
)
187191
created += 1
188192
except DuplicateCourseError:
@@ -235,7 +239,7 @@ def _validate_source_course_keys(self, course_groups):
235239
raise CommandError(msg)
236240

237241
def _create_course( # noqa: PLR0913
238-
self, source_key, course_key_str, display_name, videos, video_id_map, user_id
242+
self, source_key, course_key_str, display_name, videos, video_id_map, user
239243
):
240244
"""
241245
Clone the source course, strip its sections, then populate with UAI content.
@@ -248,6 +252,7 @@ def _create_course( # noqa: PLR0913
248252
All modulestore writes are wrapped in bulk_operations for performance.
249253
"""
250254
parsed_key = CourseKey.from_string(course_key_str)
255+
user_id = user.id
251256
store = modulestore()
252257
with store.bulk_operations(parsed_key):
253258
course = clone_course_in_modulestore(
@@ -295,13 +300,13 @@ def _create_course( # noqa: PLR0913
295300
title,
296301
user_id,
297302
)
298-
create_content_block(
303+
video_block = create_content_block(
299304
unit,
300305
BLOCK_TYPE_VIDEO,
301306
title,
302307
user_id,
303-
edx_video_id=edx_video_id,
304308
)
309+
save_video_block_with_edx_video_id(video_block, user, edx_video_id)
305310

306311
store.publish(course.location, user_id)
307312
if unmapped:

src/ol_openedx_uai_content_customization/ol_openedx_uai_content_customization/modulestore_utils.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77

88
import logging
99

10+
from cms.djangoapps.contentstore.xblock_storage_handlers.view_handlers import (
11+
save_xblock_with_callback,
12+
)
1013
from xmodule.modulestore import ModuleStoreEnum
1114
from xmodule.modulestore.django import modulestore
15+
from xmodule.modulestore.inheritance import own_metadata
1216

1317
from ol_openedx_uai_content_customization.constants import (
1418
BLOCK_TYPE_CHAPTER,
@@ -134,3 +138,41 @@ def create_content_block(parent, block_type, display_name, user_id, **extra_fiel
134138
)
135139

136140
return block
141+
142+
143+
def save_video_block_with_edx_video_id(video_block, user, edx_video_id):
144+
"""
145+
Save a newly-created video block using the CMS callback pipeline.
146+
147+
This mirrors Studio's save flow and normalizes legacy fallback fields so
148+
an explicit ``edx_video_id`` is used as the canonical playback source.
149+
150+
Args:
151+
video_block: Video XBlock descriptor created in modulestore.
152+
user: Django user object performing the operation.
153+
edx_video_id: VAL/Open edX video identifier to bind to the block.
154+
155+
Returns:
156+
Updated video block descriptor returned by save callback utility.
157+
"""
158+
old_metadata = own_metadata(video_block)
159+
video_block.edx_video_id = edx_video_id.strip()
160+
161+
# Keep legacy fallback metadata empty to avoid default placeholder IDs.
162+
for field_name, value in (
163+
("youtube_id_0_75", ""),
164+
("youtube_id_1_0", ""),
165+
("youtube_id_1_25", ""),
166+
("youtube_id_1_5", ""),
167+
("sub", ""),
168+
("source", ""),
169+
("html5_sources", []),
170+
):
171+
if hasattr(video_block, field_name):
172+
setattr(video_block, field_name, value)
173+
174+
return save_xblock_with_callback(
175+
video_block,
176+
user,
177+
old_metadata=old_metadata,
178+
)

0 commit comments

Comments
 (0)