server/general: authenticated_user->auth_user

This commit is contained in:
rr- 2016-08-14 10:53:19 +02:00
parent c2bbf7b62c
commit f6f07a35df
4 changed files with 33 additions and 31 deletions

View file

@ -5,19 +5,19 @@ from szurubooru.func import users, scores, util
class CommentNotFoundError(errors.NotFoundError): pass class CommentNotFoundError(errors.NotFoundError): pass
class EmptyCommentTextError(errors.ValidationError): pass class EmptyCommentTextError(errors.ValidationError): pass
def serialize_comment(comment, authenticated_user, options=None): def serialize_comment(comment, auth_user, options=None):
return util.serialize_entity( return util.serialize_entity(
comment, comment,
{ {
'id': lambda: comment.comment_id, 'id': lambda: comment.comment_id,
'user': lambda: users.serialize_micro_user(comment.user), 'user': lambda: users.serialize_micro_user(comment.user, auth_user),
'postId': lambda: comment.post.post_id, 'postId': lambda: comment.post.post_id,
'version': lambda: comment.version, 'version': lambda: comment.version,
'text': lambda: comment.text, 'text': lambda: comment.text,
'creationTime': lambda: comment.creation_time, 'creationTime': lambda: comment.creation_time,
'lastEditTime': lambda: comment.last_edit_time, 'lastEditTime': lambda: comment.last_edit_time,
'score': lambda: comment.score, 'score': lambda: comment.score,
'ownScore': lambda: scores.get_score(comment, authenticated_user), 'ownScore': lambda: scores.get_score(comment, auth_user),
}, },
options) options)

View file

@ -68,7 +68,7 @@ def serialize_note(note):
'text': note.text, 'text': note.text,
} }
def serialize_post(post, authenticated_user, options=None): def serialize_post(post, auth_user, options=None):
return util.serialize_entity( return util.serialize_entity(
post, post,
{ {
@ -93,16 +93,17 @@ def serialize_post(post, authenticated_user, options=None):
{ {
post['id']: post['id']:
post for post in [ post for post in [
serialize_micro_post(rel) for rel in post.relations serialize_micro_post(rel, auth_user) \
for rel in post.relations
] ]
}.values(), }.values(),
key=lambda post: post['id']), key=lambda post: post['id']),
'user': lambda: users.serialize_micro_user(post.user), 'user': lambda: users.serialize_micro_user(post.user, auth_user),
'score': lambda: post.score, 'score': lambda: post.score,
'ownScore': lambda: scores.get_score(post, authenticated_user), 'ownScore': lambda: scores.get_score(post, auth_user),
'ownFavorite': lambda: len( 'ownFavorite': lambda: len(
[user for user in post.favorited_by \ [user for user in post.favorited_by \
if user.user_id == authenticated_user.user_id]) > 0, if user.user_id == auth_user.user_id]) > 0,
'tagCount': lambda: post.tag_count, 'tagCount': lambda: post.tag_count,
'favoriteCount': lambda: post.favorite_count, 'favoriteCount': lambda: post.favorite_count,
'commentCount': lambda: post.comment_count, 'commentCount': lambda: post.comment_count,
@ -111,7 +112,7 @@ def serialize_post(post, authenticated_user, options=None):
'featureCount': lambda: post.feature_count, 'featureCount': lambda: post.feature_count,
'lastFeatureTime': lambda: post.last_feature_time, 'lastFeatureTime': lambda: post.last_feature_time,
'favoritedBy': lambda: [ 'favoritedBy': lambda: [
users.serialize_micro_user(rel.user) \ users.serialize_micro_user(rel.user, auth_user) \
for rel in post.favorited_by], for rel in post.favorited_by],
'hasCustomThumbnail': 'hasCustomThumbnail':
lambda: files.has(get_post_thumbnail_backup_path(post)), lambda: files.has(get_post_thumbnail_backup_path(post)),
@ -119,7 +120,7 @@ def serialize_post(post, authenticated_user, options=None):
[serialize_note(note) for note in post.notes], [serialize_note(note) for note in post.notes],
key=lambda x: x['polygon']), key=lambda x: x['polygon']),
'comments': lambda: [ 'comments': lambda: [
comments.serialize_comment(comment, authenticated_user) \ comments.serialize_comment(comment, auth_user) \
for comment in sorted( for comment in sorted(
post.comments, post.comments,
key=lambda comment: comment.creation_time)], key=lambda comment: comment.creation_time)],
@ -127,10 +128,10 @@ def serialize_post(post, authenticated_user, options=None):
}, },
options) options)
def serialize_micro_post(post): def serialize_micro_post(post, auth_user):
return serialize_post( return serialize_post(
post, post,
authenticated_user=None, auth_user=auth_user,
options=['id', 'thumbnailUrl']) options=['id', 'thumbnailUrl'])
def get_post_count(): def get_post_count():

View file

