Пример #1
0
    def test_with_body_containing_mention(self, video,
                                          comment_body_with_mention):
        parent_comment = services.create_video_comment(
            video_id=video.id,
            body='Hello world',
            commenter_channel_id=video.channel_id,
        )
        comment_body, mentioned_channel = comment_body_with_mention

        comment_reply = services.create_video_comment(
            video_id=video.id,
            body=comment_body,
            commenter_channel_id=video.channel_id,
            parent_comment_id=parent_comment.id,
        )

        assert isinstance(comment_reply, services.models.VideoComment)
        assert comment_reply.video == video
        assert comment_reply.body == comment_body
        assert comment_reply.commenter_channel == video.channel
        assert comment_reply.parent_comment == parent_comment
        assert comment_reply.replies_count == 0
        parent_comment.refresh_from_db()
        assert parent_comment.replies_count == 1
        # Check mention record was created
        assert comment_reply.mentions.count() == 1
        mention = comment_reply.mentions.first()
        assert mention.video_comment == comment_reply
        assert mention.mentioned_channel == mentioned_channel
Пример #2
0
    def test_raises_if_parent_comment_is_for_a_different_video(
            self, video, video_factory):
        parent_comment_diff_video = services.create_video_comment(
            video_id=video_factory().id,
            body='Hello world',
            commenter_channel_id=video.channel_id,
        )

        with pytest.raises(ValidationError):
            services.create_video_comment(
                video_id=video.id,
                body='Hello world',
                commenter_channel_id=video.channel_id,
                parent_comment_id=parent_comment_diff_video.id,
            )
Пример #3
0
    def test_raises_if_parent_comment_is_a_reply(self, video):
        parent_comment = services.create_video_comment(
            video_id=video.id,
            body='Hello world',
            commenter_channel_id=video.channel_id,
        )
        comment_reply = services.create_video_comment(
            video_id=video.id,
            body='Hello world',
            commenter_channel_id=video.channel_id,
            parent_comment_id=parent_comment.id,
        )

        with pytest.raises(ValidationError):
            services.create_video_comment(
                video_id=video.id,
                body='Hello world',
                commenter_channel_id=video.channel_id,
                parent_comment_id=comment_reply.id,
            )
Пример #4
0
    def test_with_parent_comment(self, video):
        parent_comment = services.create_video_comment(
            video_id=video.id,
            body='Hello world',
            commenter_channel_id=video.channel_id,
        )

        comment_reply = services.create_video_comment(
            video_id=video.id,
            body='Hello world',
            commenter_channel_id=video.channel_id,
            parent_comment_id=parent_comment.id,
        )

        assert isinstance(comment_reply, services.models.VideoComment)
        assert comment_reply.video == video
        assert comment_reply.body == 'Hello world'
        assert comment_reply.commenter_channel == video.channel
        assert comment_reply.parent_comment == parent_comment
        assert comment_reply.replies_count == 0
        parent_comment.refresh_from_db()
        assert parent_comment.replies_count == 1
Пример #5
0
    def test(self, video):
        comment = services.create_video_comment(
            video_id=video.id,
            body='Hello world',
            commenter_channel_id=video.channel.id,
        )

        assert isinstance(comment, services.models.VideoComment)
        assert comment.video == video
        assert comment.body == 'Hello world'
        assert comment.commenter_channel == video.channel
        assert comment.parent_comment is None
        assert comment.replies_count == 0
Пример #6
0
    def test_raises_if_body_contains_multiple_mentions(self, video,
                                                       user_factory,
                                                       channel_factory,
                                                       settings):
        parent_comment = services.create_video_comment(
            video_id=video.id,
            body='Hello world',
            commenter_channel_id=video.channel_id,
        )
        mentioned_channel = channel_factory(user=user_factory())
        mentioned_channel2 = channel_factory(user=user_factory())
        delim = settings.COMMENT_MENTION_DELIM

        comment_body = (f'{delim}{mentioned_channel.id}{delim}Hello world!'
                        f'{delim}{mentioned_channel2.id}{delim}')

        with pytest.raises(DRFValidationError):
            services.create_video_comment(
                video_id=video.id,
                body=comment_body,
                commenter_channel_id=video.channel_id,
                parent_comment_id=parent_comment.id,
            )
Пример #7
0
 def make(
     video_id=None,
     commenter_channel_id=None,
     body='Hello world',
     parent_comment_id=None,
 ):
     video_id = video_id or request.getfixturevalue('video').id
     commenter_channel_id = (commenter_channel_id
                             or request.getfixturevalue('channel').id)
     return comment_services.create_video_comment(
         video_id=video_id,
         commenter_channel_id=commenter_channel_id,
         body=body,
         parent_comment_id=parent_comment_id,
     )
Пример #8
0
 def video_comment(self, video):
     return services.create_video_comment(
         video_id=video.id,
         body='Hello world',
         commenter_channel_id=video.channel_id,
     )
Пример #9
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,
                    )