Esempio n. 1
0
def _validate_transcripts_data(request):
    """
    Validates, that request contains all proper data for transcripts processing.

    Returns tuple of 3 elements::

        data: dict, loaded json from request,
        videos: parsed `data` to useful format,
        item:  video item from storage

    Raises `TranscriptsRequestValidationException` if validation is unsuccessful
    or `PermissionDenied` if user has no access.
    """
    data = json.loads(request.GET.get('data', '{}'))
    if not data:
        raise TranscriptsRequestValidationException(_('Incoming video data is empty.'))

    try:
        item = _get_item(request, data)
    except (ItemNotFoundError, InvalidLocationError, InsufficientSpecificationError):
        raise TranscriptsRequestValidationException(_("Can't find item by locator."))

    if item.category != 'video':
        raise TranscriptsRequestValidationException(_('Transcripts are supported only for "video" modules.'))

    # parse data form request.GET.['data']['video'] to useful format
    videos = {'youtube': '', 'html5': {}}
    for video_data in data.get('videos'):
        if video_data['type'] == 'youtube':
            videos['youtube'] = video_data['video']
        else:  # do not add same html5 videos
            if videos['html5'].get('video') != video_data['video']:
                videos['html5'][video_data['video']] = video_data['mode']

    return data, videos, item
Esempio n. 2
0
def _validate_location(location, course_key):
    try:
        location = UsageKey.from_string(location)
        item = modulestore().get_item(location)
    except (ItemNotFoundError, InvalidKeyError,
            InsufficientSpecificationError):
        raise TranscriptsRequestValidationException(
            _("Can't find item by locator."))

    if item.category != 'video':
        raise TranscriptsRequestValidationException(
            _('Transcripts are supported only for "video" modules.'))
    return item
Esempio n. 3
0
def _validate_captions_data_update(request, course_key):
    """
    Happens on 'POST'. Validates, that request contains all proper data for transcripts processing.

    Returns data: dict, loaded json from request

    Raises `TranscriptsRequestValidationException` if validation is unsuccessful
    or `PermissionDenied` if user has no access.
    """
    try:
        data = json.loads(request.POST.get('update_array', '[]'))
    except ValueError:
        raise TranscriptsRequestValidationException(_("Invalid locations."))

    if not data:
        raise TranscriptsRequestValidationException(
            _('Incoming update_array data is empty.'))

    for location in data:
        _validate_location(location, course_key)
    return data
Esempio n. 4
0
def _validate_captions_data_get(request, course_key):
    """
    Happens on 'GET'. Validates, that request contains all proper data for transcripts processing.

    Returns touple of two elements:
        data: dict, loaded json from request,
        item: video item from storage

    Raises `TranscriptsRequestValidationException` if validation is unsuccessful
    or `PermissionDenied` if user has no access.
    """
    try:
        data = json.loads(request.GET.get('video', '{}'))
    except ValueError:
        raise TranscriptsRequestValidationException(_("Invalid location."))

    if not data:
        raise TranscriptsRequestValidationException(
            _('Incoming video data is empty.'))

    location = data.get('location')
    item = _validate_location(location, course_key)
    return data, item