2017-02-04 00:08:12 +00:00
|
|
|
import sqlalchemy as sa
|
2020-06-05 22:03:37 +00:00
|
|
|
|
2017-02-04 00:08:12 +00:00
|
|
|
from szurubooru.model.base import Base
|
|
|
|
from szurubooru.model.comment import Comment
|
2020-06-05 22:03:37 +00:00
|
|
|
from szurubooru.model.post import Post, PostFavorite, PostScore
|
2016-03-19 20:37:04 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2016-03-19 20:37:04 +00:00
|
|
|
class User(Base):
|
2020-06-05 22:03:37 +00:00
|
|
|
__tablename__ = "user"
|
|
|
|
|
|
|
|
AVATAR_GRAVATAR = "gravatar"
|
|
|
|
AVATAR_MANUAL = "manual"
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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(128), nullable=False)
|
|
|
|
password_salt = sa.Column("password_salt", sa.Unicode(32))
|
2018-02-25 05:45:00 +00:00
|
|
|
password_revision = sa.Column(
|
2020-06-05 22:03:37 +00:00
|
|
|
"password_revision", sa.SmallInteger, default=0, nullable=False
|
|
|
|
)
|
|
|
|
email = sa.Column("email", sa.Unicode(64), nullable=True)
|
|
|
|
rank = sa.Column("rank", sa.Unicode(32), nullable=False)
|
2017-02-04 00:08:12 +00:00
|
|
|
avatar_style = sa.Column(
|
2020-06-05 22:03:37 +00:00
|
|
|
"avatar_style", sa.Unicode(32), nullable=False, default=AVATAR_GRAVATAR
|
|
|
|
)
|
2016-06-03 19:18:17 +00:00
|
|
|
|
2020-06-05 22:03:37 +00:00
|
|
|
comments = sa.orm.relationship("Comment")
|
2016-08-02 09:56:19 +00:00
|
|
|
|
2016-06-13 22:07:48 +00:00
|
|
|
@property
|
2017-02-04 00:08:12 +00:00
|
|
|
def post_count(self) -> int:
|
2016-06-16 18:03:40 +00:00
|
|
|
from szurubooru.db import session
|
2020-06-05 22:03:37 +00:00
|
|
|
|
2017-02-03 20:42:15 +00:00
|
|
|
return (
|
2020-06-05 22:03:37 +00:00
|
|
|
session.query(sa.sql.expression.func.sum(1))
|
2016-08-14 12:22:53 +00:00
|
|
|
.filter(Post.user_id == self.user_id)
|
2020-06-05 22:03:37 +00:00
|
|
|
.one()[0]
|
|
|
|
or 0
|
|
|
|
)
|
2016-06-13 22:07:48 +00:00
|
|
|
|
|
|
|
@property
|
2017-02-04 00:08:12 +00:00
|
|
|
def comment_count(self) -> int:
|
2016-06-16 18:03:40 +00:00
|
|
|
from szurubooru.db import session
|
2020-06-05 22:03:37 +00:00
|
|
|
|
2017-02-03 20:42:15 +00:00
|
|
|
return (
|
2020-06-05 22:03:37 +00:00
|
|
|
session.query(sa.sql.expression.func.sum(1))
|
2016-08-14 12:22:53 +00:00
|
|
|
.filter(Comment.user_id == self.user_id)
|
2020-06-05 22:03:37 +00:00
|
|
|
.one()[0]
|
|
|
|
or 0
|
|
|
|
)
|
2016-06-13 22:07:48 +00:00
|
|
|
|
|
|
|
@property
|
2017-02-04 00:08:12 +00:00
|
|
|
def favorite_post_count(self) -> int:
|
2016-06-16 18:03:40 +00:00
|
|
|
from szurubooru.db import session
|
2020-06-05 22:03:37 +00:00
|
|
|
|
2017-02-03 20:42:15 +00:00
|
|
|
return (
|
2020-06-05 22:03:37 +00:00
|
|
|
session.query(sa.sql.expression.func.sum(1))
|
2016-08-14 12:22:53 +00:00
|
|
|
.filter(PostFavorite.user_id == self.user_id)
|
2020-06-05 22:03:37 +00:00
|
|
|
.one()[0]
|
|
|
|
or 0
|
|
|
|
)
|
2016-06-13 22:07:48 +00:00
|
|
|
|
|
|
|
@property
|
2017-02-04 00:08:12 +00:00
|
|
|
def liked_post_count(self) -> int:
|
2016-06-16 18:03:40 +00:00
|
|
|
from szurubooru.db import session
|
2020-06-05 22:03:37 +00:00
|
|
|
|
2017-02-03 20:42:15 +00:00
|
|
|
return (
|
2020-06-05 22:03:37 +00:00
|
|
|
session.query(sa.sql.expression.func.sum(1))
|
2016-08-14 12:22:53 +00:00
|
|
|
.filter(PostScore.user_id == self.user_id)
|
|
|
|
.filter(PostScore.score == 1)
|
2020-06-05 22:03:37 +00:00
|
|
|
.one()[0]
|
|
|
|
or 0
|
|
|
|
)
|
2016-06-13 22:07:48 +00:00
|
|
|
|
|
|
|
@property
|
2017-02-04 00:08:12 +00:00
|
|
|
def disliked_post_count(self) -> int:
|
2016-06-16 18:03:40 +00:00
|
|
|
from szurubooru.db import session
|
2020-06-05 22:03:37 +00:00
|
|
|
|
2017-02-03 20:42:15 +00:00
|
|
|
return (
|
2020-06-05 22:03:37 +00:00
|
|
|
session.query(sa.sql.expression.func.sum(1))
|
2016-08-14 12:22:53 +00:00
|
|
|
.filter(PostScore.user_id == self.user_id)
|
|
|
|
.filter(PostScore.score == -1)
|
2020-06-05 22:03:37 +00:00
|
|
|
.one()[0]
|
|
|
|
or 0
|
|
|
|
)
|
2016-08-14 18:30:48 +00:00
|
|
|
|
|
|
|
__mapper_args__ = {
|
2020-06-05 22:03:37 +00:00
|
|
|
"version_id_col": version,
|
|
|
|
"version_id_generator": False,
|
2016-08-14 18:30:48 +00:00
|
|
|
}
|
2018-02-25 10:44:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserToken(Base):
|
2020-06-05 22:03:37 +00:00
|
|
|
__tablename__ = "user_token"
|
2018-02-25 10:44:02 +00:00
|
|
|
|
2020-06-05 22:03:37 +00:00
|
|
|
user_token_id = sa.Column("id", sa.Integer, primary_key=True)
|
2018-02-25 10:44:02 +00:00
|
|
|
user_id = sa.Column(
|
2020-06-05 22:03:37 +00:00
|
|
|
"user_id",
|
2018-02-25 10:44:02 +00:00
|
|
|
sa.Integer,
|
2020-06-05 22:03:37 +00:00
|
|
|
sa.ForeignKey("user.id", ondelete="CASCADE"),
|
2018-02-25 10:44:02 +00:00
|
|
|
nullable=False,
|
2020-06-05 22:03:37 +00:00
|
|
|
index=True,
|
|
|
|
)
|
|
|
|
token = sa.Column("token", sa.Unicode(36), nullable=False)
|
|
|
|
note = sa.Column("note", sa.Unicode(128), nullable=True)
|
|
|
|
enabled = sa.Column("enabled", sa.Boolean, nullable=False, default=True)
|
|
|
|
expiration_time = sa.Column("expiration_time", sa.DateTime, nullable=True)
|
|
|
|
creation_time = sa.Column("creation_time", sa.DateTime, nullable=False)
|
|
|
|
last_edit_time = sa.Column("last_edit_time", sa.DateTime)
|
|
|
|
last_usage_time = sa.Column("last_usage_time", sa.DateTime)
|
|
|
|
version = sa.Column("version", sa.Integer, default=1, nullable=False)
|
|
|
|
|
|
|
|
user = sa.orm.relationship("User")
|