Skip to content

Commit

Permalink
Refactor validation of 3PlayMedia options
Browse files Browse the repository at this point in the history
  • Loading branch information
wowkalucky committed Jul 7, 2017
1 parent 9e8f9a5 commit c0fbacd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
20 changes: 18 additions & 2 deletions video_xblock/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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})


Expand Down
11 changes: 4 additions & 7 deletions video_xblock/static/js/studio-edit/studio-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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) {
Expand Down

0 comments on commit c0fbacd

Please sign in to comment.