-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BD-27] FEAT: Add new API endpoint for uploading transcripts #27844
Merged
alangsto
merged 2 commits into
openedx:master
from
open-craft:josh/BB-4323-transcript-upload-api
Jun 8, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,14 @@ | |
update_transcript_credentials_state_for_org | ||
) | ||
from opaque_keys.edx.keys import CourseKey | ||
from rest_framework.decorators import api_view | ||
from rest_framework.response import Response | ||
|
||
from common.djangoapps.student.auth import has_studio_write_access | ||
from common.djangoapps.util.json_request import JsonResponse, expect_json | ||
from openedx.core.djangoapps.video_config.models import VideoTranscriptEnabledFlag | ||
from openedx.core.djangoapps.video_pipeline.api import update_3rd_party_transcription_service_credentials | ||
from openedx.core.lib.api.view_utils import view_auth_classes | ||
from xmodule.video_module.transcripts_utils import Transcript, TranscriptsGenerationException | ||
|
||
from .videos import TranscriptProvider | ||
|
@@ -33,7 +36,8 @@ | |
'transcript_credentials_handler', | ||
'transcript_download_handler', | ||
'transcript_upload_handler', | ||
'transcript_delete_handler' | ||
'transcript_delete_handler', | ||
'transcript_upload_api', | ||
] | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
@@ -173,6 +177,41 @@ def transcript_download_handler(request): | |
return response | ||
|
||
|
||
def upload_transcript(request): | ||
edx_video_id = request.POST['edx_video_id'] | ||
language_code = request.POST['language_code'] | ||
new_language_code = request.POST['new_language_code'] | ||
transcript_file = request.FILES['file'] | ||
try: | ||
# Convert SRT transcript into an SJSON format | ||
# and upload it to S3. | ||
sjson_subs = Transcript.convert( | ||
content=transcript_file.read().decode('utf-8'), | ||
input_format=Transcript.SRT, | ||
output_format=Transcript.SJSON | ||
).encode() | ||
create_or_update_video_transcript( | ||
video_id=edx_video_id, | ||
language_code=language_code, | ||
metadata={ | ||
'provider': TranscriptProvider.CUSTOM, | ||
'file_format': Transcript.SJSON, | ||
'language_code': new_language_code | ||
}, | ||
file_data=ContentFile(sjson_subs), | ||
) | ||
response = JsonResponse(status=201) | ||
except (TranscriptsGenerationException, UnicodeDecodeError): | ||
LOGGER.error("Unable to update transcript on edX video %s for language %s", edx_video_id, new_language_code) | ||
response = JsonResponse( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please log this error! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a log message here. Does the language look alright? |
||
{'error': _('There is a problem with this transcript file. Try to upload a different file.')}, | ||
status=400 | ||
) | ||
finally: | ||
LOGGER.info("Updated transcript on edX video %s for language %s", edx_video_id, new_language_code) | ||
return response | ||
|
||
|
||
def validate_transcript_upload_data(data, files): | ||
""" | ||
Validates video transcript file. | ||
|
@@ -202,6 +241,30 @@ def validate_transcript_upload_data(data, files): | |
return error | ||
|
||
|
||
@api_view(['POST']) | ||
@view_auth_classes() | ||
@expect_json | ||
def transcript_upload_api(request): | ||
""" | ||
API View for uploading transcript files. | ||
|
||
Arguments: | ||
request: A WSGI request object | ||
|
||
Transcript file in SRT format | ||
|
||
Returns: | ||
- A 400 if any validation fails | ||
- A 200 if the transcript has been uploaded successfully | ||
""" | ||
error = validate_transcript_upload_data(data=request.POST, files=request.FILES) | ||
if error: | ||
response = JsonResponse({'error': error}, status=400) | ||
else: | ||
response = upload_transcript(request) | ||
return response | ||
|
||
|
||
@login_required | ||
@require_POST | ||
def transcript_upload_handler(request): | ||
|
@@ -222,35 +285,7 @@ def transcript_upload_handler(request): | |
if error: | ||
response = JsonResponse({'error': error}, status=400) | ||
else: | ||
edx_video_id = request.POST['edx_video_id'] | ||
language_code = request.POST['language_code'] | ||
new_language_code = request.POST['new_language_code'] | ||
transcript_file = request.FILES['file'] | ||
try: | ||
# Convert SRT transcript into an SJSON format | ||
# and upload it to S3. | ||
sjson_subs = Transcript.convert( | ||
content=transcript_file.read().decode('utf-8'), | ||
input_format=Transcript.SRT, | ||
output_format=Transcript.SJSON | ||
).encode() | ||
create_or_update_video_transcript( | ||
video_id=edx_video_id, | ||
language_code=language_code, | ||
metadata={ | ||
'provider': TranscriptProvider.CUSTOM, | ||
'file_format': Transcript.SJSON, | ||
'language_code': new_language_code | ||
}, | ||
file_data=ContentFile(sjson_subs), | ||
) | ||
response = JsonResponse(status=201) | ||
except (TranscriptsGenerationException, UnicodeDecodeError): | ||
response = JsonResponse( | ||
{'error': _('There is a problem with this transcript file. Try to upload a different file.')}, | ||
status=400 | ||
) | ||
|
||
response = upload_transcript(request) | ||
return response | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we please add logging statement for the success case? It really helps monitoring
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a message here as well, also including the edX video ID and transcript language.