From c0fbacd6fdee517c97f6d32b8160af8c0ead4b29 Mon Sep 17 00:00:00 2001 From: wowkalucky Date: Fri, 7 Jul 2017 01:04:41 +0300 Subject: [PATCH] Refactor validation of 3PlayMedia options --- video_xblock/mixins.py | 20 +++++++++++++++++-- .../static/js/studio-edit/studio-edit.js | 11 ++++------ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/video_xblock/mixins.py b/video_xblock/mixins.py index 645b663e..5fc85011 100644 --- a/video_xblock/mixins.py +++ b/video_xblock/mixins.py @@ -220,6 +220,8 @@ def fetch_available_3pm_transcripts(self): def get_available_3pm_transcripts(self, file_id, apikey): """ Make API request to fetch list of available transcripts for given file ID. + + :return: (list of dicts OR dict) all available transcripts attached to file with ID OR error dict """ domain = self.THREE_PLAY_MEDIA_API_DOMAIN response = requests.get( @@ -410,14 +412,28 @@ def validate_three_play_media_config(self, request, _suffix=''): """ api_key = request.json.get('api_key') file_id = request.json.get('file_id') + streaming_enabled = request.json.get('streaming_enabled') + + # the very first request during xblock creating: + if api_key is None and file_id is None: + return Response(json={'isValid': True, 'message': _("Initialization")}) + # the case when no options provided, and streaming is disabled: + success_message = _('Success') + if not api_key and not file_id and not streaming_enabled: + return Response(json={'isValid': True, 'message': success_message}) + + # options partially provided or both empty, but streaming is enabled: + message = _('Check provided 3PlayMedia configuration') if not (api_key and file_id): - return Response(status=400, json={"message": _("'api_key' or 'file_id' is missing")}) + return Response(json={'isValid': False, 'message': message}) results = self.get_available_3pm_transcripts(file_id, api_key) is_valid = False if isinstance(results, dict) else True - message = _('Success') if is_valid else _('Check provided 3PlayMedia configuration') + if is_valid: + message = success_message + return Response(json={'isValid': is_valid, 'message': message}) diff --git a/video_xblock/static/js/studio-edit/studio-edit.js b/video_xblock/static/js/studio-edit/studio-edit.js index da749d1c..cabcc223 100644 --- a/video_xblock/static/js/studio-edit/studio-edit.js +++ b/video_xblock/static/js/studio-edit/studio-edit.js @@ -396,10 +396,6 @@ function StudioEditableXBlock(runtime, element) { data: JSON.stringify(data) }; - if (!(data.api_key && data.file_id)) { - return {isValid: true}; - } - return $.ajax( options ) @@ -425,10 +421,11 @@ function StudioEditableXBlock(runtime, element) { * @returns (object) 3PlayMedia's: apiKey + fileId */ function getThreePlayMediaConfig() { - var $apiKey = $('.threeplaymedia-api-key', element).val(); - var $fileId = $('#xb-field-edit-threeplaymedia_file_id', element).val(); + var apiKey = $('.threeplaymedia-api-key', element).val(); + var fileId = $('#xb-field-edit-threeplaymedia_file_id', element).val(); + var streamingEnabled = $('#xb-field-edit-threeplaymedia_streaming', element).prop('selectedIndex'); - return {api_key: $apiKey, file_id: $fileId}; + return {api_key: apiKey, file_id: fileId, streaming_enabled: !streamingEnabled}; } $('.save-button', element).bind('click', function(event) {