server/users: don't fetch stats eagerly

This commit is contained in:
rr- 2016-06-14 00:07:48 +02:00
parent d0aaf8fa8f
commit 48cf3b47c0
2 changed files with 33 additions and 26 deletions

View File

@ -1,5 +1,5 @@
from sqlalchemy import Column, Integer, Unicode, DateTime from sqlalchemy import Column, Integer, Unicode, DateTime
from sqlalchemy.orm import column_property from sqlalchemy.orm import object_session
from sqlalchemy.sql.expression import func, select from sqlalchemy.sql.expression import func, select
from szurubooru.db.base import Base from szurubooru.db.base import Base
from szurubooru.db.post import Post, PostScore, PostFavorite from szurubooru.db.post import Post, PostScore, PostFavorite
@ -30,29 +30,39 @@ class User(Base):
avatar_style = Column( avatar_style = Column(
'avatar_style', Unicode(32), nullable=False, default=AVATAR_GRAVATAR) 'avatar_style', Unicode(32), nullable=False, default=AVATAR_GRAVATAR)
post_count = column_property( @property
select([func.coalesce(func.count(1), 0)]) \ def post_count(self):
.where(Post.user_id == user_id) \ return object_session(self) \
.correlate_except(Post)) .query(func.sum(1)) \
.filter(Post.user_id == self.user_id) \
.one()[0] or 0
comment_count = column_property( @property
select([func.coalesce(func.count(1), 0)]) \ def comment_count(self):
.where(Comment.user_id == user_id) \ return object_session(self) \
.correlate_except(Comment)) .query(func.sum(1)) \
.filter(Comment.user_id == self.user_id) \
.one()[0] or 0
favorite_post_count = column_property( @property
select([func.coalesce(func.count(1), 0)]) \ def favorite_post_count(self):
.where(PostFavorite.user_id == user_id) \ return object_session(self) \
.correlate_except(PostFavorite)) .query(func.sum(1)) \
.filter(PostFavorite.user_id == self.user_id) \
.one()[0] or 0
liked_post_count = column_property( @property
select([func.coalesce(func.count(1), 0)]) \ def liked_post_count(self):
.where(PostScore.user_id == user_id) \ return object_session(self) \
.where(PostScore.score == 1) \ .query(func.sum(1)) \
.correlate_except(PostScore)) .filter(PostScore.user_id == self.user_id) \
.filter(PostScore.score == 1) \
.one()[0] or 0
disliked_post_count = column_property( @property
select([func.coalesce(func.count(1), 0)]) \ def disliked_post_count(self):
.where(PostScore.user_id == user_id) \ return object_session(self) \
.where(PostScore.score == -1) \ .query(func.sum(1)) \
.correlate_except(PostScore)) .filter(PostScore.user_id == self.user_id) \
.filter(PostScore.score == -1) \
.one()[0] or 0

View File

@ -40,8 +40,6 @@ class Authenticator(object):
def _authenticate(self, username, password): def _authenticate(self, username, password):
''' Try to authenticate user. Throw AuthError for invalid users. ''' ''' Try to authenticate user. Throw AuthError for invalid users. '''
user = users.get_user_by_name(username) user = users.get_user_by_name(username)
if not user:
raise errors.AuthError('No such user.')
if not auth.is_valid_password(user, password): if not auth.is_valid_password(user, password):
raise errors.AuthError('Invalid password.') raise errors.AuthError('Invalid password.')
return user return user
@ -50,5 +48,4 @@ class Authenticator(object):
user = db.User() user = db.User()
user.name = None user.name = None
user.rank = 'anonymous' user.rank = 'anonymous'
user.password = None
return user return user