fix python lint issues

This commit is contained in:
Shyam Sunder 2020-06-03 11:55:50 -04:00
parent 1be947e946
commit b0f1b8c230
13 changed files with 66 additions and 32 deletions

View file

@ -17,7 +17,8 @@ def _get_pool(params: Dict[str, str]) -> model.Pool:
@rest.routes.get('/pools/?')
def get_pools(ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response:
def get_pools(
ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response:
auth.verify_privilege(ctx.user, 'pools:list')
return _search_executor.execute_and_serialize(
ctx, lambda pool: _serialize(ctx, pool))

View file

@ -131,8 +131,12 @@ class PoolSerializer(serialization.BaseSerializer):
return self.pool.post_count
def serialize_posts(self) -> Any:
return [post for post in
[posts.serialize_micro_post(rel, None) for rel in self.pool.posts]]
return [
post for post in [
posts.serialize_micro_post(rel, None)
for rel in self.pool.posts
]
]
def serialize_pool(
@ -310,7 +314,8 @@ def update_pool_posts(pool: model.Pool, post_ids: List[int]) -> None:
if len(post_ids) != len(ret):
missing = set(post_ids) - set(post.post_id for post in ret)
missing = ', '.join(list(str(x) for x in missing))
raise InvalidPoolNonexistentPostError('The following posts do not exist: ' + missing)
raise InvalidPoolNonexistentPostError(
'The following posts do not exist: ' + missing)
pool.posts.clear()
for post in ret:
pool.posts.append(post)

View file

@ -308,7 +308,6 @@ class PostSerializer(serialization.BaseSerializer):
key=lambda pool: pool.creation_time)]
def serialize_post(
post: Optional[model.Post],
auth_user: model.User,

View file

@ -33,6 +33,7 @@ class PoolCategory(Base):
'version_id_generator': False,
}
def upgrade():
session = sa.orm.session.Session(bind=op.get_bind())
if session.query(PoolCategory).count() == 0:

View file

@ -14,6 +14,7 @@ down_revision = '1e280b5d5df1'
branch_labels = None
depends_on = None
def upgrade():
op.create_table(
'pool_category',
@ -54,6 +55,7 @@ def upgrade():
sa.ForeignKeyConstraint(['post_id'], ['post.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('pool_id', 'post_id'))
def downgrade():
op.drop_index(op.f('ix_pool_name_ord'), table_name='pool_name')
op.drop_table('pool_post')

View file

@ -20,7 +20,7 @@ class PoolName(Base):
def __init__(self, name: str, order: int) -> None:
self.name = name
self.order = order
class PoolPost(Base):
__tablename__ = 'pool_post'
@ -47,6 +47,7 @@ class PoolPost(Base):
def __init__(self, post) -> None:
self.post_id = post.post_id
class Pool(Base):
__tablename__ = 'pool'

View file

@ -18,7 +18,8 @@ class PoolCategory(Base):
self.name = name
pool_count = sa.orm.column_property(
sa.sql.expression.select([sa.sql.expression.func.count('Pool.pool_id')])
sa.sql.expression.select(
[sa.sql.expression.func.count('Pool.pool_id')])
.where(Pool.category_id == pool_category_id)
.correlate_except(sa.table('Pool')))

View file

@ -26,13 +26,15 @@ def test_creating_category(
patch('szurubooru.func.pool_categories.update_category_name'), \
patch('szurubooru.func.snapshots.create'):
pool_categories.create_category.return_value = category
pool_categories.update_category_name.side_effect = _update_category_name
pool_categories.update_category_name.side_effect = \
_update_category_name
pool_categories.serialize_category.return_value = 'serialized category'
result = api.pool_category_api.create_pool_category(
context_factory(
params={'name': 'meta', 'color': 'black'}, user=auth_user))
assert result == 'serialized category'
pool_categories.create_category.assert_called_once_with('meta', 'black')
pool_categories.create_category.assert_called_once_with(
'meta', 'black')
snapshots.create.assert_called_once_with(category, auth_user)

View file

@ -28,7 +28,8 @@ def test_simple_updating(user_factory, pool_category_factory, context_factory):
patch('szurubooru.func.pool_categories.update_category_name'), \
patch('szurubooru.func.pool_categories.update_category_color'), \
patch('szurubooru.func.snapshots.modify'):
pool_categories.update_category_name.side_effect = _update_category_name
pool_categories.update_category_name.side_effect = \
_update_category_name
pool_categories.serialize_category.return_value = 'serialized category'
result = api.pool_category_api.update_pool_category(
context_factory(
@ -93,7 +94,8 @@ def test_set_as_default(user_factory, pool_category_factory, context_factory):
db.session.commit()
with patch('szurubooru.func.pool_categories.serialize_category'), \
patch('szurubooru.func.pool_categories.set_default_category'):
pool_categories.update_category_name.side_effect = _update_category_name
pool_categories.update_category_name.side_effect = \
_update_category_name
pool_categories.serialize_category.return_value = 'serialized category'
result = api.pool_category_api.set_pool_category_as_default(
context_factory(

View file

@ -23,13 +23,13 @@ def test_simple_updating(user_factory, pool_factory, context_factory):
db.session.add(pool)
db.session.commit()
with patch('szurubooru.func.pools.create_pool'), \
patch('szurubooru.func.posts.get_posts_by_ids'), \
patch('szurubooru.func.pools.update_pool_names'), \
patch('szurubooru.func.pools.update_pool_category_name'), \
patch('szurubooru.func.pools.update_pool_description'), \
patch('szurubooru.func.pools.update_pool_posts'), \
patch('szurubooru.func.pools.serialize_pool'), \
patch('szurubooru.func.snapshots.modify'):
patch('szurubooru.func.posts.get_posts_by_ids'), \
patch('szurubooru.func.pools.update_pool_names'), \
patch('szurubooru.func.pools.update_pool_category_name'), \
patch('szurubooru.func.pools.update_pool_description'), \
patch('szurubooru.func.pools.update_pool_posts'), \
patch('szurubooru.func.pools.serialize_pool'), \
patch('szurubooru.func.snapshots.modify'):
posts.get_posts_by_ids.return_value = ([], [])
pools.serialize_pool.return_value = 'serialized pool'
result = api.pool_api.update_pool(
@ -72,9 +72,9 @@ def test_omitting_optional_field(
}
del params[field]
with patch('szurubooru.func.pools.create_pool'), \
patch('szurubooru.func.pools.update_pool_names'), \
patch('szurubooru.func.pools.update_pool_category_name'), \
patch('szurubooru.func.pools.serialize_pool'):
patch('szurubooru.func.pools.update_pool_names'), \
patch('szurubooru.func.pools.update_pool_category_name'), \
patch('szurubooru.func.pools.serialize_pool'):
api.pool_api.update_pool(
context_factory(
params={**params, **{'version': 1}},
@ -113,11 +113,14 @@ def test_trying_to_create_pools_without_privileges(
pool = pool_factory(id=1)
db.session.add(pool)
db.session.commit()
config_injector({'privileges': {
'pools:create': model.User.RANK_ADMINISTRATOR,
'pools:edit:posts': model.User.RANK_REGULAR,
},
'delete_source_files': False})
config_injector(
{
'privileges': {
'pools:create': model.User.RANK_ADMINISTRATOR,
'pools:edit:posts': model.User.RANK_REGULAR,
},
'delete_source_files': False,
})
with patch('szurubooru.func.posts.get_posts_by_ids'):
posts.get_posts_by_ids.return_value = ([], ['new-post'])
with pytest.raises(errors.AuthError):

View file

@ -214,7 +214,8 @@ def pool_category_factory():
@pytest.fixture
def pool_factory():
def factory(id=None, names=None, description=None, category=None, time=None):
def factory(
id=None, names=None, description=None, category=None, time=None):
if not category:
category = model.PoolCategory(get_unique_name())
db.session.add(category)

View file

@ -212,7 +212,9 @@ def test_serialize_post(
'posts': [
{
'id': 1,
'thumbnailUrl': 'http://example.com/generated-thumbnails/1_244c8840887984c4.jpg',
'thumbnailUrl':
'http://example.com/'
'generated-thumbnails/1_244c8840887984c4.jpg',
}
],
'version': 1,
@ -228,7 +230,9 @@ def test_serialize_post(
'posts': [
{
'id': 1,
'thumbnailUrl': 'http://example.com/generated-thumbnails/1_244c8840887984c4.jpg',
'thumbnailUrl':
'http://example.com/'
'generated-thumbnails/1_244c8840887984c4.jpg',
}
],
'version': 1,

View file

@ -222,7 +222,11 @@ def test_filter_by_edit_time(
('post-count-max:1', ['t2']),
])
def test_filter_by_post_count(
verify_unpaged, pool_factory, post_factory, input, expected_pool_names):
verify_unpaged,
pool_factory,
post_factory,
input,
expected_pool_names):
post1 = post_factory(id=1)
post2 = post_factory(id=2)
pool1 = pool_factory(id=1, names=['t1'])
@ -257,7 +261,11 @@ def test_filter_by_invalid_input(executor, input):
('-sort:name,asc', ['t2', 't1']),
('-sort:name,desc', ['t1', 't2']),
])
def test_sort_by_name(verify_unpaged, pool_factory, input, expected_pool_names):
def test_sort_by_name(
verify_unpaged,
pool_factory,
input,
expected_pool_names):
db.session.add(pool_factory(id=2, names=['t2']))
db.session.add(pool_factory(id=1, names=['t1']))
db.session.flush()
@ -306,7 +314,11 @@ def test_sort_by_last_edit_time(
('sort:post-count', ['t2', 't1']),
])
def test_sort_by_post_count(
verify_unpaged, pool_factory, post_factory, input, expected_pool_names):
verify_unpaged,
pool_factory,
post_factory,
input,
expected_pool_names):
post1 = post_factory(id=1)
post2 = post_factory(id=2)
pool1 = pool_factory(id=1, names=['t1'])