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 EmptyCommentTextError(errors.ValidationError): pass
def serialize_comment(comment, authenticated_user, options=None):
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),
'user': lambda: users.serialize_micro_user(comment.user, auth_user),
'postId': lambda: comment.post.post_id,
'version': lambda: comment.version,
'text': lambda: comment.text,
'creationTime': lambda: comment.creation_time,
'lastEditTime': lambda: comment.last_edit_time,
'score': lambda: comment.score,
'ownScore': lambda: scores.get_score(comment, authenticated_user),
'ownScore': lambda: scores.get_score(comment, auth_user),
},
options)

View file

@ -68,7 +68,7 @@ def serialize_note(note):
'text': note.text,
}
def serialize_post(post, authenticated_user, options=None):
def serialize_post(post, auth_user, options=None):
return util.serialize_entity(
post,
{
@ -93,16 +93,17 @@ def serialize_post(post, authenticated_user, options=None):
{
post['id']:
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(),
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,
'ownScore': lambda: scores.get_score(post, authenticated_user),
'ownScore': lambda: scores.get_score(post, auth_user),
'ownFavorite': lambda: len(
[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,
'favoriteCount': lambda: post.favorite_count,
'commentCount': lambda: post.comment_count,
@ -111,7 +112,7 @@ def serialize_post(post, authenticated_user, options=None):
'featureCount': lambda: post.feature_count,
'lastFeatureTime': lambda: post.last_feature_time,
'favoritedBy': lambda: [
users.serialize_micro_user(rel.user) \
users.serialize_micro_user(rel.user, auth_user) \
for rel in post.favorited_by],
'hasCustomThumbnail':
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],
key=lambda x: x['polygon']),
'comments': lambda: [
comments.serialize_comment(comment, authenticated_user) \
comments.serialize_comment(comment, auth_user) \
for comment in sorted(
post.comments,
key=lambda comment: comment.creation_time)],
@ -127,10 +128,10 @@ def serialize_post(post, authenticated_user, options=None):
},
options)
def serialize_micro_post(post):
def serialize_micro_post(post, auth_user):
return serialize_post(
post,
authenticated_user=None,
auth_user=auth_user,
options=['id', 'thumbnailUrl'])
def get_post_count():

View file

@ -27,30 +27,30 @@ def _get_avatar_url(user):
return '%s/avatars/%s.png' % (
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 authenticated_user
assert auth_user
if not force_show_email \
and authenticated_user.user_id != user.user_id \
and not auth.has_privilege(authenticated_user, 'users:edit:any:email'):
and auth_user.user_id != user.user_id \
and not auth.has_privilege(auth_user, 'users:edit:any:email'):
return False
return user.email
def _get_liked_post_count(user, authenticated_user):
def _get_liked_post_count(user, auth_user):
assert user
assert authenticated_user
if authenticated_user.user_id != user.user_id:
assert auth_user
if auth_user.user_id != user.user_id:
return False
return user.liked_post_count
def _get_disliked_post_count(user, authenticated_user):
def _get_disliked_post_count(user, auth_user):
assert user
assert authenticated_user
if authenticated_user.user_id != user.user_id:
assert auth_user
if auth_user.user_id != user.user_id:
return False
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(
user,
{
@ -65,18 +65,18 @@ def serialize_user(user, authenticated_user, options=None, force_show_email=Fals
'uploadedPostCount': lambda: user.post_count,
'favoritePostCount': lambda: user.favorite_post_count,
'likedPostCount':
lambda: _get_liked_post_count(user, authenticated_user),
lambda: _get_liked_post_count(user, auth_user),
'dislikedPostCount':
lambda: _get_disliked_post_count(user, authenticated_user),
lambda: _get_disliked_post_count(user, auth_user),
'email':
lambda: _get_email(user, authenticated_user, force_show_email),
lambda: _get_email(user, auth_user, force_show_email),
},
options)
def serialize_micro_user(user):
def serialize_micro_user(user, auth_user):
return serialize_user(
user,
authenticated_user=None,
auth_user=auth_user,
options=['name', 'avatarUrl'])
def get_user_count():
@ -162,7 +162,7 @@ def update_user_email(user, email):
raise InvalidEmailError('E-mail is invalid.')
user.email = email
def update_user_rank(user, rank, authenticated_user):
def update_user_rank(user, rank, auth_user):
assert user
if not rank:
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)
if rank in (db.User.RANK_ANONYMOUS, db.User.RANK_NOBODY):
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:
raise errors.AuthError('Trying to set higher rank than your own.')
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.posts.files.has', return_value=True), \
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 \
= lambda comment, auth_user: comment.user.name
snapshots.get_serialized_history.return_value = 'snapshot history'