gallery.accords-library.com/server/szurubooru/model/user.py

87 lines
2.8 KiB
Python
Raw Normal View History

import sqlalchemy as sa
from szurubooru.model.base import Base
from szurubooru.model.post import Post, PostScore, PostFavorite
from szurubooru.model.comment import Comment
class User(Base):
__tablename__ = 'user'
2016-04-09 19:41:10 +00:00
AVATAR_GRAVATAR = 'gravatar'
AVATAR_MANUAL = 'manual'
2016-05-08 14:59:25 +00:00
RANK_ANONYMOUS = 'anonymous'
RANK_RESTRICTED = 'restricted'
RANK_REGULAR = 'regular'
RANK_POWER = 'power'
RANK_MODERATOR = 'moderator'
RANK_ADMINISTRATOR = 'administrator'
RANK_NOBODY = 'nobody' # unattainable, used for privileges
2016-05-08 14:59:25 +00:00
user_id = sa.Column('id', sa.Integer, primary_key=True)
creation_time = sa.Column('creation_time', sa.DateTime, nullable=False)
last_login_time = sa.Column('last_login_time', sa.DateTime)
version = sa.Column('version', sa.Integer, default=1, nullable=False)
name = sa.Column('name', sa.Unicode(50), nullable=False, unique=True)
password_hash = sa.Column('password_hash', sa.Unicode(64), nullable=False)
password_salt = sa.Column('password_salt', sa.Unicode(32))
email = sa.Column('email', sa.Unicode(64), nullable=True)
rank = sa.Column('rank', sa.Unicode(32), nullable=False)
avatar_style = sa.Column(
'avatar_style', sa.Unicode(32), nullable=False,
default=AVATAR_GRAVATAR)
comments = sa.orm.relationship('Comment')
2016-08-02 09:56:19 +00:00
@property
def post_count(self) -> int:
2016-06-16 18:03:40 +00:00
from szurubooru.db import session
2017-02-03 20:42:15 +00:00
return (
session
.query(sa.sql.expression.func.sum(1))
.filter(Post.user_id == self.user_id)
.one()[0] or 0)
@property
def comment_count(self) -> int:
2016-06-16 18:03:40 +00:00
from szurubooru.db import session
2017-02-03 20:42:15 +00:00
return (
session
.query(sa.sql.expression.func.sum(1))
.filter(Comment.user_id == self.user_id)
.one()[0] or 0)
@property
def favorite_post_count(self) -> int:
2016-06-16 18:03:40 +00:00
from szurubooru.db import session
2017-02-03 20:42:15 +00:00
return (
session
.query(sa.sql.expression.func.sum(1))
.filter(PostFavorite.user_id == self.user_id)
.one()[0] or 0)
@property
def liked_post_count(self) -> int:
2016-06-16 18:03:40 +00:00
from szurubooru.db import session
2017-02-03 20:42:15 +00:00
return (
session
.query(sa.sql.expression.func.sum(1))
.filter(PostScore.user_id == self.user_id)
.filter(PostScore.score == 1)
.one()[0] or 0)
@property
def disliked_post_count(self) -> int:
2016-06-16 18:03:40 +00:00
from szurubooru.db import session
2017-02-03 20:42:15 +00:00
return (
session
.query(sa.sql.expression.func.sum(1))
.filter(PostScore.user_id == self.user_id)
.filter(PostScore.score == -1)
.one()[0] or 0)
__mapper_args__ = {
'version_id_col': version,
'version_id_generator': False,
}