def test_featuring_one_post_after_another(user_factory, post_factory,
                                          context_factory, fake_datetime):
    db.session.add(post_factory(id=1))
    db.session.add(post_factory(id=2))
    db.session.commit()
    assert posts.try_get_featured_post() is None
    assert not posts.get_post_by_id(1).is_featured
    assert not posts.get_post_by_id(2).is_featured
    with patch("szurubooru.func.posts.serialize_post"), patch(
            "szurubooru.func.snapshots._post_to_webhooks"):
        with fake_datetime("1997"):
            api.post_api.set_featured_post(
                context_factory(
                    params={"id": 1},
                    user=user_factory(rank=model.User.RANK_REGULAR),
                ))
        with fake_datetime("1998"):
            api.post_api.set_featured_post(
                context_factory(
                    params={"id": 2},
                    user=user_factory(rank=model.User.RANK_REGULAR),
                ))
        assert posts.try_get_featured_post() is not None
        assert posts.try_get_featured_post().post_id == 2
        assert not posts.get_post_by_id(1).is_featured
        assert posts.get_post_by_id(2).is_featured
Example #2
0
def test_feature_post(post_factory, user_factory):
    post = post_factory()
    user = user_factory()
    previous_featured_post = posts.try_get_featured_post()
    db.session.flush()
    posts.feature_post(post, user)
    db.session.flush()
    new_featured_post = posts.try_get_featured_post()
    assert previous_featured_post is None
    assert new_featured_post == post
Example #3
0
def test_feature_post(post_factory, user_factory):
    post = post_factory()
    user = user_factory()
    previous_featured_post = posts.try_get_featured_post()
    db.session.flush()
    posts.feature_post(post, user)
    db.session.flush()
    new_featured_post = posts.try_get_featured_post()
    assert previous_featured_post is None
    assert new_featured_post == post
Example #4
0
def test_featuring_post(post_factory, user_factory):
    post = post_factory()
    user = user_factory()

    previous_featured_post = posts.try_get_featured_post()
    posts.feature_post(post, user)
    new_featured_post = posts.try_get_featured_post()

    assert previous_featured_post is None
    assert new_featured_post == post
Example #5
0
 def get(self, ctx):
     featured_post = posts.try_get_featured_post()
     return {
         'postCount': posts.get_post_count(),
         'diskUsage': self._get_disk_usage(),
         'featuredPost': posts.serialize_post(featured_post, ctx.user),
     }
def test_featuring(user_factory, post_factory, context_factory):
    auth_user = user_factory(rank=model.User.RANK_REGULAR)
    post = post_factory(id=1)
    db.session.add(post)
    db.session.flush()
    assert not posts.get_post_by_id(1).is_featured
    with patch("szurubooru.func.posts.serialize_post"), patch(
            "szurubooru.func.snapshots.modify"):
        posts.serialize_post.return_value = "serialized post"
        result = api.post_api.set_featured_post(
            context_factory(params={"id": 1}, user=auth_user))
        assert result == "serialized post"
        assert posts.try_get_featured_post() is not None
        assert posts.try_get_featured_post().post_id == 1
        assert posts.get_post_by_id(1).is_featured
        result = api.post_api.get_featured_post(
            context_factory(user=user_factory(rank=model.User.RANK_REGULAR)))
        assert result == "serialized post"
        snapshots.modify.assert_called_once_with(post, auth_user)
Example #7
0
def test_featuring(user_factory, post_factory, context_factory):
    auth_user = user_factory(rank=db.User.RANK_REGULAR)
    post = post_factory(id=1)
    db.session.add(post)
    db.session.flush()
    assert not posts.get_post_by_id(1).is_featured
    with patch('szurubooru.func.posts.serialize_post'), \
            patch('szurubooru.func.snapshots.modify'):
        posts.serialize_post.return_value = 'serialized post'
        result = api.post_api.set_featured_post(
            context_factory(params={'id': 1}, user=auth_user))
        assert result == 'serialized post'
        assert posts.try_get_featured_post() is not None
        assert posts.try_get_featured_post().post_id == 1
        assert posts.get_post_by_id(1).is_featured
        result = api.post_api.get_featured_post(
            context_factory(
                user=user_factory(rank=db.User.RANK_REGULAR)))
        assert result == 'serialized post'
        snapshots.modify.assert_called_once_with(post, auth_user)
