Skip to content

Commit c42e223

Browse files
committed
refactor: Adds new edx_video_id when copy to course
1 parent 95b51e8 commit c42e223

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

cms/djangoapps/contentstore/helpers.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import re
1111

1212
from attrs import frozen, Factory
13+
from django.core.files.base import ContentFile
1314
from django.conf import settings
1415
from django.contrib.auth import get_user_model
1516
from django.utils.translation import gettext as _
@@ -23,7 +24,8 @@
2324
from xmodule.exceptions import NotFoundError
2425
from xmodule.modulestore.django import modulestore
2526
from xmodule.xml_block import XmlMixin
26-
from xmodule.video_block.transcripts_utils import build_components_import_path
27+
from xmodule.video_block.transcripts_utils import Transcript, build_components_import_path
28+
from edxval.api import create_external_video, create_or_update_video_transcript
2729

2830
from cms.djangoapps.models.settings.course_grading import CourseGradingModel
2931
from cms.lib.xblock.upstream_sync import UpstreamLink, UpstreamLinkException, fetch_customizable_fields
@@ -300,13 +302,21 @@ def import_staged_content_from_user_clipboard(parent_key: UsageKey, request) ->
300302
tags=user_clipboard.content.tags,
301303
)
302304

305+
usage_key = new_xblock.scope_ids.usage_id
306+
if usage_key.block_type == 'video':
307+
# The edx_video_id must always be new so as not
308+
# to interfere with the data of the copied block
309+
new_xblock.edx_video_id = create_external_video(display_name='external video')
310+
store.update_item(new_xblock, request.user.id)
311+
303312
# Now handle static files that need to go into Files & Uploads.
304313
static_files = content_staging_api.get_staged_content_static_files(user_clipboard.content.id)
305314
notices, substitutions = _import_files_into_course(
315+
block=new_xblock,
306316
course_key=parent_key.context_key,
307317
staged_content_id=user_clipboard.content.id,
308318
static_files=static_files,
309-
usage_key=new_xblock.scope_ids.usage_id,
319+
usage_key=usage_key,
310320
)
311321

312322
# Rewrite the OLX's static asset references to point to the new
@@ -505,6 +515,7 @@ def _import_xml_node_to_parent(
505515

506516

507517
def _import_files_into_course(
518+
block: XBlock,
508519
course_key: CourseKey,
509520
staged_content_id: int,
510521
static_files: list[content_staging_api.StagedContentFileData],
@@ -541,6 +552,7 @@ def _import_files_into_course(
541552
# At this point, we know this is a "Files & Uploads" asset that we may need to copy into the course:
542553
try:
543554
result, substitution_for_file = _import_file_into_course(
555+
block,
544556
course_key,
545557
staged_content_id,
546558
file_data_obj,
@@ -567,6 +579,7 @@ def _import_files_into_course(
567579

568580

569581
def _import_file_into_course(
582+
block: XBlock,
570583
course_key: CourseKey,
571584
staged_content_id: int,
572585
file_data_obj: content_staging_api.StagedContentFileData,
@@ -617,6 +630,24 @@ def _import_file_into_course(
617630
if thumbnail_content is not None:
618631
content.thumbnail_location = thumbnail_location
619632
contentstore().save(content)
633+
if usage_key.block_type == 'video':
634+
# Adding transcripts to VAL using the nex edx_video_id
635+
language_code = next((k for k, v in block.transcripts.items() if v == filename), None)
636+
if language_code:
637+
sjson_subs = Transcript.convert(
638+
content=data,
639+
input_format=Transcript.SRT,
640+
output_format=Transcript.SJSON
641+
).encode()
642+
create_or_update_video_transcript(
643+
video_id=block.edx_video_id,
644+
language_code=language_code,
645+
metadata={
646+
'file_format': Transcript.SJSON,
647+
'language_code': language_code
648+
},
649+
file_data=ContentFile(sjson_subs),
650+
)
620651
return True, {clipboard_file_path: f"static/{import_path}"}
621652
elif current_file.content_digest == file_data_obj.md5_hash:
622653
# The file already exists and matches exactly, so no action is needed

xmodule/video_block/video_block.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,8 @@ def parse_xml_new_runtime(cls, node, runtime, keys):
732732
if key not in cls.fields: # lint-amnesty, pylint: disable=unsupported-membership-test
733733
continue # parse_video_xml returns some old non-fields like 'source'
734734
setattr(video_block, key, cls.fields[key].from_json(val)) # lint-amnesty, pylint: disable=unsubscriptable-object
735+
# Don't use VAL in the new runtime:
736+
video_block.edx_video_id = None
735737
return video_block
736738

737739
@classmethod

0 commit comments

Comments
 (0)