server/model: use new sqlalchemy import style
This commit is contained in:
parent
f40a8875c4
commit
49e5975254
5 changed files with 227 additions and 198 deletions
|
@ -1,6 +1,4 @@
|
||||||
from sqlalchemy import Column, Integer, DateTime, UnicodeText, ForeignKey
|
import sqlalchemy as sa
|
||||||
from sqlalchemy.orm import relationship, backref
|
|
||||||
from sqlalchemy.sql.expression import func
|
|
||||||
from szurubooru.db import get_session
|
from szurubooru.db import get_session
|
||||||
from szurubooru.model.base import Base
|
from szurubooru.model.base import Base
|
||||||
|
|
||||||
|
@ -8,51 +6,59 @@ from szurubooru.model.base import Base
|
||||||
class CommentScore(Base):
|
class CommentScore(Base):
|
||||||
__tablename__ = 'comment_score'
|
__tablename__ = 'comment_score'
|
||||||
|
|
||||||
comment_id = Column(
|
comment_id = sa.Column(
|
||||||
'comment_id',
|
'comment_id',
|
||||||
Integer,
|
sa.Integer,
|
||||||
ForeignKey('comment.id'),
|
sa.ForeignKey('comment.id'),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
primary_key=True)
|
primary_key=True)
|
||||||
user_id = Column(
|
user_id = sa.Column(
|
||||||
'user_id',
|
'user_id',
|
||||||
Integer,
|
sa.Integer,
|
||||||
ForeignKey('user.id'),
|
sa.ForeignKey('user.id'),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
index=True)
|
index=True)
|
||||||
time = Column('time', DateTime, nullable=False)
|
time = sa.Column('time', sa.DateTime, nullable=False)
|
||||||
score = Column('score', Integer, nullable=False)
|
score = sa.Column('score', sa.Integer, nullable=False)
|
||||||
|
|
||||||
comment = relationship('Comment')
|
comment = sa.orm.relationship('Comment')
|
||||||
user = relationship(
|
user = sa.orm.relationship(
|
||||||
'User',
|
'User',
|
||||||
backref=backref('comment_scores', cascade='all, delete-orphan'))
|
backref=sa.orm.backref('comment_scores', cascade='all, delete-orphan'))
|
||||||
|
|
||||||
|
|
||||||
class Comment(Base):
|
class Comment(Base):
|
||||||
__tablename__ = 'comment'
|
__tablename__ = 'comment'
|
||||||
|
|
||||||
comment_id = Column('id', Integer, primary_key=True)
|
comment_id = sa.Column('id', sa.Integer, primary_key=True)
|
||||||
post_id = Column(
|
post_id = sa.Column(
|
||||||
'post_id', Integer, ForeignKey('post.id'), nullable=False, index=True)
|
'post_id',
|
||||||
user_id = Column(
|
sa.Integer,
|
||||||
'user_id', Integer, ForeignKey('user.id'), nullable=True, index=True)
|
sa.ForeignKey('post.id'),
|
||||||
version = Column('version', Integer, default=1, nullable=False)
|
nullable=False,
|
||||||
creation_time = Column('creation_time', DateTime, nullable=False)
|
index=True)
|
||||||
last_edit_time = Column('last_edit_time', DateTime)
|
user_id = sa.Column(
|
||||||
text = Column('text', UnicodeText, default=None)
|
'user_id',
|
||||||
|
sa.Integer,
|
||||||
|
sa.ForeignKey('user.id'),
|
||||||
|
nullable=True,
|
||||||
|
index=True)
|
||||||
|
version = sa.Column('version', sa.Integer, default=1, nullable=False)
|
||||||
|
creation_time = sa.Column('creation_time', sa.DateTime, nullable=False)
|
||||||
|
last_edit_time = sa.Column('last_edit_time', sa.DateTime)
|
||||||
|
text = sa.Column('text', sa.UnicodeText, default=None)
|
||||||
|
|
||||||
user = relationship('User')
|
user = sa.orm.relationship('User')
|
||||||
post = relationship('Post')
|
post = sa.orm.relationship('Post')
|
||||||
scores = relationship(
|
scores = sa.orm.relationship(
|
||||||
'CommentScore', cascade='all, delete-orphan', lazy='joined')
|
'CommentScore', cascade='all, delete-orphan', lazy='joined')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def score(self) -> int:
|
def score(self) -> int:
|
||||||
return (
|
return (
|
||||||
get_session()
|
get_session()
|
||||||
.query(func.sum(CommentScore.score))
|
.query(sa.sql.expression.func.sum(CommentScore.score))
|
||||||
.filter(CommentScore.comment_id == self.comment_id)
|
.filter(CommentScore.comment_id == self.comment_id)
|
||||||
.one()[0] or 0)
|
.one()[0] or 0)
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,4 @@
|
||||||
from sqlalchemy.sql.expression import func, select
|
import sqlalchemy as sa
|
||||||
from sqlalchemy import (
|
|
||||||
Column, Integer, DateTime, Unicode, UnicodeText, PickleType, ForeignKey)
|
|
||||||
from sqlalchemy.orm import (
|
|
||||||
relationship, column_property, object_session, backref)
|
|
||||||
from szurubooru.model.base import Base
|
from szurubooru.model.base import Base
|
||||||
from szurubooru.model.comment import Comment
|
from szurubooru.model.comment import Comment
|
||||||
|
|
||||||
|
@ -10,95 +6,109 @@ from szurubooru.model.comment import Comment
|
||||||
class PostFeature(Base):
|
class PostFeature(Base):
|
||||||
__tablename__ = 'post_feature'
|
__tablename__ = 'post_feature'
|
||||||
|
|
||||||
post_feature_id = Column('id', Integer, primary_key=True)
|
post_feature_id = sa.Column('id', sa.Integer, primary_key=True)
|
||||||
post_id = Column(
|
post_id = sa.Column(
|
||||||
'post_id', Integer, ForeignKey('post.id'), nullable=False, index=True)
|
'post_id',
|
||||||
user_id = Column(
|
sa.Integer,
|
||||||
'user_id', Integer, ForeignKey('user.id'), nullable=False, index=True)
|
sa.ForeignKey('post.id'),
|
||||||
time = Column('time', DateTime, nullable=False)
|
nullable=False,
|
||||||
|
index=True)
|
||||||
|
user_id = sa.Column(
|
||||||
|
'user_id',
|
||||||
|
sa.Integer,
|
||||||
|
sa.ForeignKey('user.id'),
|
||||||
|
nullable=False,
|
||||||
|
index=True)
|
||||||
|
time = sa.Column('time', sa.DateTime, nullable=False)
|
||||||
|
|
||||||
post = relationship('Post') # type: Post
|
post = sa.orm.relationship('Post') # type: Post
|
||||||
user = relationship(
|
user = sa.orm.relationship(
|
||||||
'User', backref=backref('post_features', cascade='all, delete-orphan'))
|
'User',
|
||||||
|
backref=sa.orm.backref(
|
||||||
|
'post_features', cascade='all, delete-orphan'))
|
||||||
|
|
||||||
|
|
||||||
class PostScore(Base):
|
class PostScore(Base):
|
||||||
__tablename__ = 'post_score'
|
__tablename__ = 'post_score'
|
||||||
|
|
||||||
post_id = Column(
|
post_id = sa.Column(
|
||||||
'post_id',
|
'post_id',
|
||||||
Integer,
|
sa.Integer,
|
||||||
ForeignKey('post.id'),
|
sa.ForeignKey('post.id'),
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
index=True)
|
index=True)
|
||||||
user_id = Column(
|
user_id = sa.Column(
|
||||||
'user_id',
|
'user_id',
|
||||||
Integer,
|
sa.Integer,
|
||||||
ForeignKey('user.id'),
|
sa.ForeignKey('user.id'),
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
index=True)
|
index=True)
|
||||||
time = Column('time', DateTime, nullable=False)
|
time = sa.Column('time', sa.DateTime, nullable=False)
|
||||||
score = Column('score', Integer, nullable=False)
|
score = sa.Column('score', sa.Integer, nullable=False)
|
||||||
|
|
||||||
post = relationship('Post')
|
post = sa.orm.relationship('Post')
|
||||||
user = relationship(
|
user = sa.orm.relationship(
|
||||||
'User',
|
'User',
|
||||||
backref=backref('post_scores', cascade='all, delete-orphan'))
|
backref=sa.orm.backref('post_scores', cascade='all, delete-orphan'))
|
||||||
|
|
||||||
|
|
||||||
class PostFavorite(Base):
|
class PostFavorite(Base):
|
||||||
__tablename__ = 'post_favorite'
|
__tablename__ = 'post_favorite'
|
||||||
|
|
||||||
post_id = Column(
|
post_id = sa.Column(
|
||||||
'post_id',
|
'post_id',
|
||||||
Integer,
|
sa.Integer,
|
||||||
ForeignKey('post.id'),
|
sa.ForeignKey('post.id'),
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
index=True)
|
index=True)
|
||||||
user_id = Column(
|
user_id = sa.Column(
|
||||||
'user_id',
|
'user_id',
|
||||||
Integer,
|
sa.Integer,
|
||||||
ForeignKey('user.id'),
|
sa.ForeignKey('user.id'),
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
index=True)
|
index=True)
|
||||||
time = Column('time', DateTime, nullable=False)
|
time = sa.Column('time', sa.DateTime, nullable=False)
|
||||||
|
|
||||||
post = relationship('Post')
|
post = sa.orm.relationship('Post')
|
||||||
user = relationship(
|
user = sa.orm.relationship(
|
||||||
'User',
|
'User',
|
||||||
backref=backref('post_favorites', cascade='all, delete-orphan'))
|
backref=sa.orm.backref('post_favorites', cascade='all, delete-orphan'))
|
||||||
|
|
||||||
|
|
||||||
class PostNote(Base):
|
class PostNote(Base):
|
||||||
__tablename__ = 'post_note'
|
__tablename__ = 'post_note'
|
||||||
|
|
||||||
post_note_id = Column('id', Integer, primary_key=True)
|
post_note_id = sa.Column('id', sa.Integer, primary_key=True)
|
||||||
post_id = Column(
|
post_id = sa.Column(
|
||||||
'post_id', Integer, ForeignKey('post.id'), nullable=False, index=True)
|
'post_id',
|
||||||
polygon = Column('polygon', PickleType, nullable=False)
|
sa.Integer,
|
||||||
text = Column('text', UnicodeText, nullable=False)
|
sa.ForeignKey('post.id'),
|
||||||
|
nullable=False,
|
||||||
|
index=True)
|
||||||
|
polygon = sa.Column('polygon', sa.PickleType, nullable=False)
|
||||||
|
text = sa.Column('text', sa.UnicodeText, nullable=False)
|
||||||
|
|
||||||
post = relationship('Post')
|
post = sa.orm.relationship('Post')
|
||||||
|
|
||||||
|
|
||||||
class PostRelation(Base):
|
class PostRelation(Base):
|
||||||
__tablename__ = 'post_relation'
|
__tablename__ = 'post_relation'
|
||||||
|
|
||||||
parent_id = Column(
|
parent_id = sa.Column(
|
||||||
'parent_id',
|
'parent_id',
|
||||||
Integer,
|
sa.Integer,
|
||||||
ForeignKey('post.id'),
|
sa.ForeignKey('post.id'),
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
index=True)
|
index=True)
|
||||||
child_id = Column(
|
child_id = sa.Column(
|
||||||
'child_id',
|
'child_id',
|
||||||
Integer,
|
sa.Integer,
|
||||||
ForeignKey('post.id'),
|
sa.ForeignKey('post.id'),
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
index=True)
|
index=True)
|
||||||
|
@ -111,17 +121,17 @@ class PostRelation(Base):
|
||||||
class PostTag(Base):
|
class PostTag(Base):
|
||||||
__tablename__ = 'post_tag'
|
__tablename__ = 'post_tag'
|
||||||
|
|
||||||
post_id = Column(
|
post_id = sa.Column(
|
||||||
'post_id',
|
'post_id',
|
||||||
Integer,
|
sa.Integer,
|
||||||
ForeignKey('post.id'),
|
sa.ForeignKey('post.id'),
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
index=True)
|
index=True)
|
||||||
tag_id = Column(
|
tag_id = sa.Column(
|
||||||
'tag_id',
|
'tag_id',
|
||||||
Integer,
|
sa.Integer,
|
||||||
ForeignKey('tag.id'),
|
sa.ForeignKey('tag.id'),
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
index=True)
|
index=True)
|
||||||
|
@ -146,111 +156,123 @@ class Post(Base):
|
||||||
FLAG_LOOP = 'loop'
|
FLAG_LOOP = 'loop'
|
||||||
|
|
||||||
# basic meta
|
# basic meta
|
||||||
post_id = Column('id', Integer, primary_key=True)
|
post_id = sa.Column('id', sa.Integer, primary_key=True)
|
||||||
user_id = Column(
|
user_id = sa.Column(
|
||||||
'user_id',
|
'user_id',
|
||||||
Integer,
|
sa.Integer,
|
||||||
ForeignKey('user.id', ondelete='SET NULL'),
|
sa.ForeignKey('user.id', ondelete='SET NULL'),
|
||||||
nullable=True,
|
nullable=True,
|
||||||
index=True)
|
index=True)
|
||||||
version = Column('version', Integer, default=1, nullable=False)
|
version = sa.Column('version', sa.Integer, default=1, nullable=False)
|
||||||
creation_time = Column('creation_time', DateTime, nullable=False)
|
creation_time = sa.Column('creation_time', sa.DateTime, nullable=False)
|
||||||
last_edit_time = Column('last_edit_time', DateTime)
|
last_edit_time = sa.Column('last_edit_time', sa.DateTime)
|
||||||
safety = Column('safety', Unicode(32), nullable=False)
|
safety = sa.Column('safety', sa.Unicode(32), nullable=False)
|
||||||
source = Column('source', Unicode(200))
|
source = sa.Column('source', sa.Unicode(200))
|
||||||
flags = Column('flags', PickleType, default=None)
|
flags = sa.Column('flags', sa.PickleType, default=None)
|
||||||
|
|
||||||
# content description
|
# content description
|
||||||
type = Column('type', Unicode(32), nullable=False)
|
type = sa.Column('type', sa.Unicode(32), nullable=False)
|
||||||
checksum = Column('checksum', Unicode(64), nullable=False)
|
checksum = sa.Column('checksum', sa.Unicode(64), nullable=False)
|
||||||
file_size = Column('file_size', Integer)
|
file_size = sa.Column('file_size', sa.Integer)
|
||||||
canvas_width = Column('image_width', Integer)
|
canvas_width = sa.Column('image_width', sa.Integer)
|
||||||
canvas_height = Column('image_height', Integer)
|
canvas_height = sa.Column('image_height', sa.Integer)
|
||||||
mime_type = Column('mime-type', Unicode(32), nullable=False)
|
mime_type = sa.Column('mime-type', sa.Unicode(32), nullable=False)
|
||||||
|
|
||||||
# foreign tables
|
# foreign tables
|
||||||
user = relationship('User')
|
user = sa.orm.relationship('User')
|
||||||
tags = relationship('Tag', backref='posts', secondary='post_tag')
|
tags = sa.orm.relationship('Tag', backref='posts', secondary='post_tag')
|
||||||
relations = relationship(
|
relations = sa.orm.relationship(
|
||||||
'Post',
|
'Post',
|
||||||
secondary='post_relation',
|
secondary='post_relation',
|
||||||
primaryjoin=post_id == PostRelation.parent_id,
|
primaryjoin=post_id == PostRelation.parent_id,
|
||||||
secondaryjoin=post_id == PostRelation.child_id, lazy='joined',
|
secondaryjoin=post_id == PostRelation.child_id, lazy='joined',
|
||||||
backref='related_by')
|
backref='related_by')
|
||||||
features = relationship(
|
features = sa.orm.relationship(
|
||||||
'PostFeature', cascade='all, delete-orphan', lazy='joined')
|
'PostFeature', cascade='all, delete-orphan', lazy='joined')
|
||||||
scores = relationship(
|
scores = sa.orm.relationship(
|
||||||
'PostScore', cascade='all, delete-orphan', lazy='joined')
|
'PostScore', cascade='all, delete-orphan', lazy='joined')
|
||||||
favorited_by = relationship(
|
favorited_by = sa.orm.relationship(
|
||||||
'PostFavorite', cascade='all, delete-orphan', lazy='joined')
|
'PostFavorite', cascade='all, delete-orphan', lazy='joined')
|
||||||
notes = relationship(
|
notes = sa.orm.relationship(
|
||||||
'PostNote', cascade='all, delete-orphan', lazy='joined')
|
'PostNote', cascade='all, delete-orphan', lazy='joined')
|
||||||
comments = relationship('Comment', cascade='all, delete-orphan')
|
comments = sa.orm.relationship('Comment', cascade='all, delete-orphan')
|
||||||
|
|
||||||
# dynamic columns
|
# dynamic columns
|
||||||
tag_count = column_property(
|
tag_count = sa.orm.column_property(
|
||||||
select([func.count(PostTag.tag_id)])
|
sa.sql.expression.select(
|
||||||
|
[sa.sql.expression.func.count(PostTag.tag_id)])
|
||||||
.where(PostTag.post_id == post_id)
|
.where(PostTag.post_id == post_id)
|
||||||
.correlate_except(PostTag))
|
.correlate_except(PostTag))
|
||||||
|
|
||||||
canvas_area = column_property(canvas_width * canvas_height)
|
canvas_area = sa.orm.column_property(canvas_width * canvas_height)
|
||||||
canvas_aspect_ratio = column_property(canvas_width / canvas_height)
|
canvas_aspect_ratio = sa.orm.column_property(canvas_width / canvas_height)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_featured(self) -> bool:
|
def is_featured(self) -> bool:
|
||||||
featured_post = object_session(self) \
|
featured_post = sa.orm.object_session(self) \
|
||||||
.query(PostFeature) \
|
.query(PostFeature) \
|
||||||
.order_by(PostFeature.time.desc()) \
|
.order_by(PostFeature.time.desc()) \
|
||||||
.first()
|
.first()
|
||||||
return featured_post and featured_post.post_id == self.post_id
|
return featured_post and featured_post.post_id == self.post_id
|
||||||
|
|
||||||
score = column_property(
|
score = sa.orm.column_property(
|
||||||
select([func.coalesce(func.sum(PostScore.score), 0)])
|
sa.sql.expression.select(
|
||||||
|
[sa.sql.expression.func.coalesce(
|
||||||
|
sa.sql.expression.func.sum(PostScore.score), 0)])
|
||||||
.where(PostScore.post_id == post_id)
|
.where(PostScore.post_id == post_id)
|
||||||
.correlate_except(PostScore))
|
.correlate_except(PostScore))
|
||||||
|
|
||||||
favorite_count = column_property(
|
favorite_count = sa.orm.column_property(
|
||||||
select([func.count(PostFavorite.post_id)])
|
sa.sql.expression.select(
|
||||||
|
[sa.sql.expression.func.count(PostFavorite.post_id)])
|
||||||
.where(PostFavorite.post_id == post_id)
|
.where(PostFavorite.post_id == post_id)
|
||||||
.correlate_except(PostFavorite))
|
.correlate_except(PostFavorite))
|
||||||
|
|
||||||
last_favorite_time = column_property(
|
last_favorite_time = sa.orm.column_property(
|
||||||
select([func.max(PostFavorite.time)])
|
sa.sql.expression.select(
|
||||||
|
[sa.sql.expression.func.max(PostFavorite.time)])
|
||||||
.where(PostFavorite.post_id == post_id)
|
.where(PostFavorite.post_id == post_id)
|
||||||
.correlate_except(PostFavorite))
|
.correlate_except(PostFavorite))
|
||||||
|
|
||||||
feature_count = column_property(
|
feature_count = sa.orm.column_property(
|
||||||
select([func.count(PostFeature.post_id)])
|
sa.sql.expression.select(
|
||||||
|
[sa.sql.expression.func.count(PostFeature.post_id)])
|
||||||
.where(PostFeature.post_id == post_id)
|
.where(PostFeature.post_id == post_id)
|
||||||
.correlate_except(PostFeature))
|
.correlate_except(PostFeature))
|
||||||
|
|
||||||
last_feature_time = column_property(
|
last_feature_time = sa.orm.column_property(
|
||||||
select([func.max(PostFeature.time)])
|
sa.sql.expression.select(
|
||||||
|
[sa.sql.expression.func.max(PostFeature.time)])
|
||||||
.where(PostFeature.post_id == post_id)
|
.where(PostFeature.post_id == post_id)
|
||||||
.correlate_except(PostFeature))
|
.correlate_except(PostFeature))
|
||||||
|
|
||||||
comment_count = column_property(
|
comment_count = sa.orm.column_property(
|
||||||
select([func.count(Comment.post_id)])
|
sa.sql.expression.select(
|
||||||
|
[sa.sql.expression.func.count(Comment.post_id)])
|
||||||
.where(Comment.post_id == post_id)
|
.where(Comment.post_id == post_id)
|
||||||
.correlate_except(Comment))
|
.correlate_except(Comment))
|
||||||
|
|
||||||
last_comment_creation_time = column_property(
|
last_comment_creation_time = sa.orm.column_property(
|
||||||
select([func.max(Comment.creation_time)])
|
sa.sql.expression.select(
|
||||||
|
[sa.sql.expression.func.max(Comment.creation_time)])
|
||||||
.where(Comment.post_id == post_id)
|
.where(Comment.post_id == post_id)
|
||||||
.correlate_except(Comment))
|
.correlate_except(Comment))
|
||||||
|
|
||||||
last_comment_edit_time = column_property(
|
last_comment_edit_time = sa.orm.column_property(
|
||||||
select([func.max(Comment.last_edit_time)])
|
sa.sql.expression.select(
|
||||||
|
[sa.sql.expression.func.max(Comment.last_edit_time)])
|
||||||
.where(Comment.post_id == post_id)
|
.where(Comment.post_id == post_id)
|
||||||
.correlate_except(Comment))
|
.correlate_except(Comment))
|
||||||
|
|
||||||
note_count = column_property(
|
note_count = sa.orm.column_property(
|
||||||
select([func.count(PostNote.post_id)])
|
sa.sql.expression.select(
|
||||||
|
[sa.sql.expression.func.count(PostNote.post_id)])
|
||||||
.where(PostNote.post_id == post_id)
|
.where(PostNote.post_id == post_id)
|
||||||
.correlate_except(PostNote))
|
.correlate_except(PostNote))
|
||||||
|
|
||||||
relation_count = column_property(
|
relation_count = sa.orm.column_property(
|
||||||
select([func.count(PostRelation.child_id)])
|
sa.sql.expression.select(
|
||||||
|
[sa.sql.expression.func.count(PostRelation.child_id)])
|
||||||
.where(
|
.where(
|
||||||
(PostRelation.parent_id == post_id) |
|
(PostRelation.parent_id == post_id) |
|
||||||
(PostRelation.child_id == post_id))
|
(PostRelation.child_id == post_id))
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
from sqlalchemy.orm import relationship
|
import sqlalchemy as sa
|
||||||
from sqlalchemy import (
|
|
||||||
Column, Integer, DateTime, Unicode, PickleType, ForeignKey)
|
|
||||||
from szurubooru.model.base import Base
|
from szurubooru.model.base import Base
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,20 +10,20 @@ class Snapshot(Base):
|
||||||
OPERATION_DELETED = 'deleted'
|
OPERATION_DELETED = 'deleted'
|
||||||
OPERATION_MERGED = 'merged'
|
OPERATION_MERGED = 'merged'
|
||||||
|
|
||||||
snapshot_id = Column('id', Integer, primary_key=True)
|
snapshot_id = sa.Column('id', sa.Integer, primary_key=True)
|
||||||
creation_time = Column('creation_time', DateTime, nullable=False)
|
creation_time = sa.Column('creation_time', sa.DateTime, nullable=False)
|
||||||
operation = Column('operation', Unicode(16), nullable=False)
|
operation = sa.Column('operation', sa.Unicode(16), nullable=False)
|
||||||
resource_type = Column(
|
resource_type = sa.Column(
|
||||||
'resource_type', Unicode(32), nullable=False, index=True)
|
'resource_type', sa.Unicode(32), nullable=False, index=True)
|
||||||
resource_pkey = Column(
|
resource_pkey = sa.Column(
|
||||||
'resource_pkey', Integer, nullable=False, index=True)
|
'resource_pkey', sa.Integer, nullable=False, index=True)
|
||||||
resource_name = Column(
|
resource_name = sa.Column(
|
||||||
'resource_name', Unicode(64), nullable=False)
|
'resource_name', sa.Unicode(64), nullable=False)
|
||||||
user_id = Column(
|
user_id = sa.Column(
|
||||||
'user_id',
|
'user_id',
|
||||||
Integer,
|
sa.Integer,
|
||||||
ForeignKey('user.id', ondelete='set null'),
|
sa.ForeignKey('user.id', ondelete='set null'),
|
||||||
nullable=True)
|
nullable=True)
|
||||||
data = Column('data', PickleType)
|
data = sa.Column('data', sa.PickleType)
|
||||||
|
|
||||||
user = relationship('User')
|
user = sa.orm.relationship('User')
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
from sqlalchemy import (
|
import sqlalchemy as sa
|
||||||
Column, Integer, DateTime, Unicode, UnicodeText, ForeignKey)
|
|
||||||
from sqlalchemy.orm import relationship, column_property
|
|
||||||
from sqlalchemy.sql.expression import func, select
|
|
||||||
from szurubooru.model.base import Base
|
from szurubooru.model.base import Base
|
||||||
from szurubooru.model.post import PostTag
|
from szurubooru.model.post import PostTag
|
||||||
|
|
||||||
|
@ -9,17 +6,17 @@ from szurubooru.model.post import PostTag
|
||||||
class TagSuggestion(Base):
|
class TagSuggestion(Base):
|
||||||
__tablename__ = 'tag_suggestion'
|
__tablename__ = 'tag_suggestion'
|
||||||
|
|
||||||
parent_id = Column(
|
parent_id = sa.Column(
|
||||||
'parent_id',
|
'parent_id',
|
||||||
Integer,
|
sa.Integer,
|
||||||
ForeignKey('tag.id'),
|
sa.ForeignKey('tag.id'),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
index=True)
|
index=True)
|
||||||
child_id = Column(
|
child_id = sa.Column(
|
||||||
'child_id',
|
'child_id',
|
||||||
Integer,
|
sa.Integer,
|
||||||
ForeignKey('tag.id'),
|
sa.ForeignKey('tag.id'),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
index=True)
|
index=True)
|
||||||
|
@ -32,17 +29,17 @@ class TagSuggestion(Base):
|
||||||
class TagImplication(Base):
|
class TagImplication(Base):
|
||||||
__tablename__ = 'tag_implication'
|
__tablename__ = 'tag_implication'
|
||||||
|
|
||||||
parent_id = Column(
|
parent_id = sa.Column(
|
||||||
'parent_id',
|
'parent_id',
|
||||||
Integer,
|
sa.Integer,
|
||||||
ForeignKey('tag.id'),
|
sa.ForeignKey('tag.id'),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
index=True)
|
index=True)
|
||||||
child_id = Column(
|
child_id = sa.Column(
|
||||||
'child_id',
|
'child_id',
|
||||||
Integer,
|
sa.Integer,
|
||||||
ForeignKey('tag.id'),
|
sa.ForeignKey('tag.id'),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
primary_key=True,
|
primary_key=True,
|
||||||
index=True)
|
index=True)
|
||||||
|
@ -55,11 +52,15 @@ class TagImplication(Base):
|
||||||
class TagName(Base):
|
class TagName(Base):
|
||||||
__tablename__ = 'tag_name'
|
__tablename__ = 'tag_name'
|
||||||
|
|
||||||
tag_name_id = Column('tag_name_id', Integer, primary_key=True)
|
tag_name_id = sa.Column('tag_name_id', sa.Integer, primary_key=True)
|
||||||
tag_id = Column(
|
tag_id = sa.Column(
|
||||||
'tag_id', Integer, ForeignKey('tag.id'), nullable=False, index=True)
|
'tag_id',
|
||||||
name = Column('name', Unicode(64), nullable=False, unique=True)
|
sa.Integer,
|
||||||
order = Column('ord', Integer, nullable=False, index=True)
|
sa.ForeignKey('tag.id'),
|
||||||
|
nullable=False,
|
||||||
|
index=True)
|
||||||
|
name = sa.Column('name', sa.Unicode(64), nullable=False, unique=True)
|
||||||
|
order = sa.Column('ord', sa.Integer, nullable=False, index=True)
|
||||||
|
|
||||||
def __init__(self, name: str, order: int) -> None:
|
def __init__(self, name: str, order: int) -> None:
|
||||||
self.name = name
|
self.name = name
|
||||||
|
@ -69,45 +70,46 @@ class TagName(Base):
|
||||||
class Tag(Base):
|
class Tag(Base):
|
||||||
__tablename__ = 'tag'
|
__tablename__ = 'tag'
|
||||||
|
|
||||||
tag_id = Column('id', Integer, primary_key=True)
|
tag_id = sa.Column('id', sa.Integer, primary_key=True)
|
||||||
category_id = Column(
|
category_id = sa.Column(
|
||||||
'category_id',
|
'category_id',
|
||||||
Integer,
|
sa.Integer,
|
||||||
ForeignKey('tag_category.id'),
|
sa.ForeignKey('tag_category.id'),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
index=True)
|
index=True)
|
||||||
version = Column('version', Integer, default=1, nullable=False)
|
version = sa.Column('version', sa.Integer, default=1, nullable=False)
|
||||||
creation_time = Column('creation_time', DateTime, nullable=False)
|
creation_time = sa.Column('creation_time', sa.DateTime, nullable=False)
|
||||||
last_edit_time = Column('last_edit_time', DateTime)
|
last_edit_time = sa.Column('last_edit_time', sa.DateTime)
|
||||||
description = Column('description', UnicodeText, default=None)
|
description = sa.Column('description', sa.UnicodeText, default=None)
|
||||||
|
|
||||||
category = relationship('TagCategory', lazy='joined')
|
category = sa.orm.relationship('TagCategory', lazy='joined')
|
||||||
names = relationship(
|
names = sa.orm.relationship(
|
||||||
'TagName',
|
'TagName',
|
||||||
cascade='all,delete-orphan',
|
cascade='all,delete-orphan',
|
||||||
lazy='joined',
|
lazy='joined',
|
||||||
order_by='TagName.order')
|
order_by='TagName.order')
|
||||||
suggestions = relationship(
|
suggestions = sa.orm.relationship(
|
||||||
'Tag',
|
'Tag',
|
||||||
secondary='tag_suggestion',
|
secondary='tag_suggestion',
|
||||||
primaryjoin=tag_id == TagSuggestion.parent_id,
|
primaryjoin=tag_id == TagSuggestion.parent_id,
|
||||||
secondaryjoin=tag_id == TagSuggestion.child_id,
|
secondaryjoin=tag_id == TagSuggestion.child_id,
|
||||||
lazy='joined')
|
lazy='joined')
|
||||||
implications = relationship(
|
implications = sa.orm.relationship(
|
||||||
'Tag',
|
'Tag',
|
||||||
secondary='tag_implication',
|
secondary='tag_implication',
|
||||||
primaryjoin=tag_id == TagImplication.parent_id,
|
primaryjoin=tag_id == TagImplication.parent_id,
|
||||||
secondaryjoin=tag_id == TagImplication.child_id,
|
secondaryjoin=tag_id == TagImplication.child_id,
|
||||||
lazy='joined')
|
lazy='joined')
|
||||||
|
|
||||||
post_count = column_property(
|
post_count = sa.orm.column_property(
|
||||||
select([func.count(PostTag.post_id)])
|
sa.sql.expression.select(
|
||||||
|
[sa.sql.expression.func.count(PostTag.post_id)])
|
||||||
.where(PostTag.tag_id == tag_id)
|
.where(PostTag.tag_id == tag_id)
|
||||||
.correlate_except(PostTag))
|
.correlate_except(PostTag))
|
||||||
|
|
||||||
first_name = column_property(
|
first_name = sa.orm.column_property(
|
||||||
(
|
(
|
||||||
select([TagName.name])
|
sa.sql.expression.select([TagName.name])
|
||||||
.where(TagName.tag_id == tag_id)
|
.where(TagName.tag_id == tag_id)
|
||||||
.order_by(TagName.order)
|
.order_by(TagName.order)
|
||||||
.limit(1)
|
.limit(1)
|
||||||
|
@ -115,17 +117,19 @@ class Tag(Base):
|
||||||
),
|
),
|
||||||
deferred=True)
|
deferred=True)
|
||||||
|
|
||||||
suggestion_count = column_property(
|
suggestion_count = sa.orm.column_property(
|
||||||
(
|
(
|
||||||
select([func.count(TagSuggestion.child_id)])
|
sa.sql.expression.select(
|
||||||
|
[sa.sql.expression.func.count(TagSuggestion.child_id)])
|
||||||
.where(TagSuggestion.parent_id == tag_id)
|
.where(TagSuggestion.parent_id == tag_id)
|
||||||
.as_scalar()
|
.as_scalar()
|
||||||
),
|
),
|
||||||
deferred=True)
|
deferred=True)
|
||||||
|
|
||||||
implication_count = column_property(
|
implication_count = sa.orm.column_property(
|
||||||
(
|
(
|
||||||
select([func.count(TagImplication.child_id)])
|
sa.sql.expression.select(
|
||||||
|
[sa.sql.expression.func.count(TagImplication.child_id)])
|
||||||
.where(TagImplication.parent_id == tag_id)
|
.where(TagImplication.parent_id == tag_id)
|
||||||
.as_scalar()
|
.as_scalar()
|
||||||
),
|
),
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from sqlalchemy import Column, Integer, Unicode, Boolean, table
|
import sqlalchemy as sa
|
||||||
from sqlalchemy.orm import column_property
|
|
||||||
from sqlalchemy.sql.expression import func, select
|
|
||||||
from szurubooru.model.base import Base
|
from szurubooru.model.base import Base
|
||||||
from szurubooru.model.tag import Tag
|
from szurubooru.model.tag import Tag
|
||||||
|
|
||||||
|
@ -9,19 +7,20 @@ from szurubooru.model.tag import Tag
|
||||||
class TagCategory(Base):
|
class TagCategory(Base):
|
||||||
__tablename__ = 'tag_category'
|
__tablename__ = 'tag_category'
|
||||||
|
|
||||||
tag_category_id = Column('id', Integer, primary_key=True)
|
tag_category_id = sa.Column('id', sa.Integer, primary_key=True)
|
||||||
version = Column('version', Integer, default=1, nullable=False)
|
version = sa.Column('version', sa.Integer, default=1, nullable=False)
|
||||||
name = Column('name', Unicode(32), nullable=False)
|
name = sa.Column('name', sa.Unicode(32), nullable=False)
|
||||||
color = Column('color', Unicode(32), nullable=False, default='#000000')
|
color = sa.Column(
|
||||||
default = Column('default', Boolean, nullable=False, default=False)
|
'color', sa.Unicode(32), nullable=False, default='#000000')
|
||||||
|
default = sa.Column('default', sa.Boolean, nullable=False, default=False)
|
||||||
|
|
||||||
def __init__(self, name: Optional[str]=None) -> None:
|
def __init__(self, name: Optional[str]=None) -> None:
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
tag_count = column_property(
|
tag_count = sa.orm.column_property(
|
||||||
select([func.count('Tag.tag_id')])
|
sa.sql.expression.select([sa.sql.expression.func.count('Tag.tag_id')])
|
||||||
.where(Tag.category_id == tag_category_id)
|
.where(Tag.category_id == tag_category_id)
|
||||||
.correlate_except(table('Tag')))
|
.correlate_except(sa.table('Tag')))
|
||||||
|
|
||||||
__mapper_args__ = {
|
__mapper_args__ = {
|
||||||
'version_id_col': version,
|
'version_id_col': version,
|
||||||
|
|
Loading…
Reference in a new issue