示例#1
0
    def test_translation_delete_w_edx_video_id(self):
        """
        Verify that DELETE dispatch works as expected when video has edx_video_id
        """
        request_body = json.dumps({'lang': self.LANGUAGE_CODE_UK, 'edx_video_id': self.EDX_VIDEO_ID})
        api.create_video({
            'edx_video_id': self.EDX_VIDEO_ID,
            'status': 'upload',
            'client_video_id': 'awesome.mp4',
            'duration': 0,
            'encoded_videos': [],
            'courses': [unicode(self.course.id)]
        })
        api.create_video_transcript(
            video_id=self.EDX_VIDEO_ID,
            language_code=self.LANGUAGE_CODE_UK,
            file_format='srt',
            content=ContentFile(SRT_content)
        )

        # verify that a video transcript exists for expected data
        self.assertTrue(api.get_video_transcript_data(video_id=self.EDX_VIDEO_ID, language_code=self.LANGUAGE_CODE_UK))

        request = Request(self.REQUEST_META, body=request_body)
        self.item_descriptor.edx_video_id = self.EDX_VIDEO_ID
        response = self.item_descriptor.studio_transcript(request=request, dispatch='translation')
        self.assertEqual(response.status_code, 200)

        # verify that a video transcript dose not exist for expected data
        self.assertFalse(api.get_video_transcript_data(video_id=self.EDX_VIDEO_ID, language_code=self.LANGUAGE_CODE_UK))
    def test_transcript_delete_handler(self, is_staff, is_course_staff):
        """
        Tests that transcript delete handler works as expected with combinations of staff and course's staff.
        """
        # Setup user's roles
        self.user.is_staff = is_staff
        self.user.save()
        course_staff_role = CourseStaffRole(self.course.id)
        if is_course_staff:
            course_staff_role.add_users(self.user)
        else:
            course_staff_role.remove_users(self.user)

        # Assert the user role
        self.assertEqual(self.user.is_staff, is_staff)
        self.assertEqual(CourseStaffRole(self.course.id).has_user(self.user), is_course_staff)

        video_id, language_code = u'1234', u'en'
        # Create a real transcript in VAL.
        api.create_or_update_video_transcript(
            video_id=video_id,
            language_code=language_code,
            metadata={'file_format': 'srt'}
        )

        # Make request to transcript deletion handler
        response = self.client.delete(self.get_url_for_course_key(
            self.course.id,
            edx_video_id=video_id,
            language_code=language_code
        ))
        self.assertEqual(response.status_code, 200)
        self.assertFalse(api.get_video_transcript_data(video_id=video_id, language_code=language_code))
示例#3
0
    def test_transcript_delete_handler(self, is_staff, is_course_staff):
        """
        Tests that transcript delete handler works as expected with combinations of staff and course's staff.
        """
        # Setup user's roles
        self.user.is_staff = is_staff
        self.user.save()
        course_staff_role = CourseStaffRole(self.course.id)
        if is_course_staff:
            course_staff_role.add_users(self.user)
        else:
            course_staff_role.remove_users(self.user)

        # Assert the user role
        self.assertEqual(self.user.is_staff, is_staff)
        self.assertEqual(
            CourseStaffRole(self.course.id).has_user(self.user),
            is_course_staff)

        video_id, language_code = u'1234', u'en'
        # Create a real transcript in VAL.
        api.create_or_update_video_transcript(video_id=video_id,
                                              language_code=language_code,
                                              metadata={'file_format': 'srt'})

        # Make request to transcript deletion handler
        response = self.client.delete(
            self.get_url_for_course_key(self.course.id,
                                        edx_video_id=video_id,
                                        language_code=language_code))
        self.assertEqual(response.status_code, 200)
        self.assertFalse(
            api.get_video_transcript_data(video_id=video_id,
                                          language_code=language_code))
示例#4
0
def get_video_transcript_content(edx_video_id, language_code):
    """
    Gets video transcript content, only if the corresponding feature flag is enabled for the given `course_id`.

    Arguments:
        language_code(unicode): Language code of the requested transcript
        edx_video_id(unicode): edx-val's video identifier

    Returns:
        A dict containing transcript's file name and its sjson content.
    """
    transcript = None
    edx_video_id = clean_video_id(edx_video_id)
    if edxval_api and edx_video_id:
        try:
            transcript = edxval_api.get_video_transcript_data(edx_video_id, language_code)
        except ValueError:
            log.exception(
                f"Error getting transcript from edx-val id: {edx_video_id}: language code {language_code}"
            )
            content = '{"start": [1],"end": [2],"text": ["An error occured obtaining the transcript."]}'
            transcript = dict(
                file_name='error-{edx_video_id}-{language_code}.srt',
                content=Transcript.convert(content, 'sjson', 'srt')
            )
    return transcript
