diff options
| author | rr- | 2016-05-30 22:07:44 +0200 |
|---|---|---|
| committer | rr- | 2016-05-30 22:54:33 +0200 |
| commit | d0314813cb284cbe50101fcea32bf7126a59ba74 (patch) | |
| tree | 231361c313b48f6c0834900f8be9c339b542f0f6 /server/szurubooru/func | |
| parent | 48bcbbff83e18d614ccff0b11c655fa86291f62c (diff) | |
server/general: move extra details to resources
Diffstat (limited to 'server/szurubooru/func')
| -rw-r--r-- | server/szurubooru/func/comments.py | 27 | ||||
| -rw-r--r-- | server/szurubooru/func/posts.py | 85 | ||||
| -rw-r--r-- | server/szurubooru/func/tag_categories.py | 21 | ||||
| -rw-r--r-- | server/szurubooru/func/tags.py | 31 | ||||
| -rw-r--r-- | server/szurubooru/func/users.py | 44 | ||||
| -rw-r--r-- | server/szurubooru/func/util.py | 8 |
6 files changed, 101 insertions, 115 deletions
diff --git a/server/szurubooru/func/comments.py b/server/szurubooru/func/comments.py index 2ebb30e..8ea06e3 100644 --- a/server/szurubooru/func/comments.py +++ b/server/szurubooru/func/comments.py @@ -1,25 +1,22 @@ import datetime from szurubooru import db, errors -from szurubooru.func import users, posts, scores +from szurubooru.func import users, scores, util class CommentNotFoundError(errors.NotFoundError): pass class EmptyCommentTextError(errors.ValidationError): pass def serialize_comment(comment, authenticated_user): - ret = { - 'id': comment.comment_id, - 'user': users.serialize_user(comment.user, authenticated_user), - 'post': posts.serialize_post(comment.post, authenticated_user), - 'text': comment.text, - 'creationTime': comment.creation_time, - 'lastEditTime': comment.last_edit_time, - } - if authenticated_user: - ret['ownScore'] = scores.get_score(comment, authenticated_user) - return ret - -def serialize_comment_with_details(comment, authenticated_user): - return {'comment': serialize_comment(comment, authenticated_user)} + return util.serialize_entity( + comment, + { + 'id': lambda: comment.comment_id, + 'user': lambda: users.serialize_user(comment.user, authenticated_user), + 'postId': lambda: comment.post.post_id, + 'text': lambda: comment.text, + 'creationTime': lambda: comment.creation_time, + 'lastEditTime': lambda: comment.last_edit_time, + 'ownScore': lambda: scores.get_score(comment, authenticated_user), + }) def try_get_comment_by_id(comment_id): return db.session \ diff --git a/server/szurubooru/func/posts.py b/server/szurubooru/func/posts.py index 81b68b4..08859b8 100644 --- a/server/szurubooru/func/posts.py +++ b/server/szurubooru/func/posts.py @@ -62,54 +62,43 @@ def serialize_note(note): } def serialize_post(post, authenticated_user): - if not post: - return None - - ret = { - 'id': post.post_id, - 'creationTime': post.creation_time, - 'lastEditTime': post.last_edit_time, - 'safety': SAFETY_MAP[post.safety], - 'source': post.source, - 'type': TYPE_MAP[post.type], - 'checksum': post.checksum, - 'fileSize': post.file_size, - 'canvasWidth': post.canvas_width, - 'canvasHeight': post.canvas_height, - 'contentUrl': get_post_content_url(post), - 'thumbnailUrl': get_post_thumbnail_url(post), - 'flags': post.flags, - 'tags': [tag.names[0].name for tag in post.tags], - 'relations': [rel.post_id for rel in post.relations], - 'notes': sorted( - [ serialize_note(note) for note in post.notes], - key=lambda x: x['polygon']), - 'user': users.serialize_user(post.user, authenticated_user), - 'score': post.score, - 'featureCount': post.feature_count, - 'lastFeatureTime': post.last_feature_time, - 'favoritedBy': [users.serialize_user(rel.user, authenticated_user) \ - for rel in post.favorited_by], - 'hasCustomThumbnail': files.has(get_post_thumbnail_backup_path(post)), - 'mimeType': post.mime_type, - } - - if authenticated_user: - ret['ownScore'] = scores.get_score(post, authenticated_user) - - return ret - -def serialize_post_with_details(post, authenticated_user): - comment_list = [] - if post: - for comment in post.comments: - comment_list.append( - comments.serialize_comment(comment, authenticated_user)) - return { - 'post': serialize_post(post, authenticated_user), - 'snapshots': snapshots.get_serialized_history(post), - 'comments': comment_list, - } + return util.serialize_entity( + post, + { + 'id': lambda: post.post_id, + 'creationTime': lambda: post.creation_time, + 'lastEditTime': lambda: post.last_edit_time, + 'safety': lambda: SAFETY_MAP[post.safety], + 'source': lambda: post.source, + 'type': lambda: TYPE_MAP[post.type], + 'mimeType': lambda: post.mime_type, + 'checksum': lambda: post.checksum, + 'fileSize': lambda: post.file_size, + 'canvasWidth': lambda: post.canvas_width, + 'canvasHeight': lambda: post.canvas_height, + 'contentUrl': lambda: get_post_content_url(post), + 'thumbnailUrl': lambda: get_post_thumbnail_url(post), + 'flags': lambda: post.flags, + 'tags': lambda: [tag.names[0].name for tag in post.tags], + 'relations': lambda: [rel.post_id for rel in post.relations], + 'user': lambda: users.serialize_user(post.user, authenticated_user), + 'score': lambda: post.score, + 'ownScore': lambda: scores.get_score(post, authenticated_user), + 'featureCount': lambda: post.feature_count, + 'lastFeatureTime': lambda: post.last_feature_time, + 'favoritedBy': lambda: [ + users.serialize_user(rel.user, authenticated_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, authenticated_user) \ + for comment in post.comments], + 'snapshots': lambda: snapshots.get_serialized_history(post), + }) def get_post_count(): return db.session.query(sqlalchemy.func.count(db.Post.post_id)).one()[0] diff --git a/server/szurubooru/func/tag_categories.py b/server/szurubooru/func/tag_categories.py index 6bd7601..6d3f8f7 100644 --- a/server/szurubooru/func/tag_categories.py +++ b/server/szurubooru/func/tag_categories.py @@ -15,18 +15,15 @@ def _verify_name_validity(name): 'Name must satisfy regex %r.' % name_regex) def serialize_category(category): - return { - 'name': category.name, - 'color': category.color, - 'usages': category.tag_count, - 'default': category.default, - } - -def serialize_category_with_details(category): - return { - 'tagCategory': serialize_category(category), - 'snapshots': snapshots.get_serialized_history(category), - } + return util.serialize_entity( + category, + { + 'name': lambda: category.name, + 'color': lambda: category.color, + 'usages': lambda: category.tag_count, + 'default': lambda: category.default, + 'snapshots': lambda: snapshots.get_serialized_history(category), + }) def create_category(name, color): category = db.TagCategory() diff --git a/server/szurubooru/func/tags.py b/server/szurubooru/func/tags.py index 3b3cd7b..9f07cdd 100644 --- a/server/szurubooru/func/tags.py +++ b/server/szurubooru/func/tags.py @@ -37,23 +37,20 @@ def _get_default_category_name(): return DEFAULT_CATEGORY_NAME def serialize_tag(tag): - return { - 'names': [tag_name.name for tag_name in tag.names], - 'category': tag.category.name, - 'suggestions': [ - relation.names[0].name for relation in tag.suggestions], - 'implications': [ - relation.names[0].name for relation in tag.implications], - 'creationTime': tag.creation_time, - 'lastEditTime': tag.last_edit_time, - 'usages': tag.post_count, - } - -def serialize_tag_with_details(tag): - return { - 'tag': serialize_tag(tag), - 'snapshots': snapshots.get_serialized_history(tag), - } + return util.serialize_entity( + tag, + { + 'names': lambda: [tag_name.name for tag_name in tag.names], + 'category': lambda: tag.category.name, + 'creationTime': lambda: tag.creation_time, + 'lastEditTime': lambda: tag.last_edit_time, + 'usages': lambda: tag.post_count, + 'suggestions': lambda: [ + relation.names[0].name for relation in tag.suggestions], + 'implications': lambda: [ + relation.names[0].name for relation in tag.implications], + 'snapshots': lambda: snapshots.get_serialized_history(tag), + }) def export_to_json(): output = { diff --git a/server/szurubooru/func/users.py b/server/szurubooru/func/users.py index 3c39fb5..128e626 100644 --- a/server/szurubooru/func/users.py +++ b/server/szurubooru/func/users.py @@ -12,36 +12,34 @@ class InvalidPasswordError(errors.ValidationError): pass class InvalidRankError(errors.ValidationError): pass class InvalidAvatarError(errors.ValidationError): pass -def serialize_user(user, authenticated_user, force_show_email=False): - if not user: - return {} - - ret = { - 'name': user.name, - 'rank': user.rank, - 'creationTime': user.creation_time, - 'lastLoginTime': user.last_login_time, - 'avatarStyle': user.avatar_style, - 'email': user.email, - } - +def _get_avatar_url(user): if user.avatar_style == user.AVATAR_GRAVATAR: - ret['avatarUrl'] = 'http://gravatar.com/avatar/%s?d=retro&s=%d' % ( + return 'http://gravatar.com/avatar/%s?d=retro&s=%d' % ( util.get_md5((user.email or user.name).lower()), config.config['thumbnails']['avatar_width']) else: - ret['avatarUrl'] = '%s/avatars/%s.png' % ( + return '%s/avatars/%s.png' % ( config.config['data_url'].rstrip('/'), user.name.lower()) - if authenticated_user.user_id != user.user_id \ - and not force_show_email \ - and not auth.has_privilege(authenticated_user, 'users:edit:any:email'): - del ret['email'] - - return ret +def _get_email(user, authenticated_user, force_show_email): + if not force_show_email \ + and authenticated_user.user_id != user.user_id \ + and not auth.has_privilege(authenticated_user, 'users:edit:any:email'): + return False + return user.email -def serialize_user_with_details(user, authenticated_user, **kwargs): - return {'user': serialize_user(user, authenticated_user, **kwargs)} +def serialize_user(user, authenticated_user, force_show_email=False): + return util.serialize_entity( + user, + { + 'name': lambda: user.name, + 'rank': lambda: user.rank, + 'creationTime': lambda: user.creation_time, + 'lastLoginTime': lambda: user.last_login_time, + 'avatarStyle': lambda: user.avatar_style, + 'avatarUrl': lambda: _get_avatar_url(user), + 'email': lambda: _get_email(user, authenticated_user, force_show_email), + }) def get_user_count(): return db.session.query(db.User).count() diff --git a/server/szurubooru/func/util.py b/server/szurubooru/func/util.py index cdb9f81..3a4208a 100644 --- a/server/szurubooru/func/util.py +++ b/server/szurubooru/func/util.py @@ -6,6 +6,14 @@ import tempfile from contextlib import contextmanager from szurubooru.errors import ValidationError +def serialize_entity(entity, field_factories): + if not entity: + return None + ret = {} + for key, factory in field_factories.items(): + ret[key] = factory() + return ret + @contextmanager def create_temp_file(**kwargs): (handle, path) = tempfile.mkstemp(**kwargs) |