Skip to content

Commit 15327d6

Browse files
committed
chore: update references to video service method
1 parent 9b7b53f commit 15327d6

File tree

3 files changed

+221
-202
lines changed

3 files changed

+221
-202
lines changed

xblocks_contrib/video/transcripts_utils.py

Lines changed: 82 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from opaque_keys.edx.locator import LibraryLocatorV2
2323

2424
from xblocks_contrib.video.content import StaticContent
25-
from xblocks_contrib.video.exceptions import NotFoundError, TranscriptsGenerationException
25+
from xblocks_contrib.video.exceptions import NotFoundError, TranscriptNotFoundError, TranscriptsGenerationException
2626

2727

2828
try:
@@ -36,6 +36,24 @@
3636
NON_EXISTENT_TRANSCRIPT = 'non_existent_dummy_file_name'
3737

3838

39+
def get_transcript_from_store(video_block, location, subs_id, lang='en', filename=None):
40+
"""
41+
Get transcript from video config service.
42+
"""
43+
video_config_service = video_block.runtime.service(video_block, 'video_config')
44+
if not video_config_service:
45+
raise TranscriptNotFoundError("Video config service was not found")
46+
47+
# HACK Warning! this is temporary and will be removed once edx-val take over the
48+
# transcript module and contentstore will only function as fallback until all the
49+
# data is migrated to edx-val. It will be saving a contentstore hit for a hardcoded
50+
# dummy-non-existent-transcript name.
51+
if NON_EXISTENT_TRANSCRIPT in [subs_id, filename]:
52+
raise TranscriptNotFoundError
53+
asset_filename = subs_filename(subs_id, lang) if not filename else filename
54+
return video_config_service.get_transcript_from_store(location.course_key, asset_filename)
55+
56+
3957
class TranscriptException(Exception):
4058
pass
4159

@@ -281,12 +299,12 @@ def save_subs_to_store(video_block, subs, subs_id, item, language='en'):
281299
# return subs
282300

283301

284-
def remove_subs_from_store(subs_id, item, lang='en'):
285-
"""
286-
Remove from store, if transcripts content exists.
287-
"""
288-
filename = subs_filename(subs_id, lang)
289-
Transcript.delete_asset(item, item.location, filename)
302+
# def remove_subs_from_store(subs_id, item, lang='en'):
303+
# """
304+
# Remove from store, if transcripts content exists.
305+
# """
306+
# filename = subs_filename(subs_id, lang)
307+
# Transcript.delete_asset(item, item.location, filename)
290308

291309

292310
def generate_subs_from_source(video_block, speed_subs, subs_type, subs_filedata, block, language='en'):
@@ -494,7 +512,10 @@ def manage_video_subtitles_save(item, user, old_metadata=None, generate_translat
494512
for lang in old_langs.difference(new_langs): # 3a
495513
for video_id in possible_video_id_list:
496514
if video_id:
497-
remove_subs_from_store(video_id, item, lang)
515+
# remove_subs_from_store(video_id, item, lang)
516+
filename = subs_filename(video_id, lang)
517+
video_config_service = item.runtime.service(item, 'video_config')
518+
video_config_service.delete_transcript_from_store(item.location.course_key, filename)
498519

499520
reraised_message = ''
500521
if not isinstance(item.usage_key.context_key, LibraryLocatorV2):
@@ -539,9 +560,13 @@ def generate_sjson_for_all_speeds(block, user_filename, result_subs_dict, lang):
539560
"""
540561
_ = block.runtime.service(block, "i18n").gettext
541562

563+
video_config_service = block.runtime.service(block, 'video_config')
564+
if not video_config_service:
565+
raise TranscriptNotFoundError("Video config service was not found")
566+
542567
try:
543-
srt_transcripts = Transcript.find_transcript_from_store(block, block.location.course_key, user_filename)
544-
except NotFoundError as ex:
568+
srt_transcripts = video_config_service.find_transcript_from_store(block.location.course_key, user_filename)
569+
except TranscriptNotFoundError as ex:
545570
raise TranscriptException(_("{exception_message}: Can't find uploaded transcripts: {user_filename}").format( # lint-amnesty, pylint: disable=raise-missing-from
546571
exception_message=str(ex),
547572
user_filename=user_filename
@@ -579,10 +604,10 @@ def get_or_create_sjson(block, transcripts):
579604
user_subs_id = os.path.splitext(user_filename)[0]
580605
source_subs_id, result_subs_dict = user_subs_id, {1.0: user_subs_id}
581606
try:
582-
sjson_transcript = Transcript.asset(block, block.location, source_subs_id, block.transcript_language).data
583-
except NotFoundError: # generating sjson from srt
607+
sjson_transcript = get_transcript_from_store(block, block.location, source_subs_id, block.transcript_language).data
608+
except TranscriptNotFoundError: # generating sjson from srt
584609
generate_sjson_for_all_speeds(block, user_filename, result_subs_dict, block.transcript_language)
585-
sjson_transcript = Transcript.asset(block, block.location, source_subs_id, block.transcript_language).data
610+
sjson_transcript = get_transcript_from_store(block, block.location, source_subs_id, block.transcript_language).data
586611
return sjson_transcript
587612

588613

@@ -710,14 +735,6 @@ class Transcript:
710735
SJSON: 'application/json',
711736
}
712737

713-
@staticmethod
714-
def find_transcript_from_store(video_block, course_key, filename):
715-
"""
716-
Find transcript from store by course_key and filename.
717-
"""
718-
video_config_service = video_block.runtime.service(video_block, 'video_config')
719-
return video_config_service.find_transcript_from_store(course_key, filename)
720-
721738
@staticmethod
722739
def convert(content, input_format, output_format):
723740
"""
@@ -780,48 +797,48 @@ def convert(content, input_format, output_format):
780797
elif output_format == 'srt':
781798
return generate_srt_from_sjson(content_dict, speed=1.0)
782799

783-
@staticmethod
784-
def asset(video_block, location, subs_id, lang='en', filename=None):
785-
"""
786-
Get asset from contentstore, asset location is built from subs_id and lang.
787-
788-
`location` is block location.
789-
"""
790-
# HACK Warning! this is temporary and will be removed once edx-val take over the
791-
# transcript module and contentstore will only function as fallback until all the
792-
# data is migrated to edx-val. It will be saving a contentstore hit for a hardcoded
793-
# dummy-non-existent-transcript name.
794-
if NON_EXISTENT_TRANSCRIPT in [subs_id, filename]:
795-
raise NotFoundError
796-
797-
asset_filename = subs_filename(subs_id, lang) if not filename else filename
798-
return Transcript.get_asset(video_block, location, asset_filename)
799-
800-
@staticmethod
801-
def get_asset(video_block, location, filename):
802-
"""
803-
Return asset by location and filename.
804-
"""
805-
video_config_service = video_block.runtime.service(video_block, 'video_config')
806-
return video_config_service.get_transcript_from_store(location.course_key, filename)
807-
808-
@staticmethod
809-
def asset_location(location, filename):
810-
"""
811-
Return asset location. `location` is block location.
812-
"""
813-
# If user transcript filename is empty, raise `TranscriptException` to avoid `InvalidKeyError`.
814-
if not filename:
815-
raise TranscriptException("Transcript not uploaded yet")
816-
return StaticContent.compute_location(location.course_key, filename)
817-
818-
@staticmethod
819-
def delete_asset(video_block, location, filename):
820-
"""
821-
Delete asset by location and filename.
822-
"""
823-
video_config_service = video_block.runtime.service(video_block, 'video_config')
824-
video_config_service.delete_transcript_from_store(location.course_key, filename)
800+
# @staticmethod
801+
# def asset(video_block, location, subs_id, lang='en', filename=None):
802+
# """
803+
# Get asset from contentstore, asset location is built from subs_id and lang.
804+
805+
# `location` is block location.
806+
# """
807+
# # HACK Warning! this is temporary and will be removed once edx-val take over the
808+
# # transcript module and contentstore will only function as fallback until all the
809+
# # data is migrated to edx-val. It will be saving a contentstore hit for a hardcoded
810+
# # dummy-non-existent-transcript name.
811+
# if NON_EXISTENT_TRANSCRIPT in [subs_id, filename]:
812+
# raise NotFoundError
813+
814+
# asset_filename = subs_filename(subs_id, lang) if not filename else filename
815+
# return Transcript.get_asset(video_block, location, asset_filename)
816+
817+
# @staticmethod
818+
# def get_asset(video_block, location, filename):
819+
# """
820+
# Return asset by location and filename.
821+
# """
822+
# video_config_service = video_block.runtime.service(video_block, 'video_config')
823+
# return video_config_service.get_transcript_from_store(location.course_key, filename)
824+
825+
# @staticmethod
826+
# def asset_location(location, filename):
827+
# """
828+
# Return asset location. `location` is block location.
829+
# """
830+
# # If user transcript filename is empty, raise `TranscriptException` to avoid `InvalidKeyError`.
831+
# if not filename:
832+
# raise TranscriptException("Transcript not uploaded yet")
833+
# return StaticContent.compute_location(location.course_key, filename)
834+
835+
# @staticmethod
836+
# def delete_asset(video_block, location, filename):
837+
# """
838+
# Delete asset by location and filename.
839+
# """
840+
# video_config_service = video_block.runtime.service(video_block, 'video_config')
841+
# video_config_service.delete_transcript_from_store(location.course_key, filename)
825842

826843

827844
class VideoTranscriptsMixin:
@@ -993,11 +1010,11 @@ def get_transcript_for_video(video_block, video_location, subs_id, file_name, la
9931010
try:
9941011
if subs_id is None:
9951012
raise NotFoundError
996-
content = Transcript.asset(video_block, video_location, subs_id, language).data.decode('utf-8')
1013+
content = get_transcript_from_store(video_block, video_location, subs_id, language).data.decode('utf-8')
9971014
base_name = subs_id
9981015
input_format = Transcript.SJSON
9991016
except NotFoundError:
1000-
content = Transcript.asset(video_block, video_location, None, language, file_name).data.decode('utf-8')
1017+
content = get_transcript_from_store(video_block, video_location, None, language, file_name).data.decode('utf-8')
10011018
base_name = os.path.splitext(file_name)[0]
10021019
input_format = Transcript.SRT
10031020

xblocks_contrib/video/video.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
clean_video_id,
6565
get_endonym_or_label,
6666
get_html5_ids,
67+
get_transcript_from_store,
6768
subs_filename,
6869
manage_video_subtitles_save,
6970
)
@@ -748,8 +749,9 @@ def editor_saved(self, user, old_metadata, old_content): # lint-amnesty, pylint
748749
html5_ids = get_html5_ids(self.html5_sources)
749750
for subs_id in html5_ids:
750751
try:
751-
Transcript.asset(self, self.location, subs_id)
752-
except NotFoundError:
752+
get_transcript_from_store(self, self.location, subs_id)
753+
# Transcript.asset(self, self.location, subs_id)
754+
except TranscriptNotFoundError:
753755
# If a transcript does not not exist with particular html5_id then there is no need to check other
754756
# html5_ids because we have to create a new transcript with this missing html5_id by turning on
755757
# metadata_was_changed_by_user flag.

0 commit comments

Comments
 (0)