def transcript_download_handler(request):
    """
    JSON view handler to download a transcript.

    Arguments:
        request: WSGI request object

    Returns:
        - A 200 response with SRT transcript file attached.
        - A 400 if there is a validation error.
        - A 404 if there is no such transcript.
    """
    missing = [
        attr for attr in ['edx_video_id', 'language_code']
        if attr not in request.GET
    ]
    if missing:
        return JsonResponse(
            {
                'error':
                _(u'The following parameters are required: {missing}.').format(
                    missing=', '.join(missing))
            },
            status=400)

    edx_video_id = request.GET['edx_video_id']
    language_code = request.GET['language_code']
    transcript = get_video_transcript_data(video_id=edx_video_id,
                                           language_code=language_code)
    if transcript:
        name_and_extension = os.path.splitext(transcript['file_name'])
        basename, file_format = name_and_extension[0], name_and_extension[1][
            1:]
        transcript_filename = '{base_name}.{ext}'.format(base_name=basename,
                                                         ext=Transcript.SRT)
        transcript_content = Transcript.convert(content=transcript['content'],
                                                input_format=file_format,
                                                output_format=Transcript.SRT)
        # Construct an HTTP response
        response = HttpResponse(
            transcript_content,
            content_type=Transcript.mime_types[Transcript.SRT])
        response[
            'Content-Disposition'] = u'attachment; filename="{filename}"'.format(
                filename=transcript_filename)
    else:
        response = HttpResponseNotFound()

    return response
def get_video_transcript_content(edx_video_id, language_code):
    """
    Gets video transcript content, only if the corresponding feature flag is enabled for the given `course_id`.

    Arguments:
        language_code(unicode): Language code of the requested transcript
        edx_video_id(unicode): edx-val's video identifier

    Returns:
        A dict containing transcript's file name and its sjson content.
    """
    transcript = None
    edx_video_id = clean_video_id(edx_video_id)
    if edxval_api and edx_video_id:
        transcript = edxval_api.get_video_transcript_data(edx_video_id, language_code)

    return transcript
示例#7
0
def get_video_transcript_content(edx_video_id, language_code):
    """
    Gets video transcript content, only if the corresponding feature flag is enabled for the given `course_id`.

    Arguments:
        language_code(unicode): Language code of the requested transcript
        edx_video_id(unicode): edx-val's video identifier

    Returns:
        A dict containing transcript's file name and its sjson content.
    """
    transcript = None
    edx_video_id = clean_video_id(edx_video_id)
    if edxval_api and edx_video_id:
        transcript = edxval_api.get_video_transcript_data(edx_video_id, language_code)

    return transcript
示例#8
0
def get_video_transcript_content(language_code, edx_video_id, youtube_id_1_0, html5_sources):
    """
    Gets video transcript content, only if the corresponding feature flag is enabled for the given `course_id`.

    Arguments:
        language_code(unicode): Language code of the requested transcript
        edx_video_id(unicode): edx-val's video identifier
        youtube_id_1_0(unicode): A youtube source identifier
        html5_sources(list): A list containing html5 sources

    Returns:
        A dict containing transcript's file name and its sjson content.
    """
    transcript = None
    if edxval_api:
        __, video_candidate_ids = get_video_ids_info(edx_video_id, youtube_id_1_0, html5_sources)
        transcript = edxval_api.get_video_transcript_data(video_candidate_ids, language_code)

    return transcript
def get_video_transcript_content(language_code, edx_video_id, youtube_id_1_0, html5_sources):
    """
    Gets video transcript content, only if the corresponding feature flag is enabled for the given `course_id`.

    Arguments:
        language_code(unicode): Language code of the requested transcript
        edx_video_id(unicode): edx-val's video identifier
        youtube_id_1_0(unicode): A youtube source identifier
        html5_sources(list): A list containing html5 sources

    Returns:
        A dict containing transcript's file name and its sjson content.
    """
    transcript = None
    if edxval_api:
        __, video_candidate_ids = get_video_ids_info(edx_video_id, youtube_id_1_0, html5_sources)
        transcript = edxval_api.get_video_transcript_data(video_candidate_ids, language_code)

    return transcript
示例#10
0
def transcript_download_handler(request):
    """
    JSON view handler to download a transcript.

    Arguments:
        request: WSGI request object

    Returns:
        - A 200 response with SRT transcript file attached.
        - A 400 if there is a validation error.
        - A 404 if there is no such transcript.
    """
    missing = [attr for attr in ['edx_video_id', 'language_code'] if attr not in request.GET]
    if missing:
        return JsonResponse(
            {'error': _(u'The following parameters are required: {missing}.').format(missing=', '.join(missing))},
            status=400
        )

    edx_video_id = request.GET['edx_video_id']
    language_code = request.GET['language_code']
    transcript = get_video_transcript_data(video_id=edx_video_id, language_code=language_code)
    if transcript:
        name_and_extension = os.path.splitext(transcript['file_name'])
        basename, file_format = name_and_extension[0], name_and_extension[1][1:]
        transcript_filename = '{base_name}.{ext}'.format(base_name=basename.encode('utf8'), ext=Transcript.SRT)
        transcript_content = Transcript.convert(
            content=transcript['content'],
            input_format=file_format,
            output_format=Transcript.SRT
        )
        # Construct an HTTP response
        response = HttpResponse(transcript_content, content_type=Transcript.mime_types[Transcript.SRT])
        response['Content-Disposition'] = 'attachment; filename="{filename}"'.format(filename=transcript_filename)
    else:
        response = HttpResponseNotFound()

    return response