Exemple #1
0
    def test_with_transcode_async_false(
        self, user, channel_factory, mocker, parts
    ):
        mock_create_transcodes = mocker.patch(
            f'{MODULE}.transcode_manager.create_transcodes'
        )
        channel = channel_factory(user=user)
        upload, video = upload_manager.prepare(
            user=user,
            filename='MyFile.mp4',
            channel_id=channel.id,
            num_parts=len(parts),
        )

        mock_provider_complete_multipart_upload = mocker.patch(
            f'{MODULE}._provider'
        )().complete_multipart_upload

        upload_manager.complete(
            upload_id=upload.id,
            parts=parts,
            transcode_async=False,
        )

        assert mock_create_transcodes.called
        mock_create_transcodes.assert_called_once_with(
            video_id=video.id, transcode_async=False
        )
        upload.refresh_from_db()
        assert upload.status == 'uploaded'
        assert mock_provider_complete_multipart_upload.called
Exemple #2
0
    def test_does_nothing_if_upload_status_completed(
        self,
        mocker,
        parts,
        user,
        channel_factory,
    ):
        mock_create_transcodes = mocker.patch(
            f'{MODULE}.transcode_manager.create_transcodes'
        )
        channel = channel_factory(user=user)
        upload, video = upload_manager.prepare(
            user=user,
            filename='MyFile.mp4',
            channel_id=channel.id,
            num_parts=len(parts),
        )
        upload.status = 'completed'
        upload.save()

        upload_manager.complete(upload_id=upload.id, parts=parts)

        assert not mock_create_transcodes.called
        upload.refresh_from_db()
        assert upload.status == 'completed'
def test_prepare(user):
    upload, video = upload_manager.prepare(user=user, filename='MyFile.mp4')

    assert isinstance(upload, models.Upload)
    assert upload.media_type == 'video'
    assert upload.presigned_upload_url.startswith('http')
    assert upload.file
    assert isinstance(video, models.Video)
    assert not video.title
    assert video.visibility == 'draft'
Exemple #4
0
def test_prepare(user, channel_factory):
    channel = channel_factory(user=user)

    upload, video = upload_manager.prepare(user=user,
                                           filename='MyFile.mp4',
                                           channel_id=channel.id)

    assert isinstance(upload, models.Upload)
    assert upload.media_type == 'video'
    assert upload.presigned_upload_url.startswith('http')
    assert upload.file
    assert isinstance(video, models.Video)
    assert video.title == 'MyFile'
    assert video.visibility == 'public'
    assert upload.channel == channel
    assert video.channel == channel
Exemple #5
0
def _run():
    seed_data = Path(settings.BASE_DIR / 'seed_data.json')
    with seed_data.open('r') as file_:
        data = json.load(file_)
    for row in data:
        user = get_user_model().objects.create(
            username=row['user']['username'],
            email=row['user']['email'],
            is_staff=True,
            is_superuser=True,
        )
        user.set_password(row['user']['password'])
        user.save()
        for channel in row['channels']:
            channel_record = channel_services.create_channel(
                name=channel['name'],
                description=channel['description'],
                sync_videos_interested=channel['sync_videos_interested'],
                language='en',
                user=user,
            )
            print('Created Channel', channel_record.id)
            for video in channel['videos']:
                path = Path(video['path'])
                upload, video_record = upload_manager.prepare(
                    user=user,
                    filename=path.name,
                    channel_id=channel_record.id)
                video_record.title = video['title']
                video_record.description = video['description']
                video_record.save()
                print('Created Video', video_record.id)
                with path.open('rb') as file_:
                    # Upload the file completely outside of Django
                    resp = requests.put(upload.presigned_upload_url,
                                        io.BytesIO(file_.read()))
                    resp.raise_for_status()
                    upload.file = File(file_)
                    upload.save()
                upload_manager.complete(upload_id=upload.id)
