server/general: embrace most of PEP8
Ignored only the rules about continuing / hanging indentation. Also, added __init__.py to tests so that pylint discovers them. (I don't buy pytest's BS about installing your package.)
This commit is contained in:
parent
af62f8c45a
commit
9aea55e3d1
129 changed files with 2251 additions and 1077 deletions
|
@ -8,11 +8,26 @@ good-names=ex,_,logger
|
|||
dummy-variables-rgx=_|dummy
|
||||
|
||||
[format]
|
||||
max-line-length=90
|
||||
max-line-length=79
|
||||
|
||||
[messages control]
|
||||
disable=missing-docstring,no-self-use,too-few-public-methods,multiple-statements
|
||||
reports=no
|
||||
disable=
|
||||
# we're not java
|
||||
missing-docstring,
|
||||
|
||||
# covered better by pycodestyle
|
||||
bad-continuation,
|
||||
|
||||
# we're adults
|
||||
redefined-builtin,
|
||||
duplicate-code,
|
||||
too-many-return-statements,
|
||||
too-many-arguments,
|
||||
|
||||
# plain stupid
|
||||
no-self-use,
|
||||
too-few-public-methods
|
||||
|
||||
[typecheck]
|
||||
generated-members=add|add_all
|
||||
|
|
|
@ -3,20 +3,24 @@ from szurubooru import search
|
|||
from szurubooru.func import auth, comments, posts, scores, util
|
||||
from szurubooru.rest import routes
|
||||
|
||||
|
||||
_search_executor = search.Executor(search.configs.CommentSearchConfig())
|
||||
|
||||
|
||||
def _serialize(ctx, comment, **kwargs):
|
||||
return comments.serialize_comment(
|
||||
comment,
|
||||
ctx.user,
|
||||
options=util.get_serialization_options(ctx), **kwargs)
|
||||
|
||||
|
||||
@routes.get('/comments/?')
|
||||
def get_comments(ctx, _params=None):
|
||||
auth.verify_privilege(ctx.user, 'comments:list')
|
||||
return _search_executor.execute_and_serialize(
|
||||
ctx, lambda comment: _serialize(ctx, comment))
|
||||
|
||||
|
||||
@routes.post('/comments/?')
|
||||
def create_comment(ctx, _params=None):
|
||||
auth.verify_privilege(ctx.user, 'comments:create')
|
||||
|
@ -28,12 +32,14 @@ def create_comment(ctx, _params=None):
|
|||
ctx.session.commit()
|
||||
return _serialize(ctx, comment)
|
||||
|
||||
|
||||
@routes.get('/comment/(?P<comment_id>[^/]+)/?')
|
||||
def get_comment(ctx, params):
|
||||
auth.verify_privilege(ctx.user, 'comments:view')
|
||||
comment = comments.get_comment_by_id(params['comment_id'])
|
||||
return _serialize(ctx, comment)
|
||||
|
||||
|
||||
@routes.put('/comment/(?P<comment_id>[^/]+)/?')
|
||||
def update_comment(ctx, params):
|
||||
comment = comments.get_comment_by_id(params['comment_id'])
|
||||
|
@ -47,6 +53,7 @@ def update_comment(ctx, params):
|
|||
ctx.session.commit()
|
||||
return _serialize(ctx, comment)
|
||||
|
||||
|
||||
@routes.delete('/comment/(?P<comment_id>[^/]+)/?')
|
||||
def delete_comment(ctx, params):
|
||||
comment = comments.get_comment_by_id(params['comment_id'])
|
||||
|
@ -57,6 +64,7 @@ def delete_comment(ctx, params):
|
|||
ctx.session.commit()
|
||||
return {}
|
||||
|
||||
|
||||
@routes.put('/comment/(?P<comment_id>[^/]+)/score/?')
|
||||
def set_comment_score(ctx, params):
|
||||
auth.verify_privilege(ctx.user, 'comments:score')
|
||||
|
@ -66,6 +74,7 @@ def set_comment_score(ctx, params):
|
|||
ctx.session.commit()
|
||||
return _serialize(ctx, comment)
|
||||
|
||||
|
||||
@routes.delete('/comment/(?P<comment_id>[^/]+)/score/?')
|
||||
def delete_comment_score(ctx, params):
|
||||
auth.verify_privilege(ctx.user, 'comments:score')
|
||||
|
|
|
@ -4,9 +4,11 @@ from szurubooru import config
|
|||
from szurubooru.func import posts, users, util
|
||||
from szurubooru.rest import routes
|
||||
|
||||
|
||||
_cache_time = None
|
||||
_cache_result = None
|
||||
|
||||
|
||||
def _get_disk_usage():
|
||||
global _cache_time, _cache_result # pylint: disable=global-statement
|
||||
threshold = datetime.timedelta(hours=1)
|
||||
|
@ -22,16 +24,19 @@ def _get_disk_usage():
|
|||
_cache_result = total_size
|
||||
return total_size
|
||||
|
||||
|
||||
@routes.get('/info/?')
|
||||
def get_info(ctx, _params=None):
|
||||
post_feature = posts.try_get_current_post_feature()
|
||||
return {
|
||||
'postCount': posts.get_post_count(),
|
||||
'diskUsage': _get_disk_usage(),
|
||||
'featuredPost': posts.serialize_post(post_feature.post, ctx.user) \
|
||||
'featuredPost':
|
||||
posts.serialize_post(post_feature.post, ctx.user)
|
||||
if post_feature else None,
|
||||
'featuringTime': post_feature.time if post_feature else None,
|
||||
'featuringUser': users.serialize_user(post_feature.user, ctx.user) \
|
||||
'featuringUser':
|
||||
users.serialize_user(post_feature.user, ctx.user)
|
||||
if post_feature else None,
|
||||
'serverTime': datetime.datetime.utcnow(),
|
||||
'config': {
|
||||
|
@ -40,7 +45,8 @@ def get_info(ctx, _params=None):
|
|||
'tagNameRegex': config.config['tag_name_regex'],
|
||||
'tagCategoryNameRegex': config.config['tag_category_name_regex'],
|
||||
'defaultUserRank': config.config['default_rank'],
|
||||
'privileges': util.snake_case_to_lower_camel_case_keys(
|
||||
'privileges':
|
||||
util.snake_case_to_lower_camel_case_keys(
|
||||
config.config['privileges']),
|
||||
},
|
||||
}
|
||||
|
|
|
@ -2,12 +2,14 @@ from szurubooru import config, errors
|
|||
from szurubooru.func import auth, mailer, users, util
|
||||
from szurubooru.rest import routes
|
||||
|
||||
|
||||
MAIL_SUBJECT = 'Password reset for {name}'
|
||||
MAIL_BODY = \
|
||||
'You (or someone else) requested to reset your password on {name}.\n' \
|
||||
'If you wish to proceed, click this link: {url}\n' \
|
||||
'Otherwise, please ignore this email.'
|
||||
|
||||
|
||||
@routes.get('/password-reset/(?P<user_name>[^/]+)/?')
|
||||
def start_password_reset(_ctx, params):
|
||||
''' Send a mail with secure token to the correlated user. '''
|
||||
|
@ -27,6 +29,7 @@ def start_password_reset(_ctx, params):
|
|||
MAIL_BODY.format(name=config.config['name'], url=url))
|
||||
return {}
|
||||
|
||||
|
||||
@routes.post('/password-reset/(?P<user_name>[^/]+)/?')
|
||||
def finish_password_reset(ctx, params):
|
||||
''' Verify token from mail, generate a new password and return it. '''
|
||||
|
|
|
@ -1,16 +1,20 @@
|
|||
import datetime
|
||||
from szurubooru import search
|
||||
from szurubooru.func import auth, tags, posts, snapshots, favorites, scores, util
|
||||
from szurubooru.rest import routes
|
||||
from szurubooru.func import (
|
||||
auth, tags, posts, snapshots, favorites, scores, util)
|
||||
|
||||
|
||||
_search_executor = search.Executor(search.configs.PostSearchConfig())
|
||||
|
||||
|
||||
def _serialize_post(ctx, post):
|
||||
return posts.serialize_post(
|
||||
post,
|
||||
ctx.user,
|
||||
options=util.get_serialization_options(ctx))
|
||||
|
||||
|
||||
@routes.get('/posts/?')
|
||||
def get_posts(ctx, _params=None):
|
||||
auth.verify_privilege(ctx.user, 'posts:list')
|
||||
|
@ -18,6 +22,7 @@ def get_posts(ctx, _params=None):
|
|||
return _search_executor.execute_and_serialize(
|
||||
ctx, lambda post: _serialize_post(ctx, post))
|
||||
|
||||
|
||||
@routes.post('/posts/?')
|
||||
def create_post(ctx, _params=None):
|
||||
anonymous = ctx.get_param_as_bool('anonymous', default=False)
|
||||
|
@ -52,12 +57,14 @@ def create_post(ctx, _params=None):
|
|||
tags.export_to_json()
|
||||
return _serialize_post(ctx, post)
|
||||
|
||||
|
||||
@routes.get('/post/(?P<post_id>[^/]+)/?')
|
||||
def get_post(ctx, params):
|
||||
auth.verify_privilege(ctx.user, 'posts:view')
|
||||
post = posts.get_post_by_id(params['post_id'])
|
||||
return _serialize_post(ctx, post)
|
||||
|
||||
|
||||
@routes.put('/post/(?P<post_id>[^/]+)/?')
|
||||
def update_post(ctx, params):
|
||||
post = posts.get_post_by_id(params['post_id'])
|
||||
|
@ -98,6 +105,7 @@ def update_post(ctx, params):
|
|||
tags.export_to_json()
|
||||
return _serialize_post(ctx, post)
|
||||
|
||||
|
||||
@routes.delete('/post/(?P<post_id>[^/]+)/?')
|
||||
def delete_post(ctx, params):
|
||||
auth.verify_privilege(ctx.user, 'posts:delete')
|
||||
|
@ -109,11 +117,13 @@ def delete_post(ctx, params):
|
|||
tags.export_to_json()
|
||||
return {}
|
||||
|
||||
|
||||
@routes.get('/featured-post/?')
|
||||
def get_featured_post(ctx, _params=None):
|
||||
post = posts.try_get_featured_post()
|
||||
return _serialize_post(ctx, post)
|
||||
|
||||
|
||||
@routes.post('/featured-post/?')
|
||||
def set_featured_post(ctx, _params=None):
|
||||
auth.verify_privilege(ctx.user, 'posts:feature')
|
||||
|
@ -130,6 +140,7 @@ def set_featured_post(ctx, _params=None):
|
|||
ctx.session.commit()
|
||||
return _serialize_post(ctx, post)
|
||||
|
||||
|
||||
@routes.put('/post/(?P<post_id>[^/]+)/score/?')
|
||||
def set_post_score(ctx, params):
|
||||
auth.verify_privilege(ctx.user, 'posts:score')
|
||||
|
@ -139,6 +150,7 @@ def set_post_score(ctx, params):
|
|||
ctx.session.commit()
|
||||
return _serialize_post(ctx, post)
|
||||
|
||||
|
||||
@routes.delete('/post/(?P<post_id>[^/]+)/score/?')
|
||||
def delete_post_score(ctx, params):
|
||||
auth.verify_privilege(ctx.user, 'posts:score')
|
||||
|
@ -147,6 +159,7 @@ def delete_post_score(ctx, params):
|
|||
ctx.session.commit()
|
||||
return _serialize_post(ctx, post)
|
||||
|
||||
|
||||
@routes.post('/post/(?P<post_id>[^/]+)/favorite/?')
|
||||
def add_post_to_favorites(ctx, params):
|
||||
auth.verify_privilege(ctx.user, 'posts:favorite')
|
||||
|
@ -155,6 +168,7 @@ def add_post_to_favorites(ctx, params):
|
|||
ctx.session.commit()
|
||||
return _serialize_post(ctx, post)
|
||||
|
||||
|
||||
@routes.delete('/post/(?P<post_id>[^/]+)/favorite/?')
|
||||
def delete_post_from_favorites(ctx, params):
|
||||
auth.verify_privilege(ctx.user, 'posts:favorite')
|
||||
|
@ -163,6 +177,7 @@ def delete_post_from_favorites(ctx, params):
|
|||
ctx.session.commit()
|
||||
return _serialize_post(ctx, post)
|
||||
|
||||
|
||||
@routes.get('/post/(?P<post_id>[^/]+)/around/?')
|
||||
def get_posts_around(ctx, params):
|
||||
auth.verify_privilege(ctx.user, 'posts:list')
|
||||
|
|
|
@ -2,8 +2,9 @@ from szurubooru import search
|
|||
from szurubooru.func import auth, snapshots
|
||||
from szurubooru.rest import routes
|
||||
|
||||
_search_executor = search.Executor(
|
||||
search.configs.SnapshotSearchConfig())
|
||||
|
||||
_search_executor = search.Executor(search.configs.SnapshotSearchConfig())
|
||||
|
||||
|
||||
@routes.get('/snapshots/?')
|
||||
def get_snapshots(ctx, _params=None):
|
||||
|
|
|
@ -3,12 +3,15 @@ from szurubooru import db, search
|
|||
from szurubooru.func import auth, tags, util, snapshots
|
||||
from szurubooru.rest import routes
|
||||
|
||||
|
||||
_search_executor = search.Executor(search.configs.TagSearchConfig())
|
||||
|
||||
|
||||
def _serialize(ctx, tag):
|
||||
return tags.serialize_tag(
|
||||
tag, options=util.get_serialization_options(ctx))
|
||||
|
||||
|
||||
def _create_if_needed(tag_names, user):
|
||||
if not tag_names:
|
||||
return
|
||||
|
@ -19,12 +22,14 @@ def _create_if_needed(tag_names, user):
|
|||
for tag in new_tags:
|
||||
snapshots.save_entity_creation(tag, user)
|
||||
|
||||
|
||||
@routes.get('/tags/?')
|
||||
def get_tags(ctx, _params=None):
|
||||
auth.verify_privilege(ctx.user, 'tags:list')
|
||||
return _search_executor.execute_and_serialize(
|
||||
ctx, lambda tag: _serialize(ctx, tag))
|
||||
|
||||
|
||||
@routes.post('/tags/?')
|
||||
def create_tag(ctx, _params=None):
|
||||
auth.verify_privilege(ctx.user, 'tags:create')
|
||||
|
@ -50,12 +55,14 @@ def create_tag(ctx, _params=None):
|
|||
tags.export_to_json()
|
||||
return _serialize(ctx, tag)
|
||||
|
||||
|
||||
@routes.get('/tag/(?P<tag_name>[^/]+)/?')
|
||||
def get_tag(ctx, params):
|
||||
auth.verify_privilege(ctx.user, 'tags:view')
|
||||
tag = tags.get_tag_by_name(params['tag_name'])
|
||||
return _serialize(ctx, tag)
|
||||
|
||||
|
||||
@routes.put('/tag/(?P<tag_name>[^/]+)/?')
|
||||
def update_tag(ctx, params):
|
||||
tag = tags.get_tag_by_name(params['tag_name'])
|
||||
|
@ -89,6 +96,7 @@ def update_tag(ctx, params):
|
|||
tags.export_to_json()
|
||||
return _serialize(ctx, tag)
|
||||
|
||||
|
||||
@routes.delete('/tag/(?P<tag_name>[^/]+)/?')
|
||||
def delete_tag(ctx, params):
|
||||
tag = tags.get_tag_by_name(params['tag_name'])
|
||||
|
@ -100,6 +108,7 @@ def delete_tag(ctx, params):
|
|||
tags.export_to_json()
|
||||
return {}
|
||||
|
||||
|
||||
@routes.post('/tag-merge/?')
|
||||
def merge_tags(ctx, _params=None):
|
||||
source_tag_name = ctx.get_param_as_string('remove', required=True) or ''
|
||||
|
@ -116,6 +125,7 @@ def merge_tags(ctx, _params=None):
|
|||
tags.export_to_json()
|
||||
return _serialize(ctx, target_tag)
|
||||
|
||||
|
||||
@routes.get('/tag-siblings/(?P<tag_name>[^/]+)/?')
|
||||
def get_tag_siblings(ctx, params):
|
||||
auth.verify_privilege(ctx.user, 'tags:view')
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
from szurubooru.rest import routes
|
||||
from szurubooru.func import auth, tags, tag_categories, util, snapshots
|
||||
|
||||
|
||||
def _serialize(ctx, category):
|
||||
return tag_categories.serialize_category(
|
||||
category, options=util.get_serialization_options(ctx))
|
||||
|
||||
|
||||
@routes.get('/tag-categories/?')
|
||||
def get_tag_categories(ctx, _params=None):
|
||||
auth.verify_privilege(ctx.user, 'tag_categories:list')
|
||||
|
@ -13,6 +15,7 @@ def get_tag_categories(ctx, _params=None):
|
|||
'results': [_serialize(ctx, category) for category in categories],
|
||||
}
|
||||
|
||||
|
||||
@routes.post('/tag-categories/?')
|
||||
def create_tag_category(ctx, _params=None):
|
||||
auth.verify_privilege(ctx.user, 'tag_categories:create')
|
||||
|
@ -26,12 +29,14 @@ def create_tag_category(ctx, _params=None):
|
|||
tags.export_to_json()
|
||||
return _serialize(ctx, category)
|
||||
|
||||
|
||||
@routes.get('/tag-category/(?P<category_name>[^/]+)/?')
|
||||
def get_tag_category(ctx, params):
|
||||
auth.verify_privilege(ctx.user, 'tag_categories:view')
|
||||
category = tag_categories.get_category_by_name(params['category_name'])
|
||||
return _serialize(ctx, category)
|
||||
|
||||
|
||||
@routes.put('/tag-category/(?P<category_name>[^/]+)/?')
|
||||
def update_tag_category(ctx, params):
|
||||
category = tag_categories.get_category_by_name(params['category_name'])
|
||||
|
@ -51,6 +56,7 @@ def update_tag_category(ctx, params):
|
|||
tags.export_to_json()
|
||||
return _serialize(ctx, category)
|
||||
|
||||
|
||||
@routes.delete('/tag-category/(?P<category_name>[^/]+)/?')
|
||||
def delete_tag_category(ctx, params):
|
||||
category = tag_categories.get_category_by_name(params['category_name'])
|
||||
|
@ -62,6 +68,7 @@ def delete_tag_category(ctx, params):
|
|||
tags.export_to_json()
|
||||
return {}
|
||||
|
||||
|
||||
@routes.put('/tag-category/(?P<category_name>[^/]+)/default/?')
|
||||
def set_tag_category_as_default(ctx, params):
|
||||
auth.verify_privilege(ctx.user, 'tag_categories:set_default')
|
||||
|
|
|
@ -2,8 +2,10 @@ from szurubooru import search
|
|||
from szurubooru.func import auth, users, util
|
||||
from szurubooru.rest import routes
|
||||
|
||||
|
||||
_search_executor = search.Executor(search.configs.UserSearchConfig())
|
||||
|
||||
|
||||
def _serialize(ctx, user, **kwargs):
|
||||
return users.serialize_user(
|
||||
user,
|
||||
|
@ -11,12 +13,14 @@ def _serialize(ctx, user, **kwargs):
|
|||
options=util.get_serialization_options(ctx),
|
||||
**kwargs)
|
||||
|
||||
|
||||
@routes.get('/users/?')
|
||||
def get_users(ctx, _params=None):
|
||||
auth.verify_privilege(ctx.user, 'users:list')
|
||||
return _search_executor.execute_and_serialize(
|
||||
ctx, lambda user: _serialize(ctx, user))
|
||||
|
||||
|
||||
@routes.post('/users/?')
|
||||
def create_user(ctx, _params=None):
|
||||
auth.verify_privilege(ctx.user, 'users:create')
|
||||
|
@ -36,6 +40,7 @@ def create_user(ctx, _params=None):
|
|||
ctx.session.commit()
|
||||
return _serialize(ctx, user, force_show_email=True)
|
||||
|
||||
|
||||
@routes.get('/user/(?P<user_name>[^/]+)/?')
|
||||
def get_user(ctx, params):
|
||||
user = users.get_user_by_name(params['user_name'])
|
||||
|
@ -43,6 +48,7 @@ def get_user(ctx, params):
|
|||
auth.verify_privilege(ctx.user, 'users:view')
|
||||
return _serialize(ctx, user)
|
||||
|
||||
|
||||
@routes.put('/user/(?P<user_name>[^/]+)/?')
|
||||
def update_user(ctx, params):
|
||||
user = users.get_user_by_name(params['user_name'])
|
||||
|
@ -72,6 +78,7 @@ def update_user(ctx, params):
|
|||
ctx.session.commit()
|
||||
return _serialize(ctx, user)
|
||||
|
||||
|
||||
@routes.delete('/user/(?P<user_name>[^/]+)/?')
|
||||
def delete_user(ctx, params):
|
||||
user = users.get_user_by_name(params['user_name'])
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import os
|
||||
import yaml
|
||||
|
||||
|
||||
def merge(left, right):
|
||||
for key in right:
|
||||
if key in left:
|
||||
|
@ -12,6 +13,7 @@ def merge(left, right):
|
|||
left[key] = right[key]
|
||||
return left
|
||||
|
||||
|
||||
def read_config():
|
||||
with open('../config.yaml.dist') as handle:
|
||||
ret = yaml.load(handle.read())
|
||||
|
@ -20,4 +22,5 @@ def read_config():
|
|||
ret = merge(ret, yaml.load(handle.read()))
|
||||
return ret
|
||||
|
||||
|
||||
config = read_config() # pylint: disable=invalid-name
|
||||
|
|
|
@ -1,2 +1,4 @@
|
|||
from sqlalchemy.ext.declarative import declarative_base
|
||||
|
||||
|
||||
Base = declarative_base() # pylint: disable=invalid-name
|
||||
|
|
|
@ -3,13 +3,18 @@ from sqlalchemy.orm import relationship, backref
|
|||
from sqlalchemy.sql.expression import func
|
||||
from szurubooru.db.base import Base
|
||||
|
||||
|
||||
class CommentScore(Base):
|
||||
__tablename__ = 'comment_score'
|
||||
|
||||
comment_id = Column(
|
||||
'comment_id', Integer, ForeignKey('comment.id'), primary_key=True)
|
||||
user_id = Column(
|
||||
'user_id', Integer, ForeignKey('user.id'), primary_key=True, index=True)
|
||||
'user_id',
|
||||
Integer,
|
||||
ForeignKey('user.id'),
|
||||
primary_key=True,
|
||||
index=True)
|
||||
time = Column('time', DateTime, nullable=False)
|
||||
score = Column('score', Integer, nullable=False)
|
||||
|
||||
|
@ -18,14 +23,14 @@ class CommentScore(Base):
|
|||
'User',
|
||||
backref=backref('comment_scores', cascade='all, delete-orphan'))
|
||||
|
||||
|
||||
class Comment(Base):
|
||||
__tablename__ = 'comment'
|
||||
|
||||
comment_id = Column('id', Integer, primary_key=True)
|
||||
post_id = Column(
|
||||
'post_id', Integer, ForeignKey('post.id'), index=True, nullable=False)
|
||||
user_id = Column(
|
||||
'user_id', Integer, ForeignKey('user.id'), index=True)
|
||||
user_id = Column('user_id', Integer, ForeignKey('user.id'), index=True)
|
||||
version = Column('version', Integer, default=1, nullable=False)
|
||||
creation_time = Column('creation_time', DateTime, nullable=False)
|
||||
last_edit_time = Column('last_edit_time', DateTime)
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
from sqlalchemy.sql.expression import func, select
|
||||
from sqlalchemy import (
|
||||
Column, Integer, DateTime, Unicode, UnicodeText, PickleType, ForeignKey)
|
||||
from sqlalchemy.orm import relationship, column_property, object_session, backref
|
||||
from sqlalchemy.sql.expression import func, select
|
||||
from sqlalchemy.orm import (
|
||||
relationship, column_property, object_session, backref)
|
||||
from szurubooru.db.base import Base
|
||||
from szurubooru.db.comment import Comment
|
||||
|
||||
|
||||
class PostFeature(Base):
|
||||
__tablename__ = 'post_feature'
|
||||
|
||||
|
@ -20,13 +22,22 @@ class PostFeature(Base):
|
|||
'User',
|
||||
backref=backref('post_features', cascade='all, delete-orphan'))
|
||||
|
||||
|
||||
class PostScore(Base):
|
||||
__tablename__ = 'post_score'
|
||||
|
||||
post_id = Column(
|
||||
'post_id', Integer, ForeignKey('post.id'), primary_key=True, index=True)
|
||||
'post_id',
|
||||
Integer,
|
||||
ForeignKey('post.id'),
|
||||
primary_key=True,
|
||||
index=True)
|
||||
user_id = Column(
|
||||
'user_id', Integer, ForeignKey('user.id'), primary_key=True, index=True)
|
||||
'user_id',
|
||||
Integer,
|
||||
ForeignKey('user.id'),
|
||||
primary_key=True,
|
||||
index=True)
|
||||
time = Column('time', DateTime, nullable=False)
|
||||
score = Column('score', Integer, nullable=False)
|
||||
|
||||
|
@ -35,13 +46,22 @@ class PostScore(Base):
|
|||
'User',
|
||||
backref=backref('post_scores', cascade='all, delete-orphan'))
|
||||
|
||||
|
||||
class PostFavorite(Base):
|
||||
__tablename__ = 'post_favorite'
|
||||
|
||||
post_id = Column(
|
||||
'post_id', Integer, ForeignKey('post.id'), primary_key=True, index=True)
|
||||
'post_id',
|
||||
Integer,
|
||||
ForeignKey('post.id'),
|
||||
primary_key=True,
|
||||
index=True)
|
||||
user_id = Column(
|
||||
'user_id', Integer, ForeignKey('user.id'), primary_key=True, index=True)
|
||||
'user_id',
|
||||
Integer,
|
||||
ForeignKey('user.id'),
|
||||
primary_key=True,
|
||||
index=True)
|
||||
time = Column('time', DateTime, nullable=False)
|
||||
|
||||
post = relationship('Post')
|
||||
|
@ -49,6 +69,7 @@ class PostFavorite(Base):
|
|||
'User',
|
||||
backref=backref('post_favorites', cascade='all, delete-orphan'))
|
||||
|
||||
|
||||
class PostNote(Base):
|
||||
__tablename__ = 'post_note'
|
||||
|
||||
|
@ -60,23 +81,37 @@ class PostNote(Base):
|
|||
|
||||
post = relationship('Post')
|
||||
|
||||
|
||||
class PostRelation(Base):
|
||||
__tablename__ = 'post_relation'
|
||||
|
||||
parent_id = Column(
|
||||
'parent_id', Integer, ForeignKey('post.id'), primary_key=True, index=True)
|
||||
'parent_id',
|
||||
Integer,
|
||||
ForeignKey('post.id'),
|
||||
primary_key=True,
|
||||
index=True)
|
||||
child_id = Column(
|
||||
'child_id', Integer, ForeignKey('post.id'), primary_key=True, index=True)
|
||||
'child_id',
|
||||
Integer,
|
||||
ForeignKey('post.id'),
|
||||
primary_key=True,
|
||||
index=True)
|
||||
|
||||
def __init__(self, parent_id, child_id):
|
||||
self.parent_id = parent_id
|
||||
self.child_id = child_id
|
||||
|
||||
|
||||
class PostTag(Base):
|
||||
__tablename__ = 'post_tag'
|
||||
|
||||
post_id = Column(
|
||||
'post_id', Integer, ForeignKey('post.id'), primary_key=True, index=True)
|
||||
'post_id',
|
||||
Integer,
|
||||
ForeignKey('post.id'),
|
||||
primary_key=True,
|
||||
index=True)
|
||||
tag_id = Column(
|
||||
'tag_id', Integer, ForeignKey('tag.id'), primary_key=True, index=True)
|
||||
|
||||
|
@ -84,6 +119,7 @@ class PostTag(Base):
|
|||
self.post_id = post_id
|
||||
self.tag_id = tag_id
|
||||
|
||||
|
||||
class Post(Base):
|
||||
__tablename__ = 'post'
|
||||
|
||||
|
@ -136,8 +172,8 @@ class Post(Base):
|
|||
|
||||
# dynamic columns
|
||||
tag_count = column_property(
|
||||
select([func.count(PostTag.tag_id)]) \
|
||||
.where(PostTag.post_id == post_id) \
|
||||
select([func.count(PostTag.tag_id)])
|
||||
.where(PostTag.post_id == post_id)
|
||||
.correlate_except(PostTag))
|
||||
|
||||
canvas_area = column_property(canvas_width * canvas_height)
|
||||
|
@ -151,53 +187,53 @@ class Post(Base):
|
|||
return featured_post and featured_post.post_id == self.post_id
|
||||
|
||||
score = column_property(
|
||||
select([func.coalesce(func.sum(PostScore.score), 0)]) \
|
||||
.where(PostScore.post_id == post_id) \
|
||||
select([func.coalesce(func.sum(PostScore.score), 0)])
|
||||
.where(PostScore.post_id == post_id)
|
||||
.correlate_except(PostScore))
|
||||
|
||||
favorite_count = column_property(
|
||||
select([func.count(PostFavorite.post_id)]) \
|
||||
.where(PostFavorite.post_id == post_id) \
|
||||
select([func.count(PostFavorite.post_id)])
|
||||
.where(PostFavorite.post_id == post_id)
|
||||
.correlate_except(PostFavorite))
|
||||
|
||||
last_favorite_time = column_property(
|
||||
select([func.max(PostFavorite.time)]) \
|
||||
.where(PostFavorite.post_id == post_id) \
|
||||
select([func.max(PostFavorite.time)])
|
||||
.where(PostFavorite.post_id == post_id)
|
||||
.correlate_except(PostFavorite))
|
||||
|
||||
feature_count = column_property(
|
||||
select([func.count(PostFeature.post_id)]) \
|
||||
.where(PostFeature.post_id == post_id) \
|
||||
select([func.count(PostFeature.post_id)])
|
||||
.where(PostFeature.post_id == post_id)
|
||||
.correlate_except(PostFeature))
|
||||
|
||||
last_feature_time = column_property(
|
||||
select([func.max(PostFeature.time)]) \
|
||||
.where(PostFeature.post_id == post_id) \
|
||||
select([func.max(PostFeature.time)])
|
||||
.where(PostFeature.post_id == post_id)
|
||||
.correlate_except(PostFeature))
|
||||
|
||||
comment_count = column_property(
|
||||
select([func.count(Comment.post_id)]) \
|
||||
.where(Comment.post_id == post_id) \
|
||||
select([func.count(Comment.post_id)])
|
||||
.where(Comment.post_id == post_id)
|
||||
.correlate_except(Comment))
|
||||
|
||||
last_comment_creation_time = column_property(
|
||||
select([func.max(Comment.creation_time)]) \
|
||||
.where(Comment.post_id == post_id) \
|
||||
select([func.max(Comment.creation_time)])
|
||||
.where(Comment.post_id == post_id)
|
||||
.correlate_except(Comment))
|
||||
|
||||
last_comment_edit_time = column_property(
|
||||
select([func.max(Comment.last_edit_time)]) \
|
||||
.where(Comment.post_id == post_id) \
|
||||
select([func.max(Comment.last_edit_time)])
|
||||
.where(Comment.post_id == post_id)
|
||||
.correlate_except(Comment))
|
||||
|
||||
note_count = column_property(
|
||||
select([func.count(PostNote.post_id)]) \
|
||||
.where(PostNote.post_id == post_id) \
|
||||
select([func.count(PostNote.post_id)])
|
||||
.where(PostNote.post_id == post_id)
|
||||
.correlate_except(PostNote))
|
||||
|
||||
relation_count = column_property(
|
||||
select([func.count(PostRelation.child_id)]) \
|
||||
select([func.count(PostRelation.child_id)])
|
||||
.where(
|
||||
(PostRelation.parent_id == post_id) \
|
||||
| (PostRelation.child_id == post_id)) \
|
||||
(PostRelation.parent_id == post_id)
|
||||
| (PostRelation.child_id == post_id))
|
||||
.correlate_except(PostRelation))
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import sqlalchemy
|
||||
from szurubooru import config
|
||||
|
||||
|
||||
class QueryCounter(object):
|
||||
_query_count = 0
|
||||
|
||||
|
@ -16,6 +17,7 @@ class QueryCounter(object):
|
|||
def get():
|
||||
return QueryCounter._query_count
|
||||
|
||||
|
||||
def create_session():
|
||||
_engine = sqlalchemy.create_engine(
|
||||
'{schema}://{user}:{password}@{host}:{port}/{name}'.format(
|
||||
|
@ -30,6 +32,7 @@ def create_session():
|
|||
_session_maker = sqlalchemy.orm.sessionmaker(bind=_engine)
|
||||
return sqlalchemy.orm.scoped_session(_session_maker)
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
session = create_session()
|
||||
reset_query_count = QueryCounter.reset
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
from sqlalchemy import Column, Integer, DateTime, Unicode, PickleType, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy import (
|
||||
Column, Integer, DateTime, Unicode, PickleType, ForeignKey)
|
||||
from szurubooru.db.base import Base
|
||||
|
||||
|
||||
class Snapshot(Base):
|
||||
__tablename__ = 'snapshot'
|
||||
|
||||
|
@ -11,7 +13,8 @@ class Snapshot(Base):
|
|||
|
||||
snapshot_id = Column('id', Integer, primary_key=True)
|
||||
creation_time = Column('creation_time', DateTime, nullable=False)
|
||||
resource_type = Column('resource_type', Unicode(32), nullable=False, index=True)
|
||||
resource_type = Column(
|
||||
'resource_type', Unicode(32), nullable=False, index=True)
|
||||
resource_id = Column('resource_id', Integer, nullable=False, index=True)
|
||||
resource_repr = Column('resource_repr', Unicode(64), nullable=False)
|
||||
operation = Column('operation', Unicode(16), nullable=False)
|
||||
|
|
|
@ -1,49 +1,73 @@
|
|||
from sqlalchemy import Column, Integer, DateTime, Unicode, UnicodeText, ForeignKey
|
||||
from sqlalchemy import (
|
||||
Column, Integer, DateTime, Unicode, UnicodeText, ForeignKey)
|
||||
from sqlalchemy.orm import relationship, column_property
|
||||
from sqlalchemy.sql.expression import func, select
|
||||
from szurubooru.db.base import Base
|
||||
from szurubooru.db.post import PostTag
|
||||
|
||||
|
||||
class TagSuggestion(Base):
|
||||
__tablename__ = 'tag_suggestion'
|
||||
|
||||
parent_id = Column(
|
||||
'parent_id', Integer, ForeignKey('tag.id'), primary_key=True, index=True)
|
||||
'parent_id',
|
||||
Integer,
|
||||
ForeignKey('tag.id'),
|
||||
primary_key=True, index=True)
|
||||
child_id = Column(
|
||||
'child_id', Integer, ForeignKey('tag.id'), primary_key=True, index=True)
|
||||
'child_id',
|
||||
Integer,
|
||||
ForeignKey('tag.id'),
|
||||
primary_key=True, index=True)
|
||||
|
||||
def __init__(self, parent_id, child_id):
|
||||
self.parent_id = parent_id
|
||||
self.child_id = child_id
|
||||
|
||||
|
||||
class TagImplication(Base):
|
||||
__tablename__ = 'tag_implication'
|
||||
|
||||
parent_id = Column(
|
||||
'parent_id', Integer, ForeignKey('tag.id'), primary_key=True, index=True)
|
||||
'parent_id',
|
||||
Integer,
|
||||
ForeignKey('tag.id'),
|
||||
primary_key=True,
|
||||
index=True)
|
||||
child_id = Column(
|
||||
'child_id', Integer, ForeignKey('tag.id'), primary_key=True, index=True)
|
||||
'child_id',
|
||||
Integer,
|
||||
ForeignKey('tag.id'),
|
||||
primary_key=True,
|
||||
index=True)
|
||||
|
||||
def __init__(self, parent_id, child_id):
|
||||
self.parent_id = parent_id
|
||||
self.child_id = child_id
|
||||
|
||||
|
||||
class TagName(Base):
|
||||
__tablename__ = 'tag_name'
|
||||
|
||||
tag_name_id = Column('tag_name_id', Integer, primary_key=True)
|
||||
tag_id = Column('tag_id', Integer, ForeignKey('tag.id'), nullable=False, index=True)
|
||||
tag_id = Column(
|
||||
'tag_id', Integer, ForeignKey('tag.id'), nullable=False, index=True)
|
||||
name = Column('name', Unicode(64), nullable=False, unique=True)
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
||||
|
||||
class Tag(Base):
|
||||
__tablename__ = 'tag'
|
||||
|
||||
tag_id = Column('id', Integer, primary_key=True)
|
||||
category_id = Column(
|
||||
'category_id', Integer, ForeignKey('tag_category.id'), nullable=False, index=True)
|
||||
'category_id',
|
||||
Integer,
|
||||
ForeignKey('tag_category.id'),
|
||||
nullable=False,
|
||||
index=True)
|
||||
version = Column('version', Integer, default=1, nullable=False)
|
||||
creation_time = Column('creation_time', DateTime, nullable=False)
|
||||
last_edit_time = Column('last_edit_time', DateTime)
|
||||
|
@ -69,25 +93,25 @@ class Tag(Base):
|
|||
lazy='joined')
|
||||
|
||||
post_count = column_property(
|
||||
select([func.count(PostTag.post_id)]) \
|
||||
.where(PostTag.tag_id == tag_id) \
|
||||
select([func.count(PostTag.post_id)])
|
||||
.where(PostTag.tag_id == tag_id)
|
||||
.correlate_except(PostTag))
|
||||
|
||||
first_name = column_property(
|
||||
select([TagName.name]) \
|
||||
.where(TagName.tag_id == tag_id) \
|
||||
.limit(1) \
|
||||
select([TagName.name])
|
||||
.where(TagName.tag_id == tag_id)
|
||||
.limit(1)
|
||||
.as_scalar(),
|
||||
deferred=True)
|
||||
|
||||
suggestion_count = column_property(
|
||||
select([func.count(TagSuggestion.child_id)]) \
|
||||
.where(TagSuggestion.parent_id == tag_id) \
|
||||
select([func.count(TagSuggestion.child_id)])
|
||||
.where(TagSuggestion.parent_id == tag_id)
|
||||
.as_scalar(),
|
||||
deferred=True)
|
||||
|
||||
implication_count = column_property(
|
||||
select([func.count(TagImplication.child_id)]) \
|
||||
.where(TagImplication.parent_id == tag_id) \
|
||||
select([func.count(TagImplication.child_id)])
|
||||
.where(TagImplication.parent_id == tag_id)
|
||||
.as_scalar(),
|
||||
deferred=True)
|
||||
|
|
|
@ -4,6 +4,7 @@ from sqlalchemy.sql.expression import func, select
|
|||
from szurubooru.db.base import Base
|
||||
from szurubooru.db.tag import Tag
|
||||
|
||||
|
||||
class TagCategory(Base):
|
||||
__tablename__ = 'tag_category'
|
||||
|
||||
|
@ -17,6 +18,6 @@ class TagCategory(Base):
|
|||
self.name = name
|
||||
|
||||
tag_count = column_property(
|
||||
select([func.count('Tag.tag_id')]) \
|
||||
.where(Tag.category_id == tag_category_id) \
|
||||
select([func.count('Tag.tag_id')])
|
||||
.where(Tag.category_id == tag_category_id)
|
||||
.correlate_except(table('Tag')))
|
||||
|
|
|
@ -5,6 +5,7 @@ from szurubooru.db.base import Base
|
|||
from szurubooru.db.post import Post, PostScore, PostFavorite
|
||||
from szurubooru.db.comment import Comment
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = 'user'
|
||||
|
||||
|
@ -17,7 +18,7 @@ class User(Base):
|
|||
RANK_POWER = 'power'
|
||||
RANK_MODERATOR = 'moderator'
|
||||
RANK_ADMINISTRATOR = 'administrator'
|
||||
RANK_NOBODY = 'nobody' # used for privileges: "nobody can be higher than admin"
|
||||
RANK_NOBODY = 'nobody' # unattainable, used for privileges
|
||||
|
||||
user_id = Column('id', Integer, primary_key=True)
|
||||
creation_time = Column('creation_time', DateTime, nullable=False)
|
||||
|
@ -36,41 +37,41 @@ class User(Base):
|
|||
@property
|
||||
def post_count(self):
|
||||
from szurubooru.db import session
|
||||
return session \
|
||||
.query(func.sum(1)) \
|
||||
.filter(Post.user_id == self.user_id) \
|
||||
.one()[0] or 0
|
||||
return (session
|
||||
.query(func.sum(1))
|
||||
.filter(Post.user_id == self.user_id)
|
||||
.one()[0] or 0)
|
||||
|
||||
@property
|
||||
def comment_count(self):
|
||||
from szurubooru.db import session
|
||||
return session \
|
||||
.query(func.sum(1)) \
|
||||
.filter(Comment.user_id == self.user_id) \
|
||||
.one()[0] or 0
|
||||
return (session
|
||||
.query(func.sum(1))
|
||||
.filter(Comment.user_id == self.user_id)
|
||||
.one()[0] or 0)
|
||||
|
||||
@property
|
||||
def favorite_post_count(self):
|
||||
from szurubooru.db import session
|
||||
return session \
|
||||
.query(func.sum(1)) \
|
||||
.filter(PostFavorite.user_id == self.user_id) \
|
||||
.one()[0] or 0
|
||||
return (session
|
||||
.query(func.sum(1))
|
||||
.filter(PostFavorite.user_id == self.user_id)
|
||||
.one()[0] or 0)
|
||||
|
||||
@property
|
||||
def liked_post_count(self):
|
||||
from szurubooru.db import session
|
||||
return session \
|
||||
.query(func.sum(1)) \
|
||||
.filter(PostScore.user_id == self.user_id) \
|
||||
.filter(PostScore.score == 1) \
|
||||
.one()[0] or 0
|
||||
return (session
|
||||
.query(func.sum(1))
|
||||
.filter(PostScore.user_id == self.user_id)
|
||||
.filter(PostScore.score == 1)
|
||||
.one()[0] or 0)
|
||||
|
||||
@property
|
||||
def disliked_post_count(self):
|
||||
from szurubooru.db import session
|
||||
return session \
|
||||
.query(func.sum(1)) \
|
||||
.filter(PostScore.user_id == self.user_id) \
|
||||
.filter(PostScore.score == -1) \
|
||||
.one()[0] or 0
|
||||
return (session
|
||||
.query(func.sum(1))
|
||||
.filter(PostScore.user_id == self.user_id)
|
||||
.filter(PostScore.score == -1)
|
||||
.one()[0] or 0)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from sqlalchemy.inspection import inspect
|
||||
|
||||
|
||||
def get_resource_info(entity):
|
||||
serializers = {
|
||||
'tag': lambda tag: tag.first_name,
|
||||
|
@ -23,6 +24,7 @@ def get_resource_info(entity):
|
|||
|
||||
return (resource_type, resource_id, resource_repr)
|
||||
|
||||
|
||||
def get_aux_entity(session, get_table_info, entity, user):
|
||||
table, get_column = get_table_info(entity)
|
||||
return session \
|
||||
|
|
|
@ -1,11 +1,38 @@
|
|||
class ConfigError(RuntimeError): pass
|
||||
class AuthError(RuntimeError): pass
|
||||
class IntegrityError(RuntimeError): pass
|
||||
class ValidationError(RuntimeError): pass
|
||||
class SearchError(RuntimeError): pass
|
||||
class NotFoundError(RuntimeError): pass
|
||||
class ProcessingError(RuntimeError): pass
|
||||
class ConfigError(RuntimeError):
|
||||
pass
|
||||
|
||||
class MissingRequiredFileError(ValidationError): pass
|
||||
class MissingRequiredParameterError(ValidationError): pass
|
||||
class InvalidParameterError(ValidationError): pass
|
||||
|
||||
class AuthError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class IntegrityError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class ValidationError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class SearchError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class NotFoundError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class ProcessingError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class MissingRequiredFileError(ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class MissingRequiredParameterError(ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidParameterError(ValidationError):
|
||||
pass
|
||||
|
|
|
@ -7,30 +7,37 @@ from szurubooru import config, errors, rest
|
|||
# pylint: disable=unused-import
|
||||
from szurubooru import api, middleware
|
||||
|
||||
|
||||
def _on_auth_error(ex):
|
||||
raise rest.errors.HttpForbidden(
|
||||
title='Authentication error', description=str(ex))
|
||||
|
||||
|
||||
def _on_validation_error(ex):
|
||||
raise rest.errors.HttpBadRequest(
|
||||
title='Validation error', description=str(ex))
|
||||
|
||||
|
||||
def _on_search_error(ex):
|
||||
raise rest.errors.HttpBadRequest(
|
||||
title='Search error', description=str(ex))
|
||||
|
||||
|
||||
def _on_integrity_error(ex):
|
||||
raise rest.errors.HttpConflict(
|
||||
title='Integrity violation', description=ex.args[0])
|
||||
|
||||
|
||||
def _on_not_found_error(ex):
|
||||
raise rest.errors.HttpNotFound(
|
||||
title='Not found', description=str(ex))
|
||||
|
||||
|
||||
def _on_processing_error(ex):
|
||||
raise rest.errors.HttpBadRequest(
|
||||
title='Processing error', description=str(ex))
|
||||
|
||||
|
||||
def validate_config():
|
||||
'''
|
||||
Check whether config doesn't contain errors that might prove
|
||||
|
@ -60,6 +67,7 @@ def validate_config():
|
|||
raise errors.ConfigError(
|
||||
'Database is not configured: %r is missing' % key)
|
||||
|
||||
|
||||
def create_app():
|
||||
''' Create a WSGI compatible App object. '''
|
||||
validate_config()
|
||||
|
|
|
@ -4,6 +4,7 @@ from collections import OrderedDict
|
|||
from szurubooru import config, db, errors
|
||||
from szurubooru.func import util
|
||||
|
||||
|
||||
RANK_MAP = OrderedDict([
|
||||
(db.User.RANK_ANONYMOUS, 'anonymous'),
|
||||
(db.User.RANK_RESTRICTED, 'restricted'),
|
||||
|
@ -14,6 +15,7 @@ RANK_MAP = OrderedDict([
|
|||
(db.User.RANK_NOBODY, 'nobody'),
|
||||
])
|
||||
|
||||
|
||||
def get_password_hash(salt, password):
|
||||
''' Retrieve new-style password hash. '''
|
||||
digest = hashlib.sha256()
|
||||
|
@ -22,6 +24,7 @@ def get_password_hash(salt, password):
|
|||
digest.update(password.encode('utf8'))
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def get_legacy_password_hash(salt, password):
|
||||
''' Retrieve old-style password hash. '''
|
||||
digest = hashlib.sha1()
|
||||
|
@ -30,6 +33,7 @@ def get_legacy_password_hash(salt, password):
|
|||
digest.update(password.encode('utf8'))
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def create_password():
|
||||
alphabet = {
|
||||
'c': list('bcdfghijklmnpqrstvwxyz'),
|
||||
|
@ -39,6 +43,7 @@ def create_password():
|
|||
pattern = 'cvcvnncvcv'
|
||||
return ''.join(random.choice(alphabet[l]) for l in list(pattern))
|
||||
|
||||
|
||||
def is_valid_password(user, password):
|
||||
assert user
|
||||
salt, valid_hash = user.password_salt, user.password_hash
|
||||
|
@ -48,6 +53,7 @@ def is_valid_password(user, password):
|
|||
]
|
||||
return valid_hash in possible_hashes
|
||||
|
||||
|
||||
def has_privilege(user, privilege_name):
|
||||
assert user
|
||||
all_ranks = list(RANK_MAP.keys())
|
||||
|
@ -58,11 +64,13 @@ def has_privilege(user, privilege_name):
|
|||
good_ranks = all_ranks[all_ranks.index(minimal_rank):]
|
||||
return user.rank in good_ranks
|
||||
|
||||
|
||||
def verify_privilege(user, privilege_name):
|
||||
assert user
|
||||
if not has_privilege(user, privilege_name):
|
||||
raise errors.AuthError('Insufficient privileges to do this.')
|
||||
|
||||
|
||||
def generate_authentication_token(user):
|
||||
''' Generate nonguessable challenge (e.g. links in password reminder). '''
|
||||
assert user
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
from datetime import datetime
|
||||
|
||||
|
||||
class LruCacheItem(object):
|
||||
def __init__(self, key, value):
|
||||
self.key = key
|
||||
self.value = value
|
||||
self.timestamp = datetime.utcnow()
|
||||
|
||||
|
||||
class LruCache(object):
|
||||
def __init__(self, length, delta=None):
|
||||
self.length = length
|
||||
|
@ -15,8 +17,9 @@ class LruCache(object):
|
|||
|
||||
def insert_item(self, item):
|
||||
if item.key in self.hash:
|
||||
item_index = next(i \
|
||||
for i, v in enumerate(self.item_list) \
|
||||
item_index = next(
|
||||
i
|
||||
for i, v in enumerate(self.item_list)
|
||||
if v.key == item.key)
|
||||
self.item_list[:] \
|
||||
= self.item_list[:item_index] \
|
||||
|
@ -36,16 +39,21 @@ class LruCache(object):
|
|||
del self.hash[item.key]
|
||||
del self.item_list[self.item_list.index(item)]
|
||||
|
||||
|
||||
_CACHE = LruCache(length=100)
|
||||
|
||||
|
||||
def purge():
|
||||
_CACHE.remove_all()
|
||||
|
||||
|
||||
def has(key):
|
||||
return key in _CACHE.hash
|
||||
|
||||
|
||||
def get(key):
|
||||
return _CACHE.hash[key].value
|
||||
|
||||
|
||||
def put(key, value):
|
||||
_CACHE.insert_item(LruCacheItem(key, value))
|
||||
|
|
|
@ -2,16 +2,26 @@ import datetime
|
|||
from szurubooru import db, errors
|
||||
from szurubooru.func import users, scores, util
|
||||
|
||||
class InvalidCommentIdError(errors.ValidationError): pass
|
||||
class CommentNotFoundError(errors.NotFoundError): pass
|
||||
class EmptyCommentTextError(errors.ValidationError): pass
|
||||
|
||||
class InvalidCommentIdError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class CommentNotFoundError(errors.NotFoundError):
|
||||
pass
|
||||
|
||||
|
||||
class EmptyCommentTextError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
def serialize_comment(comment, auth_user, options=None):
|
||||
return util.serialize_entity(
|
||||
comment,
|
||||
{
|
||||
'id': lambda: comment.comment_id,
|
||||
'user': lambda: users.serialize_micro_user(comment.user, auth_user),
|
||||
'user':
|
||||
lambda: users.serialize_micro_user(comment.user, auth_user),
|
||||
'postId': lambda: comment.post.post_id,
|
||||
'version': lambda: comment.version,
|
||||
'text': lambda: comment.text,
|
||||
|
@ -22,6 +32,7 @@ def serialize_comment(comment, auth_user, options=None):
|
|||
},
|
||||
options)
|
||||
|
||||
|
||||
def try_get_comment_by_id(comment_id):
|
||||
try:
|
||||
comment_id = int(comment_id)
|
||||
|
@ -32,12 +43,14 @@ def try_get_comment_by_id(comment_id):
|
|||
.filter(db.Comment.comment_id == comment_id) \
|
||||
.one_or_none()
|
||||
|
||||
|
||||
def get_comment_by_id(comment_id):
|
||||
comment = try_get_comment_by_id(comment_id)
|
||||
if comment:
|
||||
return comment
|
||||
raise CommentNotFoundError('Comment %r not found.' % comment_id)
|
||||
|
||||
|
||||
def create_comment(user, post, text):
|
||||
comment = db.Comment()
|
||||
comment.user = user
|
||||
|
@ -46,6 +59,7 @@ def create_comment(user, post, text):
|
|||
comment.creation_time = datetime.datetime.utcnow()
|
||||
return comment
|
||||
|
||||
|
||||
def update_comment_text(comment, text):
|
||||
assert comment
|
||||
if not text:
|
||||
|
|
|
@ -2,7 +2,10 @@ import datetime
|
|||
from szurubooru import db, errors
|
||||
from szurubooru.func import scores
|
||||
|
||||
class InvalidFavoriteTargetError(errors.ValidationError): pass
|
||||
|
||||
class InvalidFavoriteTargetError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
def _get_table_info(entity):
|
||||
assert entity
|
||||
|
@ -11,16 +14,19 @@ def _get_table_info(entity):
|
|||
return db.PostFavorite, lambda table: table.post_id
|
||||
raise InvalidFavoriteTargetError()
|
||||
|
||||
|
||||
def _get_fav_entity(entity, user):
|
||||
assert entity
|
||||
assert user
|
||||
return db.util.get_aux_entity(db.session, _get_table_info, entity, user)
|
||||
|
||||
|
||||
def has_favorited(entity, user):
|
||||
assert entity
|
||||
assert user
|
||||
return _get_fav_entity(entity, user) is not None
|
||||
|
||||
|
||||
def unset_favorite(entity, user):
|
||||
assert entity
|
||||
assert user
|
||||
|
@ -28,6 +34,7 @@ def unset_favorite(entity, user):
|
|||
if fav_entity:
|
||||
db.session.delete(fav_entity)
|
||||
|
||||
|
||||
def set_favorite(entity, user):
|
||||
assert entity
|
||||
assert user
|
||||
|
|
|
@ -1,20 +1,25 @@
|
|||
import os
|
||||
from szurubooru import config
|
||||
|
||||
|
||||
def _get_full_path(path):
|
||||
return os.path.join(config.config['data_dir'], path)
|
||||
|
||||
|
||||
def delete(path):
|
||||
full_path = _get_full_path(path)
|
||||
if os.path.exists(full_path):
|
||||
os.unlink(full_path)
|
||||
|
||||
|
||||
def has(path):
|
||||
return os.path.exists(_get_full_path(path))
|
||||
|
||||
|
||||
def move(source_path, target_path):
|
||||
return os.rename(_get_full_path(source_path), _get_full_path(target_path))
|
||||
|
||||
|
||||
def get(path):
|
||||
full_path = _get_full_path(path)
|
||||
if not os.path.exists(full_path):
|
||||
|
@ -22,6 +27,7 @@ def get(path):
|
|||
with open(full_path, 'rb') as handle:
|
||||
return handle.read()
|
||||
|
||||
|
||||
def save(path, content):
|
||||
full_path = _get_full_path(path)
|
||||
os.makedirs(os.path.dirname(full_path), exist_ok=True)
|
||||
|
|
|
@ -6,11 +6,14 @@ import math
|
|||
from szurubooru import errors
|
||||
from szurubooru.func import mime, util
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
_SCALE_FIT_FMT = \
|
||||
r'scale=iw*max({width}/iw\,{height}/ih):ih*max({width}/iw\,{height}/ih)'
|
||||
|
||||
|
||||
class Image(object):
|
||||
def __init__(self, content):
|
||||
self.content = content
|
||||
|
@ -38,11 +41,12 @@ class Image(object):
|
|||
'-',
|
||||
]
|
||||
if 'duration' in self.info['format'] \
|
||||
and float(self.info['format']['duration']) > 3 \
|
||||
and self.info['format']['format_name'] != 'swf':
|
||||
duration = float(self.info['format']['duration'])
|
||||
if duration > 3:
|
||||
cli = [
|
||||
'-ss',
|
||||
'%d' % math.floor(float(self.info['format']['duration']) * 0.3),
|
||||
'%d' % math.floor(duration * 0.3),
|
||||
] + cli
|
||||
self.content = self._execute(cli)
|
||||
assert self.content
|
||||
|
|
|
@ -2,6 +2,7 @@ import smtplib
|
|||
import email.mime.text
|
||||
from szurubooru import config
|
||||
|
||||
|
||||
def send_mail(sender, recipient, subject, body):
|
||||
msg = email.mime.text.MIMEText(body)
|
||||
msg['Subject'] = subject
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import re
|
||||
|
||||
# pylint: disable=too-many-return-statements
|
||||
|
||||
def get_mime_type(content):
|
||||
if not content:
|
||||
return 'application/octet-stream'
|
||||
|
@ -25,6 +25,7 @@ def get_mime_type(content):
|
|||
|
||||
return 'application/octet-stream'
|
||||
|
||||
|
||||
def get_extension(mime_type):
|
||||
extension_map = {
|
||||
'application/x-shockwave-flash': 'swf',
|
||||
|
@ -37,15 +38,19 @@ def get_extension(mime_type):
|
|||
}
|
||||
return extension_map.get((mime_type or '').strip().lower(), None)
|
||||
|
||||
|
||||
def is_flash(mime_type):
|
||||
return mime_type.lower() == 'application/x-shockwave-flash'
|
||||
|
||||
|
||||
def is_video(mime_type):
|
||||
return mime_type.lower() in ('application/ogg', 'video/mp4', 'video/webm')
|
||||
|
||||
|
||||
def is_image(mime_type):
|
||||
return mime_type.lower() in ('image/jpeg', 'image/png', 'image/gif')
|
||||
|
||||
|
||||
def is_animated_gif(content):
|
||||
return get_mime_type(content) == 'image/gif' \
|
||||
and len(re.findall(b'\x21\xF9\x04.{4}\x00[\x2C\x21]', content)) > 1
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import urllib.request
|
||||
|
||||
|
||||
def download(url):
|
||||
assert url
|
||||
request = urllib.request.Request(url)
|
||||
|
|
|
@ -4,37 +4,71 @@ from szurubooru import config, db, errors
|
|||
from szurubooru.func import (
|
||||
users, snapshots, scores, comments, tags, util, mime, images, files)
|
||||
|
||||
|
||||
EMPTY_PIXEL = \
|
||||
b'\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00' \
|
||||
b'\xff\xff\xff\x21\xf9\x04\x01\x00\x00\x01\x00\x2c\x00\x00\x00\x00' \
|
||||
b'\x01\x00\x01\x00\x00\x02\x02\x4c\x01\x00\x3b'
|
||||
|
||||
class PostNotFoundError(errors.NotFoundError): pass
|
||||
class PostAlreadyFeaturedError(errors.ValidationError): pass
|
||||
class PostAlreadyUploadedError(errors.ValidationError): pass
|
||||
class InvalidPostIdError(errors.ValidationError): pass
|
||||
class InvalidPostSafetyError(errors.ValidationError): pass
|
||||
class InvalidPostSourceError(errors.ValidationError): pass
|
||||
class InvalidPostContentError(errors.ValidationError): pass
|
||||
class InvalidPostRelationError(errors.ValidationError): pass
|
||||
class InvalidPostNoteError(errors.ValidationError): pass
|
||||
class InvalidPostFlagError(errors.ValidationError): pass
|
||||
|
||||
class PostNotFoundError(errors.NotFoundError):
|
||||
pass
|
||||
|
||||
|
||||
class PostAlreadyFeaturedError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class PostAlreadyUploadedError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidPostIdError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidPostSafetyError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidPostSourceError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidPostContentError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidPostRelationError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidPostNoteError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidPostFlagError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
SAFETY_MAP = {
|
||||
db.Post.SAFETY_SAFE: 'safe',
|
||||
db.Post.SAFETY_SKETCHY: 'sketchy',
|
||||
db.Post.SAFETY_UNSAFE: 'unsafe',
|
||||
}
|
||||
|
||||
TYPE_MAP = {
|
||||
db.Post.TYPE_IMAGE: 'image',
|
||||
db.Post.TYPE_ANIMATION: 'animation',
|
||||
db.Post.TYPE_VIDEO: 'video',
|
||||
db.Post.TYPE_FLASH: 'flash',
|
||||
}
|
||||
|
||||
FLAG_MAP = {
|
||||
db.Post.FLAG_LOOP: 'loop',
|
||||
}
|
||||
|
||||
|
||||
def get_post_content_url(post):
|
||||
assert post
|
||||
return '%s/posts/%d.%s' % (
|
||||
|
@ -42,25 +76,30 @@ def get_post_content_url(post):
|
|||
post.post_id,
|
||||
mime.get_extension(post.mime_type) or 'dat')
|
||||
|
||||
|
||||
def get_post_thumbnail_url(post):
|
||||
assert post
|
||||
return '%s/generated-thumbnails/%d.jpg' % (
|
||||
config.config['data_url'].rstrip('/'),
|
||||
post.post_id)
|
||||
|
||||
|
||||
def get_post_content_path(post):
|
||||
assert post
|
||||
return 'posts/%d.%s' % (
|
||||
post.post_id, mime.get_extension(post.mime_type) or 'dat')
|
||||
|
||||
|
||||
def get_post_thumbnail_path(post):
|
||||
assert post
|
||||
return 'generated-thumbnails/%d.jpg' % (post.post_id)
|
||||
|
||||
|
||||
def get_post_thumbnail_backup_path(post):
|
||||
assert post
|
||||
return 'posts/custom-thumbnails/%d.dat' % (post.post_id)
|
||||
|
||||
|
||||
def serialize_note(note):
|
||||
assert note
|
||||
return {
|
||||
|
@ -68,6 +107,7 @@ def serialize_note(note):
|
|||
'text': note.text,
|
||||
}
|
||||
|
||||
|
||||
def serialize_post(post, auth_user, options=None):
|
||||
return util.serialize_entity(
|
||||
post,
|
||||
|
@ -93,17 +133,17 @@ def serialize_post(post, auth_user, options=None):
|
|||
{
|
||||
post['id']:
|
||||
post for post in [
|
||||
serialize_micro_post(rel, auth_user) \
|
||||
for rel in post.relations
|
||||
]
|
||||
serialize_micro_post(rel, auth_user)
|
||||
for rel in post.relations]
|
||||
}.values(),
|
||||
key=lambda post: post['id']),
|
||||
'user': lambda: users.serialize_micro_user(post.user, auth_user),
|
||||
'score': lambda: post.score,
|
||||
'ownScore': lambda: scores.get_score(post, auth_user),
|
||||
'ownFavorite': lambda: len(
|
||||
[user for user in post.favorited_by \
|
||||
if user.user_id == auth_user.user_id]) > 0,
|
||||
'ownFavorite': lambda: len([
|
||||
user for user in post.favorited_by
|
||||
if user.user_id == auth_user.user_id]
|
||||
) > 0,
|
||||
'tagCount': lambda: post.tag_count,
|
||||
'favoriteCount': lambda: post.favorite_count,
|
||||
'commentCount': lambda: post.comment_count,
|
||||
|
@ -112,15 +152,16 @@ def serialize_post(post, auth_user, options=None):
|
|||
'featureCount': lambda: post.feature_count,
|
||||
'lastFeatureTime': lambda: post.last_feature_time,
|
||||
'favoritedBy': lambda: [
|
||||
users.serialize_micro_user(rel.user, auth_user) \
|
||||
for rel in post.favorited_by],
|
||||
users.serialize_micro_user(rel.user, auth_user)
|
||||
for rel in post.favorited_by
|
||||
],
|
||||
'hasCustomThumbnail':
|
||||
lambda: files.has(get_post_thumbnail_backup_path(post)),
|
||||
'notes': lambda: sorted(
|
||||
[serialize_note(note) for note in post.notes],
|
||||
key=lambda x: x['polygon']),
|
||||
'comments': lambda: [
|
||||
comments.serialize_comment(comment, auth_user) \
|
||||
comments.serialize_comment(comment, auth_user)
|
||||
for comment in sorted(
|
||||
post.comments,
|
||||
key=lambda comment: comment.creation_time)],
|
||||
|
@ -128,15 +169,18 @@ def serialize_post(post, auth_user, options=None):
|
|||
},
|
||||
options)
|
||||
|
||||
|
||||
def serialize_micro_post(post, auth_user):
|
||||
return serialize_post(
|
||||
post,
|
||||
auth_user=auth_user,
|
||||
options=['id', 'thumbnailUrl'])
|
||||
|
||||
|
||||
def get_post_count():
|
||||
return db.session.query(sqlalchemy.func.count(db.Post.post_id)).one()[0]
|
||||
|
||||
|
||||
def try_get_post_by_id(post_id):
|
||||
try:
|
||||
post_id = int(post_id)
|
||||
|
@ -147,22 +191,26 @@ def try_get_post_by_id(post_id):
|
|||
.filter(db.Post.post_id == post_id) \
|
||||
.one_or_none()
|
||||
|
||||
|
||||
def get_post_by_id(post_id):
|
||||
post = try_get_post_by_id(post_id)
|
||||
if not post:
|
||||
raise PostNotFoundError('Post %r not found.' % post_id)
|
||||
return post
|
||||
|
||||
|
||||
def try_get_current_post_feature():
|
||||
return db.session \
|
||||
.query(db.PostFeature) \
|
||||
.order_by(db.PostFeature.time.desc()) \
|
||||
.first()
|
||||
|
||||
|
||||
def try_get_featured_post():
|
||||
post_feature = try_get_current_post_feature()
|
||||
return post_feature.post if post_feature else None
|
||||
|
||||
|
||||
def create_post(content, tag_names, user):
|
||||
post = db.Post()
|
||||
post.safety = db.Post.SAFETY_SAFE
|
||||
|
@ -181,6 +229,7 @@ def create_post(content, tag_names, user):
|
|||
new_tags = update_post_tags(post, tag_names)
|
||||
return (post, new_tags)
|
||||
|
||||
|
||||
def update_post_safety(post, safety):
|
||||
assert post
|
||||
safety = util.flip(SAFETY_MAP).get(safety, None)
|
||||
|
@ -189,12 +238,14 @@ def update_post_safety(post, safety):
|
|||
'Safety can be either of %r.' % list(SAFETY_MAP.values()))
|
||||
post.safety = safety
|
||||
|
||||
|
||||
def update_post_source(post, source):
|
||||
assert post
|
||||
if util.value_exceeds_column_size(source, db.Post.source):
|
||||
raise InvalidPostSourceError('Source is too long.')
|
||||
post.source = source
|
||||
|
||||
|
||||
def update_post_content(post, content):
|
||||
assert post
|
||||
if not content:
|
||||
|
@ -210,7 +261,8 @@ def update_post_content(post, content):
|
|||
elif mime.is_video(post.mime_type):
|
||||
post.type = db.Post.TYPE_VIDEO
|
||||
else:
|
||||
raise InvalidPostContentError('Unhandled file type: %r' % post.mime_type)
|
||||
raise InvalidPostContentError(
|
||||
'Unhandled file type: %r' % post.mime_type)
|
||||
|
||||
post.checksum = util.get_md5(content)
|
||||
other_post = db.session \
|
||||
|
@ -236,6 +288,7 @@ def update_post_content(post, content):
|
|||
files.save(get_post_content_path(post), content)
|
||||
update_post_thumbnail(post, content=None, do_delete=False)
|
||||
|
||||
|
||||
def update_post_thumbnail(post, content=None, do_delete=True):
|
||||
assert post
|
||||
if not content:
|
||||
|
@ -246,6 +299,7 @@ def update_post_thumbnail(post, content=None, do_delete=True):
|
|||
files.save(get_post_thumbnail_backup_path(post), content)
|
||||
generate_post_thumbnail(post)
|
||||
|
||||
|
||||
def generate_post_thumbnail(post):
|
||||
assert post
|
||||
if files.has(get_post_thumbnail_backup_path(post)):
|
||||
|
@ -261,12 +315,14 @@ def generate_post_thumbnail(post):
|
|||
except errors.ProcessingError:
|
||||
files.save(get_post_thumbnail_path(post), EMPTY_PIXEL)
|
||||
|
||||
|
||||
def update_post_tags(post, tag_names):
|
||||
assert post
|
||||
existing_tags, new_tags = tags.get_or_create_tags_by_names(tag_names)
|
||||
post.tags = existing_tags + new_tags
|
||||
return new_tags
|
||||
|
||||
|
||||
def update_post_relations(post, new_post_ids):
|
||||
assert post
|
||||
old_posts = post.relations
|
||||
|
@ -287,6 +343,7 @@ def update_post_relations(post, new_post_ids):
|
|||
post.relations.append(relation)
|
||||
relation.relations.append(post)
|
||||
|
||||
|
||||
def update_post_notes(post, notes):
|
||||
assert post
|
||||
post.notes = []
|
||||
|
@ -323,6 +380,7 @@ def update_post_notes(post, notes):
|
|||
post.notes.append(
|
||||
db.PostNote(polygon=note['polygon'], text=str(note['text'])))
|
||||
|
||||
|
||||
def update_post_flags(post, flags):
|
||||
assert post
|
||||
target_flags = []
|
||||
|
@ -334,6 +392,7 @@ def update_post_flags(post, flags):
|
|||
target_flags.append(flag)
|
||||
post.flags = target_flags
|
||||
|
||||
|
||||
def feature_post(post, user):
|
||||
assert post
|
||||
post_feature = db.PostFeature()
|
||||
|
@ -342,6 +401,7 @@ def feature_post(post, user):
|
|||
post_feature.user = user
|
||||
db.session.add(post_feature)
|
||||
|
||||
|
||||
def delete(post):
|
||||
assert post
|
||||
db.session.delete(post)
|
||||
|
|
|
@ -2,8 +2,14 @@ import datetime
|
|||
from szurubooru import db, errors
|
||||
from szurubooru.func import favorites
|
||||
|
||||
class InvalidScoreTargetError(errors.ValidationError): pass
|
||||
class InvalidScoreValueError(errors.ValidationError): pass
|
||||
|
||||
class InvalidScoreTargetError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidScoreValueError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
def _get_table_info(entity):
|
||||
assert entity
|
||||
|
@ -14,10 +20,12 @@ def _get_table_info(entity):
|
|||
return db.CommentScore, lambda table: table.comment_id
|
||||
raise InvalidScoreTargetError()
|
||||
|
||||
|
||||
def _get_score_entity(entity, user):
|
||||
assert user
|
||||
return db.util.get_aux_entity(db.session, _get_table_info, entity, user)
|
||||
|
||||
|
||||
def delete_score(entity, user):
|
||||
assert entity
|
||||
assert user
|
||||
|
@ -25,6 +33,7 @@ def delete_score(entity, user):
|
|||
if score_entity:
|
||||
db.session.delete(score_entity)
|
||||
|
||||
|
||||
def get_score(entity, user):
|
||||
assert entity
|
||||
assert user
|
||||
|
@ -36,6 +45,7 @@ def get_score(entity, user):
|
|||
.one_or_none()
|
||||
return row[0] if row else 0
|
||||
|
||||
|
||||
def set_score(entity, user, score):
|
||||
assert entity
|
||||
assert user
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import datetime
|
||||
from szurubooru import db
|
||||
|
||||
|
||||
def get_tag_snapshot(tag):
|
||||
return {
|
||||
'names': [tag_name.name for tag_name in tag.names],
|
||||
|
@ -9,6 +10,7 @@ def get_tag_snapshot(tag):
|
|||
'implications': sorted(rel.first_name for rel in tag.implications),
|
||||
}
|
||||
|
||||
|
||||
def get_post_snapshot(post):
|
||||
return {
|
||||
'source': post.source,
|
||||
|
@ -25,6 +27,7 @@ def get_post_snapshot(post):
|
|||
'featured': post.is_featured,
|
||||
}
|
||||
|
||||
|
||||
def get_tag_category_snapshot(category):
|
||||
return {
|
||||
'name': category.name,
|
||||
|
@ -32,6 +35,7 @@ def get_tag_category_snapshot(category):
|
|||
'default': True if category.default else False,
|
||||
}
|
||||
|
||||
|
||||
def get_previous_snapshot(snapshot):
|
||||
assert snapshot
|
||||
return db.session \
|
||||
|
@ -43,6 +47,7 @@ def get_previous_snapshot(snapshot):
|
|||
.limit(1) \
|
||||
.first()
|
||||
|
||||
|
||||
def get_snapshots(entity):
|
||||
assert entity
|
||||
resource_type, resource_id, _ = db.util.get_resource_info(entity)
|
||||
|
@ -53,6 +58,7 @@ def get_snapshots(entity):
|
|||
.order_by(db.Snapshot.creation_time.desc()) \
|
||||
.all()
|
||||
|
||||
|
||||
def serialize_snapshot(snapshot, earlier_snapshot=()):
|
||||
assert snapshot
|
||||
if earlier_snapshot is ():
|
||||
|
@ -67,6 +73,7 @@ def serialize_snapshot(snapshot, earlier_snapshot=()):
|
|||
'time': snapshot.creation_time,
|
||||
}
|
||||
|
||||
|
||||
def get_serialized_history(entity):
|
||||
if not entity:
|
||||
return []
|
||||
|
@ -77,6 +84,7 @@ def get_serialized_history(entity):
|
|||
earlier_snapshot = snapshot
|
||||
return ret
|
||||
|
||||
|
||||
def _save(operation, entity, auth_user):
|
||||
assert operation
|
||||
assert entity
|
||||
|
@ -86,7 +94,8 @@ def _save(operation, entity, auth_user):
|
|||
'post': get_post_snapshot,
|
||||
}
|
||||
|
||||
resource_type, resource_id, resource_repr = db.util.get_resource_info(entity)
|
||||
resource_type, resource_id, resource_repr = (
|
||||
db.util.get_resource_info(entity))
|
||||
now = datetime.datetime.utcnow()
|
||||
|
||||
snapshot = db.Snapshot()
|
||||
|
@ -118,14 +127,17 @@ def _save(operation, entity, auth_user):
|
|||
else:
|
||||
db.session.add(snapshot)
|
||||
|
||||
|
||||
def save_entity_creation(entity, auth_user):
|
||||
assert entity
|
||||
_save(db.Snapshot.OPERATION_CREATED, entity, auth_user)
|
||||
|
||||
|
||||
def save_entity_modification(entity, auth_user):
|
||||
assert entity
|
||||
_save(db.Snapshot.OPERATION_MODIFIED, entity, auth_user)
|
||||
|
||||
|
||||
def save_entity_deletion(entity, auth_user):
|
||||
assert entity
|
||||
_save(db.Snapshot.OPERATION_DELETED, entity, auth_user)
|
||||
|
|
|
@ -3,11 +3,26 @@ import sqlalchemy
|
|||
from szurubooru import config, db, errors
|
||||
from szurubooru.func import util, snapshots, cache
|
||||
|
||||
class TagCategoryNotFoundError(errors.NotFoundError): pass
|
||||
class TagCategoryAlreadyExistsError(errors.ValidationError): pass
|
||||
class TagCategoryIsInUseError(errors.ValidationError): pass
|
||||
class InvalidTagCategoryNameError(errors.ValidationError): pass
|
||||
class InvalidTagCategoryColorError(errors.ValidationError): pass
|
||||
|
||||
class TagCategoryNotFoundError(errors.NotFoundError):
|
||||
pass
|
||||
|
||||
|
||||
class TagCategoryAlreadyExistsError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class TagCategoryIsInUseError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidTagCategoryNameError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidTagCategoryColorError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
def _verify_name_validity(name):
|
||||
name_regex = config.config['tag_category_name_regex']
|
||||
|
@ -15,6 +30,7 @@ def _verify_name_validity(name):
|
|||
raise InvalidTagCategoryNameError(
|
||||
'Name must satisfy regex %r.' % name_regex)
|
||||
|
||||
|
||||
def serialize_category(category, options=None):
|
||||
return util.serialize_entity(
|
||||
category,
|
||||
|
@ -28,6 +44,7 @@ def serialize_category(category, options=None):
|
|||
},
|
||||
options)
|
||||
|
||||
|
||||
def create_category(name, color):
|
||||
category = db.TagCategory()
|
||||
update_category_name(category, name)
|
||||
|
@ -36,13 +53,15 @@ def create_category(name, color):
|
|||
category.default = True
|
||||
return category
|
||||
|
||||
|
||||
def update_category_name(category, name):
|
||||
assert category
|
||||
if not name:
|
||||
raise InvalidTagCategoryNameError('Name cannot be empty.')
|
||||
expr = sqlalchemy.func.lower(db.TagCategory.name) == name.lower()
|
||||
if category.tag_category_id:
|
||||
expr = expr & (db.TagCategory.tag_category_id != category.tag_category_id)
|
||||
expr = expr & (
|
||||
db.TagCategory.tag_category_id != category.tag_category_id)
|
||||
already_exists = db.session.query(db.TagCategory).filter(expr).count() > 0
|
||||
if already_exists:
|
||||
raise TagCategoryAlreadyExistsError(
|
||||
|
@ -52,6 +71,7 @@ def update_category_name(category, name):
|
|||
_verify_name_validity(name)
|
||||
category.name = name
|
||||
|
||||
|
||||
def update_category_color(category, color):
|
||||
assert category
|
||||
if not color:
|
||||
|
@ -62,24 +82,29 @@ def update_category_color(category, color):
|
|||
raise InvalidTagCategoryColorError('Color is too long.')
|
||||
category.color = color
|
||||
|
||||
|
||||
def try_get_category_by_name(name):
|
||||
return db.session \
|
||||
.query(db.TagCategory) \
|
||||
.filter(sqlalchemy.func.lower(db.TagCategory.name) == name.lower()) \
|
||||
.one_or_none()
|
||||
|
||||
|
||||
def get_category_by_name(name):
|
||||
category = try_get_category_by_name(name)
|
||||
if not category:
|
||||
raise TagCategoryNotFoundError('Tag category %r not found.' % name)
|
||||
return category
|
||||
|
||||
|
||||
def get_all_category_names():
|
||||
return [row[0] for row in db.session.query(db.TagCategory.name).all()]
|
||||
|
||||
|
||||
def get_all_categories():
|
||||
return db.session.query(db.TagCategory).all()
|
||||
|
||||
|
||||
def try_get_default_category():
|
||||
key = 'default-tag-category'
|
||||
if cache.has(key):
|
||||
|
@ -98,12 +123,14 @@ def try_get_default_category():
|
|||
cache.put(key, category)
|
||||
return category
|
||||
|
||||
|
||||
def get_default_category():
|
||||
category = try_get_default_category()
|
||||
if not category:
|
||||
raise TagCategoryNotFoundError('No tag category created yet.')
|
||||
return category
|
||||
|
||||
|
||||
def set_default_category(category):
|
||||
assert category
|
||||
old_category = try_get_default_category()
|
||||
|
@ -111,6 +138,7 @@ def set_default_category(category):
|
|||
old_category.default = False
|
||||
category.default = True
|
||||
|
||||
|
||||
def delete_category(category):
|
||||
assert category
|
||||
if len(get_all_category_names()) == 1:
|
||||
|
|
|
@ -6,32 +6,57 @@ import sqlalchemy
|
|||
from szurubooru import config, db, errors
|
||||
from szurubooru.func import util, tag_categories, snapshots
|
||||
|
||||
class TagNotFoundError(errors.NotFoundError): pass
|
||||
class TagAlreadyExistsError(errors.ValidationError): pass
|
||||
class TagIsInUseError(errors.ValidationError): pass
|
||||
class InvalidTagNameError(errors.ValidationError): pass
|
||||
class InvalidTagRelationError(errors.ValidationError): pass
|
||||
class InvalidTagCategoryError(errors.ValidationError): pass
|
||||
class InvalidTagDescriptionError(errors.ValidationError): pass
|
||||
|
||||
class TagNotFoundError(errors.NotFoundError):
|
||||
pass
|
||||
|
||||
|
||||
class TagAlreadyExistsError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class TagIsInUseError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidTagNameError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidTagRelationError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidTagCategoryError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidTagDescriptionError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
def _verify_name_validity(name):
|
||||
name_regex = config.config['tag_name_regex']
|
||||
if not re.match(name_regex, name):
|
||||
raise InvalidTagNameError('Name must satisfy regex %r.' % name_regex)
|
||||
|
||||
def _get_plain_names(tag):
|
||||
|
||||
def _get_names(tag):
|
||||
assert tag
|
||||
return [tag_name.name for tag_name in tag.names]
|
||||
|
||||
|
||||
def _lower_list(names):
|
||||
return [name.lower() for name in names]
|
||||
|
||||
def _check_name_intersection(names1, names2):
|
||||
return len(set(_lower_list(names1)).intersection(_lower_list(names2))) > 0
|
||||
|
||||
def _check_name_intersection_case_sensitive(names1, names2):
|
||||
def _check_name_intersection(names1, names2, case_sensitive):
|
||||
if not case_sensitive:
|
||||
names1 = _lower_list(names1)
|
||||
names2 = _lower_list(names2)
|
||||
return len(set(names1).intersection(names2)) > 0
|
||||
|
||||
|
||||
def sort_tags(tags):
|
||||
default_category = tag_categories.try_get_default_category()
|
||||
default_category_name = default_category.name if default_category else None
|
||||
|
@ -43,6 +68,7 @@ def sort_tags(tags):
|
|||
tag.names[0].name)
|
||||
)
|
||||
|
||||
|
||||
def serialize_tag(tag, options=None):
|
||||
return util.serialize_entity(
|
||||
tag,
|
||||
|
@ -55,15 +81,16 @@ def serialize_tag(tag, options=None):
|
|||
'lastEditTime': lambda: tag.last_edit_time,
|
||||
'usages': lambda: tag.post_count,
|
||||
'suggestions': lambda: [
|
||||
relation.names[0].name \
|
||||
relation.names[0].name
|
||||
for relation in sort_tags(tag.suggestions)],
|
||||
'implications': lambda: [
|
||||
relation.names[0].name \
|
||||
relation.names[0].name
|
||||
for relation in sort_tags(tag.implications)],
|
||||
'snapshots': lambda: snapshots.get_serialized_history(tag),
|
||||
},
|
||||
options)
|
||||
|
||||
|
||||
def export_to_json():
|
||||
tags = {}
|
||||
categories = {}
|
||||
|
@ -82,19 +109,19 @@ def export_to_json():
|
|||
tags[result[0]] = {'names': []}
|
||||
tags[result[0]]['names'].append(result[1])
|
||||
|
||||
for result in db.session \
|
||||
.query(db.TagSuggestion.parent_id, db.TagName.name) \
|
||||
.join(db.TagName, db.TagName.tag_id == db.TagSuggestion.child_id) \
|
||||
.all():
|
||||
if not 'suggestions' in tags[result[0]]:
|
||||
for result in (db.session
|
||||
.query(db.TagSuggestion.parent_id, db.TagName.name)
|
||||
.join(db.TagName, db.TagName.tag_id == db.TagSuggestion.child_id)
|
||||
.all()):
|
||||
if 'suggestions' not in tags[result[0]]:
|
||||
tags[result[0]]['suggestions'] = []
|
||||
tags[result[0]]['suggestions'].append(result[1])
|
||||
|
||||
for result in db.session \
|
||||
.query(db.TagImplication.parent_id, db.TagName.name) \
|
||||
.join(db.TagName, db.TagName.tag_id == db.TagImplication.child_id) \
|
||||
.all():
|
||||
if not 'implications' in tags[result[0]]:
|
||||
for result in (db.session
|
||||
.query(db.TagImplication.parent_id, db.TagName.name)
|
||||
.join(db.TagName, db.TagName.tag_id == db.TagImplication.child_id)
|
||||
.all()):
|
||||
if 'implications' not in tags[result[0]]:
|
||||
tags[result[0]]['implications'] = []
|
||||
tags[result[0]]['implications'].append(result[1])
|
||||
|
||||
|
@ -114,12 +141,14 @@ def export_to_json():
|
|||
with open(export_path, 'w') as handle:
|
||||
handle.write(json.dumps(output, separators=(',', ':')))
|
||||
|
||||
|
||||
def try_get_tag_by_name(name):
|
||||
return db.session \
|
||||
.query(db.Tag) \
|
||||
.join(db.TagName) \
|
||||
.filter(sqlalchemy.func.lower(db.TagName.name) == name.lower()) \
|
||||
.one_or_none()
|
||||
return (db.session
|
||||
.query(db.Tag)
|
||||
.join(db.TagName)
|
||||
.filter(sqlalchemy.func.lower(db.TagName.name) == name.lower())
|
||||
.one_or_none())
|
||||
|
||||
|
||||
def get_tag_by_name(name):
|
||||
tag = try_get_tag_by_name(name)
|
||||
|
@ -127,6 +156,7 @@ def get_tag_by_name(name):
|
|||
raise TagNotFoundError('Tag %r not found.' % name)
|
||||
return tag
|
||||
|
||||
|
||||
def get_tags_by_names(names):
|
||||
names = util.icase_unique(names)
|
||||
if len(names) == 0:
|
||||
|
@ -136,6 +166,7 @@ def get_tags_by_names(names):
|
|||
expr = expr | (sqlalchemy.func.lower(db.TagName.name) == name.lower())
|
||||
return db.session.query(db.Tag).join(db.TagName).filter(expr).all()
|
||||
|
||||
|
||||
def get_or_create_tags_by_names(names):
|
||||
names = util.icase_unique(names)
|
||||
existing_tags = get_tags_by_names(names)
|
||||
|
@ -144,7 +175,8 @@ def get_or_create_tags_by_names(names):
|
|||
for name in names:
|
||||
found = False
|
||||
for existing_tag in existing_tags:
|
||||
if _check_name_intersection(_get_plain_names(existing_tag), [name]):
|
||||
if _check_name_intersection(
|
||||
_get_names(existing_tag), [name], False):
|
||||
found = True
|
||||
break
|
||||
if not found:
|
||||
|
@ -157,32 +189,35 @@ def get_or_create_tags_by_names(names):
|
|||
new_tags.append(new_tag)
|
||||
return existing_tags, new_tags
|
||||
|
||||
|
||||
def get_tag_siblings(tag):
|
||||
assert tag
|
||||
tag_alias = sqlalchemy.orm.aliased(db.Tag)
|
||||
pt_alias1 = sqlalchemy.orm.aliased(db.PostTag)
|
||||
pt_alias2 = sqlalchemy.orm.aliased(db.PostTag)
|
||||
result = db.session \
|
||||
.query(tag_alias, sqlalchemy.func.count(pt_alias2.post_id)) \
|
||||
.join(pt_alias1, pt_alias1.tag_id == tag_alias.tag_id) \
|
||||
.join(pt_alias2, pt_alias2.post_id == pt_alias1.post_id) \
|
||||
.filter(pt_alias2.tag_id == tag.tag_id) \
|
||||
.filter(pt_alias1.tag_id != tag.tag_id) \
|
||||
.group_by(tag_alias.tag_id) \
|
||||
.order_by(sqlalchemy.func.count(pt_alias2.post_id).desc()) \
|
||||
.limit(50)
|
||||
result = (db.session
|
||||
.query(tag_alias, sqlalchemy.func.count(pt_alias2.post_id))
|
||||
.join(pt_alias1, pt_alias1.tag_id == tag_alias.tag_id)
|
||||
.join(pt_alias2, pt_alias2.post_id == pt_alias1.post_id)
|
||||
.filter(pt_alias2.tag_id == tag.tag_id)
|
||||
.filter(pt_alias1.tag_id != tag.tag_id)
|
||||
.group_by(tag_alias.tag_id)
|
||||
.order_by(sqlalchemy.func.count(pt_alias2.post_id).desc())
|
||||
.limit(50))
|
||||
return result
|
||||
|
||||
|
||||
def delete(source_tag):
|
||||
assert source_tag
|
||||
db.session.execute(
|
||||
sqlalchemy.sql.expression.delete(db.TagSuggestion) \
|
||||
sqlalchemy.sql.expression.delete(db.TagSuggestion)
|
||||
.where(db.TagSuggestion.child_id == source_tag.tag_id))
|
||||
db.session.execute(
|
||||
sqlalchemy.sql.expression.delete(db.TagImplication) \
|
||||
sqlalchemy.sql.expression.delete(db.TagImplication)
|
||||
.where(db.TagImplication.child_id == source_tag.tag_id))
|
||||
db.session.delete(source_tag)
|
||||
|
||||
|
||||
def merge_tags(source_tag, target_tag):
|
||||
assert source_tag
|
||||
assert target_tag
|
||||
|
@ -191,15 +226,16 @@ def merge_tags(source_tag, target_tag):
|
|||
pt1 = db.PostTag
|
||||
pt2 = sqlalchemy.orm.util.aliased(db.PostTag)
|
||||
|
||||
update_stmt = sqlalchemy.sql.expression.update(pt1) \
|
||||
.where(db.PostTag.tag_id == source_tag.tag_id) \
|
||||
.where(~sqlalchemy.exists() \
|
||||
.where(pt2.post_id == pt1.post_id) \
|
||||
.where(pt2.tag_id == target_tag.tag_id)) \
|
||||
.values(tag_id=target_tag.tag_id)
|
||||
update_stmt = (sqlalchemy.sql.expression.update(pt1)
|
||||
.where(db.PostTag.tag_id == source_tag.tag_id)
|
||||
.where(~sqlalchemy.exists()
|
||||
.where(pt2.post_id == pt1.post_id)
|
||||
.where(pt2.tag_id == target_tag.tag_id))
|
||||
.values(tag_id=target_tag.tag_id))
|
||||
db.session.execute(update_stmt)
|
||||
delete(source_tag)
|
||||
|
||||
|
||||
def create_tag(names, category_name, suggestions, implications):
|
||||
tag = db.Tag()
|
||||
tag.creation_time = datetime.datetime.utcnow()
|
||||
|
@ -209,10 +245,12 @@ def create_tag(names, category_name, suggestions, implications):
|
|||
update_tag_implications(tag, implications)
|
||||
return tag
|
||||
|
||||
|
||||
def update_tag_category_name(tag, category_name):
|
||||
assert tag
|
||||
tag.category = tag_categories.get_category_by_name(category_name)
|
||||
|
||||
|
||||
def update_tag_names(tag, names):
|
||||
assert tag
|
||||
names = util.icase_unique([name for name in names if name])
|
||||
|
@ -232,26 +270,29 @@ def update_tag_names(tag, names):
|
|||
raise TagAlreadyExistsError(
|
||||
'One of names is already used by another tag.')
|
||||
for tag_name in tag.names[:]:
|
||||
if not _check_name_intersection_case_sensitive([tag_name.name], names):
|
||||
if not _check_name_intersection([tag_name.name], names, True):
|
||||
tag.names.remove(tag_name)
|
||||
for name in names:
|
||||
if not _check_name_intersection_case_sensitive(_get_plain_names(tag), [name]):
|
||||
if not _check_name_intersection(_get_names(tag), [name], True):
|
||||
tag.names.append(db.TagName(name))
|
||||
|
||||
|
||||
# TODO: what to do with relations that do not yet exist?
|
||||
def update_tag_implications(tag, relations):
|
||||
assert tag
|
||||
if _check_name_intersection(_get_plain_names(tag), relations):
|
||||
if _check_name_intersection(_get_names(tag), relations, False):
|
||||
raise InvalidTagRelationError('Tag cannot imply itself.')
|
||||
tag.implications = get_tags_by_names(relations)
|
||||
|
||||
|
||||
# TODO: what to do with relations that do not yet exist?
|
||||
def update_tag_suggestions(tag, relations):
|
||||
assert tag
|
||||
if _check_name_intersection(_get_plain_names(tag), relations):
|
||||
if _check_name_intersection(_get_names(tag), relations, False):
|
||||
raise InvalidTagRelationError('Tag cannot suggest itself.')
|
||||
tag.suggestions = get_tags_by_names(relations)
|
||||
|
||||
|
||||
def update_tag_description(tag, description):
|
||||
assert tag
|
||||
if util.value_exceeds_column_size(description, db.Tag.description):
|
||||
|
|
|
@ -4,17 +4,39 @@ from sqlalchemy import func
|
|||
from szurubooru import config, db, errors
|
||||
from szurubooru.func import auth, util, files, images
|
||||
|
||||
class UserNotFoundError(errors.NotFoundError): pass
|
||||
class UserAlreadyExistsError(errors.ValidationError): pass
|
||||
class InvalidUserNameError(errors.ValidationError): pass
|
||||
class InvalidEmailError(errors.ValidationError): pass
|
||||
class InvalidPasswordError(errors.ValidationError): pass
|
||||
class InvalidRankError(errors.ValidationError): pass
|
||||
class InvalidAvatarError(errors.ValidationError): pass
|
||||
|
||||
class UserNotFoundError(errors.NotFoundError):
|
||||
pass
|
||||
|
||||
|
||||
class UserAlreadyExistsError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidUserNameError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidEmailError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidPasswordError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidRankError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidAvatarError(errors.ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
def get_avatar_path(user_name):
|
||||
return 'avatars/' + user_name.lower() + '.png'
|
||||
|
||||
|
||||
def get_avatar_url(user):
|
||||
assert user
|
||||
if user.avatar_style == user.AVATAR_GRAVATAR:
|
||||
|
@ -27,6 +49,7 @@ def get_avatar_url(user):
|
|||
return '%s/avatars/%s.png' % (
|
||||
config.config['data_url'].rstrip('/'), user.name.lower())
|
||||
|
||||
|
||||
def get_email(user, auth_user, force_show_email):
|
||||
assert user
|
||||
assert auth_user
|
||||
|
@ -36,6 +59,7 @@ def get_email(user, auth_user, force_show_email):
|
|||
return False
|
||||
return user.email
|
||||
|
||||
|
||||
def get_liked_post_count(user, auth_user):
|
||||
assert user
|
||||
assert auth_user
|
||||
|
@ -43,6 +67,7 @@ def get_liked_post_count(user, auth_user):
|
|||
return False
|
||||
return user.liked_post_count
|
||||
|
||||
|
||||
def get_disliked_post_count(user, auth_user):
|
||||
assert user
|
||||
assert auth_user
|
||||
|
@ -50,6 +75,7 @@ def get_disliked_post_count(user, auth_user):
|
|||
return False
|
||||
return user.disliked_post_count
|
||||
|
||||
|
||||
def serialize_user(user, auth_user, options=None, force_show_email=False):
|
||||
return util.serialize_entity(
|
||||
user,
|
||||
|
@ -73,34 +99,40 @@ def serialize_user(user, auth_user, options=None, force_show_email=False):
|
|||
},
|
||||
options)
|
||||
|
||||
|
||||
def serialize_micro_user(user, auth_user):
|
||||
return serialize_user(
|
||||
user,
|
||||
auth_user=auth_user,
|
||||
options=['name', 'avatarUrl'])
|
||||
|
||||
|
||||
def get_user_count():
|
||||
return db.session.query(db.User).count()
|
||||
|
||||
|
||||
def try_get_user_by_name(name):
|
||||
return db.session \
|
||||
.query(db.User) \
|
||||
.filter(func.lower(db.User.name) == func.lower(name)) \
|
||||
.one_or_none()
|
||||
|
||||
|
||||
def get_user_by_name(name):
|
||||
user = try_get_user_by_name(name)
|
||||
if not user:
|
||||
raise UserNotFoundError('User %r not found.' % name)
|
||||
return user
|
||||
|
||||
|
||||
def try_get_user_by_name_or_email(name_or_email):
|
||||
return db.session \
|
||||
.query(db.User) \
|
||||
return (db.session
|
||||
.query(db.User)
|
||||
.filter(
|
||||
(func.lower(db.User.name) == func.lower(name_or_email))
|
||||
| (func.lower(db.User.email) == func.lower(name_or_email))) \
|
||||
.one_or_none()
|
||||
(func.lower(db.User.name) == func.lower(name_or_email)) |
|
||||
(func.lower(db.User.email) == func.lower(name_or_email)))
|
||||
.one_or_none())
|
||||
|
||||
|
||||
def get_user_by_name_or_email(name_or_email):
|
||||
user = try_get_user_by_name_or_email(name_or_email)
|
||||
|
@ -108,6 +140,7 @@ def get_user_by_name_or_email(name_or_email):
|
|||
raise UserNotFoundError('User %r not found.' % name_or_email)
|
||||
return user
|
||||
|
||||
|
||||
def create_user(name, password, email):
|
||||
user = db.User()
|
||||
update_user_name(user, name)
|
||||
|
@ -121,6 +154,7 @@ def create_user(name, password, email):
|
|||
user.avatar_style = db.User.AVATAR_GRAVATAR
|
||||
return user
|
||||
|
||||
|
||||
def update_user_name(user, name):
|
||||
assert user
|
||||
if not name:
|
||||
|
@ -139,6 +173,7 @@ def update_user_name(user, name):
|
|||
files.move(get_avatar_path(user.name), get_avatar_path(name))
|
||||
user.name = name
|
||||
|
||||
|
||||
def update_user_password(user, password):
|
||||
assert user
|
||||
if not password:
|
||||
|
@ -150,6 +185,7 @@ def update_user_password(user, password):
|
|||
user.password_salt = auth.create_password()
|
||||
user.password_hash = auth.get_password_hash(user.password_salt, password)
|
||||
|
||||
|
||||
def update_user_email(user, email):
|
||||
assert user
|
||||
if email:
|
||||
|
@ -162,6 +198,7 @@ def update_user_email(user, email):
|
|||
raise InvalidEmailError('E-mail is invalid.')
|
||||
user.email = email
|
||||
|
||||
|
||||
def update_user_rank(user, rank, auth_user):
|
||||
assert user
|
||||
if not rank:
|
||||
|
@ -178,6 +215,7 @@ def update_user_rank(user, rank, auth_user):
|
|||
raise errors.AuthError('Trying to set higher rank than your own.')
|
||||
user.rank = rank
|
||||
|
||||
|
||||
def update_user_avatar(user, avatar_style, avatar_content=None):
|
||||
assert user
|
||||
if avatar_style == 'gravatar':
|
||||
|
@ -199,10 +237,12 @@ def update_user_avatar(user, avatar_style, avatar_content=None):
|
|||
'Avatar style %r is invalid. Valid avatar styles: %r.' % (
|
||||
avatar_style, ['gravatar', 'manual']))
|
||||
|
||||
|
||||
def bump_user_login_time(user):
|
||||
assert user
|
||||
user.last_login_time = datetime.datetime.utcnow()
|
||||
|
||||
|
||||
def reset_user_password(user):
|
||||
assert user
|
||||
password = auth.create_password()
|
||||
|
|
|
@ -1,18 +1,22 @@
|
|||
import os
|
||||
import datetime
|
||||
import hashlib
|
||||
import re
|
||||
import tempfile
|
||||
from datetime import datetime, timedelta
|
||||
from contextlib import contextmanager
|
||||
from szurubooru import errors
|
||||
|
||||
|
||||
def snake_case_to_lower_camel_case(text):
|
||||
components = text.split('_')
|
||||
return components[0].lower() + \
|
||||
''.join(word[0].upper() + word[1:].lower() for word in components[1:])
|
||||
|
||||
|
||||
def snake_case_to_upper_train_case(text):
|
||||
return '-'.join(word[0].upper() + word[1:].lower() for word in text.split('_'))
|
||||
return '-'.join(
|
||||
word[0].upper() + word[1:].lower() for word in text.split('_'))
|
||||
|
||||
|
||||
def snake_case_to_lower_camel_case_keys(source):
|
||||
target = {}
|
||||
|
@ -20,9 +24,11 @@ def snake_case_to_lower_camel_case_keys(source):
|
|||
target[snake_case_to_lower_camel_case(key)] = value
|
||||
return target
|
||||
|
||||
|
||||
def get_serialization_options(ctx):
|
||||
return ctx.get_param_as_list('fields', required=False, default=None)
|
||||
|
||||
|
||||
def serialize_entity(entity, field_factories, options):
|
||||
if not entity:
|
||||
return None
|
||||
|
@ -30,13 +36,14 @@ def serialize_entity(entity, field_factories, options):
|
|||
options = field_factories.keys()
|
||||
ret = {}
|
||||
for key in options:
|
||||
if not key in field_factories:
|
||||
if key not in field_factories:
|
||||
raise errors.ValidationError('Invalid key: %r. Valid keys: %r.' % (
|
||||
key, list(sorted(field_factories.keys()))))
|
||||
factory = field_factories[key]
|
||||
ret[key] = factory()
|
||||
return ret
|
||||
|
||||
|
||||
@contextmanager
|
||||
def create_temp_file(**kwargs):
|
||||
(handle, path) = tempfile.mkstemp(**kwargs)
|
||||
|
@ -47,6 +54,7 @@ def create_temp_file(**kwargs):
|
|||
finally:
|
||||
os.remove(path)
|
||||
|
||||
|
||||
def unalias_dict(input_dict):
|
||||
output_dict = {}
|
||||
for key_list, value in input_dict.items():
|
||||
|
@ -56,6 +64,7 @@ def unalias_dict(input_dict):
|
|||
output_dict[key] = value
|
||||
return output_dict
|
||||
|
||||
|
||||
def get_md5(source):
|
||||
if not isinstance(source, bytes):
|
||||
source = source.encode('utf-8')
|
||||
|
@ -63,13 +72,16 @@ def get_md5(source):
|
|||
md5.update(source)
|
||||
return md5.hexdigest()
|
||||
|
||||
|
||||
def flip(source):
|
||||
return {v: k for k, v in source.items()}
|
||||
|
||||
|
||||
def is_valid_email(email):
|
||||
''' Return whether given email address is valid or empty. '''
|
||||
return not email or re.match(r'^[^@]*@[^@]*\.[^@]*$', email)
|
||||
|
||||
|
||||
class dotdict(dict): # pylint: disable=invalid-name
|
||||
''' dot.notation access to dictionary attributes. '''
|
||||
def __getattr__(self, attr):
|
||||
|
@ -77,43 +89,41 @@ class dotdict(dict): # pylint: disable=invalid-name
|
|||
__setattr__ = dict.__setitem__
|
||||
__delattr__ = dict.__delitem__
|
||||
|
||||
|
||||
def parse_time_range(value):
|
||||
''' Return tuple containing min/max time for given text representation. '''
|
||||
one_day = datetime.timedelta(days=1)
|
||||
one_second = datetime.timedelta(seconds=1)
|
||||
one_day = timedelta(days=1)
|
||||
one_second = timedelta(seconds=1)
|
||||
|
||||
value = value.lower()
|
||||
if not value:
|
||||
raise errors.ValidationError('Empty date format.')
|
||||
|
||||
if value == 'today':
|
||||
now = datetime.datetime.utcnow()
|
||||
now = datetime.utcnow()
|
||||
return (
|
||||
datetime.datetime(now.year, now.month, now.day, 0, 0, 0),
|
||||
datetime.datetime(now.year, now.month, now.day, 0, 0, 0) \
|
||||
datetime(now.year, now.month, now.day, 0, 0, 0),
|
||||
datetime(now.year, now.month, now.day, 0, 0, 0)
|
||||
+ one_day - one_second)
|
||||
|
||||
if value == 'yesterday':
|
||||
now = datetime.datetime.utcnow()
|
||||
now = datetime.utcnow()
|
||||
return (
|
||||
datetime.datetime(now.year, now.month, now.day, 0, 0, 0) - one_day,
|
||||
datetime.datetime(now.year, now.month, now.day, 0, 0, 0) \
|
||||
- one_second)
|
||||
datetime(now.year, now.month, now.day, 0, 0, 0) - one_day,
|
||||
datetime(now.year, now.month, now.day, 0, 0, 0) - one_second)
|
||||
|
||||
match = re.match(r'^(\d{4})$', value)
|
||||
if match:
|
||||
year = int(match.group(1))
|
||||
return (
|
||||
datetime.datetime(year, 1, 1),
|
||||
datetime.datetime(year + 1, 1, 1) - one_second)
|
||||
return (datetime(year, 1, 1), datetime(year + 1, 1, 1) - one_second)
|
||||
|
||||
match = re.match(r'^(\d{4})-(\d{1,2})$', value)
|
||||
if match:
|
||||
year = int(match.group(1))
|
||||
month = int(match.group(2))
|
||||
return (
|
||||
datetime.datetime(year, month, 1),
|
||||
datetime.datetime(year, month + 1, 1) - one_second)
|
||||
datetime(year, month, 1),
|
||||
datetime(year, month + 1, 1) - one_second)
|
||||
|
||||
match = re.match(r'^(\d{4})-(\d{1,2})-(\d{1,2})$', value)
|
||||
if match:
|
||||
|
@ -121,11 +131,12 @@ def parse_time_range(value):
|
|||
month = int(match.group(2))
|
||||
day = int(match.group(3))
|
||||
return (
|
||||
datetime.datetime(year, month, day),
|
||||
datetime.datetime(year, month, day + 1) - one_second)
|
||||
datetime(year, month, day),
|
||||
datetime(year, month, day + 1) - one_second)
|
||||
|
||||
raise errors.ValidationError('Invalid date format: %r.' % value)
|
||||
|
||||
|
||||
def icase_unique(source):
|
||||
target = []
|
||||
target_low = []
|
||||
|
@ -135,6 +146,7 @@ def icase_unique(source):
|
|||
target_low.append(source_item.lower())
|
||||
return target
|
||||
|
||||
|
||||
def value_exceeds_column_size(value, column):
|
||||
if not value:
|
||||
return False
|
||||
|
@ -143,6 +155,7 @@ def value_exceeds_column_size(value, column):
|
|||
return False
|
||||
return len(value) > max_length
|
||||
|
||||
|
||||
def verify_version(entity, context, field_name='version'):
|
||||
actual_version = context.get_param_as_int(field_name, required=True)
|
||||
expected_version = entity.version
|
||||
|
@ -151,5 +164,6 @@ def verify_version(entity, context, field_name='version'):
|
|||
'Someone else modified this in the meantime. ' +
|
||||
'Please try again.')
|
||||
|
||||
|
||||
def bump_version(entity):
|
||||
entity.version += 1
|
||||
|
|
|
@ -4,6 +4,7 @@ from szurubooru.func import auth, users
|
|||
from szurubooru.rest import middleware
|
||||
from szurubooru.rest.errors import HttpBadRequest
|
||||
|
||||
|
||||
def _authenticate(username, password):
|
||||
''' Try to authenticate user. Throw AuthError for invalid users. '''
|
||||
user = users.get_user_by_name(username)
|
||||
|
@ -11,23 +12,25 @@ def _authenticate(username, password):
|
|||
raise errors.AuthError('Invalid password.')
|
||||
return user
|
||||
|
||||
|
||||
def _create_anonymous_user():
|
||||
user = db.User()
|
||||
user.name = None
|
||||
user.rank = 'anonymous'
|
||||
return user
|
||||
|
||||
|
||||
def _get_user(ctx):
|
||||
if not ctx.has_header('Authorization'):
|
||||
return _create_anonymous_user()
|
||||
|
||||
try:
|
||||
auth_type, user_and_password = ctx.get_header('Authorization').split(' ', 1)
|
||||
auth_type, credentials = ctx.get_header('Authorization').split(' ', 1)
|
||||
if auth_type.lower() != 'basic':
|
||||
raise HttpBadRequest(
|
||||
'Only basic HTTP authentication is supported.')
|
||||
username, password = base64.decodebytes(
|
||||
user_and_password.encode('ascii')).decode('utf8').split(':')
|
||||
credentials.encode('ascii')).decode('utf8').split(':')
|
||||
return _authenticate(username, password)
|
||||
except ValueError as err:
|
||||
msg = 'Basic authentication header value are not properly formed. ' \
|
||||
|
@ -35,6 +38,7 @@ def _get_user(ctx):
|
|||
raise HttpBadRequest(
|
||||
msg.format(ctx.get_header('Authorization'), str(err)))
|
||||
|
||||
|
||||
@middleware.pre_hook
|
||||
def process_request(ctx):
|
||||
''' Bind the user to request. Update last login time if needed. '''
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from szurubooru.func import cache
|
||||
from szurubooru.rest import middleware
|
||||
|
||||
|
||||
@middleware.pre_hook
|
||||
def process_request(ctx):
|
||||
if ctx.method != 'GET':
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
from szurubooru import db
|
||||
from szurubooru.rest import middleware
|
||||
|
||||
|
||||
@middleware.pre_hook
|
||||
def _process_request(ctx):
|
||||
ctx.session = db.session()
|
||||
db.reset_query_count()
|
||||
|
||||
|
||||
@middleware.post_hook
|
||||
def _process_response(_ctx):
|
||||
db.session.remove()
|
||||
|
|
|
@ -2,8 +2,10 @@ import logging
|
|||
from szurubooru import db
|
||||
from szurubooru.rest import middleware
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@middleware.post_hook
|
||||
def process_response(ctx):
|
||||
logger.info(
|
||||
|
|
|
@ -28,6 +28,7 @@ alembic_config.set_main_option(
|
|||
|
||||
target_metadata = szurubooru.db.Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline():
|
||||
'''
|
||||
Run migrations in 'offline' mode.
|
||||
|
|
|
@ -13,6 +13,7 @@ down_revision = 'e5c1216a8503'
|
|||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.create_table(
|
||||
'tag_category',
|
||||
|
@ -55,6 +56,7 @@ def upgrade():
|
|||
sa.ForeignKeyConstraint(['child_id'], ['tag.id']),
|
||||
sa.PrimaryKeyConstraint('parent_id', 'child_id'))
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_table('tag_suggestion')
|
||||
op.drop_table('tag_implication')
|
||||
|
|
|
@ -13,10 +13,16 @@ down_revision = '49ab4e1139ef'
|
|||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.add_column('tag_category', sa.Column('default', sa.Boolean(), nullable=True))
|
||||
op.execute(sa.table('tag_category', sa.column('default')).update().values(default=False))
|
||||
op.add_column(
|
||||
'tag_category', sa.Column('default', sa.Boolean(), nullable=True))
|
||||
op.execute(
|
||||
sa.table('tag_category', sa.column('default'))
|
||||
.update()
|
||||
.values(default=False))
|
||||
op.alter_column('tag_category', 'default', nullable=False)
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_column('tag_category', 'default')
|
||||
|
|
|
@ -13,8 +13,11 @@ down_revision = 'ed6dd16a30f3'
|
|||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.add_column('post', sa.Column('mime-type', sa.Unicode(length=32), nullable=False))
|
||||
op.add_column(
|
||||
'post', sa.Column('mime-type', sa.Unicode(length=32), nullable=False))
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_column('post', 'mime-type')
|
||||
|
|
|
@ -13,6 +13,7 @@ down_revision = '00cb3a2734db'
|
|||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.create_table(
|
||||
'post',
|
||||
|
@ -56,6 +57,7 @@ def upgrade():
|
|||
sa.ForeignKeyConstraint(['tag_id'], ['tag.id']),
|
||||
sa.PrimaryKeyConstraint('post_id', 'tag_id'))
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_table('post_tag')
|
||||
op.drop_table('post_relation')
|
||||
|
|
|
@ -13,10 +13,12 @@ down_revision = '565e01e3cf6d'
|
|||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.add_column(
|
||||
'snapshot',
|
||||
sa.Column('resource_repr', sa.Unicode(length=64), nullable=False))
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_column('snapshot', 'resource_repr')
|
||||
|
|
|
@ -13,6 +13,7 @@ down_revision = '84bd402f15f0'
|
|||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.create_table(
|
||||
'comment',
|
||||
|
@ -36,6 +37,7 @@ def upgrade():
|
|||
sa.ForeignKeyConstraint(['user_id'], ['user.id']),
|
||||
sa.PrimaryKeyConstraint('comment_id', 'user_id'))
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_table('comment_score')
|
||||
op.drop_table('comment')
|
||||
|
|
|
@ -13,52 +13,59 @@ down_revision = '23abaf4a0a4b'
|
|||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.create_index(op.f('ix_comment_post_id'), 'comment', ['post_id'], unique=False)
|
||||
op.create_index(op.f('ix_comment_user_id'), 'comment', ['user_id'], unique=False)
|
||||
op.create_index(op.f('ix_comment_score_user_id'), 'comment_score', ['user_id'], unique=False)
|
||||
op.create_index(op.f('ix_post_user_id'), 'post', ['user_id'], unique=False)
|
||||
op.create_index(op.f('ix_post_favorite_post_id'), 'post_favorite', ['post_id'], unique=False)
|
||||
op.create_index(op.f('ix_post_favorite_user_id'), 'post_favorite', ['user_id'], unique=False)
|
||||
op.create_index(op.f('ix_post_feature_post_id'), 'post_feature', ['post_id'], unique=False)
|
||||
op.create_index(op.f('ix_post_feature_user_id'), 'post_feature', ['user_id'], unique=False)
|
||||
op.create_index(op.f('ix_post_note_post_id'), 'post_note', ['post_id'], unique=False)
|
||||
op.create_index(op.f('ix_post_relation_child_id'), 'post_relation', ['child_id'], unique=False)
|
||||
op.create_index(op.f('ix_post_relation_parent_id'), 'post_relation', ['parent_id'], unique=False)
|
||||
op.create_index(op.f('ix_post_score_post_id'), 'post_score', ['post_id'], unique=False)
|
||||
op.create_index(op.f('ix_post_score_user_id'), 'post_score', ['user_id'], unique=False)
|
||||
op.create_index(op.f('ix_post_tag_post_id'), 'post_tag', ['post_id'], unique=False)
|
||||
op.create_index(op.f('ix_post_tag_tag_id'), 'post_tag', ['tag_id'], unique=False)
|
||||
op.create_index(op.f('ix_snapshot_resource_id'), 'snapshot', ['resource_id'], unique=False)
|
||||
op.create_index(op.f('ix_snapshot_resource_type'), 'snapshot', ['resource_type'], unique=False)
|
||||
op.create_index(op.f('ix_tag_category_id'), 'tag', ['category_id'], unique=False)
|
||||
op.create_index(op.f('ix_tag_implication_child_id'), 'tag_implication', ['child_id'], unique=False)
|
||||
op.create_index(op.f('ix_tag_implication_parent_id'), 'tag_implication', ['parent_id'], unique=False)
|
||||
op.create_index(op.f('ix_tag_name_tag_id'), 'tag_name', ['tag_id'], unique=False)
|
||||
op.create_index(op.f('ix_tag_suggestion_child_id'), 'tag_suggestion', ['child_id'], unique=False)
|
||||
op.create_index(op.f('ix_tag_suggestion_parent_id'), 'tag_suggestion', ['parent_id'], unique=False)
|
||||
for index_name, table_name, column_name in [
|
||||
('ix_comment_post_id', 'comment', 'post_id'),
|
||||
('ix_comment_user_id', 'comment', 'user_id'),
|
||||
('ix_comment_score_user_id', 'comment_score', 'user_id'),
|
||||
('ix_post_user_id', 'post', 'user_id'),
|
||||
('ix_post_favorite_post_id', 'post_favorite', 'post_id'),
|
||||
('ix_post_favorite_user_id', 'post_favorite', 'user_id'),
|
||||
('ix_post_feature_post_id', 'post_feature', 'post_id'),
|
||||
('ix_post_feature_user_id', 'post_feature', 'user_id'),
|
||||
('ix_post_note_post_id', 'post_note', 'post_id'),
|
||||
('ix_post_relation_child_id', 'post_relation', 'child_id'),
|
||||
('ix_post_relation_parent_id', 'post_relation', 'parent_id'),
|
||||
('ix_post_score_post_id', 'post_score', 'post_id'),
|
||||
('ix_post_score_user_id', 'post_score', 'user_id'),
|
||||
('ix_post_tag_post_id', 'post_tag', 'post_id'),
|
||||
('ix_post_tag_tag_id', 'post_tag', 'tag_id'),
|
||||
('ix_snapshot_resource_id', 'snapshot', 'resource_id'),
|
||||
('ix_snapshot_resource_type', 'snapshot', 'resource_type'),
|
||||
('ix_tag_category_id', 'tag', 'category_id'),
|
||||
('ix_tag_implication_child_id', 'tag_implication', 'child_id'),
|
||||
('ix_tag_implication_parent_id', 'tag_implication', 'parent_id'),
|
||||
('ix_tag_name_tag_id', 'tag_name', 'tag_id'),
|
||||
('ix_tag_suggestion_child_id', 'tag_suggestion', 'child_id'),
|
||||
('ix_tag_suggestion_parent_id', 'tag_suggestion', 'parent_id')]:
|
||||
op.create_index(
|
||||
op.f(index_name), table_name, [column_name], unique=False)
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_index(op.f('ix_tag_suggestion_parent_id'), table_name='tag_suggestion')
|
||||
op.drop_index(op.f('ix_tag_suggestion_child_id'), table_name='tag_suggestion')
|
||||
op.drop_index(op.f('ix_tag_name_tag_id'), table_name='tag_name')
|
||||
op.drop_index(op.f('ix_tag_implication_parent_id'), table_name='tag_implication')
|
||||
op.drop_index(op.f('ix_tag_implication_child_id'), table_name='tag_implication')
|
||||
op.drop_index(op.f('ix_tag_category_id'), table_name='tag')
|
||||
op.drop_index(op.f('ix_snapshot_resource_type'), table_name='snapshot')
|
||||
op.drop_index(op.f('ix_snapshot_resource_id'), table_name='snapshot')
|
||||
op.drop_index(op.f('ix_post_tag_tag_id'), table_name='post_tag')
|
||||
op.drop_index(op.f('ix_post_tag_post_id'), table_name='post_tag')
|
||||
op.drop_index(op.f('ix_post_score_user_id'), table_name='post_score')
|
||||
op.drop_index(op.f('ix_post_score_post_id'), table_name='post_score')
|
||||
op.drop_index(op.f('ix_post_relation_parent_id'), table_name='post_relation')
|
||||
op.drop_index(op.f('ix_post_relation_child_id'), table_name='post_relation')
|
||||
op.drop_index(op.f('ix_post_note_post_id'), table_name='post_note')
|
||||
op.drop_index(op.f('ix_post_feature_user_id'), table_name='post_feature')
|
||||
op.drop_index(op.f('ix_post_feature_post_id'), table_name='post_feature')
|
||||
op.drop_index(op.f('ix_post_favorite_user_id'), table_name='post_favorite')
|
||||
op.drop_index(op.f('ix_post_favorite_post_id'), table_name='post_favorite')
|
||||
op.drop_index(op.f('ix_post_user_id'), table_name='post')
|
||||
op.drop_index(op.f('ix_comment_score_user_id'), table_name='comment_score')
|
||||
op.drop_index(op.f('ix_comment_user_id'), table_name='comment')
|
||||
op.drop_index(op.f('ix_comment_post_id'), table_name='comment')
|
||||
for index_name, table_name in [
|
||||
('ix_tag_suggestion_parent_id', 'tag_suggestion'),
|
||||
('ix_tag_suggestion_child_id', 'tag_suggestion'),
|
||||
('ix_tag_name_tag_id', 'tag_name'),
|
||||
('ix_tag_implication_parent_id', 'tag_implication'),
|
||||
('ix_tag_implication_child_id', 'tag_implication'),
|
||||
('ix_tag_category_id', 'tag'),
|
||||
('ix_snapshot_resource_type', 'snapshot'),
|
||||
('ix_snapshot_resource_id', 'snapshot'),
|
||||
('ix_post_tag_tag_id', 'post_tag'),
|
||||
('ix_post_tag_post_id', 'post_tag'),
|
||||
('ix_post_score_user_id', 'post_score'),
|
||||
('ix_post_score_post_id', 'post_score'),
|
||||
('ix_post_relation_parent_id', 'post_relation'),
|
||||
('ix_post_relation_child_id', 'post_relation'),
|
||||
('ix_post_note_post_id', 'post_note'),
|
||||
('ix_post_feature_user_id', 'post_feature'),
|
||||
('ix_post_feature_post_id', 'post_feature'),
|
||||
('ix_post_favorite_user_id', 'post_favorite'),
|
||||
('ix_post_favorite_post_id', 'post_favorite'),
|
||||
('ix_post_user_id', 'post'),
|
||||
('ix_comment_score_user_id', 'comment_score'),
|
||||
('ix_comment_user_id', 'comment'),
|
||||
('ix_comment_post_id', 'comment')]:
|
||||
op.drop_index(op.f(index_name), table_name=table_name)
|
||||
|
|
|
@ -13,8 +13,11 @@ down_revision = '055d0e048fb3'
|
|||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.add_column('tag', sa.Column('description', sa.UnicodeText(), nullable=True))
|
||||
op.add_column(
|
||||
'tag', sa.Column('description', sa.UnicodeText(), nullable=True))
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_column('tag', 'description')
|
||||
|
|
|
@ -13,6 +13,7 @@ down_revision = '336a76ec1338'
|
|||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.create_table(
|
||||
'snapshot',
|
||||
|
@ -26,5 +27,6 @@ def upgrade():
|
|||
sa.ForeignKeyConstraint(['user_id'], ['user.id']),
|
||||
sa.PrimaryKeyConstraint('id'))
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_table('snapshot')
|
||||
|
|
|
@ -15,12 +15,17 @@ depends_on = None
|
|||
|
||||
tables = ['tag_category', 'tag', 'user', 'post', 'comment']
|
||||
|
||||
|
||||
def upgrade():
|
||||
for table in tables:
|
||||
op.add_column(table, sa.Column('version', sa.Integer(), nullable=True))
|
||||
op.execute(sa.table(table, sa.column('version')).update().values(version=1))
|
||||
op.execute(
|
||||
sa.table(table, sa.column('version'))
|
||||
.update()
|
||||
.values(version=1))
|
||||
op.alter_column(table, 'version', nullable=False)
|
||||
|
||||
|
||||
def downgrade():
|
||||
for table in tables:
|
||||
op.drop_column(table, 'version')
|
||||
|
|
|
@ -13,10 +13,14 @@ down_revision = '9587de88a84b'
|
|||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.drop_column('post', 'flags')
|
||||
op.add_column('post', sa.Column('flags', sa.PickleType(), nullable=True))
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_column('post', 'flags')
|
||||
op.add_column('post', sa.Column('flags', sa.Integer(), autoincrement=False, nullable=False))
|
||||
op.add_column(
|
||||
'post',
|
||||
sa.Column('flags', sa.Integer(), autoincrement=False, nullable=False))
|
||||
|
|
|
@ -13,6 +13,7 @@ down_revision = '46cd5229839b'
|
|||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.create_table(
|
||||
'post_favorite',
|
||||
|
@ -52,6 +53,7 @@ def upgrade():
|
|||
sa.ForeignKeyConstraint(['user_id'], ['user.id']),
|
||||
sa.PrimaryKeyConstraint('post_id', 'user_id'))
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_table('post_score')
|
||||
op.drop_table('post_note')
|
||||
|
|
|
@ -13,6 +13,7 @@ down_revision = None
|
|||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.create_table(
|
||||
'user',
|
||||
|
@ -28,5 +29,6 @@ def upgrade():
|
|||
sa.PrimaryKeyConstraint('id'))
|
||||
op.create_unique_constraint('uq_user_name', 'user', ['name'])
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_table('user')
|
||||
|
|
|
@ -13,24 +13,36 @@ down_revision = '46df355634dc'
|
|||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.drop_column('post', 'auto_comment_edit_time')
|
||||
op.drop_column('post', 'auto_fav_count')
|
||||
op.drop_column('post', 'auto_comment_creation_time')
|
||||
op.drop_column('post', 'auto_feature_count')
|
||||
op.drop_column('post', 'auto_comment_count')
|
||||
op.drop_column('post', 'auto_score')
|
||||
op.drop_column('post', 'auto_fav_time')
|
||||
op.drop_column('post', 'auto_feature_time')
|
||||
op.drop_column('post', 'auto_note_count')
|
||||
for column_name in [
|
||||
'auto_comment_edit_time'
|
||||
'auto_fav_count',
|
||||
'auto_comment_creation_time',
|
||||
'auto_feature_count',
|
||||
'auto_comment_count',
|
||||
'auto_score',
|
||||
'auto_fav_time',
|
||||
'auto_feature_time',
|
||||
'auto_note_count']:
|
||||
op.drop_column('post', column_name)
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.add_column('post', sa.Column('auto_note_count', sa.INTEGER(), autoincrement=False, nullable=False))
|
||||
op.add_column('post', sa.Column('auto_feature_time', sa.INTEGER(), autoincrement=False, nullable=False))
|
||||
op.add_column('post', sa.Column('auto_fav_time', sa.INTEGER(), autoincrement=False, nullable=False))
|
||||
op.add_column('post', sa.Column('auto_score', sa.INTEGER(), autoincrement=False, nullable=False))
|
||||
op.add_column('post', sa.Column('auto_comment_count', sa.INTEGER(), autoincrement=False, nullable=False))
|
||||
op.add_column('post', sa.Column('auto_feature_count', sa.INTEGER(), autoincrement=False, nullable=False))
|
||||
op.add_column('post', sa.Column('auto_comment_creation_time', sa.INTEGER(), autoincrement=False, nullable=False))
|
||||
op.add_column('post', sa.Column('auto_fav_count', sa.INTEGER(), autoincrement=False, nullable=False))
|
||||
op.add_column('post', sa.Column('auto_comment_edit_time', sa.INTEGER(), autoincrement=False, nullable=False))
|
||||
for column_name in [
|
||||
'auto_note_count',
|
||||
'auto_feature_time',
|
||||
'auto_fav_time',
|
||||
'auto_score',
|
||||
'auto_comment_count',
|
||||
'auto_feature_count',
|
||||
'auto_comment_creation_time',
|
||||
'auto_fav_count',
|
||||
'auto_comment_edit_time']:
|
||||
op.add_column(
|
||||
'post',
|
||||
sa.Column(
|
||||
column_name,
|
||||
sa.INTEGER(),
|
||||
autoincrement=False,
|
||||
nullable=False))
|
||||
|
|
|
@ -6,6 +6,7 @@ from datetime import datetime
|
|||
from szurubooru.func import util
|
||||
from szurubooru.rest import errors, middleware, routes, context
|
||||
|
||||
|
||||
def _json_serializer(obj):
|
||||
''' JSON serializer for objects not serializable by default JSON code '''
|
||||
if isinstance(obj, datetime):
|
||||
|
@ -13,9 +14,11 @@ def _json_serializer(obj):
|
|||
return serial
|
||||
raise TypeError('Type not serializable')
|
||||
|
||||
|
||||
def _dump_json(obj):
|
||||
return json.dumps(obj, default=_json_serializer, indent=2)
|
||||
|
||||
|
||||
def _read(env):
|
||||
length = int(env.get('CONTENT_LENGTH', 0))
|
||||
output = io.BytesIO()
|
||||
|
@ -28,6 +31,7 @@ def _read(env):
|
|||
output.seek(0)
|
||||
return output
|
||||
|
||||
|
||||
def _get_headers(env):
|
||||
headers = {}
|
||||
for key, value in env.items():
|
||||
|
@ -36,6 +40,7 @@ def _get_headers(env):
|
|||
headers[key] = value
|
||||
return headers
|
||||
|
||||
|
||||
def _create_context(env):
|
||||
method = env['REQUEST_METHOD']
|
||||
path = '/' + env['PATH_INFO'].lstrip('/')
|
||||
|
@ -56,7 +61,7 @@ def _create_context(env):
|
|||
if isinstance(form[key], cgi.MiniFieldStorage):
|
||||
params[key] = form.getvalue(key)
|
||||
else:
|
||||
_original_file_name = getattr(form[key], 'filename', None)
|
||||
# _user_file_name = getattr(form[key], 'filename', None)
|
||||
files[key] = form.getvalue(key)
|
||||
if 'metadata' in form:
|
||||
body = form.getvalue('metadata')
|
||||
|
@ -79,10 +84,11 @@ def _create_context(env):
|
|||
|
||||
return context.Context(method, path, headers, params, files)
|
||||
|
||||
|
||||
def application(env, start_response):
|
||||
try:
|
||||
ctx = _create_context(env)
|
||||
if not 'application/json' in ctx.get_header('Accept'):
|
||||
if 'application/json' not in ctx.get_header('Accept'):
|
||||
raise errors.HttpNotAcceptable(
|
||||
'This API only supports JSON responses.')
|
||||
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
from szurubooru import errors
|
||||
from szurubooru.func import net
|
||||
|
||||
|
||||
def _lower_first(source):
|
||||
return source[0].lower() + source[1:]
|
||||
|
||||
|
||||
def _param_wrapper(func):
|
||||
def wrapper(self, name, required=False, default=None, **kwargs):
|
||||
# pylint: disable=protected-access
|
||||
|
@ -22,8 +24,8 @@ def _param_wrapper(func):
|
|||
'Required parameter %r is missing.' % name)
|
||||
return wrapper
|
||||
|
||||
|
||||
class Context():
|
||||
# pylint: disable=too-many-arguments
|
||||
def __init__(self, method, url, headers=None, params=None, files=None):
|
||||
self.method = method
|
||||
self.url = url
|
||||
|
@ -74,7 +76,6 @@ class Context():
|
|||
raise errors.InvalidParameterError('Expected simple string.')
|
||||
return value
|
||||
|
||||
# pylint: disable=redefined-builtin
|
||||
@_param_wrapper
|
||||
def get_param_as_int(self, value, min=None, max=None):
|
||||
try:
|
||||
|
@ -97,4 +98,5 @@ class Context():
|
|||
return True
|
||||
if value in ['0', 'n', 'no', 'nope', 'f', 'false']:
|
||||
return False
|
||||
raise errors.InvalidParameterError('The value must be a boolean value.')
|
||||
raise errors.InvalidParameterError(
|
||||
'The value must be a boolean value.')
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
error_handlers = {} # pylint: disable=invalid-name
|
||||
|
||||
|
||||
class BaseHttpError(RuntimeError):
|
||||
code = None
|
||||
reason = None
|
||||
|
@ -9,29 +10,36 @@ class BaseHttpError(RuntimeError):
|
|||
self.description = description
|
||||
self.title = title or self.reason
|
||||
|
||||
|
||||
class HttpBadRequest(BaseHttpError):
|
||||
code = 400
|
||||
reason = 'Bad Request'
|
||||
|
||||
|
||||
class HttpForbidden(BaseHttpError):
|
||||
code = 403
|
||||
reason = 'Forbidden'
|
||||
|
||||
|
||||
class HttpNotFound(BaseHttpError):
|
||||
code = 404
|
||||
reason = 'Not Found'
|
||||
|
||||
|
||||
class HttpNotAcceptable(BaseHttpError):
|
||||
code = 406
|
||||
reason = 'Not Acceptable'
|
||||
|
||||
|
||||
class HttpConflict(BaseHttpError):
|
||||
code = 409
|
||||
reason = 'Conflict'
|
||||
|
||||
|
||||
class HttpMethodNotAllowed(BaseHttpError):
|
||||
code = 405
|
||||
reason = 'Method Not Allowed'
|
||||
|
||||
|
||||
def handle(exception_type, handler):
|
||||
error_handlers[exception_type] = handler
|
||||
|
|
|
@ -2,8 +2,10 @@
|
|||
pre_hooks = []
|
||||
post_hooks = []
|
||||
|
||||
|
||||
def pre_hook(handler):
|
||||
pre_hooks.append(handler)
|
||||
|
||||
|
||||
def post_hook(handler):
|
||||
post_hooks.insert(0, handler)
|
||||
|
|
|
@ -1,25 +1,30 @@
|
|||
from collections import defaultdict
|
||||
|
||||
|
||||
routes = defaultdict(dict) # pylint: disable=invalid-name
|
||||
|
||||
|
||||
def get(url):
|
||||
def wrapper(handler):
|
||||
routes[url]['GET'] = handler
|
||||
return handler
|
||||
return wrapper
|
||||
|
||||
|
||||
def put(url):
|
||||
def wrapper(handler):
|
||||
routes[url]['PUT'] = handler
|
||||
return handler
|
||||
return wrapper
|
||||
|
||||
|
||||
def post(url):
|
||||
def wrapper(handler):
|
||||
routes[url]['POST'] = handler
|
||||
return handler
|
||||
return wrapper
|
||||
|
||||
|
||||
def delete(url):
|
||||
def wrapper(handler):
|
||||
routes[url]['DELETE'] = handler
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from szurubooru.search.configs.user_search_config import UserSearchConfig
|
||||
from szurubooru.search.configs.snapshot_search_config import SnapshotSearchConfig
|
||||
from szurubooru.search.configs.tag_search_config import TagSearchConfig
|
||||
from szurubooru.search.configs.comment_search_config import CommentSearchConfig
|
||||
from szurubooru.search.configs.post_search_config import PostSearchConfig
|
||||
from .user_search_config import UserSearchConfig
|
||||
from .tag_search_config import TagSearchConfig
|
||||
from .post_search_config import PostSearchConfig
|
||||
from .snapshot_search_config import SnapshotSearchConfig
|
||||
from .comment_search_config import CommentSearchConfig
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from szurubooru.search import tokens
|
||||
|
||||
|
||||
class BaseSearchConfig(object):
|
||||
SORT_ASC = tokens.SortToken.SORT_ASC
|
||||
SORT_DESC = tokens.SortToken.SORT_DESC
|
||||
|
|
|
@ -3,6 +3,7 @@ from szurubooru import db
|
|||
from szurubooru.search.configs import util as search_util
|
||||
from szurubooru.search.configs.base_search_config import BaseSearchConfig
|
||||
|
||||
|
||||
class CommentSearchConfig(BaseSearchConfig):
|
||||
def create_filter_query(self):
|
||||
return db.session.query(db.Comment).join(db.User)
|
||||
|
@ -22,12 +23,18 @@ class CommentSearchConfig(BaseSearchConfig):
|
|||
'user': search_util.create_str_filter(db.User.name),
|
||||
'author': search_util.create_str_filter(db.User.name),
|
||||
'text': search_util.create_str_filter(db.Comment.text),
|
||||
'creation-date': search_util.create_date_filter(db.Comment.creation_time),
|
||||
'creation-time': search_util.create_date_filter(db.Comment.creation_time),
|
||||
'last-edit-date': search_util.create_date_filter(db.Comment.last_edit_time),
|
||||
'last-edit-time': search_util.create_date_filter(db.Comment.last_edit_time),
|
||||
'edit-date': search_util.create_date_filter(db.Comment.last_edit_time),
|
||||
'edit-time': search_util.create_date_filter(db.Comment.last_edit_time),
|
||||
'creation-date':
|
||||
search_util.create_date_filter(db.Comment.creation_time),
|
||||
'creation-time':
|
||||
search_util.create_date_filter(db.Comment.creation_time),
|
||||
'last-edit-date':
|
||||
search_util.create_date_filter(db.Comment.last_edit_time),
|
||||
'last-edit-time':
|
||||
search_util.create_date_filter(db.Comment.last_edit_time),
|
||||
'edit-date':
|
||||
search_util.create_date_filter(db.Comment.last_edit_time),
|
||||
'edit-time':
|
||||
search_util.create_date_filter(db.Comment.last_edit_time),
|
||||
}
|
||||
|
||||
@property
|
||||
|
|
|
@ -6,6 +6,7 @@ from szurubooru.search import criteria, tokens
|
|||
from szurubooru.search.configs import util as search_util
|
||||
from szurubooru.search.configs.base_search_config import BaseSearchConfig
|
||||
|
||||
|
||||
def _enum_transformer(available_values, value):
|
||||
try:
|
||||
return available_values[value.lower()]
|
||||
|
@ -14,6 +15,7 @@ def _enum_transformer(available_values, value):
|
|||
'Invalid value: %r. Possible values: %r.' % (
|
||||
value, list(sorted(available_values.keys()))))
|
||||
|
||||
|
||||
def _type_transformer(value):
|
||||
available_values = {
|
||||
'image': db.Post.TYPE_IMAGE,
|
||||
|
@ -28,6 +30,7 @@ def _type_transformer(value):
|
|||
}
|
||||
return _enum_transformer(available_values, value)
|
||||
|
||||
|
||||
def _safety_transformer(value):
|
||||
available_values = {
|
||||
'safe': db.Post.SAFETY_SAFE,
|
||||
|
@ -37,11 +40,12 @@ def _safety_transformer(value):
|
|||
}
|
||||
return _enum_transformer(available_values, value)
|
||||
|
||||
|
||||
def _create_score_filter(score):
|
||||
def wrapper(query, criterion, negated):
|
||||
if not getattr(criterion, 'internal', False):
|
||||
raise errors.SearchError(
|
||||
'Votes cannot be seen publicly. Did you mean %r?' \
|
||||
'Votes cannot be seen publicly. Did you mean %r?'
|
||||
% 'special:liked')
|
||||
user_alias = aliased(db.User)
|
||||
score_alias = aliased(db.PostScore)
|
||||
|
@ -57,6 +61,7 @@ def _create_score_filter(score):
|
|||
return ret
|
||||
return wrapper
|
||||
|
||||
|
||||
class PostSearchConfig(BaseSearchConfig):
|
||||
def on_search_query_parsed(self, search_query):
|
||||
new_special_tokens = []
|
||||
|
@ -64,7 +69,8 @@ class PostSearchConfig(BaseSearchConfig):
|
|||
if token.value in ('fav', 'liked', 'disliked'):
|
||||
assert self.user
|
||||
if self.user.rank == 'anonymous':
|
||||
raise errors.SearchError('Must be logged in to use this feature.')
|
||||
raise errors.SearchError(
|
||||
'Must be logged in to use this feature.')
|
||||
criterion = criteria.PlainCriterion(
|
||||
original_text=self.user.name,
|
||||
value=self.user.name)
|
||||
|
@ -99,8 +105,7 @@ class PostSearchConfig(BaseSearchConfig):
|
|||
lazyload(db.Post.user),
|
||||
lazyload(db.Post.relations),
|
||||
lazyload(db.Post.notes),
|
||||
lazyload(db.Post.favorited_by),
|
||||
)
|
||||
lazyload(db.Post.favorited_by))
|
||||
|
||||
def create_count_query(self):
|
||||
return db.session.query(db.Post)
|
||||
|
@ -153,12 +158,18 @@ class PostSearchConfig(BaseSearchConfig):
|
|||
'liked': _create_score_filter(1),
|
||||
'disliked': _create_score_filter(-1),
|
||||
'tag-count': search_util.create_num_filter(db.Post.tag_count),
|
||||
'comment-count': search_util.create_num_filter(db.Post.comment_count),
|
||||
'fav-count': search_util.create_num_filter(db.Post.favorite_count),
|
||||
'comment-count':
|
||||
search_util.create_num_filter(db.Post.comment_count),
|
||||
'fav-count':
|
||||
search_util.create_num_filter(db.Post.favorite_count),
|
||||
'note-count': search_util.create_num_filter(db.Post.note_count),
|
||||
'relation-count': search_util.create_num_filter(db.Post.relation_count),
|
||||
'feature-count': search_util.create_num_filter(db.Post.feature_count),
|
||||
'type': search_util.create_str_filter(db.Post.type, _type_transformer),
|
||||
'relation-count':
|
||||
search_util.create_num_filter(db.Post.relation_count),
|
||||
'feature-count':
|
||||
search_util.create_num_filter(db.Post.feature_count),
|
||||
'type':
|
||||
search_util.create_str_filter(
|
||||
db.Post.type, _type_transformer),
|
||||
'file-size': search_util.create_num_filter(db.Post.file_size),
|
||||
('image-width', 'width'):
|
||||
search_util.create_num_filter(db.Post.canvas_width),
|
||||
|
@ -171,13 +182,15 @@ class PostSearchConfig(BaseSearchConfig):
|
|||
('last-edit-date', 'last-edit-time', 'edit-date', 'edit-time'):
|
||||
search_util.create_date_filter(db.Post.last_edit_time),
|
||||
('comment-date', 'comment-time'):
|
||||
search_util.create_date_filter(db.Post.last_comment_creation_time),
|
||||
search_util.create_date_filter(
|
||||
db.Post.last_comment_creation_time),
|
||||
('fav-date', 'fav-time'):
|
||||
search_util.create_date_filter(db.Post.last_favorite_time),
|
||||
('feature-date', 'feature-time'):
|
||||
search_util.create_date_filter(db.Post.last_feature_time),
|
||||
('safety', 'rating'):
|
||||
search_util.create_str_filter(db.Post.safety, _safety_transformer),
|
||||
search_util.create_str_filter(
|
||||
db.Post.safety, _safety_transformer),
|
||||
})
|
||||
|
||||
@property
|
||||
|
@ -193,9 +206,12 @@ class PostSearchConfig(BaseSearchConfig):
|
|||
'relation-count': (db.Post.relation_count, self.SORT_DESC),
|
||||
'feature-count': (db.Post.feature_count, self.SORT_DESC),
|
||||
'file-size': (db.Post.file_size, self.SORT_DESC),
|
||||
('image-width', 'width'): (db.Post.canvas_width, self.SORT_DESC),
|
||||
('image-height', 'height'): (db.Post.canvas_height, self.SORT_DESC),
|
||||
('image-area', 'area'): (db.Post.canvas_area, self.SORT_DESC),
|
||||
('image-width', 'width'):
|
||||
(db.Post.canvas_width, self.SORT_DESC),
|
||||
('image-height', 'height'):
|
||||
(db.Post.canvas_height, self.SORT_DESC),
|
||||
('image-area', 'area'):
|
||||
(db.Post.canvas_area, self.SORT_DESC),
|
||||
('creation-date', 'creation-time', 'date', 'time'):
|
||||
(db.Post.creation_time, self.SORT_DESC),
|
||||
('last-edit-date', 'last-edit-time', 'edit-date', 'edit-time'):
|
||||
|
|
|
@ -2,6 +2,7 @@ from szurubooru import db
|
|||
from szurubooru.search.configs import util as search_util
|
||||
from szurubooru.search.configs.base_search_config import BaseSearchConfig
|
||||
|
||||
|
||||
class SnapshotSearchConfig(BaseSearchConfig):
|
||||
def create_filter_query(self):
|
||||
return db.session.query(db.Snapshot)
|
||||
|
|
|
@ -5,6 +5,7 @@ from szurubooru.func import util
|
|||
from szurubooru.search.configs import util as search_util
|
||||
from szurubooru.search.configs.base_search_config import BaseSearchConfig
|
||||
|
||||
|
||||
class TagSearchConfig(BaseSearchConfig):
|
||||
def create_filter_query(self):
|
||||
return self.create_count_query() \
|
||||
|
@ -13,8 +14,7 @@ class TagSearchConfig(BaseSearchConfig):
|
|||
subqueryload(db.Tag.names),
|
||||
subqueryload(db.Tag.category),
|
||||
subqueryload(db.Tag.suggestions).joinedload(db.Tag.names),
|
||||
subqueryload(db.Tag.implications).joinedload(db.Tag.names)
|
||||
)
|
||||
subqueryload(db.Tag.implications).joinedload(db.Tag.names))
|
||||
|
||||
def create_count_query(self):
|
||||
return db.session.query(db.Tag)
|
||||
|
|
|
@ -3,6 +3,7 @@ from szurubooru import db
|
|||
from szurubooru.search.configs import util as search_util
|
||||
from szurubooru.search.configs.base_search_config import BaseSearchConfig
|
||||
|
||||
|
||||
class UserSearchConfig(BaseSearchConfig):
|
||||
''' Executes searches related to the users. '''
|
||||
|
||||
|
@ -20,12 +21,18 @@ class UserSearchConfig(BaseSearchConfig):
|
|||
def named_filters(self):
|
||||
return {
|
||||
'name': search_util.create_str_filter(db.User.name),
|
||||
'creation-date': search_util.create_date_filter(db.User.creation_time),
|
||||
'creation-time': search_util.create_date_filter(db.User.creation_time),
|
||||
'last-login-date': search_util.create_date_filter(db.User.last_login_time),
|
||||
'last-login-time': search_util.create_date_filter(db.User.last_login_time),
|
||||
'login-date': search_util.create_date_filter(db.User.last_login_time),
|
||||
'login-time': search_util.create_date_filter(db.User.last_login_time),
|
||||
'creation-date':
|
||||
search_util.create_date_filter(db.User.creation_time),
|
||||
'creation-time':
|
||||
search_util.create_date_filter(db.User.creation_time),
|
||||
'last-login-date':
|
||||
search_util.create_date_filter(db.User.last_login_time),
|
||||
'last-login-time':
|
||||
search_util.create_date_filter(db.User.last_login_time),
|
||||
'login-date':
|
||||
search_util.create_date_filter(db.User.last_login_time),
|
||||
'login-time':
|
||||
search_util.create_date_filter(db.User.last_login_time),
|
||||
}
|
||||
|
||||
@property
|
||||
|
|
|
@ -3,9 +3,11 @@ from szurubooru import db, errors
|
|||
from szurubooru.func import util
|
||||
from szurubooru.search import criteria
|
||||
|
||||
|
||||
def wildcard_transformer(value):
|
||||
return value.replace('*', '%')
|
||||
|
||||
|
||||
def apply_num_criterion_to_column(column, criterion):
|
||||
'''
|
||||
Decorate SQLAlchemy filter on given column using supplied criterion.
|
||||
|
@ -32,6 +34,7 @@ def apply_num_criterion_to_column(column, criterion):
|
|||
'Criterion value %r must be a number.' % (criterion,))
|
||||
return expr
|
||||
|
||||
|
||||
def create_num_filter(column):
|
||||
def wrapper(query, criterion, negated):
|
||||
expr = apply_num_criterion_to_column(
|
||||
|
@ -41,6 +44,7 @@ def create_num_filter(column):
|
|||
return query.filter(expr)
|
||||
return wrapper
|
||||
|
||||
|
||||
def apply_str_criterion_to_column(
|
||||
column, criterion, transformer=wildcard_transformer):
|
||||
'''
|
||||
|
@ -59,6 +63,7 @@ def apply_str_criterion_to_column(
|
|||
assert False
|
||||
return expr
|
||||
|
||||
|
||||
def create_str_filter(column, transformer=wildcard_transformer):
|
||||
def wrapper(query, criterion, negated):
|
||||
expr = apply_str_criterion_to_column(
|
||||
|
@ -68,6 +73,7 @@ def create_str_filter(column, transformer=wildcard_transformer):
|
|||
return query.filter(expr)
|
||||
return wrapper
|
||||
|
||||
|
||||
def apply_date_criterion_to_column(column, criterion):
|
||||
'''
|
||||
Decorate SQLAlchemy filter on given column using supplied criterion.
|
||||
|
@ -97,6 +103,7 @@ def apply_date_criterion_to_column(column, criterion):
|
|||
assert False
|
||||
return expr
|
||||
|
||||
|
||||
def create_date_filter(column):
|
||||
def wrapper(query, criterion, negated):
|
||||
expr = apply_date_criterion_to_column(
|
||||
|
@ -106,6 +113,7 @@ def create_date_filter(column):
|
|||
return query.filter(expr)
|
||||
return wrapper
|
||||
|
||||
|
||||
def create_subquery_filter(
|
||||
left_id_column,
|
||||
right_id_column,
|
||||
|
@ -113,6 +121,7 @@ def create_subquery_filter(
|
|||
filter_factory,
|
||||
subquery_decorator=None):
|
||||
filter_func = filter_factory(filter_column)
|
||||
|
||||
def wrapper(query, criterion, negated):
|
||||
subquery = db.session.query(right_id_column.label('foreign_id'))
|
||||
if subquery_decorator:
|
||||
|
@ -121,4 +130,5 @@ def create_subquery_filter(
|
|||
subquery = filter_func(subquery, criterion, negated)
|
||||
subquery = subquery.subquery('t')
|
||||
return query.filter(left_id_column.in_(subquery))
|
||||
|
||||
return wrapper
|
||||
|
|
|
@ -5,6 +5,7 @@ class _BaseCriterion(object):
|
|||
def __repr__(self):
|
||||
return self.original_text
|
||||
|
||||
|
||||
class RangedCriterion(_BaseCriterion):
|
||||
def __init__(self, original_text, min_value, max_value):
|
||||
super().__init__(original_text)
|
||||
|
@ -14,6 +15,7 @@ class RangedCriterion(_BaseCriterion):
|
|||
def __hash__(self):
|
||||
return hash(('range', self.min_value, self.max_value))
|
||||
|
||||
|
||||
class PlainCriterion(_BaseCriterion):
|
||||
def __init__(self, original_text, value):
|
||||
super().__init__(original_text)
|
||||
|
@ -22,6 +24,7 @@ class PlainCriterion(_BaseCriterion):
|
|||
def __hash__(self):
|
||||
return hash(self.value)
|
||||
|
||||
|
||||
class ArrayCriterion(_BaseCriterion):
|
||||
def __init__(self, original_text, values):
|
||||
super().__init__(original_text)
|
||||
|
|
|
@ -3,19 +3,22 @@ from szurubooru import db, errors
|
|||
from szurubooru.func import cache
|
||||
from szurubooru.search import tokens, parser
|
||||
|
||||
|
||||
def _format_dict_keys(source):
|
||||
return list(sorted(source.keys()))
|
||||
|
||||
def _get_direction(direction, default_direction):
|
||||
if direction == tokens.SortToken.SORT_DEFAULT:
|
||||
return default_direction
|
||||
if direction == tokens.SortToken.SORT_NEGATED_DEFAULT:
|
||||
if default_direction == tokens.SortToken.SORT_ASC:
|
||||
|
||||
def _get_order(order, default_order):
|
||||
if order == tokens.SortToken.SORT_DEFAULT:
|
||||
return default_order
|
||||
if order == tokens.SortToken.SORT_NEGATED_DEFAULT:
|
||||
if default_order == tokens.SortToken.SORT_ASC:
|
||||
return tokens.SortToken.SORT_DESC
|
||||
elif default_direction == tokens.SortToken.SORT_DESC:
|
||||
elif default_order == tokens.SortToken.SORT_DESC:
|
||||
return tokens.SortToken.SORT_ASC
|
||||
assert False
|
||||
return direction
|
||||
return order
|
||||
|
||||
|
||||
class Executor(object):
|
||||
'''
|
||||
|
@ -30,20 +33,26 @@ class Executor(object):
|
|||
def get_around(self, query_text, entity_id):
|
||||
search_query = self.parser.parse(query_text)
|
||||
self.config.on_search_query_parsed(search_query)
|
||||
filter_query = self.config \
|
||||
.create_around_query() \
|
||||
.options(sqlalchemy.orm.lazyload('*'))
|
||||
filter_query = self._prepare_db_query(filter_query, search_query, False)
|
||||
prev_filter_query = filter_query \
|
||||
.filter(self.config.id_column < entity_id) \
|
||||
.order_by(None) \
|
||||
.order_by(sqlalchemy.func.abs(self.config.id_column - entity_id).asc()) \
|
||||
.limit(1)
|
||||
next_filter_query = filter_query \
|
||||
.filter(self.config.id_column > entity_id) \
|
||||
.order_by(None) \
|
||||
.order_by(sqlalchemy.func.abs(self.config.id_column - entity_id).asc()) \
|
||||
.limit(1)
|
||||
filter_query = (
|
||||
self.config
|
||||
.create_around_query()
|
||||
.options(sqlalchemy.orm.lazyload('*')))
|
||||
filter_query = self._prepare_db_query(
|
||||
filter_query, search_query, False)
|
||||
prev_filter_query = (
|
||||
filter_query
|
||||
.filter(self.config.id_column < entity_id)
|
||||
.order_by(None)
|
||||
.order_by(sqlalchemy.func.abs(
|
||||
self.config.id_column - entity_id).asc())
|
||||
.limit(1))
|
||||
next_filter_query = (
|
||||
filter_query
|
||||
.filter(self.config.id_column > entity_id)
|
||||
.order_by(None)
|
||||
.order_by(sqlalchemy.func.abs(
|
||||
self.config.id_column - entity_id).asc())
|
||||
.limit(1))
|
||||
return [
|
||||
next_filter_query.one_or_none(),
|
||||
prev_filter_query.one_or_none()]
|
||||
|
@ -92,7 +101,8 @@ class Executor(object):
|
|||
def execute_and_serialize(self, ctx, serializer):
|
||||
query = ctx.get_param_as_string('query')
|
||||
page = ctx.get_param_as_int('page', default=1, min=1)
|
||||
page_size = ctx.get_param_as_int('pageSize', default=100, min=1, max=100)
|
||||
page_size = ctx.get_param_as_int(
|
||||
'pageSize', default=100, min=1, max=100)
|
||||
count, entities = self.execute(query, page, page_size)
|
||||
return {
|
||||
'query': query,
|
||||
|
@ -124,7 +134,8 @@ class Executor(object):
|
|||
for token in search_query.special_tokens:
|
||||
if token.value not in self.config.special_filters:
|
||||
raise errors.SearchError(
|
||||
'Unknown special token: %r. Available special tokens: %r.' % (
|
||||
'Unknown special token: %r. '
|
||||
'Available special tokens: %r.' % (
|
||||
token.value,
|
||||
_format_dict_keys(self.config.special_filters)))
|
||||
db_query = self.config.special_filters[token.value](
|
||||
|
@ -134,14 +145,15 @@ class Executor(object):
|
|||
for token in search_query.sort_tokens:
|
||||
if token.name not in self.config.sort_columns:
|
||||
raise errors.SearchError(
|
||||
'Unknown sort token: %r. Available sort tokens: %r.' % (
|
||||
'Unknown sort token: %r. '
|
||||
'Available sort tokens: %r.' % (
|
||||
token.name,
|
||||
_format_dict_keys(self.config.sort_columns)))
|
||||
column, default_direction = self.config.sort_columns[token.name]
|
||||
direction = _get_direction(token.direction, default_direction)
|
||||
if direction == token.SORT_ASC:
|
||||
column, default_order = self.config.sort_columns[token.name]
|
||||
order = _get_order(token.order, default_order)
|
||||
if order == token.SORT_ASC:
|
||||
db_query = db_query.order_by(column.asc())
|
||||
elif direction == token.SORT_DESC:
|
||||
elif order == token.SORT_DESC:
|
||||
db_query = db_query.order_by(column.desc())
|
||||
|
||||
db_query = self.config.finalize_query(db_query)
|
||||
|
|
|
@ -2,6 +2,7 @@ import re
|
|||
from szurubooru import errors
|
||||
from szurubooru.search import criteria, tokens
|
||||
|
||||
|
||||
def _create_criterion(original_value, value):
|
||||
if '..' in value:
|
||||
low, high = value.split('..', 1)
|
||||
|
@ -13,10 +14,12 @@ def _create_criterion(original_value, value):
|
|||
original_value, value.split(','))
|
||||
return criteria.PlainCriterion(original_value, value)
|
||||
|
||||
|
||||
def _parse_anonymous(value, negated):
|
||||
criterion = _create_criterion(value, value)
|
||||
return tokens.AnonymousToken(criterion, negated)
|
||||
|
||||
|
||||
def _parse_named(key, value, negated):
|
||||
original_value = value
|
||||
if key.endswith('-min'):
|
||||
|
@ -28,34 +31,41 @@ def _parse_named(key, value, negated):
|
|||
criterion = _create_criterion(original_value, value)
|
||||
return tokens.NamedToken(key, criterion, negated)
|
||||
|
||||
|
||||
def _parse_special(value, negated):
|
||||
return tokens.SpecialToken(value, negated)
|
||||
|
||||
|
||||
def _parse_sort(value, negated):
|
||||
if value.count(',') == 0:
|
||||
direction_str = None
|
||||
order_str = None
|
||||
elif value.count(',') == 1:
|
||||
value, direction_str = value.split(',')
|
||||
value, order_str = value.split(',')
|
||||
else:
|
||||
raise errors.SearchError('Too many commas in sort style token.')
|
||||
try:
|
||||
direction = {
|
||||
order = {
|
||||
'asc': tokens.SortToken.SORT_ASC,
|
||||
'desc': tokens.SortToken.SORT_DESC,
|
||||
'': tokens.SortToken.SORT_DEFAULT,
|
||||
None: tokens.SortToken.SORT_DEFAULT,
|
||||
}[direction_str]
|
||||
}[order_str]
|
||||
except KeyError:
|
||||
raise errors.SearchError(
|
||||
'Unknown search direction: %r.' % direction_str)
|
||||
'Unknown search direction: %r.' % order_str)
|
||||
if negated:
|
||||
direction = {
|
||||
tokens.SortToken.SORT_ASC: tokens.SortToken.SORT_DESC,
|
||||
tokens.SortToken.SORT_DESC: tokens.SortToken.SORT_ASC,
|
||||
tokens.SortToken.SORT_DEFAULT: tokens.SortToken.SORT_NEGATED_DEFAULT,
|
||||
tokens.SortToken.SORT_NEGATED_DEFAULT: tokens.SortToken.SORT_DEFAULT,
|
||||
}[direction]
|
||||
return tokens.SortToken(value, direction)
|
||||
order = {
|
||||
tokens.SortToken.SORT_ASC:
|
||||
tokens.SortToken.SORT_DESC,
|
||||
tokens.SortToken.SORT_DESC:
|
||||
tokens.SortToken.SORT_ASC,
|
||||
tokens.SortToken.SORT_DEFAULT:
|
||||
tokens.SortToken.SORT_NEGATED_DEFAULT,
|
||||
tokens.SortToken.SORT_NEGATED_DEFAULT:
|
||||
tokens.SortToken.SORT_DEFAULT,
|
||||
}[order]
|
||||
return tokens.SortToken(value, order)
|
||||
|
||||
|
||||
class SearchQuery():
|
||||
def __init__(self):
|
||||
|
@ -71,6 +81,7 @@ class SearchQuery():
|
|||
tuple(self.special_tokens),
|
||||
tuple(self.sort_tokens)))
|
||||
|
||||
|
||||
class Parser(object):
|
||||
def parse(self, query_text):
|
||||
query = SearchQuery()
|
||||
|
@ -93,5 +104,6 @@ class Parser(object):
|
|||
query.named_tokens.append(
|
||||
_parse_named(key, value, negated))
|
||||
else:
|
||||
query.anonymous_tokens.append(_parse_anonymous(chunk, negated))
|
||||
query.anonymous_tokens.append(
|
||||
_parse_anonymous(chunk, negated))
|
||||
return query
|
||||
|
|
|
@ -6,6 +6,7 @@ class AnonymousToken(object):
|
|||
def __hash__(self):
|
||||
return hash((self.criterion, self.negated))
|
||||
|
||||
|
||||
class NamedToken(AnonymousToken):
|
||||
def __init__(self, name, criterion, negated):
|
||||
super().__init__(criterion, negated)
|
||||
|
@ -14,18 +15,20 @@ class NamedToken(AnonymousToken):
|
|||
def __hash__(self):
|
||||
return hash((self.name, self.criterion, self.negated))
|
||||
|
||||
|
||||
class SortToken(object):
|
||||
SORT_DESC = 'desc'
|
||||
SORT_ASC = 'asc'
|
||||
SORT_DEFAULT = 'default'
|
||||
SORT_NEGATED_DEFAULT = 'negated default'
|
||||
|
||||
def __init__(self, name, direction):
|
||||
def __init__(self, name, order):
|
||||
self.name = name
|
||||
self.direction = direction
|
||||
self.order = order
|
||||
|
||||
def __hash__(self):
|
||||
return hash((self.name, self.direction))
|
||||
return hash((self.name, self.order))
|
||||
|
||||
|
||||
class SpecialToken(object):
|
||||
def __init__(self, value, negated):
|
||||
|
|
0
server/szurubooru/tests/__init__.py
Normal file
0
server/szurubooru/tests/__init__.py
Normal file
0
server/szurubooru/tests/api/__init__.py
Normal file
0
server/szurubooru/tests/api/__init__.py
Normal file
|
@ -1,20 +1,22 @@
|
|||
import pytest
|
||||
import unittest.mock
|
||||
from datetime import datetime
|
||||
from unittest.mock import patch
|
||||
import pytest
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import comments, posts
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({'privileges': {'comments:create': db.User.RANK_REGULAR}})
|
||||
|
||||
|
||||
def test_creating_comment(
|
||||
user_factory, post_factory, context_factory, fake_datetime):
|
||||
post = post_factory()
|
||||
user = user_factory(rank=db.User.RANK_REGULAR)
|
||||
db.session.add_all([post, user])
|
||||
db.session.flush()
|
||||
with unittest.mock.patch('szurubooru.func.comments.serialize_comment'), \
|
||||
with patch('szurubooru.func.comments.serialize_comment'), \
|
||||
fake_datetime('1997-01-01'):
|
||||
comments.serialize_comment.return_value = 'serialized comment'
|
||||
result = api.comment_api.create_comment(
|
||||
|
@ -29,6 +31,7 @@ def test_creating_comment(
|
|||
assert comment.user and comment.user.user_id == user.user_id
|
||||
assert comment.post and comment.post.post_id == post.post_id
|
||||
|
||||
|
||||
@pytest.mark.parametrize('params', [
|
||||
{'text': None},
|
||||
{'text': ''},
|
||||
|
@ -48,6 +51,7 @@ def test_trying_to_pass_invalid_params(
|
|||
api.comment_api.create_comment(
|
||||
context_factory(params=real_params, user=user))
|
||||
|
||||
|
||||
@pytest.mark.parametrize('field', ['text', 'postId'])
|
||||
def test_trying_to_omit_mandatory_field(user_factory, context_factory, field):
|
||||
params = {
|
||||
|
@ -61,6 +65,7 @@ def test_trying_to_omit_mandatory_field(user_factory, context_factory, field):
|
|||
params={},
|
||||
user=user_factory(rank=db.User.RANK_REGULAR)))
|
||||
|
||||
|
||||
def test_trying_to_comment_non_existing(user_factory, context_factory):
|
||||
user = user_factory(rank=db.User.RANK_REGULAR)
|
||||
db.session.add_all([user])
|
||||
|
@ -70,6 +75,7 @@ def test_trying_to_comment_non_existing(user_factory, context_factory):
|
|||
context_factory(
|
||||
params={'text': 'bad', 'postId': 5}, user=user))
|
||||
|
||||
|
||||
def test_trying_to_create_without_privileges(user_factory, context_factory):
|
||||
with pytest.raises(errors.AuthError):
|
||||
api.comment_api.create_comment(
|
||||
|
|
|
@ -2,6 +2,7 @@ import pytest
|
|||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import comments
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({
|
||||
|
@ -11,6 +12,7 @@ def inject_config(config_injector):
|
|||
},
|
||||
})
|
||||
|
||||
|
||||
def test_deleting_own_comment(user_factory, comment_factory, context_factory):
|
||||
user = user_factory()
|
||||
comment = comment_factory(user=user)
|
||||
|
@ -22,6 +24,7 @@ def test_deleting_own_comment(user_factory, comment_factory, context_factory):
|
|||
assert result == {}
|
||||
assert db.session.query(db.Comment).count() == 0
|
||||
|
||||
|
||||
def test_deleting_someones_else_comment(
|
||||
user_factory, comment_factory, context_factory):
|
||||
user1 = user_factory(rank=db.User.RANK_REGULAR)
|
||||
|
@ -29,11 +32,12 @@ def test_deleting_someones_else_comment(
|
|||
comment = comment_factory(user=user1)
|
||||
db.session.add(comment)
|
||||
db.session.commit()
|
||||
result = api.comment_api.delete_comment(
|
||||
api.comment_api.delete_comment(
|
||||
context_factory(params={'version': 1}, user=user2),
|
||||
{'comment_id': comment.comment_id})
|
||||
assert db.session.query(db.Comment).count() == 0
|
||||
|
||||
|
||||
def test_trying_to_delete_someones_else_comment_without_privileges(
|
||||
user_factory, comment_factory, context_factory):
|
||||
user1 = user_factory(rank=db.User.RANK_REGULAR)
|
||||
|
@ -47,6 +51,7 @@ def test_trying_to_delete_someones_else_comment_without_privileges(
|
|||
{'comment_id': comment.comment_id})
|
||||
assert db.session.query(db.Comment).count() == 1
|
||||
|
||||
|
||||
def test_trying_to_delete_non_existing(user_factory, context_factory):
|
||||
with pytest.raises(comments.CommentNotFoundError):
|
||||
api.comment_api.delete_comment(
|
||||
|
|
|
@ -1,21 +1,23 @@
|
|||
from unittest.mock import patch
|
||||
import pytest
|
||||
import unittest.mock
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import comments, scores
|
||||
from szurubooru.func import comments
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({'privileges': {'comments:score': db.User.RANK_REGULAR}})
|
||||
|
||||
|
||||
def test_simple_rating(
|
||||
user_factory, comment_factory, context_factory, fake_datetime):
|
||||
user = user_factory(rank=db.User.RANK_REGULAR)
|
||||
comment = comment_factory(user=user)
|
||||
db.session.add(comment)
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.comments.serialize_comment'):
|
||||
with patch('szurubooru.func.comments.serialize_comment'), \
|
||||
fake_datetime('1997-12-01'):
|
||||
comments.serialize_comment.return_value = 'serialized comment'
|
||||
with fake_datetime('1997-12-01'):
|
||||
result = api.comment_api.set_comment_score(
|
||||
context_factory(params={'score': 1}, user=user),
|
||||
{'comment_id': comment.comment_id})
|
||||
|
@ -24,63 +26,67 @@ def test_simple_rating(
|
|||
assert comment is not None
|
||||
assert comment.score == 1
|
||||
|
||||
|
||||
def test_updating_rating(
|
||||
user_factory, comment_factory, context_factory, fake_datetime):
|
||||
user = user_factory(rank=db.User.RANK_REGULAR)
|
||||
comment = comment_factory(user=user)
|
||||
db.session.add(comment)
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.comments.serialize_comment'):
|
||||
with patch('szurubooru.func.comments.serialize_comment'):
|
||||
with fake_datetime('1997-12-01'):
|
||||
result = api.comment_api.set_comment_score(
|
||||
api.comment_api.set_comment_score(
|
||||
context_factory(params={'score': 1}, user=user),
|
||||
{'comment_id': comment.comment_id})
|
||||
with fake_datetime('1997-12-02'):
|
||||
result = api.comment_api.set_comment_score(
|
||||
api.comment_api.set_comment_score(
|
||||
context_factory(params={'score': -1}, user=user),
|
||||
{'comment_id': comment.comment_id})
|
||||
comment = db.session.query(db.Comment).one()
|
||||
assert db.session.query(db.CommentScore).count() == 1
|
||||
assert comment.score == -1
|
||||
|
||||
|
||||
def test_updating_rating_to_zero(
|
||||
user_factory, comment_factory, context_factory, fake_datetime):
|
||||
user = user_factory(rank=db.User.RANK_REGULAR)
|
||||
comment = comment_factory(user=user)
|
||||
db.session.add(comment)
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.comments.serialize_comment'):
|
||||
with patch('szurubooru.func.comments.serialize_comment'):
|
||||
with fake_datetime('1997-12-01'):
|
||||
result = api.comment_api.set_comment_score(
|
||||
api.comment_api.set_comment_score(
|
||||
context_factory(params={'score': 1}, user=user),
|
||||
{'comment_id': comment.comment_id})
|
||||
with fake_datetime('1997-12-02'):
|
||||
result = api.comment_api.set_comment_score(
|
||||
api.comment_api.set_comment_score(
|
||||
context_factory(params={'score': 0}, user=user),
|
||||
{'comment_id': comment.comment_id})
|
||||
comment = db.session.query(db.Comment).one()
|
||||
assert db.session.query(db.CommentScore).count() == 0
|
||||
assert comment.score == 0
|
||||
|
||||
|
||||
def test_deleting_rating(
|
||||
user_factory, comment_factory, context_factory, fake_datetime):
|
||||
user = user_factory(rank=db.User.RANK_REGULAR)
|
||||
comment = comment_factory(user=user)
|
||||
db.session.add(comment)
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.comments.serialize_comment'):
|
||||
with patch('szurubooru.func.comments.serialize_comment'):
|
||||
with fake_datetime('1997-12-01'):
|
||||
result = api.comment_api.set_comment_score(
|
||||
api.comment_api.set_comment_score(
|
||||
context_factory(params={'score': 1}, user=user),
|
||||
{'comment_id': comment.comment_id})
|
||||
with fake_datetime('1997-12-02'):
|
||||
result = api.comment_api.delete_comment_score(
|
||||
api.comment_api.delete_comment_score(
|
||||
context_factory(user=user),
|
||||
{'comment_id': comment.comment_id})
|
||||
comment = db.session.query(db.Comment).one()
|
||||
assert db.session.query(db.CommentScore).count() == 0
|
||||
assert comment.score == 0
|
||||
|
||||
|
||||
def test_ratings_from_multiple_users(
|
||||
user_factory, comment_factory, context_factory, fake_datetime):
|
||||
user1 = user_factory(rank=db.User.RANK_REGULAR)
|
||||
|
@ -88,19 +94,20 @@ def test_ratings_from_multiple_users(
|
|||
comment = comment_factory()
|
||||
db.session.add_all([user1, user2, comment])
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.comments.serialize_comment'):
|
||||
with patch('szurubooru.func.comments.serialize_comment'):
|
||||
with fake_datetime('1997-12-01'):
|
||||
result = api.comment_api.set_comment_score(
|
||||
api.comment_api.set_comment_score(
|
||||
context_factory(params={'score': 1}, user=user1),
|
||||
{'comment_id': comment.comment_id})
|
||||
with fake_datetime('1997-12-02'):
|
||||
result = api.comment_api.set_comment_score(
|
||||
api.comment_api.set_comment_score(
|
||||
context_factory(params={'score': -1}, user=user2),
|
||||
{'comment_id': comment.comment_id})
|
||||
comment = db.session.query(db.Comment).one()
|
||||
assert db.session.query(db.CommentScore).count() == 2
|
||||
assert comment.score == 0
|
||||
|
||||
|
||||
def test_trying_to_omit_mandatory_field(
|
||||
user_factory, comment_factory, context_factory):
|
||||
user = user_factory()
|
||||
|
@ -112,8 +119,8 @@ def test_trying_to_omit_mandatory_field(
|
|||
context_factory(params={}, user=user),
|
||||
{'comment_id': comment.comment_id})
|
||||
|
||||
def test_trying_to_update_non_existing(
|
||||
user_factory, comment_factory, context_factory):
|
||||
|
||||
def test_trying_to_update_non_existing(user_factory, context_factory):
|
||||
with pytest.raises(comments.CommentNotFoundError):
|
||||
api.comment_api.set_comment_score(
|
||||
context_factory(
|
||||
|
@ -121,6 +128,7 @@ def test_trying_to_update_non_existing(
|
|||
user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
{'comment_id': 5})
|
||||
|
||||
|
||||
def test_trying_to_rate_without_privileges(
|
||||
user_factory, comment_factory, context_factory):
|
||||
comment = comment_factory()
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
from unittest.mock import patch
|
||||
import pytest
|
||||
import unittest.mock
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import comments
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({
|
||||
|
@ -12,11 +13,12 @@ def inject_config(config_injector):
|
|||
},
|
||||
})
|
||||
|
||||
|
||||
def test_retrieving_multiple(user_factory, comment_factory, context_factory):
|
||||
comment1 = comment_factory(text='text 1')
|
||||
comment2 = comment_factory(text='text 2')
|
||||
db.session.add_all([comment1, comment2])
|
||||
with unittest.mock.patch('szurubooru.func.comments.serialize_comment'):
|
||||
with patch('szurubooru.func.comments.serialize_comment'):
|
||||
comments.serialize_comment.return_value = 'serialized comment'
|
||||
result = api.comment_api.get_comments(
|
||||
context_factory(
|
||||
|
@ -30,6 +32,7 @@ def test_retrieving_multiple(user_factory, comment_factory, context_factory):
|
|||
'results': ['serialized comment', 'serialized comment'],
|
||||
}
|
||||
|
||||
|
||||
def test_trying_to_retrieve_multiple_without_privileges(
|
||||
user_factory, context_factory):
|
||||
with pytest.raises(errors.AuthError):
|
||||
|
@ -38,11 +41,12 @@ def test_trying_to_retrieve_multiple_without_privileges(
|
|||
params={'query': '', 'page': 1},
|
||||
user=user_factory(rank=db.User.RANK_ANONYMOUS)))
|
||||
|
||||
|
||||
def test_retrieving_single(user_factory, comment_factory, context_factory):
|
||||
comment = comment_factory(text='dummy text')
|
||||
db.session.add(comment)
|
||||
db.session.flush()
|
||||
with unittest.mock.patch('szurubooru.func.comments.serialize_comment'):
|
||||
with patch('szurubooru.func.comments.serialize_comment'):
|
||||
comments.serialize_comment.return_value = 'serialized comment'
|
||||
result = api.comment_api.get_comment(
|
||||
context_factory(
|
||||
|
@ -50,6 +54,7 @@ def test_retrieving_single(user_factory, comment_factory, context_factory):
|
|||
{'comment_id': comment.comment_id})
|
||||
assert result == 'serialized comment'
|
||||
|
||||
|
||||
def test_trying_to_retrieve_single_non_existing(user_factory, context_factory):
|
||||
with pytest.raises(comments.CommentNotFoundError):
|
||||
api.comment_api.get_comment(
|
||||
|
@ -57,6 +62,7 @@ def test_trying_to_retrieve_single_non_existing(user_factory, context_factory):
|
|||
user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
{'comment_id': 5})
|
||||
|
||||
|
||||
def test_trying_to_retrieve_single_without_privileges(
|
||||
user_factory, context_factory):
|
||||
with pytest.raises(errors.AuthError):
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
import pytest
|
||||
import unittest.mock
|
||||
from datetime import datetime
|
||||
from unittest.mock import patch
|
||||
import pytest
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import comments
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({
|
||||
|
@ -13,13 +14,14 @@ def inject_config(config_injector):
|
|||
},
|
||||
})
|
||||
|
||||
|
||||
def test_simple_updating(
|
||||
user_factory, comment_factory, context_factory, fake_datetime):
|
||||
user = user_factory(rank=db.User.RANK_REGULAR)
|
||||
comment = comment_factory(user=user)
|
||||
db.session.add(comment)
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.comments.serialize_comment'), \
|
||||
with patch('szurubooru.func.comments.serialize_comment'), \
|
||||
fake_datetime('1997-12-01'):
|
||||
comments.serialize_comment.return_value = 'serialized comment'
|
||||
result = api.comment_api.update_comment(
|
||||
|
@ -29,6 +31,7 @@ def test_simple_updating(
|
|||
assert result == 'serialized comment'
|
||||
assert comment.last_edit_time == datetime(1997, 12, 1)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('params,expected_exception', [
|
||||
({'text': None}, comments.EmptyCommentTextError),
|
||||
({'text': ''}, comments.EmptyCommentTextError),
|
||||
|
@ -37,7 +40,11 @@ def test_simple_updating(
|
|||
({'text': ['']}, comments.EmptyCommentTextError),
|
||||
])
|
||||
def test_trying_to_pass_invalid_params(
|
||||
user_factory, comment_factory, context_factory, params, expected_exception):
|
||||
user_factory,
|
||||
comment_factory,
|
||||
context_factory,
|
||||
params,
|
||||
expected_exception):
|
||||
user = user_factory()
|
||||
comment = comment_factory(user=user)
|
||||
db.session.add(comment)
|
||||
|
@ -48,6 +55,7 @@ def test_trying_to_pass_invalid_params(
|
|||
params={**params, **{'version': 1}}, user=user),
|
||||
{'comment_id': comment.comment_id})
|
||||
|
||||
|
||||
def test_trying_to_omit_mandatory_field(
|
||||
user_factory, comment_factory, context_factory):
|
||||
user = user_factory()
|
||||
|
@ -59,6 +67,7 @@ def test_trying_to_omit_mandatory_field(
|
|||
context_factory(params={'version': 1}, user=user),
|
||||
{'comment_id': comment.comment_id})
|
||||
|
||||
|
||||
def test_trying_to_update_non_existing(user_factory, context_factory):
|
||||
with pytest.raises(comments.CommentNotFoundError):
|
||||
api.comment_api.update_comment(
|
||||
|
@ -67,6 +76,7 @@ def test_trying_to_update_non_existing(user_factory, context_factory):
|
|||
user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
{'comment_id': 5})
|
||||
|
||||
|
||||
def test_trying_to_update_someones_comment_without_privileges(
|
||||
user_factory, comment_factory, context_factory):
|
||||
user = user_factory(rank=db.User.RANK_REGULAR)
|
||||
|
@ -80,6 +90,7 @@ def test_trying_to_update_someones_comment_without_privileges(
|
|||
params={'text': 'new text', 'version': 1}, user=user2),
|
||||
{'comment_id': comment.comment_id})
|
||||
|
||||
|
||||
def test_updating_someones_comment_with_privileges(
|
||||
user_factory, comment_factory, context_factory):
|
||||
user = user_factory(rank=db.User.RANK_REGULAR)
|
||||
|
@ -87,7 +98,7 @@ def test_updating_someones_comment_with_privileges(
|
|||
comment = comment_factory(user=user)
|
||||
db.session.add(comment)
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.comments.serialize_comment'):
|
||||
with patch('szurubooru.func.comments.serialize_comment'):
|
||||
api.comment_api.update_comment(
|
||||
context_factory(
|
||||
params={'text': 'new text', 'version': 1}, user=user2),
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from datetime import datetime
|
||||
from szurubooru import api, db
|
||||
|
||||
|
||||
def test_info_api(
|
||||
tmpdir, config_injector, context_factory, post_factory, fake_datetime):
|
||||
directory = tmpdir.mkdir('data')
|
||||
|
|
|
@ -1,21 +1,23 @@
|
|||
from unittest.mock import patch
|
||||
import pytest
|
||||
import unittest.mock
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import auth, mailer
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(tmpdir, config_injector):
|
||||
def inject_config(config_injector):
|
||||
config_injector({
|
||||
'secret': 'x',
|
||||
'base_url': 'http://example.com/',
|
||||
'name': 'Test instance',
|
||||
})
|
||||
|
||||
|
||||
def test_reset_sending_email(context_factory, user_factory):
|
||||
db.session.add(user_factory(
|
||||
name='u1', rank=db.User.RANK_REGULAR, email='user@example.com'))
|
||||
for initiating_user in ['u1', 'user@example.com']:
|
||||
with unittest.mock.patch('szurubooru.func.mailer.send_mail'):
|
||||
with patch('szurubooru.func.mailer.send_mail'):
|
||||
assert api.password_reset_api.start_password_reset(
|
||||
context_factory(), {'user_name': initiating_user}) == {}
|
||||
mailer.send_mail.assert_called_once_with(
|
||||
|
@ -27,17 +29,21 @@ def test_reset_sending_email(context_factory, user_factory):
|
|||
'ink: http://example.com/password-reset/u1:4ac0be176fb36' +
|
||||
'4f13ee6b634c43220e2\nOtherwise, please ignore this email.')
|
||||
|
||||
|
||||
def test_trying_to_reset_non_existing(context_factory):
|
||||
with pytest.raises(errors.NotFoundError):
|
||||
api.password_reset_api.start_password_reset(
|
||||
context_factory(), {'user_name': 'u1'})
|
||||
|
||||
|
||||
def test_trying_to_reset_without_email(context_factory, user_factory):
|
||||
db.session.add(user_factory(name='u1', rank=db.User.RANK_REGULAR, email=None))
|
||||
db.session.add(
|
||||
user_factory(name='u1', rank=db.User.RANK_REGULAR, email=None))
|
||||
with pytest.raises(errors.ValidationError):
|
||||
api.password_reset_api.start_password_reset(
|
||||
context_factory(), {'user_name': 'u1'})
|
||||
|
||||
|
||||
def test_confirming_with_good_token(context_factory, user_factory):
|
||||
user = user_factory(
|
||||
name='u1', rank=db.User.RANK_REGULAR, email='user@example.com')
|
||||
|
@ -50,11 +56,13 @@ def test_confirming_with_good_token(context_factory, user_factory):
|
|||
assert user.password_hash != old_hash
|
||||
assert auth.is_valid_password(user, result['password']) is True
|
||||
|
||||
|
||||
def test_trying_to_confirm_non_existing(context_factory):
|
||||
with pytest.raises(errors.NotFoundError):
|
||||
api.password_reset_api.finish_password_reset(
|
||||
context_factory(), {'user_name': 'u1'})
|
||||
|
||||
|
||||
def test_trying_to_confirm_without_token(context_factory, user_factory):
|
||||
db.session.add(user_factory(
|
||||
name='u1', rank=db.User.RANK_REGULAR, email='user@example.com'))
|
||||
|
@ -62,6 +70,7 @@ def test_trying_to_confirm_without_token(context_factory, user_factory):
|
|||
api.password_reset_api.finish_password_reset(
|
||||
context_factory(params={}), {'user_name': 'u1'})
|
||||
|
||||
|
||||
def test_trying_to_confirm_with_bad_token(context_factory, user_factory):
|
||||
db.session.add(user_factory(
|
||||
name='u1', rank=db.User.RANK_REGULAR, email='user@example.com'))
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
from unittest.mock import patch
|
||||
import pytest
|
||||
import unittest.mock
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import posts, tags, snapshots, net
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({
|
||||
|
@ -13,6 +14,7 @@ def inject_config(config_injector):
|
|||
},
|
||||
})
|
||||
|
||||
|
||||
def test_creating_minimal_posts(
|
||||
context_factory, post_factory, user_factory):
|
||||
auth_user = user_factory(rank=db.User.RANK_REGULAR)
|
||||
|
@ -20,16 +22,16 @@ def test_creating_minimal_posts(
|
|||
db.session.add(post)
|
||||
db.session.flush()
|
||||
|
||||
with unittest.mock.patch('szurubooru.func.posts.create_post'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_safety'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_source'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_relations'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_notes'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_flags'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_thumbnail'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.serialize_post'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.export_to_json'), \
|
||||
unittest.mock.patch('szurubooru.func.snapshots.save_entity_creation'):
|
||||
with patch('szurubooru.func.posts.create_post'), \
|
||||
patch('szurubooru.func.posts.update_post_safety'), \
|
||||
patch('szurubooru.func.posts.update_post_source'), \
|
||||
patch('szurubooru.func.posts.update_post_relations'), \
|
||||
patch('szurubooru.func.posts.update_post_notes'), \
|
||||
patch('szurubooru.func.posts.update_post_flags'), \
|
||||
patch('szurubooru.func.posts.update_post_thumbnail'), \
|
||||
patch('szurubooru.func.posts.serialize_post'), \
|
||||
patch('szurubooru.func.tags.export_to_json'), \
|
||||
patch('szurubooru.func.snapshots.save_entity_creation'):
|
||||
posts.create_post.return_value = (post, [])
|
||||
posts.serialize_post.return_value = 'serialized post'
|
||||
|
||||
|
@ -48,32 +50,36 @@ def test_creating_minimal_posts(
|
|||
assert result == 'serialized post'
|
||||
posts.create_post.assert_called_once_with(
|
||||
'post-content', ['tag1', 'tag2'], auth_user)
|
||||
posts.update_post_thumbnail.assert_called_once_with(post, 'post-thumbnail')
|
||||
posts.update_post_thumbnail.assert_called_once_with(
|
||||
post, 'post-thumbnail')
|
||||
posts.update_post_safety.assert_called_once_with(post, 'safe')
|
||||
posts.update_post_source.assert_called_once_with(post, None)
|
||||
posts.update_post_relations.assert_called_once_with(post, [])
|
||||
posts.update_post_notes.assert_called_once_with(post, [])
|
||||
posts.update_post_flags.assert_called_once_with(post, [])
|
||||
posts.update_post_thumbnail.assert_called_once_with(post, 'post-thumbnail')
|
||||
posts.serialize_post.assert_called_once_with(post, auth_user, options=None)
|
||||
posts.update_post_thumbnail.assert_called_once_with(
|
||||
post, 'post-thumbnail')
|
||||
posts.serialize_post.assert_called_once_with(
|
||||
post, auth_user, options=None)
|
||||
tags.export_to_json.assert_called_once_with()
|
||||
snapshots.save_entity_creation.assert_called_once_with(post, auth_user)
|
||||
|
||||
|
||||
def test_creating_full_posts(context_factory, post_factory, user_factory):
|
||||
auth_user = user_factory(rank=db.User.RANK_REGULAR)
|
||||
post = post_factory()
|
||||
db.session.add(post)
|
||||
db.session.flush()
|
||||
|
||||
with unittest.mock.patch('szurubooru.func.posts.create_post'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_safety'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_source'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_relations'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_notes'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_flags'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.serialize_post'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.export_to_json'), \
|
||||
unittest.mock.patch('szurubooru.func.snapshots.save_entity_creation'):
|
||||
with patch('szurubooru.func.posts.create_post'), \
|
||||
patch('szurubooru.func.posts.update_post_safety'), \
|
||||
patch('szurubooru.func.posts.update_post_source'), \
|
||||
patch('szurubooru.func.posts.update_post_relations'), \
|
||||
patch('szurubooru.func.posts.update_post_notes'), \
|
||||
patch('szurubooru.func.posts.update_post_flags'), \
|
||||
patch('szurubooru.func.posts.serialize_post'), \
|
||||
patch('szurubooru.func.tags.export_to_json'), \
|
||||
patch('szurubooru.func.snapshots.save_entity_creation'):
|
||||
posts.create_post.return_value = (post, [])
|
||||
posts.serialize_post.return_value = 'serialized post'
|
||||
|
||||
|
@ -98,12 +104,16 @@ def test_creating_full_posts(context_factory, post_factory, user_factory):
|
|||
posts.update_post_safety.assert_called_once_with(post, 'safe')
|
||||
posts.update_post_source.assert_called_once_with(post, 'source')
|
||||
posts.update_post_relations.assert_called_once_with(post, [1, 2])
|
||||
posts.update_post_notes.assert_called_once_with(post, ['note1', 'note2'])
|
||||
posts.update_post_flags.assert_called_once_with(post, ['flag1', 'flag2'])
|
||||
posts.serialize_post.assert_called_once_with(post, auth_user, options=None)
|
||||
posts.update_post_notes.assert_called_once_with(
|
||||
post, ['note1', 'note2'])
|
||||
posts.update_post_flags.assert_called_once_with(
|
||||
post, ['flag1', 'flag2'])
|
||||
posts.serialize_post.assert_called_once_with(
|
||||
post, auth_user, options=None)
|
||||
tags.export_to_json.assert_called_once_with()
|
||||
snapshots.save_entity_creation.assert_called_once_with(post, auth_user)
|
||||
|
||||
|
||||
def test_anonymous_uploads(
|
||||
config_injector, context_factory, post_factory, user_factory):
|
||||
auth_user = user_factory(rank=db.User.RANK_REGULAR)
|
||||
|
@ -111,11 +121,11 @@ def test_anonymous_uploads(
|
|||
db.session.add(post)
|
||||
db.session.flush()
|
||||
|
||||
with unittest.mock.patch('szurubooru.func.tags.export_to_json'), \
|
||||
unittest.mock.patch('szurubooru.func.snapshots.save_entity_creation'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.serialize_post'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.create_post'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_source'):
|
||||
with patch('szurubooru.func.tags.export_to_json'), \
|
||||
patch('szurubooru.func.snapshots.save_entity_creation'), \
|
||||
patch('szurubooru.func.posts.serialize_post'), \
|
||||
patch('szurubooru.func.posts.create_post'), \
|
||||
patch('szurubooru.func.posts.update_post_source'):
|
||||
config_injector({
|
||||
'privileges': {'posts:create:anonymous': db.User.RANK_REGULAR},
|
||||
})
|
||||
|
@ -134,6 +144,7 @@ def test_anonymous_uploads(
|
|||
posts.create_post.assert_called_once_with(
|
||||
'post-content', ['tag1', 'tag2'], None)
|
||||
|
||||
|
||||
def test_creating_from_url_saves_source(
|
||||
config_injector, context_factory, post_factory, user_factory):
|
||||
auth_user = user_factory(rank=db.User.RANK_REGULAR)
|
||||
|
@ -141,12 +152,12 @@ def test_creating_from_url_saves_source(
|
|||
db.session.add(post)
|
||||
db.session.flush()
|
||||
|
||||
with unittest.mock.patch('szurubooru.func.net.download'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.export_to_json'), \
|
||||
unittest.mock.patch('szurubooru.func.snapshots.save_entity_creation'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.serialize_post'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.create_post'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_source'):
|
||||
with patch('szurubooru.func.net.download'), \
|
||||
patch('szurubooru.func.tags.export_to_json'), \
|
||||
patch('szurubooru.func.snapshots.save_entity_creation'), \
|
||||
patch('szurubooru.func.posts.serialize_post'), \
|
||||
patch('szurubooru.func.posts.create_post'), \
|
||||
patch('szurubooru.func.posts.update_post_source'):
|
||||
config_injector({
|
||||
'privileges': {'posts:create:identified': db.User.RANK_REGULAR},
|
||||
})
|
||||
|
@ -165,6 +176,7 @@ def test_creating_from_url_saves_source(
|
|||
b'content', ['tag1', 'tag2'], auth_user)
|
||||
posts.update_post_source.assert_called_once_with(post, 'example.com')
|
||||
|
||||
|
||||
def test_creating_from_url_with_source_specified(
|
||||
config_injector, context_factory, post_factory, user_factory):
|
||||
auth_user = user_factory(rank=db.User.RANK_REGULAR)
|
||||
|
@ -172,12 +184,12 @@ def test_creating_from_url_with_source_specified(
|
|||
db.session.add(post)
|
||||
db.session.flush()
|
||||
|
||||
with unittest.mock.patch('szurubooru.func.net.download'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.export_to_json'), \
|
||||
unittest.mock.patch('szurubooru.func.snapshots.save_entity_creation'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.serialize_post'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.create_post'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_source'):
|
||||
with patch('szurubooru.func.net.download'), \
|
||||
patch('szurubooru.func.tags.export_to_json'), \
|
||||
patch('szurubooru.func.snapshots.save_entity_creation'), \
|
||||
patch('szurubooru.func.posts.serialize_post'), \
|
||||
patch('szurubooru.func.posts.create_post'), \
|
||||
patch('szurubooru.func.posts.update_post_source'):
|
||||
config_injector({
|
||||
'privileges': {'posts:create:identified': db.User.RANK_REGULAR},
|
||||
})
|
||||
|
@ -197,6 +209,7 @@ def test_creating_from_url_with_source_specified(
|
|||
b'content', ['tag1', 'tag2'], auth_user)
|
||||
posts.update_post_source.assert_called_once_with(post, 'example2.com')
|
||||
|
||||
|
||||
@pytest.mark.parametrize('field', ['tags', 'safety'])
|
||||
def test_trying_to_omit_mandatory_field(context_factory, user_factory, field):
|
||||
params = {
|
||||
|
@ -211,6 +224,7 @@ def test_trying_to_omit_mandatory_field(context_factory, user_factory, field):
|
|||
files={'content': '...'},
|
||||
user=user_factory(rank=db.User.RANK_REGULAR)))
|
||||
|
||||
|
||||
def test_trying_to_omit_content(context_factory, user_factory):
|
||||
with pytest.raises(errors.MissingRequiredFileError):
|
||||
api.post_api.create_post(
|
||||
|
@ -221,12 +235,15 @@ def test_trying_to_omit_content(context_factory, user_factory):
|
|||
},
|
||||
user=user_factory(rank=db.User.RANK_REGULAR)))
|
||||
|
||||
def test_trying_to_create_post_without_privileges(context_factory, user_factory):
|
||||
|
||||
def test_trying_to_create_post_without_privileges(
|
||||
context_factory, user_factory):
|
||||
with pytest.raises(errors.AuthError):
|
||||
api.post_api.create_post(context_factory(
|
||||
params='whatever',
|
||||
user=user_factory(rank=db.User.RANK_ANONYMOUS)))
|
||||
|
||||
|
||||
def test_trying_to_create_tags_without_privileges(
|
||||
config_injector, context_factory, user_factory):
|
||||
config_injector({
|
||||
|
@ -237,8 +254,8 @@ def test_trying_to_create_tags_without_privileges(
|
|||
},
|
||||
})
|
||||
with pytest.raises(errors.AuthError), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_content'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_tags'):
|
||||
patch('szurubooru.func.posts.update_post_content'), \
|
||||
patch('szurubooru.func.posts.update_post_tags'):
|
||||
posts.update_post_tags.return_value = ['new-tag']
|
||||
api.post_api.create_post(
|
||||
context_factory(
|
||||
|
|
|
@ -1,16 +1,18 @@
|
|||
from unittest.mock import patch
|
||||
import pytest
|
||||
import unittest.mock
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import posts, tags
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({'privileges': {'posts:delete': db.User.RANK_REGULAR}})
|
||||
|
||||
|
||||
def test_deleting(user_factory, post_factory, context_factory):
|
||||
db.session.add(post_factory(id=1))
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.tags.export_to_json'):
|
||||
with patch('szurubooru.func.tags.export_to_json'):
|
||||
result = api.post_api.delete_post(
|
||||
context_factory(
|
||||
params={'version': 1},
|
||||
|
@ -20,12 +22,14 @@ def test_deleting(user_factory, post_factory, context_factory):
|
|||
assert db.session.query(db.Post).count() == 0
|
||||
tags.export_to_json.assert_called_once_with()
|
||||
|
||||
|
||||
def test_trying_to_delete_non_existing(user_factory, context_factory):
|
||||
with pytest.raises(posts.PostNotFoundError):
|
||||
api.post_api.delete_post(
|
||||
context_factory(user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
{'post_id': 999})
|
||||
|
||||
|
||||
def test_trying_to_delete_without_privileges(
|
||||
user_factory, post_factory, context_factory):
|
||||
db.session.add(post_factory(id=1))
|
||||
|
|
|
@ -1,20 +1,22 @@
|
|||
import pytest
|
||||
import unittest.mock
|
||||
from datetime import datetime
|
||||
from unittest.mock import patch
|
||||
import pytest
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import posts
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({'privileges': {'posts:favorite': db.User.RANK_REGULAR}})
|
||||
|
||||
|
||||
def test_adding_to_favorites(
|
||||
user_factory, post_factory, context_factory, fake_datetime):
|
||||
post = post_factory()
|
||||
db.session.add(post)
|
||||
db.session.commit()
|
||||
assert post.score == 0
|
||||
with unittest.mock.patch('szurubooru.func.posts.serialize_post'), \
|
||||
with patch('szurubooru.func.posts.serialize_post'), \
|
||||
fake_datetime('1997-12-01'):
|
||||
posts.serialize_post.return_value = 'serialized post'
|
||||
result = api.post_api.add_post_to_favorites(
|
||||
|
@ -27,6 +29,7 @@ def test_adding_to_favorites(
|
|||
assert post.favorite_count == 1
|
||||
assert post.score == 1
|
||||
|
||||
|
||||
def test_removing_from_favorites(
|
||||
user_factory, post_factory, context_factory, fake_datetime):
|
||||
user = user_factory()
|
||||
|
@ -34,7 +37,7 @@ def test_removing_from_favorites(
|
|||
db.session.add(post)
|
||||
db.session.commit()
|
||||
assert post.score == 0
|
||||
with unittest.mock.patch('szurubooru.func.posts.serialize_post'):
|
||||
with patch('szurubooru.func.posts.serialize_post'):
|
||||
with fake_datetime('1997-12-01'):
|
||||
api.post_api.add_post_to_favorites(
|
||||
context_factory(user=user),
|
||||
|
@ -49,13 +52,14 @@ def test_removing_from_favorites(
|
|||
assert db.session.query(db.PostFavorite).count() == 0
|
||||
assert post.favorite_count == 0
|
||||
|
||||
|
||||
def test_favoriting_twice(
|
||||
user_factory, post_factory, context_factory, fake_datetime):
|
||||
user = user_factory()
|
||||
post = post_factory()
|
||||
db.session.add(post)
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.posts.serialize_post'):
|
||||
with patch('szurubooru.func.posts.serialize_post'):
|
||||
with fake_datetime('1997-12-01'):
|
||||
api.post_api.add_post_to_favorites(
|
||||
context_factory(user=user),
|
||||
|
@ -68,13 +72,14 @@ def test_favoriting_twice(
|
|||
assert db.session.query(db.PostFavorite).count() == 1
|
||||
assert post.favorite_count == 1
|
||||
|
||||
|
||||
def test_removing_twice(
|
||||
user_factory, post_factory, context_factory, fake_datetime):
|
||||
user = user_factory()
|
||||
post = post_factory()
|
||||
db.session.add(post)
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.posts.serialize_post'):
|
||||
with patch('szurubooru.func.posts.serialize_post'):
|
||||
with fake_datetime('1997-12-01'):
|
||||
api.post_api.add_post_to_favorites(
|
||||
context_factory(user=user),
|
||||
|
@ -91,6 +96,7 @@ def test_removing_twice(
|
|||
assert db.session.query(db.PostFavorite).count() == 0
|
||||
assert post.favorite_count == 0
|
||||
|
||||
|
||||
def test_favorites_from_multiple_users(
|
||||
user_factory, post_factory, context_factory, fake_datetime):
|
||||
user1 = user_factory()
|
||||
|
@ -98,7 +104,7 @@ def test_favorites_from_multiple_users(
|
|||
post = post_factory()
|
||||
db.session.add_all([user1, user2, post])
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.posts.serialize_post'):
|
||||
with patch('szurubooru.func.posts.serialize_post'):
|
||||
with fake_datetime('1997-12-01'):
|
||||
api.post_api.add_post_to_favorites(
|
||||
context_factory(user=user1),
|
||||
|
@ -112,12 +118,14 @@ def test_favorites_from_multiple_users(
|
|||
assert post.favorite_count == 2
|
||||
assert post.last_favorite_time == datetime(1997, 12, 2)
|
||||
|
||||
|
||||
def test_trying_to_update_non_existing(user_factory, context_factory):
|
||||
with pytest.raises(posts.PostNotFoundError):
|
||||
api.post_api.add_post_to_favorites(
|
||||
context_factory(user=user_factory()),
|
||||
{'post_id': 5})
|
||||
|
||||
|
||||
def test_trying_to_rate_without_privileges(
|
||||
user_factory, post_factory, context_factory):
|
||||
post = post_factory()
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
from unittest.mock import patch
|
||||
import pytest
|
||||
import unittest.mock
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import posts
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({
|
||||
|
@ -12,14 +13,12 @@ def inject_config(config_injector):
|
|||
},
|
||||
})
|
||||
|
||||
def test_no_featured_post(user_factory, post_factory, context_factory):
|
||||
assert posts.try_get_featured_post() is None
|
||||
|
||||
def test_featuring(user_factory, post_factory, context_factory):
|
||||
db.session.add(post_factory(id=1))
|
||||
db.session.commit()
|
||||
assert not posts.get_post_by_id(1).is_featured
|
||||
with unittest.mock.patch('szurubooru.func.posts.serialize_post'):
|
||||
with patch('szurubooru.func.posts.serialize_post'):
|
||||
posts.serialize_post.return_value = 'serialized post'
|
||||
result = api.post_api.set_featured_post(
|
||||
context_factory(
|
||||
|
@ -34,18 +33,19 @@ def test_featuring(user_factory, post_factory, context_factory):
|
|||
user=user_factory(rank=db.User.RANK_REGULAR)))
|
||||
assert result == 'serialized post'
|
||||
|
||||
def test_trying_to_omit_required_parameter(
|
||||
user_factory, post_factory, context_factory):
|
||||
|
||||
def test_trying_to_omit_required_parameter(user_factory, context_factory):
|
||||
with pytest.raises(errors.MissingRequiredParameterError):
|
||||
api.post_api.set_featured_post(
|
||||
context_factory(
|
||||
user=user_factory(rank=db.User.RANK_REGULAR)))
|
||||
|
||||
|
||||
def test_trying_to_feature_the_same_post_twice(
|
||||
user_factory, post_factory, context_factory):
|
||||
db.session.add(post_factory(id=1))
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.posts.serialize_post'):
|
||||
with patch('szurubooru.func.posts.serialize_post'):
|
||||
api.post_api.set_featured_post(
|
||||
context_factory(
|
||||
params={'id': 1},
|
||||
|
@ -56,6 +56,7 @@ def test_trying_to_feature_the_same_post_twice(
|
|||
params={'id': 1},
|
||||
user=user_factory(rank=db.User.RANK_REGULAR)))
|
||||
|
||||
|
||||
def test_featuring_one_post_after_another(
|
||||
user_factory, post_factory, context_factory, fake_datetime):
|
||||
db.session.add(post_factory(id=1))
|
||||
|
@ -64,14 +65,14 @@ def test_featuring_one_post_after_another(
|
|||
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 unittest.mock.patch('szurubooru.func.posts.serialize_post'):
|
||||
with patch('szurubooru.func.posts.serialize_post'):
|
||||
with fake_datetime('1997'):
|
||||
result = api.post_api.set_featured_post(
|
||||
api.post_api.set_featured_post(
|
||||
context_factory(
|
||||
params={'id': 1},
|
||||
user=user_factory(rank=db.User.RANK_REGULAR)))
|
||||
with fake_datetime('1998'):
|
||||
result = api.post_api.set_featured_post(
|
||||
api.post_api.set_featured_post(
|
||||
context_factory(
|
||||
params={'id': 2},
|
||||
user=user_factory(rank=db.User.RANK_REGULAR)))
|
||||
|
@ -80,6 +81,7 @@ def test_featuring_one_post_after_another(
|
|||
assert not posts.get_post_by_id(1).is_featured
|
||||
assert posts.get_post_by_id(2).is_featured
|
||||
|
||||
|
||||
def test_trying_to_feature_non_existing(user_factory, context_factory):
|
||||
with pytest.raises(posts.PostNotFoundError):
|
||||
api.post_api.set_featured_post(
|
||||
|
@ -87,6 +89,7 @@ def test_trying_to_feature_non_existing(user_factory, context_factory):
|
|||
params={'id': 1},
|
||||
user=user_factory(rank=db.User.RANK_REGULAR)))
|
||||
|
||||
|
||||
def test_trying_to_feature_without_privileges(user_factory, context_factory):
|
||||
with pytest.raises(errors.AuthError):
|
||||
api.post_api.set_featured_post(
|
||||
|
@ -94,6 +97,7 @@ def test_trying_to_feature_without_privileges(user_factory, context_factory):
|
|||
params={'id': 1},
|
||||
user=user_factory(rank=db.User.RANK_ANONYMOUS)))
|
||||
|
||||
|
||||
def test_getting_featured_post_without_privileges_to_view(
|
||||
user_factory, context_factory):
|
||||
api.post_api.get_featured_post(
|
||||
|
|
|
@ -1,20 +1,22 @@
|
|||
from unittest.mock import patch
|
||||
import pytest
|
||||
import unittest.mock
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import posts, scores
|
||||
from szurubooru.func import posts
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({'privileges': {'posts:score': db.User.RANK_REGULAR}})
|
||||
|
||||
|
||||
def test_simple_rating(
|
||||
user_factory, post_factory, context_factory, fake_datetime):
|
||||
post = post_factory()
|
||||
db.session.add(post)
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.posts.serialize_post'):
|
||||
with patch('szurubooru.func.posts.serialize_post'), \
|
||||
fake_datetime('1997-12-01'):
|
||||
posts.serialize_post.return_value = 'serialized post'
|
||||
with fake_datetime('1997-12-01'):
|
||||
result = api.post_api.set_post_score(
|
||||
context_factory(
|
||||
params={'score': 1}, user=user_factory()),
|
||||
|
@ -25,63 +27,67 @@ def test_simple_rating(
|
|||
assert post is not None
|
||||
assert post.score == 1
|
||||
|
||||
|
||||
def test_updating_rating(
|
||||
user_factory, post_factory, context_factory, fake_datetime):
|
||||
user = user_factory()
|
||||
post = post_factory()
|
||||
db.session.add(post)
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.posts.serialize_post'):
|
||||
with patch('szurubooru.func.posts.serialize_post'):
|
||||
with fake_datetime('1997-12-01'):
|
||||
result = api.post_api.set_post_score(
|
||||
api.post_api.set_post_score(
|
||||
context_factory(params={'score': 1}, user=user),
|
||||
{'post_id': post.post_id})
|
||||
with fake_datetime('1997-12-02'):
|
||||
result = api.post_api.set_post_score(
|
||||
api.post_api.set_post_score(
|
||||
context_factory(params={'score': -1}, user=user),
|
||||
{'post_id': post.post_id})
|
||||
post = db.session.query(db.Post).one()
|
||||
assert db.session.query(db.PostScore).count() == 1
|
||||
assert post.score == -1
|
||||
|
||||
|
||||
def test_updating_rating_to_zero(
|
||||
user_factory, post_factory, context_factory, fake_datetime):
|
||||
user = user_factory()
|
||||
post = post_factory()
|
||||
db.session.add(post)
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.posts.serialize_post'):
|
||||
with patch('szurubooru.func.posts.serialize_post'):
|
||||
with fake_datetime('1997-12-01'):
|
||||
result = api.post_api.set_post_score(
|
||||
api.post_api.set_post_score(
|
||||
context_factory(params={'score': 1}, user=user),
|
||||
{'post_id': post.post_id})
|
||||
with fake_datetime('1997-12-02'):
|
||||
result = api.post_api.set_post_score(
|
||||
api.post_api.set_post_score(
|
||||
context_factory(params={'score': 0}, user=user),
|
||||
{'post_id': post.post_id})
|
||||
post = db.session.query(db.Post).one()
|
||||
assert db.session.query(db.PostScore).count() == 0
|
||||
assert post.score == 0
|
||||
|
||||
|
||||
def test_deleting_rating(
|
||||
user_factory, post_factory, context_factory, fake_datetime):
|
||||
user = user_factory()
|
||||
post = post_factory()
|
||||
db.session.add(post)
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.posts.serialize_post'):
|
||||
with patch('szurubooru.func.posts.serialize_post'):
|
||||
with fake_datetime('1997-12-01'):
|
||||
result = api.post_api.set_post_score(
|
||||
api.post_api.set_post_score(
|
||||
context_factory(params={'score': 1}, user=user),
|
||||
{'post_id': post.post_id})
|
||||
with fake_datetime('1997-12-02'):
|
||||
result = api.post_api.delete_post_score(
|
||||
api.post_api.delete_post_score(
|
||||
context_factory(user=user),
|
||||
{'post_id': post.post_id})
|
||||
post = db.session.query(db.Post).one()
|
||||
assert db.session.query(db.PostScore).count() == 0
|
||||
assert post.score == 0
|
||||
|
||||
|
||||
def test_ratings_from_multiple_users(
|
||||
user_factory, post_factory, context_factory, fake_datetime):
|
||||
user1 = user_factory()
|
||||
|
@ -89,19 +95,20 @@ def test_ratings_from_multiple_users(
|
|||
post = post_factory()
|
||||
db.session.add_all([user1, user2, post])
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.posts.serialize_post'):
|
||||
with patch('szurubooru.func.posts.serialize_post'):
|
||||
with fake_datetime('1997-12-01'):
|
||||
result = api.post_api.set_post_score(
|
||||
api.post_api.set_post_score(
|
||||
context_factory(params={'score': 1}, user=user1),
|
||||
{'post_id': post.post_id})
|
||||
with fake_datetime('1997-12-02'):
|
||||
result = api.post_api.set_post_score(
|
||||
api.post_api.set_post_score(
|
||||
context_factory(params={'score': -1}, user=user2),
|
||||
{'post_id': post.post_id})
|
||||
post = db.session.query(db.Post).one()
|
||||
assert db.session.query(db.PostScore).count() == 2
|
||||
assert post.score == 0
|
||||
|
||||
|
||||
def test_trying_to_omit_mandatory_field(
|
||||
user_factory, post_factory, context_factory):
|
||||
post = post_factory()
|
||||
|
@ -112,13 +119,14 @@ def test_trying_to_omit_mandatory_field(
|
|||
context_factory(params={}, user=user_factory()),
|
||||
{'post_id': post.post_id})
|
||||
|
||||
def test_trying_to_update_non_existing(
|
||||
user_factory, post_factory, context_factory):
|
||||
|
||||
def test_trying_to_update_non_existing(user_factory, context_factory):
|
||||
with pytest.raises(posts.PostNotFoundError):
|
||||
api.post_api.set_post_score(
|
||||
context_factory(params={'score': 1}, user=user_factory()),
|
||||
{'post_id': 5})
|
||||
|
||||
|
||||
def test_trying_to_rate_without_privileges(
|
||||
user_factory, post_factory, context_factory):
|
||||
post = post_factory()
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
import pytest
|
||||
import unittest.mock
|
||||
from datetime import datetime
|
||||
from unittest.mock import patch
|
||||
import pytest
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import posts
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(tmpdir, config_injector):
|
||||
def inject_config(config_injector):
|
||||
config_injector({
|
||||
'privileges': {
|
||||
'posts:list': db.User.RANK_REGULAR,
|
||||
|
@ -13,11 +14,12 @@ def inject_config(tmpdir, config_injector):
|
|||
},
|
||||
})
|
||||
|
||||
|
||||
def test_retrieving_multiple(user_factory, post_factory, context_factory):
|
||||
post1 = post_factory(id=1)
|
||||
post2 = post_factory(id=2)
|
||||
db.session.add_all([post1, post2])
|
||||
with unittest.mock.patch('szurubooru.func.posts.serialize_post'):
|
||||
with patch('szurubooru.func.posts.serialize_post'):
|
||||
posts.serialize_post.return_value = 'serialized post'
|
||||
result = api.post_api.get_posts(
|
||||
context_factory(
|
||||
|
@ -31,6 +33,7 @@ def test_retrieving_multiple(user_factory, post_factory, context_factory):
|
|||
'results': ['serialized post', 'serialized post'],
|
||||
}
|
||||
|
||||
|
||||
def test_using_special_tokens(user_factory, post_factory, context_factory):
|
||||
auth_user = user_factory(rank=db.User.RANK_REGULAR)
|
||||
post1 = post_factory(id=1)
|
||||
|
@ -39,7 +42,7 @@ def test_using_special_tokens(user_factory, post_factory, context_factory):
|
|||
user=auth_user, time=datetime.utcnow())]
|
||||
db.session.add_all([post1, post2, auth_user])
|
||||
db.session.flush()
|
||||
with unittest.mock.patch('szurubooru.func.posts.serialize_post'):
|
||||
with patch('szurubooru.func.posts.serialize_post'):
|
||||
posts.serialize_post.side_effect = \
|
||||
lambda post, *_args, **_kwargs: \
|
||||
'serialized post %d' % post.post_id
|
||||
|
@ -55,8 +58,9 @@ def test_using_special_tokens(user_factory, post_factory, context_factory):
|
|||
'results': ['serialized post 1'],
|
||||
}
|
||||
|
||||
|
||||
def test_trying_to_use_special_tokens_without_logging_in(
|
||||
user_factory, post_factory, context_factory, config_injector):
|
||||
user_factory, context_factory, config_injector):
|
||||
config_injector({
|
||||
'privileges': {'posts:list': 'anonymous'},
|
||||
})
|
||||
|
@ -66,6 +70,7 @@ def test_trying_to_use_special_tokens_without_logging_in(
|
|||
params={'query': 'special:fav', 'page': 1},
|
||||
user=user_factory(rank=db.User.RANK_ANONYMOUS)))
|
||||
|
||||
|
||||
def test_trying_to_retrieve_multiple_without_privileges(
|
||||
user_factory, context_factory):
|
||||
with pytest.raises(errors.AuthError):
|
||||
|
@ -74,21 +79,24 @@ def test_trying_to_retrieve_multiple_without_privileges(
|
|||
params={'query': '', 'page': 1},
|
||||
user=user_factory(rank=db.User.RANK_ANONYMOUS)))
|
||||
|
||||
|
||||
def test_retrieving_single(user_factory, post_factory, context_factory):
|
||||
db.session.add(post_factory(id=1))
|
||||
with unittest.mock.patch('szurubooru.func.posts.serialize_post'):
|
||||
with patch('szurubooru.func.posts.serialize_post'):
|
||||
posts.serialize_post.return_value = 'serialized post'
|
||||
result = api.post_api.get_post(
|
||||
context_factory(user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
{'post_id': 1})
|
||||
assert result == 'serialized post'
|
||||
|
||||
|
||||
def test_trying_to_retrieve_single_non_existing(user_factory, context_factory):
|
||||
with pytest.raises(posts.PostNotFoundError):
|
||||
api.post_api.get_post(
|
||||
context_factory(user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
{'post_id': 999})
|
||||
|
||||
|
||||
def test_trying_to_retrieve_single_without_privileges(
|
||||
user_factory, context_factory):
|
||||
with pytest.raises(errors.AuthError):
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
import pytest
|
||||
import unittest.mock
|
||||
from datetime import datetime
|
||||
from unittest.mock import patch
|
||||
import pytest
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import posts, tags, snapshots, net
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(tmpdir, config_injector):
|
||||
def inject_config(config_injector):
|
||||
config_injector({
|
||||
'privileges': {
|
||||
'posts:edit:tags': db.User.RANK_REGULAR,
|
||||
|
@ -20,6 +21,7 @@ def inject_config(tmpdir, config_injector):
|
|||
},
|
||||
})
|
||||
|
||||
|
||||
def test_post_updating(
|
||||
context_factory, post_factory, user_factory, fake_datetime):
|
||||
auth_user = user_factory(rank=db.User.RANK_REGULAR)
|
||||
|
@ -27,18 +29,18 @@ def test_post_updating(
|
|||
db.session.add(post)
|
||||
db.session.flush()
|
||||
|
||||
with unittest.mock.patch('szurubooru.func.posts.create_post'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_tags'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_content'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_thumbnail'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_safety'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_source'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_relations'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_notes'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_flags'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.serialize_post'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.export_to_json'), \
|
||||
unittest.mock.patch('szurubooru.func.snapshots.save_entity_modification'), \
|
||||
with patch('szurubooru.func.posts.create_post'), \
|
||||
patch('szurubooru.func.posts.update_post_tags'), \
|
||||
patch('szurubooru.func.posts.update_post_content'), \
|
||||
patch('szurubooru.func.posts.update_post_thumbnail'), \
|
||||
patch('szurubooru.func.posts.update_post_safety'), \
|
||||
patch('szurubooru.func.posts.update_post_source'), \
|
||||
patch('szurubooru.func.posts.update_post_relations'), \
|
||||
patch('szurubooru.func.posts.update_post_notes'), \
|
||||
patch('szurubooru.func.posts.update_post_flags'), \
|
||||
patch('szurubooru.func.posts.serialize_post'), \
|
||||
patch('szurubooru.func.tags.export_to_json'), \
|
||||
patch('szurubooru.func.snapshots.save_entity_modification'), \
|
||||
fake_datetime('1997-01-01'):
|
||||
posts.serialize_post.return_value = 'serialized post'
|
||||
|
||||
|
@ -64,28 +66,34 @@ def test_post_updating(
|
|||
posts.create_post.assert_not_called()
|
||||
posts.update_post_tags.assert_called_once_with(post, ['tag1', 'tag2'])
|
||||
posts.update_post_content.assert_called_once_with(post, 'post-content')
|
||||
posts.update_post_thumbnail.assert_called_once_with(post, 'post-thumbnail')
|
||||
posts.update_post_thumbnail.assert_called_once_with(
|
||||
post, 'post-thumbnail')
|
||||
posts.update_post_safety.assert_called_once_with(post, 'safe')
|
||||
posts.update_post_source.assert_called_once_with(post, 'source')
|
||||
posts.update_post_relations.assert_called_once_with(post, [1, 2])
|
||||
posts.update_post_notes.assert_called_once_with(post, ['note1', 'note2'])
|
||||
posts.update_post_flags.assert_called_once_with(post, ['flag1', 'flag2'])
|
||||
posts.serialize_post.assert_called_once_with(post, auth_user, options=None)
|
||||
posts.update_post_notes.assert_called_once_with(
|
||||
post, ['note1', 'note2'])
|
||||
posts.update_post_flags.assert_called_once_with(
|
||||
post, ['flag1', 'flag2'])
|
||||
posts.serialize_post.assert_called_once_with(
|
||||
post, auth_user, options=None)
|
||||
tags.export_to_json.assert_called_once_with()
|
||||
snapshots.save_entity_modification.assert_called_once_with(post, auth_user)
|
||||
snapshots.save_entity_modification.assert_called_once_with(
|
||||
post, auth_user)
|
||||
assert post.last_edit_time == datetime(1997, 1, 1)
|
||||
|
||||
|
||||
def test_uploading_from_url_saves_source(
|
||||
context_factory, post_factory, user_factory):
|
||||
post = post_factory()
|
||||
db.session.add(post)
|
||||
db.session.flush()
|
||||
with unittest.mock.patch('szurubooru.func.net.download'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.export_to_json'), \
|
||||
unittest.mock.patch('szurubooru.func.snapshots.save_entity_modification'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.serialize_post'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_content'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_source'):
|
||||
with patch('szurubooru.func.net.download'), \
|
||||
patch('szurubooru.func.tags.export_to_json'), \
|
||||
patch('szurubooru.func.snapshots.save_entity_modification'), \
|
||||
patch('szurubooru.func.posts.serialize_post'), \
|
||||
patch('szurubooru.func.posts.update_post_content'), \
|
||||
patch('szurubooru.func.posts.update_post_source'):
|
||||
net.download.return_value = b'content'
|
||||
api.post_api.update_post(
|
||||
context_factory(
|
||||
|
@ -96,17 +104,18 @@ def test_uploading_from_url_saves_source(
|
|||
posts.update_post_content.assert_called_once_with(post, b'content')
|
||||
posts.update_post_source.assert_called_once_with(post, 'example.com')
|
||||
|
||||
|
||||
def test_uploading_from_url_with_source_specified(
|
||||
context_factory, post_factory, user_factory):
|
||||
post = post_factory()
|
||||
db.session.add(post)
|
||||
db.session.flush()
|
||||
with unittest.mock.patch('szurubooru.func.net.download'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.export_to_json'), \
|
||||
unittest.mock.patch('szurubooru.func.snapshots.save_entity_modification'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.serialize_post'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_content'), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_source'):
|
||||
with patch('szurubooru.func.net.download'), \
|
||||
patch('szurubooru.func.tags.export_to_json'), \
|
||||
patch('szurubooru.func.snapshots.save_entity_modification'), \
|
||||
patch('szurubooru.func.posts.serialize_post'), \
|
||||
patch('szurubooru.func.posts.update_post_content'), \
|
||||
patch('szurubooru.func.posts.update_post_source'):
|
||||
net.download.return_value = b'content'
|
||||
api.post_api.update_post(
|
||||
context_factory(
|
||||
|
@ -120,6 +129,7 @@ def test_uploading_from_url_with_source_specified(
|
|||
posts.update_post_content.assert_called_once_with(post, b'content')
|
||||
posts.update_post_source.assert_called_once_with(post, 'example2.com')
|
||||
|
||||
|
||||
def test_trying_to_update_non_existing(context_factory, user_factory):
|
||||
with pytest.raises(posts.PostNotFoundError):
|
||||
api.post_api.update_post(
|
||||
|
@ -128,18 +138,19 @@ def test_trying_to_update_non_existing(context_factory, user_factory):
|
|||
user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
{'post_id': 1})
|
||||
|
||||
@pytest.mark.parametrize('privilege,files,params', [
|
||||
('posts:edit:tags', {}, {'tags': '...'}),
|
||||
('posts:edit:safety', {}, {'safety': '...'}),
|
||||
('posts:edit:source', {}, {'source': '...'}),
|
||||
('posts:edit:relations', {}, {'relations': '...'}),
|
||||
('posts:edit:notes', {}, {'notes': '...'}),
|
||||
('posts:edit:flags', {}, {'flags': '...'}),
|
||||
('posts:edit:content', {'content': '...'}, {}),
|
||||
('posts:edit:thumbnail', {'thumbnail': '...'}, {}),
|
||||
|
||||
@pytest.mark.parametrize('files,params', [
|
||||
({}, {'tags': '...'}),
|
||||
({}, {'safety': '...'}),
|
||||
({}, {'source': '...'}),
|
||||
({}, {'relations': '...'}),
|
||||
({}, {'notes': '...'}),
|
||||
({}, {'flags': '...'}),
|
||||
({'content': '...'}, {}),
|
||||
({'thumbnail': '...'}, {}),
|
||||
])
|
||||
def test_trying_to_update_field_without_privileges(
|
||||
context_factory, post_factory, user_factory, files, params, privilege):
|
||||
context_factory, post_factory, user_factory, files, params):
|
||||
post = post_factory()
|
||||
db.session.add(post)
|
||||
db.session.flush()
|
||||
|
@ -151,13 +162,14 @@ def test_trying_to_update_field_without_privileges(
|
|||
user=user_factory(rank=db.User.RANK_ANONYMOUS)),
|
||||
{'post_id': post.post_id})
|
||||
|
||||
|
||||
def test_trying_to_create_tags_without_privileges(
|
||||
context_factory, post_factory, user_factory):
|
||||
post = post_factory()
|
||||
db.session.add(post)
|
||||
db.session.flush()
|
||||
with pytest.raises(errors.AuthError), \
|
||||
unittest.mock.patch('szurubooru.func.posts.update_post_tags'):
|
||||
patch('szurubooru.func.posts.update_post_tags'):
|
||||
posts.update_post_tags.return_value = ['new-tag']
|
||||
api.post_api.update_post(
|
||||
context_factory(
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
import pytest
|
||||
from datetime import datetime
|
||||
import pytest
|
||||
from szurubooru import api, db, errors
|
||||
|
||||
|
||||
def snapshot_factory():
|
||||
snapshot = db.Snapshot()
|
||||
snapshot.creation_time = datetime(1999, 1, 1)
|
||||
|
@ -12,12 +13,14 @@ def snapshot_factory():
|
|||
snapshot.data = '{}'
|
||||
return snapshot
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({
|
||||
'privileges': {'snapshots:list': db.User.RANK_REGULAR},
|
||||
})
|
||||
|
||||
|
||||
def test_retrieving_multiple(user_factory, context_factory):
|
||||
snapshot1 = snapshot_factory()
|
||||
snapshot2 = snapshot_factory()
|
||||
|
@ -32,6 +35,7 @@ def test_retrieving_multiple(user_factory, context_factory):
|
|||
assert result['total'] == 2
|
||||
assert len(result['results']) == 2
|
||||
|
||||
|
||||
def test_trying_to_retrieve_multiple_without_privileges(
|
||||
user_factory, context_factory):
|
||||
with pytest.raises(errors.AuthError):
|
||||
|
|
|
@ -1,21 +1,24 @@
|
|||
from unittest.mock import patch
|
||||
import pytest
|
||||
import unittest.mock
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import tag_categories, tags
|
||||
|
||||
|
||||
def _update_category_name(category, name):
|
||||
category.name = name
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({
|
||||
'privileges': {'tag_categories:create': db.User.RANK_REGULAR},
|
||||
})
|
||||
|
||||
|
||||
def test_creating_category(user_factory, context_factory):
|
||||
with unittest.mock.patch('szurubooru.func.tag_categories.serialize_category'), \
|
||||
unittest.mock.patch('szurubooru.func.tag_categories.update_category_name'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.export_to_json'):
|
||||
with patch('szurubooru.func.tag_categories.serialize_category'), \
|
||||
patch('szurubooru.func.tag_categories.update_category_name'), \
|
||||
patch('szurubooru.func.tags.export_to_json'):
|
||||
tag_categories.update_category_name.side_effect = _update_category_name
|
||||
tag_categories.serialize_category.return_value = 'serialized category'
|
||||
result = api.tag_category_api.create_tag_category(
|
||||
|
@ -29,6 +32,7 @@ def test_creating_category(user_factory, context_factory):
|
|||
assert category.tag_count == 0
|
||||
tags.export_to_json.assert_called_once_with()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('field', ['name', 'color'])
|
||||
def test_trying_to_omit_mandatory_field(user_factory, context_factory, field):
|
||||
params = {
|
||||
|
@ -42,6 +46,7 @@ def test_trying_to_omit_mandatory_field(user_factory, context_factory, field):
|
|||
params=params,
|
||||
user=user_factory(rank=db.User.RANK_REGULAR)))
|
||||
|
||||
|
||||
def test_trying_to_create_without_privileges(user_factory, context_factory):
|
||||
with pytest.raises(errors.AuthError):
|
||||
api.tag_category_api.create_tag_category(
|
||||
|
|
|
@ -1,19 +1,21 @@
|
|||
from unittest.mock import patch
|
||||
import pytest
|
||||
import unittest.mock
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import tag_categories, tags
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({
|
||||
'privileges': {'tag_categories:delete': db.User.RANK_REGULAR},
|
||||
})
|
||||
|
||||
|
||||
def test_deleting(user_factory, tag_category_factory, context_factory):
|
||||
db.session.add(tag_category_factory(name='root'))
|
||||
db.session.add(tag_category_factory(name='category'))
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.tags.export_to_json'):
|
||||
with patch('szurubooru.func.tags.export_to_json'):
|
||||
result = api.tag_category_api.delete_tag_category(
|
||||
context_factory(
|
||||
params={'version': 1},
|
||||
|
@ -24,6 +26,7 @@ def test_deleting(user_factory, tag_category_factory, context_factory):
|
|||
assert db.session.query(db.TagCategory).one().name == 'root'
|
||||
tags.export_to_json.assert_called_once_with()
|
||||
|
||||
|
||||
def test_trying_to_delete_used(
|
||||
user_factory, tag_category_factory, tag_factory, context_factory):
|
||||
category = tag_category_factory(name='category')
|
||||
|
@ -40,6 +43,7 @@ def test_trying_to_delete_used(
|
|||
{'category_name': 'category'})
|
||||
assert db.session.query(db.TagCategory).count() == 1
|
||||
|
||||
|
||||
def test_trying_to_delete_last(
|
||||
user_factory, tag_category_factory, context_factory):
|
||||
db.session.add(tag_category_factory(name='root'))
|
||||
|
@ -51,12 +55,14 @@ def test_trying_to_delete_last(
|
|||
user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
{'category_name': 'root'})
|
||||
|
||||
|
||||
def test_trying_to_delete_non_existing(user_factory, context_factory):
|
||||
with pytest.raises(tag_categories.TagCategoryNotFoundError):
|
||||
api.tag_category_api.delete_tag_category(
|
||||
context_factory(user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
{'category_name': 'bad'})
|
||||
|
||||
|
||||
def test_trying_to_delete_without_privileges(
|
||||
user_factory, tag_category_factory, context_factory):
|
||||
db.session.add(tag_category_factory(name='category'))
|
||||
|
|
|
@ -2,6 +2,7 @@ import pytest
|
|||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import tag_categories
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({
|
||||
|
@ -11,6 +12,7 @@ def inject_config(config_injector):
|
|||
},
|
||||
})
|
||||
|
||||
|
||||
def test_retrieving_multiple(
|
||||
user_factory, tag_category_factory, context_factory):
|
||||
db.session.add_all([
|
||||
|
@ -21,7 +23,9 @@ def test_retrieving_multiple(
|
|||
context_factory(user=user_factory(rank=db.User.RANK_REGULAR)))
|
||||
assert [cat['name'] for cat in result['results']] == ['c1', 'c2']
|
||||
|
||||
def test_retrieving_single(user_factory, tag_category_factory, context_factory):
|
||||
|
||||
def test_retrieving_single(
|
||||
user_factory, tag_category_factory, context_factory):
|
||||
db.session.add(tag_category_factory(name='cat'))
|
||||
result = api.tag_category_api.get_tag_category(
|
||||
context_factory(user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
|
@ -35,12 +39,14 @@ def test_retrieving_single(user_factory, tag_category_factory, context_factory):
|
|||
'version': 1,
|
||||
}
|
||||
|
||||
|
||||
def test_trying_to_retrieve_single_non_existing(user_factory, context_factory):
|
||||
with pytest.raises(tag_categories.TagCategoryNotFoundError):
|
||||
api.tag_category_api.get_tag_category(
|
||||
context_factory(user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
{'category_name': '-'})
|
||||
|
||||
|
||||
def test_trying_to_retrieve_single_without_privileges(
|
||||
user_factory, context_factory):
|
||||
with pytest.raises(errors.AuthError):
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
from unittest.mock import patch
|
||||
import pytest
|
||||
import unittest.mock
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import tag_categories, tags
|
||||
|
||||
|
||||
def _update_category_name(category, name):
|
||||
category.name = name
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({
|
||||
|
@ -16,14 +18,15 @@ def inject_config(config_injector):
|
|||
},
|
||||
})
|
||||
|
||||
|
||||
def test_simple_updating(user_factory, tag_category_factory, context_factory):
|
||||
category = tag_category_factory(name='name', color='black')
|
||||
db.session.add(category)
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.tag_categories.serialize_category'), \
|
||||
unittest.mock.patch('szurubooru.func.tag_categories.update_category_name'), \
|
||||
unittest.mock.patch('szurubooru.func.tag_categories.update_category_color'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.export_to_json'):
|
||||
with patch('szurubooru.func.tag_categories.serialize_category'), \
|
||||
patch('szurubooru.func.tag_categories.update_category_name'), \
|
||||
patch('szurubooru.func.tag_categories.update_category_color'), \
|
||||
patch('szurubooru.func.tags.export_to_json'):
|
||||
tag_categories.update_category_name.side_effect = _update_category_name
|
||||
tag_categories.serialize_category.return_value = 'serialized category'
|
||||
result = api.tag_category_api.update_tag_category(
|
||||
|
@ -36,10 +39,13 @@ def test_simple_updating(user_factory, tag_category_factory, context_factory):
|
|||
user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
{'category_name': 'name'})
|
||||
assert result == 'serialized category'
|
||||
tag_categories.update_category_name.assert_called_once_with(category, 'changed')
|
||||
tag_categories.update_category_color.assert_called_once_with(category, 'white')
|
||||
tag_categories.update_category_name.assert_called_once_with(
|
||||
category, 'changed')
|
||||
tag_categories.update_category_color.assert_called_once_with(
|
||||
category, 'white')
|
||||
tags.export_to_json.assert_called_once_with()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('field', ['name', 'color'])
|
||||
def test_omitting_optional_field(
|
||||
user_factory, tag_category_factory, context_factory, field):
|
||||
|
@ -50,15 +56,16 @@ def test_omitting_optional_field(
|
|||
'color': 'white',
|
||||
}
|
||||
del params[field]
|
||||
with unittest.mock.patch('szurubooru.func.tag_categories.serialize_category'), \
|
||||
unittest.mock.patch('szurubooru.func.tag_categories.update_category_name'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.export_to_json'):
|
||||
with patch('szurubooru.func.tag_categories.serialize_category'), \
|
||||
patch('szurubooru.func.tag_categories.update_category_name'), \
|
||||
patch('szurubooru.func.tags.export_to_json'):
|
||||
api.tag_category_api.update_tag_category(
|
||||
context_factory(
|
||||
params={**params, **{'version': 1}},
|
||||
user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
{'category_name': 'name'})
|
||||
|
||||
|
||||
def test_trying_to_update_non_existing(user_factory, context_factory):
|
||||
with pytest.raises(tag_categories.TagCategoryNotFoundError):
|
||||
api.tag_category_api.update_tag_category(
|
||||
|
@ -67,6 +74,7 @@ def test_trying_to_update_non_existing(user_factory, context_factory):
|
|||
user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
{'category_name': 'bad'})
|
||||
|
||||
|
||||
@pytest.mark.parametrize('params', [
|
||||
{'name': 'whatever'},
|
||||
{'color': 'whatever'},
|
||||
|
@ -82,13 +90,14 @@ def test_trying_to_update_without_privileges(
|
|||
user=user_factory(rank=db.User.RANK_ANONYMOUS)),
|
||||
{'category_name': 'dummy'})
|
||||
|
||||
|
||||
def test_set_as_default(user_factory, tag_category_factory, context_factory):
|
||||
category = tag_category_factory(name='name', color='black')
|
||||
db.session.add(category)
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.tag_categories.serialize_category'), \
|
||||
unittest.mock.patch('szurubooru.func.tag_categories.set_default_category'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.export_to_json'):
|
||||
with patch('szurubooru.func.tag_categories.serialize_category'), \
|
||||
patch('szurubooru.func.tag_categories.set_default_category'), \
|
||||
patch('szurubooru.func.tags.export_to_json'):
|
||||
tag_categories.update_category_name.side_effect = _update_category_name
|
||||
tag_categories.serialize_category.return_value = 'serialized category'
|
||||
result = api.tag_category_api.set_tag_category_as_default(
|
||||
|
|
|
@ -1,17 +1,19 @@
|
|||
from unittest.mock import patch
|
||||
import pytest
|
||||
import unittest.mock
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import tags, tag_categories
|
||||
from szurubooru.func import tags
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({'privileges': {'tags:create': db.User.RANK_REGULAR}})
|
||||
|
||||
|
||||
def test_creating_simple_tags(tag_factory, user_factory, context_factory):
|
||||
with unittest.mock.patch('szurubooru.func.tags.create_tag'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.get_or_create_tags_by_names'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.serialize_tag'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.export_to_json'):
|
||||
with patch('szurubooru.func.tags.create_tag'), \
|
||||
patch('szurubooru.func.tags.get_or_create_tags_by_names'), \
|
||||
patch('szurubooru.func.tags.serialize_tag'), \
|
||||
patch('szurubooru.func.tags.export_to_json'):
|
||||
tags.get_or_create_tags_by_names.return_value = ([], [])
|
||||
tags.create_tag.return_value = tag_factory()
|
||||
tags.serialize_tag.return_value = 'serialized tag'
|
||||
|
@ -30,6 +32,7 @@ def test_creating_simple_tags(tag_factory, user_factory, context_factory):
|
|||
['tag1', 'tag2'], 'meta', ['sug1', 'sug2'], ['imp1', 'imp2'])
|
||||
tags.export_to_json.assert_called_once_with()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('field', ['names', 'category'])
|
||||
def test_trying_to_omit_mandatory_field(user_factory, context_factory, field):
|
||||
params = {
|
||||
|
@ -45,6 +48,7 @@ def test_trying_to_omit_mandatory_field(user_factory, context_factory, field):
|
|||
params=params,
|
||||
user=user_factory(rank=db.User.RANK_REGULAR)))
|
||||
|
||||
|
||||
@pytest.mark.parametrize('field', ['implications', 'suggestions'])
|
||||
def test_omitting_optional_field(
|
||||
tag_factory, user_factory, context_factory, field):
|
||||
|
@ -55,16 +59,18 @@ def test_omitting_optional_field(
|
|||
'implications': [],
|
||||
}
|
||||
del params[field]
|
||||
with unittest.mock.patch('szurubooru.func.tags.create_tag'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.serialize_tag'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.export_to_json'):
|
||||
with patch('szurubooru.func.tags.create_tag'), \
|
||||
patch('szurubooru.func.tags.serialize_tag'), \
|
||||
patch('szurubooru.func.tags.export_to_json'):
|
||||
tags.create_tag.return_value = tag_factory()
|
||||
api.tag_api.create_tag(
|
||||
context_factory(
|
||||
params=params,
|
||||
user=user_factory(rank=db.User.RANK_REGULAR)))
|
||||
|
||||
def test_trying_to_create_tag_without_privileges(user_factory, context_factory):
|
||||
|
||||
def test_trying_to_create_tag_without_privileges(
|
||||
user_factory, context_factory):
|
||||
with pytest.raises(errors.AuthError):
|
||||
api.tag_api.create_tag(
|
||||
context_factory(
|
||||
|
|
|
@ -1,16 +1,18 @@
|
|||
from unittest.mock import patch
|
||||
import pytest
|
||||
import unittest.mock
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import tags
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({'privileges': {'tags:delete': db.User.RANK_REGULAR}})
|
||||
|
||||
|
||||
def test_deleting(user_factory, tag_factory, context_factory):
|
||||
db.session.add(tag_factory(names=['tag']))
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.tags.export_to_json'):
|
||||
with patch('szurubooru.func.tags.export_to_json'):
|
||||
result = api.tag_api.delete_tag(
|
||||
context_factory(
|
||||
params={'version': 1},
|
||||
|
@ -20,13 +22,15 @@ def test_deleting(user_factory, tag_factory, context_factory):
|
|||
assert db.session.query(db.Tag).count() == 0
|
||||
tags.export_to_json.assert_called_once_with()
|
||||
|
||||
def test_deleting_used(user_factory, tag_factory, context_factory, post_factory):
|
||||
|
||||
def test_deleting_used(
|
||||
user_factory, tag_factory, context_factory, post_factory):
|
||||
tag = tag_factory(names=['tag'])
|
||||
post = post_factory()
|
||||
post.tags.append(tag)
|
||||
db.session.add_all([tag, post])
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.tags.export_to_json'):
|
||||
with patch('szurubooru.func.tags.export_to_json'):
|
||||
api.tag_api.delete_tag(
|
||||
context_factory(
|
||||
params={'version': 1},
|
||||
|
@ -36,12 +40,14 @@ def test_deleting_used(user_factory, tag_factory, context_factory, post_factory)
|
|||
assert db.session.query(db.Tag).count() == 0
|
||||
assert post.tags == []
|
||||
|
||||
|
||||
def test_trying_to_delete_non_existing(user_factory, context_factory):
|
||||
with pytest.raises(tags.TagNotFoundError):
|
||||
api.tag_api.delete_tag(
|
||||
context_factory(user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
{'tag_name': 'bad'})
|
||||
|
||||
|
||||
def test_trying_to_delete_without_privileges(
|
||||
user_factory, tag_factory, context_factory):
|
||||
db.session.add(tag_factory(names=['tag']))
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
from unittest.mock import patch
|
||||
import pytest
|
||||
import unittest.mock
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import tags
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({'privileges': {'tags:merge': db.User.RANK_REGULAR}})
|
||||
|
||||
|
||||
def test_merging(user_factory, tag_factory, context_factory, post_factory):
|
||||
source_tag = tag_factory(names=['source'])
|
||||
target_tag = tag_factory(names=['target'])
|
||||
|
@ -20,10 +22,10 @@ def test_merging(user_factory, tag_factory, context_factory, post_factory):
|
|||
db.session.commit()
|
||||
assert source_tag.post_count == 1
|
||||
assert target_tag.post_count == 0
|
||||
with unittest.mock.patch('szurubooru.func.tags.serialize_tag'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.merge_tags'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.export_to_json'):
|
||||
result = api.tag_api.merge_tags(
|
||||
with patch('szurubooru.func.tags.serialize_tag'), \
|
||||
patch('szurubooru.func.tags.merge_tags'), \
|
||||
patch('szurubooru.func.tags.export_to_json'):
|
||||
api.tag_api.merge_tags(
|
||||
context_factory(
|
||||
params={
|
||||
'removeVersion': 1,
|
||||
|
@ -35,6 +37,7 @@ def test_merging(user_factory, tag_factory, context_factory, post_factory):
|
|||
tags.merge_tags.called_once_with(source_tag, target_tag)
|
||||
tags.export_to_json.assert_called_once_with()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'field', ['remove', 'mergeTo', 'removeVersion', 'mergeToVersion'])
|
||||
def test_trying_to_omit_mandatory_field(
|
||||
|
@ -57,6 +60,7 @@ def test_trying_to_omit_mandatory_field(
|
|||
params=params,
|
||||
user=user_factory(rank=db.User.RANK_REGULAR)))
|
||||
|
||||
|
||||
def test_trying_to_merge_non_existing(
|
||||
user_factory, tag_factory, context_factory):
|
||||
db.session.add(tag_factory(names=['good']))
|
||||
|
@ -72,14 +76,9 @@ def test_trying_to_merge_non_existing(
|
|||
params={'remove': 'bad', 'mergeTo': 'good'},
|
||||
user=user_factory(rank=db.User.RANK_REGULAR)))
|
||||
|
||||
@pytest.mark.parametrize('params', [
|
||||
{'names': 'whatever'},
|
||||
{'category': 'whatever'},
|
||||
{'suggestions': ['whatever']},
|
||||
{'implications': ['whatever']},
|
||||
])
|
||||
|
||||
def test_trying_to_merge_without_privileges(
|
||||
user_factory, tag_factory, context_factory, params):
|
||||
user_factory, tag_factory, context_factory):
|
||||
db.session.add_all([
|
||||
tag_factory(names=['source']),
|
||||
tag_factory(names=['target']),
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
from unittest.mock import patch
|
||||
import pytest
|
||||
import unittest.mock
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import tags
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({
|
||||
|
@ -12,11 +13,12 @@ def inject_config(config_injector):
|
|||
},
|
||||
})
|
||||
|
||||
|
||||
def test_retrieving_multiple(user_factory, tag_factory, context_factory):
|
||||
tag1 = tag_factory(names=['t1'])
|
||||
tag2 = tag_factory(names=['t2'])
|
||||
db.session.add_all([tag1, tag2])
|
||||
with unittest.mock.patch('szurubooru.func.tags.serialize_tag'):
|
||||
with patch('szurubooru.func.tags.serialize_tag'):
|
||||
tags.serialize_tag.return_value = 'serialized tag'
|
||||
result = api.tag_api.get_tags(
|
||||
context_factory(
|
||||
|
@ -30,6 +32,7 @@ def test_retrieving_multiple(user_factory, tag_factory, context_factory):
|
|||
'results': ['serialized tag', 'serialized tag'],
|
||||
}
|
||||
|
||||
|
||||
def test_trying_to_retrieve_multiple_without_privileges(
|
||||
user_factory, context_factory):
|
||||
with pytest.raises(errors.AuthError):
|
||||
|
@ -38,9 +41,10 @@ def test_trying_to_retrieve_multiple_without_privileges(
|
|||
params={'query': '', 'page': 1},
|
||||
user=user_factory(rank=db.User.RANK_ANONYMOUS)))
|
||||
|
||||
|
||||
def test_retrieving_single(user_factory, tag_factory, context_factory):
|
||||
db.session.add(tag_factory(names=['tag']))
|
||||
with unittest.mock.patch('szurubooru.func.tags.serialize_tag'):
|
||||
with patch('szurubooru.func.tags.serialize_tag'):
|
||||
tags.serialize_tag.return_value = 'serialized tag'
|
||||
result = api.tag_api.get_tag(
|
||||
context_factory(
|
||||
|
@ -48,6 +52,7 @@ def test_retrieving_single(user_factory, tag_factory, context_factory):
|
|||
{'tag_name': 'tag'})
|
||||
assert result == 'serialized tag'
|
||||
|
||||
|
||||
def test_trying_to_retrieve_single_non_existing(user_factory, context_factory):
|
||||
with pytest.raises(tags.TagNotFoundError):
|
||||
api.tag_api.get_tag(
|
||||
|
@ -55,6 +60,7 @@ def test_trying_to_retrieve_single_non_existing(user_factory, context_factory):
|
|||
user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
{'tag_name': '-'})
|
||||
|
||||
|
||||
def test_trying_to_retrieve_single_without_privileges(
|
||||
user_factory, context_factory):
|
||||
with pytest.raises(errors.AuthError):
|
||||
|
|
|
@ -1,16 +1,18 @@
|
|||
from unittest.mock import patch
|
||||
import pytest
|
||||
import unittest.mock
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import tags
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({'privileges': {'tags:view': db.User.RANK_REGULAR}})
|
||||
|
||||
def test_get_tag_siblings(user_factory, tag_factory, post_factory, context_factory):
|
||||
|
||||
def test_get_tag_siblings(user_factory, tag_factory, context_factory):
|
||||
db.session.add(tag_factory(names=['tag']))
|
||||
with unittest.mock.patch('szurubooru.func.tags.serialize_tag'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.get_tag_siblings'):
|
||||
with patch('szurubooru.func.tags.serialize_tag'), \
|
||||
patch('szurubooru.func.tags.get_tag_siblings'):
|
||||
tags.serialize_tag.side_effect = \
|
||||
lambda tag, *args, **kwargs: \
|
||||
'serialized tag %s' % tag.names[0].name
|
||||
|
@ -34,12 +36,14 @@ def test_get_tag_siblings(user_factory, tag_factory, post_factory, context_facto
|
|||
],
|
||||
}
|
||||
|
||||
|
||||
def test_trying_to_retrieve_non_existing(user_factory, context_factory):
|
||||
with pytest.raises(tags.TagNotFoundError):
|
||||
api.tag_api.get_tag_siblings(
|
||||
context_factory(user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
{'tag_name': '-'})
|
||||
|
||||
|
||||
def test_trying_to_retrieve_without_privileges(user_factory, context_factory):
|
||||
with pytest.raises(errors.AuthError):
|
||||
api.tag_api.get_tag_siblings(
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
from unittest.mock import patch
|
||||
import pytest
|
||||
import unittest.mock
|
||||
from szurubooru import api, db, errors
|
||||
from szurubooru.func import tags
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def inject_config(config_injector):
|
||||
config_injector({
|
||||
|
@ -16,20 +17,21 @@ def inject_config(config_injector):
|
|||
},
|
||||
})
|
||||
|
||||
def test_simple_updating(user_factory, tag_factory, context_factory, fake_datetime):
|
||||
|
||||
def test_simple_updating(user_factory, tag_factory, context_factory):
|
||||
auth_user = user_factory(rank=db.User.RANK_REGULAR)
|
||||
tag = tag_factory(names=['tag1', 'tag2'])
|
||||
db.session.add(tag)
|
||||
db.session.commit()
|
||||
with unittest.mock.patch('szurubooru.func.tags.create_tag'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.get_or_create_tags_by_names'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.update_tag_names'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.update_tag_category_name'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.update_tag_description'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.update_tag_suggestions'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.update_tag_implications'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.serialize_tag'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.export_to_json'):
|
||||
with patch('szurubooru.func.tags.create_tag'), \
|
||||
patch('szurubooru.func.tags.get_or_create_tags_by_names'), \
|
||||
patch('szurubooru.func.tags.update_tag_names'), \
|
||||
patch('szurubooru.func.tags.update_tag_category_name'), \
|
||||
patch('szurubooru.func.tags.update_tag_description'), \
|
||||
patch('szurubooru.func.tags.update_tag_suggestions'), \
|
||||
patch('szurubooru.func.tags.update_tag_implications'), \
|
||||
patch('szurubooru.func.tags.serialize_tag'), \
|
||||
patch('szurubooru.func.tags.export_to_json'):
|
||||
tags.get_or_create_tags_by_names.return_value = ([], [])
|
||||
tags.serialize_tag.return_value = 'serialized tag'
|
||||
result = api.tag_api.update_tag(
|
||||
|
@ -49,12 +51,22 @@ def test_simple_updating(user_factory, tag_factory, context_factory, fake_dateti
|
|||
tags.update_tag_names.assert_called_once_with(tag, ['tag3'])
|
||||
tags.update_tag_category_name.assert_called_once_with(tag, 'character')
|
||||
tags.update_tag_description.assert_called_once_with(tag, 'desc')
|
||||
tags.update_tag_suggestions.assert_called_once_with(tag, ['sug1', 'sug2'])
|
||||
tags.update_tag_implications.assert_called_once_with(tag, ['imp1', 'imp2'])
|
||||
tags.serialize_tag.assert_called_once_with(tag, options=None)
|
||||
tags.update_tag_suggestions.assert_called_once_with(
|
||||
tag, ['sug1', 'sug2'])
|
||||
tags.update_tag_implications.assert_called_once_with(
|
||||
tag, ['imp1', 'imp2'])
|
||||
tags.serialize_tag.assert_called_once_with(
|
||||
tag, options=None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'field', ['names', 'category', 'description', 'implications', 'suggestions'])
|
||||
'field', [
|
||||
'names',
|
||||
'category',
|
||||
'description',
|
||||
'implications',
|
||||
'suggestions',
|
||||
])
|
||||
def test_omitting_optional_field(
|
||||
user_factory, tag_factory, context_factory, field):
|
||||
db.session.add(tag_factory(names=['tag']))
|
||||
|
@ -67,17 +79,18 @@ def test_omitting_optional_field(
|
|||
'implications': [],
|
||||
}
|
||||
del params[field]
|
||||
with unittest.mock.patch('szurubooru.func.tags.create_tag'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.update_tag_names'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.update_tag_category_name'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.serialize_tag'), \
|
||||
unittest.mock.patch('szurubooru.func.tags.export_to_json'):
|
||||
with patch('szurubooru.func.tags.create_tag'), \
|
||||
patch('szurubooru.func.tags.update_tag_names'), \
|
||||
patch('szurubooru.func.tags.update_tag_category_name'), \
|
||||
patch('szurubooru.func.tags.serialize_tag'), \
|
||||
patch('szurubooru.func.tags.export_to_json'):
|
||||
api.tag_api.update_tag(
|
||||
context_factory(
|
||||
params={**params, **{'version': 1}},
|
||||
user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
{'tag_name': 'tag'})
|
||||
|
||||
|
||||
def test_trying_to_update_non_existing(user_factory, context_factory):
|
||||
with pytest.raises(tags.TagNotFoundError):
|
||||
api.tag_api.update_tag(
|
||||
|
@ -86,6 +99,7 @@ def test_trying_to_update_non_existing(user_factory, context_factory):
|
|||
user=user_factory(rank=db.User.RANK_REGULAR)),
|
||||
{'tag_name': 'tag1'})
|
||||
|
||||
|
||||
@pytest.mark.parametrize('params', [
|
||||
{'names': 'whatever'},
|
||||
{'category': 'whatever'},
|
||||
|
@ -103,6 +117,7 @@ def test_trying_to_update_without_privileges(
|
|||
user=user_factory(rank=db.User.RANK_ANONYMOUS)),
|
||||
{'tag_name': 'tag'})
|
||||
|
||||
|
||||
def test_trying_to_create_tags_without_privileges(
|
||||
config_injector, context_factory, tag_factory, user_factory):
|
||||
tag = tag_factory(names=['tag'])
|
||||
|
@ -113,7 +128,7 @@ def test_trying_to_create_tags_without_privileges(
|
|||
'tags:edit:suggestions': db.User.RANK_REGULAR,
|
||||
'tags:edit:implications': db.User.RANK_REGULAR,
|
||||
}})
|
||||
with unittest.mock.patch('szurubooru.func.tags.get_or_create_tags_by_names'):
|
||||
with patch('szurubooru.func.tags.get_or_create_tags_by_names'):
|
||||
tags.get_or_create_tags_by_names.return_value = ([], ['new-tag'])
|
||||
with pytest.raises(errors.AuthError):
|
||||
api.tag_api.update_tag(
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue