1010import re
1111
1212from attrs import frozen , Factory
13+ from django .core .files .base import ContentFile
1314from django .conf import settings
1415from django .contrib .auth import get_user_model
1516from django .utils .translation import gettext as _
2324from xmodule .exceptions import NotFoundError
2425from xmodule .modulestore .django import modulestore
2526from 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
2830from cms .djangoapps .models .settings .course_grading import CourseGradingModel
2931from 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
507517def _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
569581def _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
0 commit comments