master
Aevann1 2021-07-24 23:10:05 +02:00
parent 093543abd5
commit fe931726ea
1 changed files with 26 additions and 74 deletions

View File

@ -5,16 +5,13 @@ from sqlalchemy.orm import joinedload, aliased
import re import re
def get_user(username, v=None, nSession=None, graceful=False): def get_user(username, v=None, g.db=None, graceful=False):
username = username.replace('\\', '') username = username.replace('\\', '')
username = username.replace('_', '\_') username = username.replace('_', '\_')
username = username.replace('%', '') username = username.replace('%', '')
if not nSession: user = g.db.query(
nSession = g.db
user = nSession.query(
User User
).filter( ).filter(
or_( or_(
@ -30,7 +27,7 @@ def get_user(username, v=None, nSession=None, graceful=False):
return None return None
if v: if v:
block = nSession.query(UserBlock).filter( block = g.db.query(UserBlock).filter(
or_( or_(
and_( and_(
UserBlock.user_id == v.id, UserBlock.user_id == v.id,
@ -47,15 +44,12 @@ def get_user(username, v=None, nSession=None, graceful=False):
return user return user
def get_account(base36id, v=None, nSession=None, graceful=False): def get_account(base36id, v=None, g.db=None, graceful=False):
if not nSession:
nSession = g.db
if isinstance(base36id, str): id = base36decode(base36id) if isinstance(base36id, str): id = base36decode(base36id)
else: id = base36id else: id = base36id
user = nSession.query(User user = g.db.query(User
).filter( ).filter(
User.id == id User.id == id
).first() ).first()
@ -67,7 +61,7 @@ def get_account(base36id, v=None, nSession=None, graceful=False):
return None return None
if v: if v:
block = nSession.query(UserBlock).filter( block = g.db.query(UserBlock).filter(
or_( or_(
and_( and_(
UserBlock.user_id == v.id, UserBlock.user_id == v.id,
@ -85,25 +79,23 @@ def get_account(base36id, v=None, nSession=None, graceful=False):
return user return user
def get_post(pid, v=None, graceful=False, nSession=None, **kwargs): def get_post(pid, v=None, graceful=False, g.db=None, **kwargs):
if isinstance(pid, str): if isinstance(pid, str):
i = base36decode(pid) i = base36decode(pid)
else: else:
i = pid i = pid
nSession = nSession or kwargs.get("session")or g.db
if v: if v:
vt = nSession.query(Vote).filter_by( vt = g.db.query(Vote).filter_by(
user_id=v.id, submission_id=i).subquery() user_id=v.id, submission_id=i).subquery()
mod = nSession.query(ModRelationship).filter_by( mod = g.db.query(ModRelationship).filter_by(
user_id=v.id, accepted=True, invite_rescinded=False).subquery() user_id=v.id, accepted=True, invite_rescinded=False).subquery()
boardblocks = nSession.query( boardblocks = g.db.query(
BoardBlock).filter_by(user_id=v.id).subquery() BoardBlock).filter_by(user_id=v.id).subquery()
blocking = v.blocking.subquery() blocking = v.blocking.subquery()
items = nSession.query( items = g.db.query(
Submission, Submission,
vt.c.vote_type, vt.c.vote_type,
aliased(ModRelationship, alias=mod), aliased(ModRelationship, alias=mod),
@ -151,7 +143,7 @@ def get_post(pid, v=None, graceful=False, nSession=None, **kwargs):
# x._is_exiled_for=items[5] or 0 # x._is_exiled_for=items[5] or 0
else: else:
items = nSession.query( items = g.db.query(
Submission, Submission,
# aliased(ModAction, alias=exile) # aliased(ModAction, alias=exile)
).options( ).options(
@ -385,16 +377,14 @@ def get_post_with_comments(pid, sort="top", v=None):
return post return post
def get_comment(cid, nSession=None, v=None, graceful=False, **kwargs): def get_comment(cid, g.db=None, v=None, graceful=False, **kwargs):
if isinstance(cid, str): if isinstance(cid, str):
i = base36decode(cid) i = base36decode(cid)
else: else:
i = cid i = cid
nSession = nSession or kwargs.get('session') or g.db exile = g.db.query(ModAction
exile = nSession.query(ModAction
).options( ).options(
lazyload('*') lazyload('*')
).filter_by( ).filter_by(
@ -404,18 +394,18 @@ def get_comment(cid, nSession=None, v=None, graceful=False, **kwargs):
if v: if v:
blocking = v.blocking.subquery() blocking = v.blocking.subquery()
blocked = v.blocked.subquery() blocked = v.blocked.subquery()
vt = nSession.query(CommentVote).filter( vt = g.db.query(CommentVote).filter(
CommentVote.user_id == v.id, CommentVote.user_id == v.id,
CommentVote.comment_id == i).subquery() CommentVote.comment_id == i).subquery()
mod=nSession.query(ModRelationship mod=g.db.query(ModRelationship
).filter_by( ).filter_by(
user_id=v.id, user_id=v.id,
accepted=True accepted=True
).subquery() ).subquery()
items = nSession.query( items = g.db.query(
Comment, Comment,
vt.c.vote_type, vt.c.vote_type,
aliased(ModRelationship, alias=mod), aliased(ModRelationship, alias=mod),
@ -454,7 +444,7 @@ def get_comment(cid, nSession=None, v=None, graceful=False, **kwargs):
x._is_guildmaster=items[2] or 0 x._is_guildmaster=items[2] or 0
x._is_exiled_for=items[3] or 0 x._is_exiled_for=items[3] or 0
block = nSession.query(UserBlock).filter( block = g.db.query(UserBlock).filter(
or_( or_(
and_( and_(
UserBlock.user_id == v.id, UserBlock.user_id == v.id,
@ -470,7 +460,7 @@ def get_comment(cid, nSession=None, v=None, graceful=False, **kwargs):
x._is_blocked = block and block.target_id == v.id x._is_blocked = block and block.target_id == v.id
else: else:
q = nSession.query( q = g.db.query(
Comment, Comment,
aliased(ModAction, alias=exile) aliased(ModAction, alias=exile)
).options( ).options(
@ -491,46 +481,24 @@ def get_comment(cid, nSession=None, v=None, graceful=False, **kwargs):
return x return x
def get_comments(cids, v=None, nSession=None, sort="new", def get_comments(cids, v=None, g.db=None, sort="new",
load_parent=False, **kwargs): load_parent=False, **kwargs):
if not cids: if not cids: return []
return []
cids=tuple(cids) cids=tuple(cids)
nSession = nSession or kwargs.get('session') or g.db
exile=nSession.query(ModAction
).options(
lazyload('*')
).filter(
ModAction.kind=="exile_user",
ModAction.target_comment_id.in_(cids)
).distinct(ModAction.target_comment_id).subquery()
if v: if v:
vt = nSession.query(CommentVote).filter( vt = g.db.query(CommentVote).filter(
CommentVote.comment_id.in_(cids), CommentVote.comment_id.in_(cids),
CommentVote.user_id==v.id CommentVote.user_id==v.id
).subquery() ).subquery()
mod=nSession.query(ModRelationship query = g.db.query(
).filter_by(
user_id=v.id,
accepted=True
).subquery()
query = nSession.query(
Comment, Comment,
aliased(CommentVote, alias=vt), aliased(CommentVote, alias=vt),
aliased(ModRelationship, alias=mod),
aliased(ModAction, alias=exile)
).options( ).options(
joinedload(Comment.author).joinedload(User.title) joinedload(Comment.author))
)
if v.admin_level >=4: if v.admin_level >=4:
query=query.options(joinedload(Comment.oauth_app)) query=query.options(joinedload(Comment.oauth_app))
@ -541,8 +509,6 @@ def get_comments(cids, v=None, nSession=None, sort="new",
Comment.parent_comment Comment.parent_comment
).joinedload( ).joinedload(
Comment.author Comment.author
).joinedload(
User.title
) )
) )
@ -553,27 +519,13 @@ def get_comments(cids, v=None, nSession=None, sort="new",
).join( ).join(
Comment.post, Comment.post,
isouter=True isouter=True
).join(
mod,
mod.c.board_id==Submission.board_id,
isouter=True
).join(
exile,
and_(exile.c.target_comment_id==Comment.id, exile.c.board_id==Comment.original_board_id),
isouter=True
).filter( ).filter(
Comment.id.in_(cids) Comment.id.in_(cids)
) )
query=query.options( output = [x[0] for x in query.all()]
# contains_eager(Comment.post).contains_eager(Submission.board)
).order_by(None).all()
comments=[x for x in query]
output = [x[0] for x in comments]
for i in range(len(output)): for i in range(len(output)):
output[i]._voted = comments[i][1].vote_type if comments[i][1] else 0 output[i]._voted = comments[i][1].vote_type if comments[i][1] else 0
output[i]._is_guildmaster = comments[i][2] output[i]._is_guildmaster = comments[i][2]
@ -582,7 +534,7 @@ def get_comments(cids, v=None, nSession=None, sort="new",
else: else:
query = nSession.query( query = g.db.query(
Comment, Comment,
aliased(ModAction, alias=exile) aliased(ModAction, alias=exile)
).options( ).options(