def test_featuring_one_post_after_another(test_ctx, fake_datetime):
    db.session.add(test_ctx.post_factory(id=1))
    db.session.add(test_ctx.post_factory(id=2))
    db.session.commit()
    assert posts.try_get_featured_post() is None
    assert not posts.get_post_by_id(1).is_featured
    assert not posts.get_post_by_id(2).is_featured
    with fake_datetime('1997'):
        result = test_ctx.api.post(
            test_ctx.context_factory(
                input={'id': 1},
                user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
    with fake_datetime('1998'):
        result = test_ctx.api.post(
            test_ctx.context_factory(
                input={'id': 2},
                user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
    assert posts.try_get_featured_post() is not None
    assert posts.try_get_featured_post().post_id == 2
    assert not posts.get_post_by_id(1).is_featured
    assert posts.get_post_by_id(2).is_featured
Example #9
0
def set_featured_post(ctx, _params=None):
    auth.verify_privilege(ctx.user, 'posts:feature')
    post_id = ctx.get_param_as_int('id', required=True)
    post = posts.get_post_by_id(post_id)
    featured_post = posts.try_get_featured_post()
    if featured_post and featured_post.post_id == post.post_id:
        raise posts.PostAlreadyFeaturedError(
            'Post %r is already featured.' % post_id)
    posts.feature_post(post, ctx.user)
    snapshots.modify(post, ctx.user)
    ctx.session.commit()
    return _serialize_post(ctx, post)
Example #10
0
def set_featured_post(ctx: rest.Context,
                      _params: Dict[str, str] = {}) -> rest.Response:
    auth.verify_privilege(ctx.user, 'posts:feature')
    post_id = ctx.get_param_as_int('id')
    post = posts.get_post_by_id(post_id)
    featured_post = posts.try_get_featured_post()
    if featured_post and featured_post.post_id == post.post_id:
        raise posts.PostAlreadyFeaturedError('짤 %r 는 이미 대문짤임.' % post_id)
    posts.feature_post(post, ctx.user)
    snapshots.modify(post, ctx.user)
    ctx.session.commit()
    return _serialize_post(ctx, post)
def test_featuring(test_ctx):
    db.session.add(test_ctx.post_factory(id=1))
    db.session.commit()
    assert not posts.get_post_by_id(1).is_featured
    result = test_ctx.api.post(
        test_ctx.context_factory(
            input={'id': 1},
            user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
    assert posts.try_get_featured_post() is not None
    assert posts.try_get_featured_post().post_id == 1
    assert posts.get_post_by_id(1).is_featured
    assert 'post' in result
    assert 'id' in result['post']
    assert 'snapshots' in result
    assert 'comments' in result
    result = test_ctx.api.get(
        test_ctx.context_factory(
            user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
    assert 'post' in result
    assert 'id' in result['post']
    assert 'snapshots' in result
    assert 'comments' in result
Example #12
0
def set_featured_post(
        ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response:
    auth.verify_privilege(ctx.user, 'posts:feature')
    post_id = ctx.get_param_as_int('id')
    post = posts.get_post_by_id(post_id)
    featured_post = posts.try_get_featured_post()
    if featured_post and featured_post.post_id == post.post_id:
        raise posts.PostAlreadyFeaturedError(
            'Post %r is already featured.' % post_id)
    posts.feature_post(post, ctx.user)
    snapshots.modify(post, ctx.user)
    ctx.session.commit()
    return _serialize_post(ctx, post)
Example #13
0
 def post(self, ctx):
     auth.verify_privilege(ctx.user, 'posts:feature')
     post_id = ctx.get_param_as_int('id', required=True)
     post = posts.get_post_by_id(post_id)
     featured_post = posts.try_get_featured_post()
     if featured_post and featured_post.post_id == post.post_id:
         raise posts.PostAlreadyFeaturedError(
             'Post %r is already featured.' % post_id)
     posts.feature_post(post, ctx.user)
     if featured_post:
         snapshots.save_entity_modification(featured_post, ctx.user)
     snapshots.save_entity_modification(post, ctx.user)
     ctx.session.commit()
     return posts.serialize_post_with_details(post, ctx.user)
Example #14
0
def test_featuring_one_post_after_another(user_factory, post_factory,
                                          context_factory, fake_datetime):
    db.session.add(post_factory(id=1))
    db.session.add(post_factory(id=2))
    db.session.commit()
    assert posts.try_get_featured_post() is None
    assert not posts.get_post_by_id(1).is_featured
    assert not posts.get_post_by_id(2).is_featured
    with patch('szurubooru.func.posts.serialize_post'):
        with fake_datetime('1997'):
            api.post_api.set_featured_post(
                context_factory(
                    params={'id': 1},
                    user=user_factory(rank=model.User.RANK_REGULAR)))
        with fake_datetime('1998'):
            api.post_api.set_featured_post(
                context_factory(
                    params={'id': 2},
                    user=user_factory(rank=model.User.RANK_REGULAR)))
        assert posts.try_get_featured_post() is not None
        assert posts.try_get_featured_post().post_id == 2
        assert not posts.get_post_by_id(1).is_featured
        assert posts.get_post_by_id(2).is_featured
Example #15
0
 def post(self, ctx):
     auth.verify_privilege(ctx.user, 'posts:feature')
     post_id = ctx.get_param_as_int('id', required=True)
     post = posts.get_post_by_id(post_id)
     featured_post = posts.try_get_featured_post()
     if featured_post and featured_post.post_id == post.post_id:
         raise posts.PostAlreadyFeaturedError(
             'Post %r is already featured.' % post_id)
     posts.feature_post(post, ctx.user)
     if featured_post:
         snapshots.save_entity_modification(featured_post, ctx.user)
     snapshots.save_entity_modification(post, ctx.user)
     ctx.session.commit()
     return posts.serialize_post_with_details(post, ctx.user)
Example #16
0
def test_featuring_one_post_after_another(
        user_factory, post_factory, context_factory, fake_datetime):
    db.session.add(post_factory(id=1))
    db.session.add(post_factory(id=2))
    db.session.commit()
    assert posts.try_get_featured_post() is None
    assert not posts.get_post_by_id(1).is_featured
    assert not posts.get_post_by_id(2).is_featured
    with patch('szurubooru.func.posts.serialize_post'):
        with fake_datetime('1997'):
            api.post_api.set_featured_post(
                context_factory(
                    params={'id': 1},
                    user=user_factory(rank=db.User.RANK_REGULAR)))
        with fake_datetime('1998'):
            api.post_api.set_featured_post(
                context_factory(
                    params={'id': 2},
                    user=user_factory(rank=db.User.RANK_REGULAR)))
        assert posts.try_get_featured_post() is not None
        assert posts.try_get_featured_post().post_id == 2
        assert not posts.get_post_by_id(1).is_featured
        assert posts.get_post_by_id(2).is_featured
Example #17
0
def get_featured_post(
        ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response:
    post = posts.try_get_featured_post()
    return _serialize_post(ctx, post)
Example #18
0
 def get(self, ctx):
     post = posts.try_get_featured_post()
     return posts.serialize_post_with_details(post, ctx.user)
Example #19
0
def get_featured_post(
        ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response:
    auth.verify_privilege(ctx.user, 'posts:view:featured')
    post = posts.try_get_featured_post()
    return _serialize_post(ctx, post)
Example #20
0
 def get(self, ctx):
     post = posts.try_get_featured_post()
     return posts.serialize_post_with_details(post, ctx.user)
Example #21
0
def get_featured_post(ctx: rest.Context,
                      _params: Dict[str, str] = {}) -> rest.Response:
    auth.verify_privilege(ctx.user, 'posts:view:featured')
    post = posts.try_get_featured_post()
    return _serialize_post(ctx, post)
Example #22
0
def get_featured_post(ctx, _params=None):
    post = posts.try_get_featured_post()
    return _serialize_post(ctx, post)
def test_no_featured_post(test_ctx):
    assert posts.try_get_featured_post() is None
    result = test_ctx.api.get(
        test_ctx.context_factory(
            user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
    assert result == {'post': None, 'snapshots': [], 'comments': []}