Exemple #6
0
    def test_does_nothing_if_upload_provider_upload_id_missing(
        self,
        mocker,
        parts,
        user,
        channel_factory,
    ):
        mock_create_transcodes = mocker.patch(
            f'{MODULE}.transcode_manager.create_transcodes'
        )
        channel = channel_factory(user=user)
        upload, video = upload_manager.prepare(
            user=user,
            filename='MyFile.mp4',
            channel_id=channel.id,
            num_parts=len(parts),
        )
        upload.provider_upload_id = None
        upload.save()

        upload_manager.complete(upload_id=upload.id, parts=parts)

        assert not mock_create_transcodes.called
Exemple #7
0
    def test(self, user, channel_factory, mocker, parts):
        mock_create_transcodes = mocker.patch(
            f'{MODULE}.transcode_manager.create_transcodes'
        )
        channel = channel_factory(user=user)
        upload, video = upload_manager.prepare(
            user=user,
            filename='MyFile.mp4',
            channel_id=channel.id,
            num_parts=len(parts),
        )

        mock_provider_complete_multipart_upload = mocker.patch(
            f'{MODULE}._provider'
        )().complete_multipart_upload

        upload_manager.complete(upload_id=upload.id, parts=parts)

        assert mock_create_transcodes.called
        mock_create_transcodes.assert_called_once_with(
            video_id=video.id, transcode_async=True
        )
        video.refresh_from_db()
        upload.refresh_from_db()
        assert upload.status == 'uploaded'
        assert mock_provider_complete_multipart_upload.called
        mock_provider_complete_multipart_upload.assert_called_once_with(
            Bucket=upload.file.field.storage.bucket_name,
            Key=upload.file.name,
            MultipartUpload={
                'Parts': [
                    {'ETag': p['etag'], 'PartNumber': p['part_number']}
                    for p in parts
                ]
            },
            UploadId=upload.provider_upload_id,
        )
Exemple #8
0
def _run():
    seed_data = Path(settings.BASE_DIR / 'seed_data.json')
    with seed_data.open('r') as file_:
        data = json.load(file_)
    for row in data:
        user = get_user_model()(
            username=row['user']['username'],
            email=row['user']['email'],
            is_staff=True,
            is_superuser=True,
        )
        user.set_password(row['user']['password'])
        user.save()
        for channel in row['channels']:
            channel_record = channel_services.create_channel(
                name=channel['name'],
                description=channel['description'],
                sync_videos_interested=channel['sync_videos_interested'],
                primary_language='en',
                user=user,
            )
            if channel['avatar_image_url']:
                file_ = tempfile.NamedTemporaryFile('wb+', suffix='.jpg')
                file_.write(requests.get(channel['avatar_image_url']).content)
                file_.flush()
                channel_services.set_channel_avatar_image(
                    channel=channel_record, avatar_image=File(file_))
                channel_record.refresh_from_db()
                assert channel_record.avatar_image
            print('Created Channel', channel_record.id)
            for video in channel['videos']:
                path = Path(video['path'])
                file_size = path.stat().st_size
                num_parts = math.ceil(file_size / UPLOAD_CHUNK_SIZE)
                upload, video_record = upload_manager.prepare(
                    user=user,
                    filename=path.name,
                    channel_id=channel_record.id,
                    num_parts=num_parts,
                )
                video_record.title = video['title']
                video_record.description = video['description']
                video_record.save(update_fields=('title', 'description'))
                print('Created Video', video_record.id)

                parts = []
                with path.open('rb') as file_:
                    for part_idx in range(num_parts):
                        chunk = file_.read(UPLOAD_CHUNK_SIZE)
                        # Upload the file completely outside of Django
                        resp = requests.put(
                            upload.presigned_upload_urls[part_idx],
                            io.BytesIO(chunk),
                        )
                        parts.append({
                            'etag': resp.headers['ETag'],
                            'part_number': part_idx + 1,
                        })
                        resp.raise_for_status()
                        print('Uploaded file chunk')
                    upload.file = File(file_)
                    upload.save(update_fields=('file', ))
                upload_manager.complete(upload_id=upload.id, parts=parts)

                for comment_body in video.get('comments', ()):
                    print('Creating video comment', comment_body)
                    comment_services.create_video_comment(
                        video_id=video_record.id,
                        commenter_channel_id=channel_record.id,
                        body=comment_body,
                    )