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.orm import column_property
from sqlalchemy.orm import object_session
from sqlalchemy.sql.expression import func, select
from szurubooru.db.base import Base
from szurubooru.db.post import Post, PostScore, PostFavorite
@ -30,29 +30,39 @@ class User(Base):
avatar_style = Column(
'avatar_style', Unicode(32), nullable=False, default=AVATAR_GRAVATAR)
post_count = column_property(
select([func.coalesce(func.count(1), 0)]) \
.where(Post.user_id == user_id) \
.correlate_except(Post))
@property
def post_count(self):
return object_session(self) \
.query(func.sum(1)) \
.filter(Post.user_id == self.user_id) \
.one()[0] or 0
comment_count = column_property(
select([func.coalesce(func.count(1), 0)]) \
.where(Comment.user_id == user_id) \
.correlate_except(Comment))
@property
def comment_count(self):
return object_session(self) \
.query(func.sum(1)) \
.filter(Comment.user_id == self.user_id) \
.one()[0] or 0
favorite_post_count = column_property(
select([func.coalesce(func.count(1), 0)]) \
.where(PostFavorite.user_id == user_id) \
.correlate_except(PostFavorite))
@property
def favorite_post_count(self):
return object_session(self) \
.query(func.sum(1)) \
.filter(PostFavorite.user_id == self.user_id) \
.one()[0] or 0
liked_post_count = column_property(
select([func.coalesce(func.count(1), 0)]) \
.where(PostScore.user_id == user_id) \
.where(PostScore.score == 1) \
.correlate_except(PostScore))
@property
def liked_post_count(self):
return object_session(self) \
.query(func.sum(1)) \
.filter(PostScore.user_id == self.user_id) \
.filter(PostScore.score == 1) \
.one()[0] or 0
disliked_post_count = column_property(
select([func.coalesce(func.count(1), 0)]) \
.where(PostScore.user_id == user_id) \
.where(PostScore.score == -1) \
.correlate_except(PostScore))
@property
def disliked_post_count(self):
return object_session(self) \
.query(func.sum(1)) \
.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):
''' Try to authenticate user. Throw AuthError for invalid users. '''
user = users.get_user_by_name(username)
if not user:
raise errors.AuthError('No such user.')
if not auth.is_valid_password(user, password):
raise errors.AuthError('Invalid password.')
return user
@ -50,5 +48,4 @@ class Authenticator(object):
user = db.User()
user.name = None
user.rank = 'anonymous'
user.password = None
return user