@ -27,30 +27,30 @@ def _get_avatar_url(user):
return '%s/avatars/%s.png' % ( return '%s/avatars/%s.png' % (
config.config['data_url'].rstrip('/'), user.name.lower()) config.config['data_url'].rstrip('/'), user.name.lower())
def _get_email(user, authenticated_user, force_show_email): def _get_email(user, auth_user, force_show_email):
assert user assert user
assert authenticated_user assert auth_user
if not force_show_email \ if not force_show_email \
and authenticated_user.user_id != user.user_id \ and auth_user.user_id != user.user_id \
and not auth.has_privilege(authenticated_user, 'users:edit:any:email'): and not auth.has_privilege(auth_user, 'users:edit:any:email'):
return False return False
return user.email return user.email
def _get_liked_post_count(user, authenticated_user): def _get_liked_post_count(user, auth_user):
assert user assert user
assert authenticated_user assert auth_user
if authenticated_user.user_id != user.user_id: if auth_user.user_id != user.user_id:
return False return False
return user.liked_post_count return user.liked_post_count
def _get_disliked_post_count(user, authenticated_user): def _get_disliked_post_count(user, auth_user):
assert user assert user
assert authenticated_user assert auth_user
if authenticated_user.user_id != user.user_id: if auth_user.user_id != user.user_id:
return False return False
return user.disliked_post_count return user.disliked_post_count
def serialize_user(user, authenticated_user, options=None, force_show_email=False): def serialize_user(user, auth_user, options=None, force_show_email=False):
return util.serialize_entity( return util.serialize_entity(
user, user,
{ {
@ -65,18 +65,18 @@ def serialize_user(user, authenticated_user, options=None, force_show_email=Fals
'uploadedPostCount': lambda: user.post_count, 'uploadedPostCount': lambda: user.post_count,
'favoritePostCount': lambda: user.favorite_post_count, 'favoritePostCount': lambda: user.favorite_post_count,
'likedPostCount': 'likedPostCount':
lambda: _get_liked_post_count(user, authenticated_user), lambda: _get_liked_post_count(user, auth_user),
'dislikedPostCount': 'dislikedPostCount':
lambda: _get_disliked_post_count(user, authenticated_user), lambda: _get_disliked_post_count(user, auth_user),
'email': 'email':
lambda: _get_email(user, authenticated_user, force_show_email), lambda: _get_email(user, auth_user, force_show_email),
}, },
options) options)
def serialize_micro_user(user): def serialize_micro_user(user, auth_user):
return serialize_user( return serialize_user(
user, user,
authenticated_user=None, auth_user=auth_user,
options=['name', 'avatarUrl']) options=['name', 'avatarUrl'])
def get_user_count(): def get_user_count():
@ -162,7 +162,7 @@ def update_user_email(user, email):
raise InvalidEmailError('E-mail is invalid.') raise InvalidEmailError('E-mail is invalid.')
user.email = email user.email = email
def update_user_rank(user, rank, authenticated_user): def update_user_rank(user, rank, auth_user):
assert user assert user
if not rank: if not rank:
raise InvalidRankError('Rank cannot be empty.') raise InvalidRankError('Rank cannot be empty.')
@ -173,7 +173,7 @@ def update_user_rank(user, rank, authenticated_user):
'Rank can be either of %r.' % all_ranks) 'Rank can be either of %r.' % all_ranks)
if rank in (db.User.RANK_ANONYMOUS, db.User.RANK_NOBODY): if rank in (db.User.RANK_ANONYMOUS, db.User.RANK_NOBODY):
raise InvalidRankError('Rank %r cannot be used.' % auth.RANK_MAP[rank]) raise InvalidRankError('Rank %r cannot be used.' % auth.RANK_MAP[rank])
if all_ranks.index(authenticated_user.rank) \ if all_ranks.index(auth_user.rank) \
< all_ranks.index(rank) and get_user_count() > 0: < all_ranks.index(rank) and get_user_count() > 0:
raise errors.AuthError('Trying to set higher rank than your own.') raise errors.AuthError('Trying to set higher rank than your own.')
user.rank = rank user.rank = rank

View file

@ -71,7 +71,8 @@ def test_serialize_post(
unittest.mock.patch('szurubooru.func.users.serialize_micro_user'), \ unittest.mock.patch('szurubooru.func.users.serialize_micro_user'), \
unittest.mock.patch('szurubooru.func.posts.files.has', return_value=True), \ unittest.mock.patch('szurubooru.func.posts.files.has', return_value=True), \
unittest.mock.patch('szurubooru.func.snapshots.get_serialized_history'): unittest.mock.patch('szurubooru.func.snapshots.get_serialized_history'):
users.serialize_micro_user.side_effect = lambda user: user.name users.serialize_micro_user.side_effect \
= lambda user, auth_user: user.name
comments.serialize_comment.side_effect \ comments.serialize_comment.side_effect \
= lambda comment, auth_user: comment.user.name = lambda comment, auth_user: comment.user.name
snapshots.get_serialized_history.return_value = 'snapshot history' snapshots.get_serialized_history.return_value = 'snapshot history'