diff --git a/files/classes/clients.py b/files/classes/clients.py index b1449e11b..0721e240e 100644 --- a/files/classes/clients.py +++ b/files/classes/clients.py @@ -27,7 +27,7 @@ class OauthApp(Base, Stndrd): def idlist(self, page=1, **kwargs): - posts = g.db.query(Submission.id).options(lazyload('*')).filter_by(app_id=self.id) + posts = g.db.query(Submission.id).options(lazyload('*')).options(lazyload('*')).filter_by(app_id=self.id) posts=posts.order_by(Submission.created_utc.desc()) @@ -37,7 +37,7 @@ class OauthApp(Base, Stndrd): def comments_idlist(self, page=1, **kwargs): - posts = g.db.query(Comment.id).options(lazyload('*')).filter_by(app_id=self.id) + posts = g.db.query(Comment.id).options(lazyload('*')).options(lazyload('*')).filter_by(app_id=self.id) posts=posts.order_by(Comment.created_utc.desc()) diff --git a/files/classes/user.py b/files/classes/user.py index 453eef371..254d49ea3 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -155,7 +155,7 @@ class User(Base, Stndrd, Age_times): def has_block(self, target): - return g.db.query(UserBlock).filter_by( + return g.db.query(UserBlock).options(lazyload('*')).filter_by( user_id=self.id, target_id=target.id).first() @property @@ -164,7 +164,7 @@ class User(Base, Stndrd, Age_times): def any_block_exists(self, other): - return g.db.query(UserBlock).filter( + return g.db.query(UserBlock).options(lazyload('*')).filter( or_(and_(UserBlock.user_id == self.id, UserBlock.target_id == other.id), and_( UserBlock.user_id == other.id, UserBlock.target_id == self.id))).first() @@ -187,7 +187,7 @@ class User(Base, Stndrd, Age_times): if self.shadowbanned and not (v and (v.admin_level >= 3 or v.id == self.id)): return [] - submissions = g.db.query(Submission).options(lazyload('*')).filter_by(author_id=self.id, is_pinned=False) + submissions = g.db.query(Submission).options(lazyload('*')).options(lazyload('*')).filter_by(author_id=self.id, is_pinned=False) if not (v and (v.admin_level >= 3 or v.id == self.id)): submissions = submissions.filter_by(deleted_utc=0, is_banned=False, private=False) @@ -233,7 +233,7 @@ class User(Base, Stndrd, Age_times): def banned_by(self): if not self.is_suspended: return None - return g.db.query(User).filter_by(id=self.is_banned).first() + return g.db.query(User).options(lazyload('*')).filter_by(id=self.is_banned).first() def has_badge(self, badgedef_id): return self.badges.filter_by(badge_id=badgedef_id).first() @@ -310,11 +310,11 @@ class User(Base, Stndrd, Age_times): awards = {} - posts_idlist = g.db.query(Submission.id).filter_by(author_id=self.id).subquery() - comments_idlist = g.db.query(Comment.id).filter_by(author_id=self.id).subquery() + posts_idlist = g.db.query(Submission.id).options(lazyload('*')).filter_by(author_id=self.id).subquery() + comments_idlist = g.db.query(Comment.id).options(lazyload('*')).filter_by(author_id=self.id).subquery() - post_awards = g.db.query(AwardRelationship).filter(AwardRelationship.submission_id.in_(posts_idlist)).all() - comment_awards = g.db.query(AwardRelationship).filter(AwardRelationship.comment_id.in_(comments_idlist)).all() + post_awards = g.db.query(AwardRelationship).options(lazyload('*')).filter(AwardRelationship.submission_id.in_(posts_idlist)).all() + comment_awards = g.db.query(AwardRelationship).options(lazyload('*')).filter(AwardRelationship.comment_id.in_(comments_idlist)).all() total_awards = post_awards + comment_awards @@ -345,7 +345,7 @@ class User(Base, Stndrd, Age_times): @lazy def alts(self): - subq = g.db.query(Alt).filter( + subq = g.db.query(Alt).options(lazyload('*')).filter( or_( Alt.user1 == self.id, Alt.user2 == self.id @@ -407,7 +407,7 @@ class User(Base, Stndrd, Age_times): def has_follower(self, user): - return g.db.query(Follow).filter_by(target_id=self.id, user_id=user.id).first() + return g.db.query(Follow).options(lazyload('*')).filter_by(target_id=self.id, user_id=user.id).first() @property def banner_url(self): @@ -511,16 +511,16 @@ class User(Base, Stndrd, Age_times): OauthApp.id.asc()).all()] def subscribed_idlist(self, page=1): - posts = g.db.query(Subscription.submission_id).filter_by(user_id=self.id).all() + posts = g.db.query(Subscription.submission_id).options(lazyload('*')).filter_by(user_id=self.id).all() return [x[0] for x in posts] def saved_idlist(self, page=1): - posts = g.db.query(Submission.id).options(lazyload('*')).filter_by(is_banned=False, + posts = g.db.query(Submission.id).options(lazyload('*')).options(lazyload('*')).filter_by(is_banned=False, deleted_utc=0 ) - saved = g.db.query(SaveRelationship.submission_id).filter(SaveRelationship.user_id == self.id).subquery() + saved = g.db.query(SaveRelationship.submission_id).options(lazyload('*')).filter(SaveRelationship.user_id == self.id).subquery() posts = posts.filter(Submission.id.in_(saved)) if self.admin_level == 0: @@ -542,9 +542,9 @@ class User(Base, Stndrd, Age_times): def saved_comment_idlist(self, page=1): - comments = g.db.query(Comment.id).options(lazyload('*')).filter_by(is_banned=False, deleted_utc=0) + comments = g.db.query(Comment.id).options(lazyload('*')).options(lazyload('*')).filter_by(is_banned=False, deleted_utc=0) - saved = g.db.query(SaveRelationship.submission_id).filter(SaveRelationship.user_id == self.id).subquery() + saved = g.db.query(SaveRelationship.submission_id).options(lazyload('*')).filter(SaveRelationship.user_id == self.id).subquery() comments = comments.filter(Comment.id.in_(saved)) if self.admin_level == 0: diff --git a/files/helpers/alerts.py b/files/helpers/alerts.py index 3cc117915..bc81e63ca 100644 --- a/files/helpers/alerts.py +++ b/files/helpers/alerts.py @@ -187,7 +187,7 @@ def send_admin(vid, text): new_aux = CommentAux(id=new_comment.id, body=text, body_html=text_html) g.db.add(new_aux) - admins = g.db.query(User).filter(User.admin_level > 0).all() + admins = g.db.query(User).options(lazyload('*')).filter(User.admin_level > 0).all() for admin in admins: notif = Notification(comment_id=new_comment.id, user_id=admin.id) g.db.add(notif) diff --git a/files/helpers/filters.py b/files/helpers/filters.py index c4c33cebe..e4c1698f2 100644 --- a/files/helpers/filters.py +++ b/files/helpers/filters.py @@ -30,7 +30,7 @@ def filter_comment_html(html_text): # search db for domain rules that prohibit commenting bans = [ - x for x in g.db.query(BannedDomain).filter(BannedDomain.domain.in_(list(domain_list))).all()] + x for x in g.db.query(BannedDomain).options(lazyload('*')).filter(BannedDomain.domain.in_(list(domain_list))).all()] if bans: return bans diff --git a/files/helpers/get.py b/files/helpers/get.py index 864832ee7..c0ecd46c8 100644 --- a/files/helpers/get.py +++ b/files/helpers/get.py @@ -24,7 +24,7 @@ def get_user(username, v=None, graceful=False): return None if v: - block = g.db.query(UserBlock).filter( + block = g.db.query(UserBlock).options(lazyload('*')).filter( or_( and_( UserBlock.user_id == v.id, @@ -43,16 +43,16 @@ def get_user(username, v=None, graceful=False): def get_account(id, v=None): - user = g.db.query(User).filter_by(id = id).first() + user = g.db.query(User).options(lazyload('*')).filter_by(id = id).first() if not user: try: id = int(str(id), 36) except: abort(404) - user = g.db.query(User).filter_by(id = id).first() + user = g.db.query(User).options(lazyload('*')).filter_by(id = id).first() if not user: abort(404) if v: - block = g.db.query(UserBlock).filter( + block = g.db.query(UserBlock).options(lazyload('*')).filter( or_( and_( UserBlock.user_id == v.id, @@ -73,7 +73,7 @@ def get_account(id, v=None): def get_post(i, v=None, graceful=False, **kwargs): if v: - vt = g.db.query(Vote).filter_by( + vt = g.db.query(Vote).options(lazyload('*')).filter_by( user_id=v.id, submission_id=i).subquery() blocking = v.blocking.subquery() @@ -122,7 +122,7 @@ def get_posts(pids, v=None): pids=tuple(pids) if v: - vt = g.db.query(Vote).filter( + vt = g.db.query(Vote).options(lazyload('*')).filter( Vote.submission_id.in_(pids), Vote.user_id==v.id ).subquery() @@ -155,7 +155,7 @@ def get_posts(pids, v=None): output[i]._is_blocking = query[i][2] or 0 output[i]._is_blocked = query[i][3] or 0 else: - output = g.db.query(Submission,).filter(Submission.id.in_(pids)).all() + output = g.db.query(Submission,).options(lazyload('*')).filter(Submission.id.in_(pids)).all() return sorted(output, key=lambda x: pids.index(x.id)) @@ -163,11 +163,11 @@ def get_comment(i, v=None, graceful=False, **kwargs): if v: - comment=g.db.query(Comment).filter(Comment.id == i).first() + comment=g.db.query(Comment).options(lazyload('*')).filter(Comment.id == i).first() if not comment and not graceful: abort(404) - block = g.db.query(UserBlock).filter( + block = g.db.query(UserBlock).options(lazyload('*')).filter( or_( and_( UserBlock.user_id == v.id, @@ -179,14 +179,14 @@ def get_comment(i, v=None, graceful=False, **kwargs): ) ).first() - vts = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id) - vt = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id).first() + vts = g.db.query(CommentVote).options(lazyload('*')).filter_by(user_id=v.id, comment_id=comment.id) + vt = g.db.query(CommentVote).options(lazyload('*')).filter_by(user_id=v.id, comment_id=comment.id).first() comment._is_blocking = block and block.user_id == v.id comment._is_blocked = block and block.target_id == v.id comment.voted = vt.vote_type if vt else 0 else: - comment = g.db.query(Comment).filter(Comment.id == i).first() + comment = g.db.query(Comment).options(lazyload('*')).filter(Comment.id == i).first() if not comment and not graceful:abort(404) return comment @@ -199,7 +199,7 @@ def get_comments(cids, v=None, load_parent=False): cids=tuple(cids) if v: - votes = g.db.query(CommentVote).filter_by(user_id=v.id).subquery() + votes = g.db.query(CommentVote).options(lazyload('*')).filter_by(user_id=v.id).subquery() blocking = v.blocking.subquery() @@ -238,7 +238,7 @@ def get_comments(cids, v=None, load_parent=False): output.append(comment) else: - output = g.db.query(Comment).join(Comment.author).filter(Comment.id.in_(cids), User.shadowbanned == False).all() + output = g.db.query(Comment).join(Comment.author).options(lazyload('*')).filter(Comment.id.in_(cids), User.shadowbanned == False).all() if load_parent: parents = [x.parent_comment_id for x in output if x.parent_comment_id] @@ -263,7 +263,7 @@ def get_domain(s): domain_list = tuple(list(domain_list)) - doms = [x for x in g.db.query(BannedDomain).filter( + doms = [x for x in g.db.query(BannedDomain).options(lazyload('*')).filter( BannedDomain.domain.in_(domain_list)).all()] if not doms: diff --git a/files/helpers/wrappers.py b/files/helpers/wrappers.py index cdc0d670a..507adee0c 100644 --- a/files/helpers/wrappers.py +++ b/files/helpers/wrappers.py @@ -13,7 +13,7 @@ def get_logged_in_user(): token = request.headers.get("Authorization") if not token: return None - client = g.db.query(ClientAuth).filter(ClientAuth.access_token == token).first() + client = g.db.query(ClientAuth).options(lazyload('*')).filter(ClientAuth.access_token == token).first() x = (client.user, client) if client else (None, None) @@ -24,7 +24,7 @@ def get_logged_in_user(): nonce = session.get("login_nonce", 0) if not uid: x= (None, None) try: - if g.db: v = g.db.query(User).filter_by(id=uid).first() + if g.db: v = g.db.query(User).options(lazyload('*')).filter_by(id=uid).first() else: v = None except: v = None @@ -54,7 +54,7 @@ def check_ban_evade(v): v.ban(reason="ban evasion") send_notification(NOTIFICATIONS_ACCOUNT, v, "Your account has been permanently suspended for the following reason:\n\n> ban evasion") - for post in g.db.query(Submission).filter_by(author_id=v.id).all(): + for post in g.db.query(Submission).options(lazyload('*')).filter_by(author_id=v.id).all(): if post.is_banned: continue @@ -74,7 +74,7 @@ def check_ban_evade(v): g.db.flush() - for comment in g.db.query(Comment).filter_by(author_id=v.id).all(): + for comment in g.db.query(Comment).options(lazyload('*')).filter_by(author_id=v.id).all(): if comment.is_banned: continue diff --git a/files/mail/mail.py b/files/mail/mail.py index de7093225..f0ca51c20 100644 --- a/files/mail/mail.py +++ b/files/mail/mail.py @@ -79,7 +79,7 @@ def activate(v): if not validate_hash(f"{email}+{id}+{timestamp}", token): abort(403) - user = g.db.query(User).filter_by(id=id).first() + user = g.db.query(User).options(lazyload('*')).filter_by(id=id).first() if not user: abort(404) diff --git a/files/routes/admin.py b/files/routes/admin.py index dacb39858..12baabaec 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -31,14 +31,14 @@ def revert_actions(v, username): user = get_user(username) if not user: abort(404) - items = g.db.query(Submission).options(lazyload('*')).filter_by(removed_by=user.id).all() + g.db.query(Comment).options(lazyload('*')).filter_by(removed_by=user.id).all() + items = g.db.query(Submission).options(lazyload('*')).options(lazyload('*')).filter_by(removed_by=user.id).all() + g.db.query(Comment).options(lazyload('*')).options(lazyload('*')).filter_by(removed_by=user.id).all() for item in items: item.is_banned = False item.removed_by = None g.db.add(item) - users = g.db.query(User).options(lazyload('*')).filter_by(is_banned=user.id).all() + users = g.db.query(User).options(lazyload('*')).options(lazyload('*')).filter_by(is_banned=user.id).all() for user in users: user.unban() @@ -144,7 +144,7 @@ def monthly(v): if 'pcm' in request.host or ('rdrama' in request.host and v.id in [1,12,28,29,747,995,1480]) or ('rdrama' not in request.host and 'pcm' not in request.host): thing = g.db.query(AwardRelationship).order_by(AwardRelationship.id.desc()).first().id _awards = [] - for u in g.db.query(User).filter(User.patron > 0).all(): + for u in g.db.query(User).options(lazyload('*')).filter(User.patron > 0).all(): grant_awards = {} if u.patron == 1: @@ -224,7 +224,7 @@ def post_rules(v): @auth_required def shadowbanned(v): if not (v and v.admin_level == 6): abort(404) - users = [x for x in g.db.query(User).filter_by(shadowbanned = True).all()] + users = [x for x in g.db.query(User).options(lazyload('*')).filter_by(shadowbanned = True).all()] return render_template("banned.html", v=v, users=users) @@ -232,7 +232,7 @@ def shadowbanned(v): @auth_required def agendaposters(v): if not (v and v.admin_level == 6): abort(404) - users = [x for x in g.db.query(User).filter_by(agendaposter = True).all()] + users = [x for x in g.db.query(User).options(lazyload('*')).filter_by(agendaposter = True).all()] return render_template("banned.html", v=v, users=users) @@ -260,7 +260,7 @@ def reported_posts(v): page = max(1, int(request.args.get("page", 1))) - posts = g.db.query(Submission).filter_by( + posts = g.db.query(Submission).options(lazyload('*')).filter_by( is_approved=0, is_banned=False ).join(Submission.flags).order_by(Submission.id.desc()).offset(25 * (page - 1)).limit(26) @@ -352,7 +352,7 @@ def badge_grant_post(v): except: abort(400) if user.has_badge(badge_id): - g.db.query(Badge).filter_by(badge_id=badge_id, user_id=user.id,).delete() + g.db.query(Badge).options(lazyload('*')).filter_by(badge_id=badge_id, user_id=user.id,).delete() return redirect("/admin/badge_grant") new_badge = Badge(badge_id=badge_id, @@ -376,7 +376,7 @@ def badge_grant_post(v): send_notification(NOTIFICATIONS_ACCOUNT, user, text) if badge_id == 16 and user.has_badge(17): - g.db.query(Badge).filter_by(badge_id=17, user_id=user.id).delete() + g.db.query(Badge).options(lazyload('*')).filter_by(badge_id=17, user_id=user.id).delete() elif badge_id in [21,22,23,24,28]: user.patron = int(str(badge_id)[-1]) @@ -446,7 +446,7 @@ def users_list(v): page = int(request.args.get("page", 1)) - users = g.db.query(User).filter_by(is_banned=0 + users = g.db.query(User).options(lazyload('*')).filter_by(is_banned=0 ).order_by(User.created_utc.desc() ).offset(25 * (page - 1)).limit(26) @@ -597,7 +597,7 @@ def admin_removed(v): page = int(request.args.get("page", 1)) - ids = g.db.query(Submission.id).options(lazyload('*')).filter_by(is_banned=True).order_by( + ids = g.db.query(Submission.id).options(lazyload('*')).options(lazyload('*')).filter_by(is_banned=True).order_by( Submission.id.desc()).offset(25 * (page - 1)).limit(26).all() ids=[x[0] for x in ids] @@ -621,7 +621,7 @@ def admin_removed(v): def admin_image_purge(v): name = request.form.get("url") - image = g.db.query(Image).filter(Image.text == name).first() + image = g.db.query(Image).options(lazyload('*')).filter(Image.text == name).first() if image: requests.delete(f'https://api.imgur.com/3/image/{image.deletehash}', headers = {"Authorization": f"Client-ID {IMGUR_KEY}"}) headers = {"Authorization": f"Bearer {CF_KEY}", "Content-Type": "application/json"} @@ -663,7 +663,7 @@ def admin_image_ban(v): h = ''.join([str(d) for d in bindigits]) #check db for existing - badpic = g.db.query(BadPic).filter_by( + badpic = g.db.query(BadPic).options(lazyload('*')).filter_by( phash=h ).first() @@ -688,7 +688,7 @@ def admin_image_ban(v): @admin_level_required(6) @validate_formkey def agendaposter(user_id, v): - user = g.db.query(User).filter_by(id=user_id).first() + user = g.db.query(User).options(lazyload('*')).filter_by(id=user_id).first() expiry = request.form.get("days", 0) if expiry: @@ -741,7 +741,7 @@ def agendaposter(user_id, v): @admin_level_required(6) @validate_formkey def shadowban(user_id, v): - user = g.db.query(User).filter_by(id=user_id).first() + user = g.db.query(User).options(lazyload('*')).filter_by(id=user_id).first() if user.admin_level != 0: abort(403) user.shadowbanned = True g.db.add(user) @@ -766,7 +766,7 @@ def shadowban(user_id, v): @admin_level_required(6) @validate_formkey def unshadowban(user_id, v): - user = g.db.query(User).filter_by(id=user_id).first() + user = g.db.query(User).options(lazyload('*')).filter_by(id=user_id).first() if user.admin_level != 0: abort(403) user.shadowbanned = False g.db.add(user) @@ -790,7 +790,7 @@ def unshadowban(user_id, v): @admin_level_required(6) @validate_formkey def verify(user_id, v): - user = g.db.query(User).filter_by(id=user_id).first() + user = g.db.query(User).options(lazyload('*')).filter_by(id=user_id).first() user.verified = "Verified" g.db.add(user) g.db.commit() @@ -800,7 +800,7 @@ def verify(user_id, v): @admin_level_required(6) @validate_formkey def unverify(user_id, v): - user = g.db.query(User).filter_by(id=user_id).first() + user = g.db.query(User).options(lazyload('*')).filter_by(id=user_id).first() user.verified = None g.db.add(user) g.db.commit() @@ -812,7 +812,7 @@ def unverify(user_id, v): @validate_formkey def admin_title_change(user_id, v): - user = g.db.query(User).filter_by(id=user_id).first() + user = g.db.query(User).options(lazyload('*')).filter_by(id=user_id).first() if user.admin_level != 0: abort(403) @@ -821,7 +821,7 @@ def admin_title_change(user_id, v): user.customtitleplain=new_name new_name = sanitize(new_name) - user=g.db.query(User).with_for_update().options(lazyload('*')).filter_by(id=user.id).first() + user=g.db.query(User).with_for_update().options(lazyload('*')).options(lazyload('*')).filter_by(id=user.id).first() user.customtitle=new_name user.flairchanged = bool(request.form.get("locked")) g.db.add(user) @@ -845,7 +845,7 @@ def admin_title_change(user_id, v): @validate_formkey def ban_user(user_id, v): - user = g.db.query(User).filter_by(id=user_id).first() + user = g.db.query(User).options(lazyload('*')).filter_by(id=user_id).first() if user.admin_level >= v.admin_level: abort(403) @@ -913,7 +913,7 @@ def ban_user(user_id, v): @validate_formkey def unban_user(user_id, v): - user = g.db.query(User).filter_by(id=user_id).first() + user = g.db.query(User).options(lazyload('*')).filter_by(id=user_id).first() if not user: abort(400) @@ -947,7 +947,7 @@ def unban_user(user_id, v): @validate_formkey def ban_post(post_id, v): - post = g.db.query(Submission).filter_by(id=post_id).first() + post = g.db.query(Submission).options(lazyload('*')).filter_by(id=post_id).first() if not post: abort(400) @@ -988,7 +988,7 @@ def ban_post(post_id, v): @validate_formkey def unban_post(post_id, v): - post = g.db.query(Submission).filter_by(id=post_id).first() + post = g.db.query(Submission).options(lazyload('*')).filter_by(id=post_id).first() if not post: abort(400) @@ -1018,7 +1018,7 @@ def unban_post(post_id, v): @validate_formkey def api_distinguish_post(post_id, v): - post = g.db.query(Submission).filter_by(id=post_id).first() + post = g.db.query(Submission).options(lazyload('*')).filter_by(id=post_id).first() if not post: abort(404) @@ -1042,7 +1042,7 @@ def api_distinguish_post(post_id, v): @admin_level_required(3) def api_sticky_post(post_id, v): - post = g.db.query(Submission).filter_by(id=post_id).first() + post = g.db.query(Submission).options(lazyload('*')).filter_by(id=post_id).first() if post: post.stickied = not (post.stickied) g.db.add(post) @@ -1064,7 +1064,7 @@ def api_sticky_post(post_id, v): @auth_required def api_pin_post(post_id, v): - post = g.db.query(Submission).filter_by(id=post_id).first() + post = g.db.query(Submission).options(lazyload('*')).filter_by(id=post_id).first() if post: post.is_pinned = not (post.is_pinned) g.db.add(post) @@ -1077,7 +1077,7 @@ def api_pin_post(post_id, v): @admin_level_required(1) def api_ban_comment(c_id, v): - comment = g.db.query(Comment).filter_by(id=c_id).first() + comment = g.db.query(Comment).options(lazyload('*')).filter_by(id=c_id).first() if not comment: abort(404) @@ -1100,7 +1100,7 @@ def api_ban_comment(c_id, v): @admin_level_required(1) def api_unban_comment(c_id, v): - comment = g.db.query(Comment).filter_by(id=c_id).first() + comment = g.db.query(Comment).options(lazyload('*')).filter_by(id=c_id).first() if not comment: abort(404) g.db.add(comment) @@ -1172,7 +1172,7 @@ def admin_toggle_ban_domain(v): reason=request.form.get("reason", "").strip() - d = g.db.query(BannedDomain).filter_by(domain=domain).first() + d = g.db.query(BannedDomain).options(lazyload('*')).filter_by(domain=domain).first() if d: g.db.delete(d) else: d = BannedDomain(domain=domain, reason=reason) @@ -1190,14 +1190,14 @@ def admin_nuke_user(v): user=get_user(request.form.get("user")) - for post in g.db.query(Submission).filter_by(author_id=user.id).all(): + for post in g.db.query(Submission).options(lazyload('*')).filter_by(author_id=user.id).all(): if post.is_banned: continue post.is_banned=True g.db.add(post) - for comment in g.db.query(Comment).filter_by(author_id=user.id).all(): + for comment in g.db.query(Comment).options(lazyload('*')).filter_by(author_id=user.id).all(): if comment.is_banned: continue @@ -1222,14 +1222,14 @@ def admin_nunuke_user(v): user=get_user(request.form.get("user")) - for post in g.db.query(Submission).filter_by(author_id=user.id).all(): + for post in g.db.query(Submission).options(lazyload('*')).filter_by(author_id=user.id).all(): if not post.is_banned: continue post.is_banned=False g.db.add(post) - for comment in g.db.query(Comment).filter_by(author_id=user.id).all(): + for comment in g.db.query(Comment).options(lazyload('*')).filter_by(author_id=user.id).all(): if not comment.is_banned: continue @@ -1273,11 +1273,11 @@ def chart(v): daily_times = [time.strftime("%d", time.gmtime(day_cutoffs[i + 1])) for i in range(len(day_cutoffs) - 1)][2:][::-1] - daily_signups = [g.db.query(User).filter(User.created_utc < day_cutoffs[i], User.created_utc > day_cutoffs[i + 1]).count() for i in range(len(day_cutoffs) - 1)][2:][::-1] + daily_signups = [g.db.query(User).options(lazyload('*')).filter(User.created_utc < day_cutoffs[i], User.created_utc > day_cutoffs[i + 1]).count() for i in range(len(day_cutoffs) - 1)][2:][::-1] - post_stats = [g.db.query(Submission).filter(Submission.created_utc < day_cutoffs[i], Submission.created_utc > day_cutoffs[i + 1], Submission.is_banned == False).count() for i in range(len(day_cutoffs) - 1)][2:][::-1] + post_stats = [g.db.query(Submission).options(lazyload('*')).filter(Submission.created_utc < day_cutoffs[i], Submission.created_utc > day_cutoffs[i + 1], Submission.is_banned == False).count() for i in range(len(day_cutoffs) - 1)][2:][::-1] - comment_stats = [g.db.query(Comment).filter(Comment.created_utc < day_cutoffs[i], Comment.created_utc > day_cutoffs[i + 1],Comment.is_banned == False, Comment.author_id != 1).count() for i in range(len(day_cutoffs) - 1)][2:][::-1] + comment_stats = [g.db.query(Comment).options(lazyload('*')).filter(Comment.created_utc < day_cutoffs[i], Comment.created_utc > day_cutoffs[i + 1],Comment.is_banned == False, Comment.author_id != 1).count() for i in range(len(day_cutoffs) - 1)][2:][::-1] # create multiple charts signup_chart = plt.subplot2grid((20, 4), (0, 0), rowspan=5, colspan=4) diff --git a/files/routes/awards.py b/files/routes/awards.py index cff39b868..9a6034194 100644 --- a/files/routes/awards.py +++ b/files/routes/awards.py @@ -209,7 +209,7 @@ def award_post(pid, v): if kind not in AWARDS: return {"error": "That award doesn't exist."}, 404 - post_award = g.db.query(AwardRelationship).filter( + post_award = g.db.query(AwardRelationship).options(lazyload('*')).filter( and_( AwardRelationship.kind == kind, AwardRelationship.user_id == v.id, @@ -221,7 +221,7 @@ def award_post(pid, v): if not post_award: return {"error": "You don't have that award."}, 404 - post = g.db.query(Submission).filter_by(id=pid).first() + post = g.db.query(Submission).options(lazyload('*')).filter_by(id=pid).first() if not post or post.is_banned or post.deleted_utc > 0: return {"error": "That post doesn't exist or has been deleted or removed."}, 404 @@ -229,7 +229,7 @@ def award_post(pid, v): if post.author_id == v.id: return {"error": "You can't award yourself."}, 403 - existing_award = g.db.query(AwardRelationship).filter( + existing_award = g.db.query(AwardRelationship).options(lazyload('*')).filter( and_( AwardRelationship.submission_id == post.id, AwardRelationship.user_id == v.id, @@ -275,7 +275,7 @@ def award_comment(cid, v): if kind not in AWARDS: return {"error": "That award doesn't exist."}, 404 - comment_award = g.db.query(AwardRelationship).filter( + comment_award = g.db.query(AwardRelationship).options(lazyload('*')).filter( and_( AwardRelationship.kind == kind, AwardRelationship.user_id == v.id, @@ -287,7 +287,7 @@ def award_comment(cid, v): if not comment_award: return {"error": "You don't have that award."}, 404 - c = g.db.query(Comment).filter_by(id=cid).first() + c = g.db.query(Comment).options(lazyload('*')).filter_by(id=cid).first() if not c or c.is_banned or c.deleted_utc > 0: return {"error": "That comment doesn't exist or has been deleted or removed."}, 404 @@ -295,7 +295,7 @@ def award_comment(cid, v): if c.author_id == v.id: return {"error": "You can't award yourself."}, 403 - existing_award = g.db.query(AwardRelationship).filter( + existing_award = g.db.query(AwardRelationship).options(lazyload('*')).filter( and_( AwardRelationship.comment_id == c.id, AwardRelationship.user_id == v.id, diff --git a/files/routes/comments.py b/files/routes/comments.py index 7deb489b9..c5fd329a2 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -82,7 +82,7 @@ def post_pid_comment_cid(cid, pid=None, anything=None, v=None): post.replies=[top_comment] if v: - votes = g.db.query(CommentVote).filter_by(user_id=v.id).subquery() + votes = g.db.query(CommentVote).options(lazyload('*')).filter_by(user_id=v.id).subquery() blocking = v.blocking.subquery() @@ -184,7 +184,7 @@ def api_comment(v): return {"error": reason}, 401 # check existing - existing = g.db.query(Comment).join(CommentAux).filter(Comment.author_id == v.id, + existing = g.db.query(Comment).join(CommentAux).options(lazyload('*')).filter(Comment.author_id == v.id, Comment.deleted_utc == 0, Comment.parent_comment_id == parent_comment_id, Comment.parent_submission == parent_submission, @@ -260,7 +260,7 @@ def api_comment(v): fragment='') check_url = urlunparse(check_url) - badlink = g.db.query(BadLink).filter( + badlink = g.db.query(BadLink).options(lazyload('*')).filter( literal(check_url).contains( BadLink.link)).first() @@ -527,7 +527,7 @@ def api_comment(v): # queue up notification for parent author notify_users = set() - for x in g.db.query(Subscription.user_id).filter_by(submission_id=c.parent_submission).all(): + for x in g.db.query(Subscription.user_id).options(lazyload('*')).filter_by(submission_id=c.parent_submission).all(): notify_users.add(x[0]) if parent.author.id != v.id: notify_users.add(parent.author.id) @@ -537,7 +537,7 @@ def api_comment(v): for mention in mentions: username = mention["href"].split("@")[1] - user = g.db.query(User).filter_by(username=username).first() + user = g.db.query(User).options(lazyload('*')).filter_by(username=username).first() if user: if v.any_block_exists(user): @@ -581,7 +581,7 @@ def api_comment(v): v.comment_count = v.comments.filter(Comment.parent_submission != None).filter_by(is_banned=False, deleted_utc=0).count() g.db.add(v) - parent_post.comment_count = g.db.query(Comment).filter_by(parent_submission=parent_post.id).count() + parent_post.comment_count = g.db.query(Comment).options(lazyload('*')).filter_by(parent_submission=parent_post.id).count() g.db.add(parent_post) g.db.commit() @@ -650,7 +650,7 @@ def edit_comment(cid, v): fragment='') check_url = urlunparse(check_url) - badlink = g.db.query(BadLink).filter( + badlink = g.db.query(BadLink).options(lazyload('*')).filter( literal(check_url).contains( BadLink.link)).first() @@ -795,7 +795,7 @@ def edit_comment(cid, v): for mention in mentions: username = mention["href"].split("@")[1] - user = g.db.query(User).filter_by(username=username).first() + user = g.db.query(User).options(lazyload('*')).filter_by(username=username).first() if user: if v.any_block_exists(user): @@ -819,7 +819,7 @@ def edit_comment(cid, v): @validate_formkey def delete_comment(cid, v): - c = g.db.query(Comment).filter_by(id=cid).first() + c = g.db.query(Comment).options(lazyload('*')).filter_by(id=cid).first() if not c: abort(404) @@ -842,7 +842,7 @@ def delete_comment(cid, v): @validate_formkey def undelete_comment(cid, v): - c = g.db.query(Comment).filter_by(id=cid).first() + c = g.db.query(Comment).options(lazyload('*')).filter_by(id=cid).first() if not c: abort(404) @@ -912,7 +912,7 @@ def unsave_comment(cid, v): comment=get_comment(cid) - save=g.db.query(SaveRelationship).filter_by(user_id=v.id, submission_id=comment.id, type=2).first() + save=g.db.query(SaveRelationship).options(lazyload('*')).filter_by(user_id=v.id, submission_id=comment.id, type=2).first() if save: g.db.delete(save) diff --git a/files/routes/discord.py b/files/routes/discord.py index 5246274e9..3a0f20071 100644 --- a/files/routes/discord.py +++ b/files/routes/discord.py @@ -97,7 +97,7 @@ def discord_redirect(v): url=f"https://discord.com/api/guilds/{SERVER_ID}/members/{v.discord_id}" requests.delete(url, headers=headers) - if g.db.query(User).filter(User.id!=v.id, User.discord_id==x["id"]).first(): + if g.db.query(User).options(lazyload('*')).filter(User.id!=v.id, User.discord_id==x["id"]).first(): return render_template("message.html", title="Discord account already linked.", error="That Discord account is already in use by another user.", v=v) v.discord_id=x["id"] diff --git a/files/routes/front.py b/files/routes/front.py index b52047316..495c3a264 100644 --- a/files/routes/front.py +++ b/files/routes/front.py @@ -25,11 +25,11 @@ def notifications(v): modmail = request.args.get('modmail', False) posts = request.args.get('posts', False) if modmail and v.admin_level == 6: - comments = g.db.query(Comment).filter(Comment.sentto==0).order_by(Comment.created_utc.desc()).offset(25*(page-1)).limit(26).all() + comments = g.db.query(Comment).options(lazyload('*')).filter(Comment.sentto==0).order_by(Comment.created_utc.desc()).offset(25*(page-1)).limit(26).all() next_exists = (len(comments) > 25) comments = comments[:25] elif messages: - comments = g.db.query(Comment).filter(or_(Comment.author_id==v.id, Comment.sentto==v.id), Comment.parent_submission == None).order_by(Comment.created_utc.desc(), not_(Comment.child_comments.any())).offset(25*(page-1)).limit(26).all() + comments = g.db.query(Comment).options(lazyload('*')).filter(or_(Comment.author_id==v.id, Comment.sentto==v.id), Comment.parent_submission == None).order_by(Comment.created_utc.desc(), not_(Comment.child_comments.any())).offset(25*(page-1)).limit(26).all() next_exists = (len(comments) > 25) comments = comments[:25] elif posts: @@ -209,8 +209,8 @@ def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, filter_words=' # g.db.add(vote) # try: g.db.flush() # except: g.db.rollback() - # post.upvotes = g.db.query(Vote).filter_by(submission_id=post.id, vote_type=1).count() - # post.downvotes = g.db.query(Vote).filter_by(submission_id=post.id, vote_type=-1).count() + # post.upvotes = g.db.query(Vote).options(lazyload('*')).filter_by(submission_id=post.id, vote_type=1).count() + # post.downvotes = g.db.query(Vote).options(lazyload('*')).filter_by(submission_id=post.id, vote_type=-1).count() # post.views = post.views + random.randint(7,10) # g.db.add(post) @@ -218,7 +218,7 @@ def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, filter_words=' posts = posts[:25] - if page == 1: posts = g.db.query(Submission).filter_by(stickied=True).all() + posts + if page == 1: posts = g.db.query(Submission).options(lazyload('*')).filter_by(stickied=True).all() + posts if ids_only: posts = [x.id for x in posts] @@ -271,7 +271,7 @@ def front_all(v): @cache.memoize(timeout=86400) def changeloglist(v=None, sort="new", page=1 ,t="all", **kwargs): - posts = g.db.query(Submission).options(lazyload('*')).filter_by(is_banned=False, private=False,).filter(Submission.deleted_utc == 0) + posts = g.db.query(Submission).options(lazyload('*')).options(lazyload('*')).filter_by(is_banned=False, private=False,).filter(Submission.deleted_utc == 0) if v and v.admin_level == 0: blocking = g.db.query( @@ -373,7 +373,7 @@ def changelog(v): @auth_desired def random_post(v): - x = g.db.query(Submission).filter(Submission.deleted_utc == 0, Submission.is_banned == False) + x = g.db.query(Submission).options(lazyload('*')).filter(Submission.deleted_utc == 0, Submission.is_banned == False) total = x.count() n = random.randint(1, total - 2) @@ -384,11 +384,11 @@ def random_post(v): def comment_idlist(page=1, v=None, nsfw=False, sort="new", t="all", **kwargs): posts = g.db.query(Submission).options(lazyload('*')) - cc_idlist = g.db.query(Submission.id).filter(Submission.club == True).subquery() + cc_idlist = g.db.query(Submission.id).options(lazyload('*')).filter(Submission.club == True).subquery() posts = posts.subquery() - comments = g.db.query(Comment).options(lazyload('*')).filter(Comment.parent_submission.notin_(cc_idlist)) + comments = g.db.query(Comment).options(lazyload('*')).options(lazyload('*')).filter(Comment.parent_submission.notin_(cc_idlist)) if v and v.admin_level <= 3: blocking = g.db.query( diff --git a/files/routes/login.py b/files/routes/login.py index 4e5a0d9e5..230e371c4 100644 --- a/files/routes/login.py +++ b/files/routes/login.py @@ -32,9 +32,9 @@ def check_for_alts(current_id): if past_id == current_id: continue - check1 = g.db.query(Alt).filter_by( + check1 = g.db.query(Alt).options(lazyload('*')).filter_by( user1=current_id, user2=past_id).first() - check2 = g.db.query(Alt).filter_by( + check2 = g.db.query(Alt).options(lazyload('*')).filter_by( user1=past_id, user2=current_id).first() if not check1 and not check2: @@ -45,7 +45,7 @@ def check_for_alts(current_id): except BaseException: pass - otheralts = g.db.query(Alt).filter(or_(Alt.user1 == past_id, Alt.user2 == past_id, Alt.user1 == current_id, Alt.user2 == current_id)).all() + otheralts = g.db.query(Alt).options(lazyload('*')).filter(or_(Alt.user1 == past_id, Alt.user2 == past_id, Alt.user1 == current_id, Alt.user2 == current_id)).all() for a in otheralts: new_alt = Alt(user1=a.user1, user2=past_id) g.db.add(new_alt) @@ -70,7 +70,7 @@ def login_post(): if not username: abort(400) if "@" in username: - account = g.db.query(User).filter( + account = g.db.query(User).options(lazyload('*')).filter( User.email.ilike(username)).first() else: account = get_user(username, graceful=True) @@ -174,7 +174,7 @@ def sign_up_get(v): # check for referral in link ref = request.args.get("ref", None) if ref: - ref_user = g.db.query(User).filter(User.username.ilike(ref)).first() + ref_user = g.db.query(User).options(lazyload('*')).filter(User.username.ilike(ref)).first() else: ref_user = None @@ -247,7 +247,7 @@ def sign_up_post(v): args = {"error": error} if request.form.get("referred_by"): - user = g.db.query(User).filter_by( + user = g.db.query(User).options(lazyload('*')).filter_by( id=request.form.get("referred_by")).first() if user: args["ref"] = user.username @@ -318,7 +318,7 @@ def sign_up_post(v): lazyload('*')).filter_by(id=ref_id).first() if ref_user: # check self-setting badges - badge_types = g.db.query(BadgeDef).filter(BadgeDef.qualification_expr.isnot(None)).all() + badge_types = g.db.query(BadgeDef).options(lazyload('*')).filter(BadgeDef.qualification_expr.isnot(None)).all() for badge in badge_types: if eval(badge.qualification_expr, {}, {'v': ref_user}): if not ref_user.has_badge(badge.id): @@ -330,7 +330,7 @@ def sign_up_post(v): g.db.add(ref_user) - id_1 = g.db.query(User).filter_by(id=6).count() + id_1 = g.db.query(User).options(lazyload('*')).filter_by(id=6).count() users_count = g.db.query(User).count() #paranoid if id_1 == 0 and users_count < 6: admin_level=6 else: admin_level=0 @@ -344,9 +344,9 @@ def sign_up_post(v): email=email, created_utc=int(time.time()), referred_by=ref_id or None, - ban_evade = int(any([x.is_banned and not x.unban_utc for x in g.db.query(User).filter(User.id.in_(tuple(session.get("history", [])))).all() if x])), - agendaposter = any([x.agendaposter for x in g.db.query(User).filter(User.id.in_(tuple(session.get("history", [])))).all() if x]), - club_banned=any([x.club_banned for x in g.db.query(User).filter(User.id.in_(tuple(session.get("history", [])))).all() if x]) + ban_evade = int(any([x.is_banned and not x.unban_utc for x in g.db.query(User).options(lazyload('*')).filter(User.id.in_(tuple(session.get("history", [])))).all() if x])), + agendaposter = any([x.agendaposter for x in g.db.query(User).options(lazyload('*')).filter(User.id.in_(tuple(session.get("history", [])))).all() if x]), + club_banned=any([x.club_banned for x in g.db.query(User).options(lazyload('*')).filter(User.id.in_(tuple(session.get("history", [])))).all() if x]) ) g.db.add(new_user) @@ -391,7 +391,7 @@ def post_forgot(): email=email.replace("_","\_") - user = g.db.query(User).filter( + user = g.db.query(User).options(lazyload('*')).filter( User.username.ilike(username), User.email.ilike(email)).first() @@ -400,7 +400,7 @@ def post_forgot(): email=email.split('+')[0] email=email.replace('.','') email=f"{email}@gmail.com" - user = g.db.query(User).filter( + user = g.db.query(User).options(lazyload('*')).filter( User.username.ilike(username), User.email.ilike(email)).first() @@ -435,7 +435,7 @@ def get_reset(): title="Password reset link expired", error="That password reset link has expired.") - user = g.db.query(User).filter_by(id=user_id).first() + user = g.db.query(User).options(lazyload('*')).filter_by(id=user_id).first() if not validate_hash(f"{user_id}+{timestamp}+forgot+{user.login_nonce}", token): abort(400) @@ -472,7 +472,7 @@ def post_reset(v): title="Password reset expired", error="That password reset form has expired.") - user = g.db.query(User).filter_by(id=user_id).first() + user = g.db.query(User).options(lazyload('*')).filter_by(id=user_id).first() if not validate_hash(f"{user_id}+{timestamp}+reset+{user.login_nonce}", token): abort(400) diff --git a/files/routes/oauth.py b/files/routes/oauth.py index 30d315731..76c37208b 100644 --- a/files/routes/oauth.py +++ b/files/routes/oauth.py @@ -10,7 +10,7 @@ from files.__main__ import app @auth_required def authorize_prompt(v): client_id = request.args.get("client_id") - application = g.db.query(OauthApp).filter_by(client_id=client_id).first() + application = g.db.query(OauthApp).options(lazyload('*')).filter_by(client_id=client_id).first() if not application: return {"oauth_error": "Invalid `client_id`"}, 401 return render_template("oauth.html", v=v, application=application) @@ -21,7 +21,7 @@ def authorize_prompt(v): def authorize(v): client_id = request.form.get("client_id") - application = g.db.query(OauthApp).filter_by(client_id=client_id).first() + application = g.db.query(OauthApp).options(lazyload('*')).filter_by(client_id=client_id).first() if not application: return {"oauth_error": "Invalid `client_id`"}, 401 access_token = secrets.token_urlsafe(128)[:128] new_auth = ClientAuth( @@ -63,9 +63,9 @@ def request_api_keys(v): def delete_oauth_app(v, aid): aid = int(aid) - app = g.db.query(OauthApp).filter_by(id=aid).first() + app = g.db.query(OauthApp).options(lazyload('*')).filter_by(id=aid).first() - for auth in g.db.query(ClientAuth).filter_by(oauth_client=app.id).all(): + for auth in g.db.query(ClientAuth).options(lazyload('*')).filter_by(oauth_client=app.id).all(): g.db.delete(auth) g.db.delete(app) @@ -81,7 +81,7 @@ def delete_oauth_app(v, aid): def edit_oauth_app(v, aid): aid = int(aid) - app = g.db.query(OauthApp).filter_by(id=aid).first() + app = g.db.query(OauthApp).options(lazyload('*')).filter_by(id=aid).first() app.redirect_uri = request.form.get('redirect_uri') app.app_name = request.form.get('name') @@ -99,7 +99,7 @@ def edit_oauth_app(v, aid): @validate_formkey def admin_app_approve(v, aid): - app = g.db.query(OauthApp).filter_by(id=aid).first() + app = g.db.query(OauthApp).options(lazyload('*')).filter_by(id=aid).first() user = app.author app.client_id = secrets.token_urlsafe(64)[:64] @@ -126,9 +126,9 @@ def admin_app_approve(v, aid): @validate_formkey def admin_app_revoke(v, aid): - app = g.db.query(OauthApp).filter_by(id=aid).first() + app = g.db.query(OauthApp).options(lazyload('*')).filter_by(id=aid).first() if app.id: - for auth in g.db.query(ClientAuth).filter_by(oauth_client=app.id).all(): g.db.delete(auth) + for auth in g.db.query(ClientAuth).options(lazyload('*')).filter_by(oauth_client=app.id).all(): g.db.delete(auth) g.db.flush() send_notification(NOTIFICATIONS_ACCOUNT, app.author, f"Your application `{app.app_name}` has been revoked.") @@ -145,9 +145,9 @@ def admin_app_revoke(v, aid): @validate_formkey def admin_app_reject(v, aid): - app = g.db.query(OauthApp).filter_by(id=aid).first() + app = g.db.query(OauthApp).options(lazyload('*')).filter_by(id=aid).first() - for auth in g.db.query(ClientAuth).filter_by(oauth_client=app.id).all(): g.db.delete(auth) + for auth in g.db.query(ClientAuth).options(lazyload('*')).filter_by(oauth_client=app.id).all(): g.db.delete(auth) g.db.flush() send_notification(NOTIFICATIONS_ACCOUNT, app.author, f"Your application `{app.app_name}` has been rejected.") @@ -229,7 +229,7 @@ def reroll_oauth_tokens(aid, v): aid = aid - a = g.db.query(OauthApp).filter_by(id=aid).first() + a = g.db.query(OauthApp).options(lazyload('*')).filter_by(id=aid).first() if a.author_id != v.id: abort(403) diff --git a/files/routes/posts.py b/files/routes/posts.py index bab6c0318..94080eea2 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -103,15 +103,15 @@ def post_id(pid, anything=None, v=None): if post.club and not (v and v.paid_dues): abort(403) if v: - votes = g.db.query(CommentVote).filter_by(user_id=v.id).subquery() + votes = g.db.query(CommentVote).options(lazyload('*')).filter_by(user_id=v.id).subquery() blocking = v.blocking.subquery() blocked = v.blocked.subquery() if not (v and v.shadowbanned) and not (v and v.admin_level == 6): - shadowbanned = g.db.query(User.id).filter(User.shadowbanned == True).subquery() - comments = g.db.query(Comment).filter(Comment.author_id.notin_(shadowbanned)) + shadowbanned = g.db.query(User.id).options(lazyload('*')).filter(User.shadowbanned == True).subquery() + comments = g.db.query(Comment).options(lazyload('*')).filter(Comment.author_id.notin_(shadowbanned)) comments = g.db.query( Comment, @@ -121,7 +121,7 @@ def post_id(pid, anything=None, v=None): ) if not (v and v.shadowbanned) and not (v and v.admin_level == 6): - shadowbanned = g.db.query(User.id).filter(User.shadowbanned == True).subquery() + shadowbanned = g.db.query(User.id).options(lazyload('*')).filter(User.shadowbanned == True).subquery() comments = comments.filter(Comment.author_id.notin_(shadowbanned)) if v.admin_level >=4: @@ -170,9 +170,9 @@ def post_id(pid, anything=None, v=None): post.preloaded_comments = output else: - shadowbanned = g.db.query(User.id).filter(User.shadowbanned == True).subquery() + shadowbanned = g.db.query(User.id).options(lazyload('*')).filter(User.shadowbanned == True).subquery() - comments = g.db.query(Comment).filter(Comment.parent_submission == post.id, Comment.author_id.notin_(shadowbanned)) + comments = g.db.query(Comment).options(lazyload('*')).filter(Comment.parent_submission == post.id, Comment.author_id.notin_(shadowbanned)) if sort == "top": comments = sorted(comments.all(), key=lambda x: x.score, reverse=True) @@ -202,8 +202,8 @@ def post_id(pid, anything=None, v=None): # g.db.add(vote) # try: g.db.flush() # except: g.db.rollback() - # comment.upvotes = g.db.query(CommentVote).filter_by(comment_id=comment.id, vote_type=1).count() - # comment.downvotes = g.db.query(CommentVote).filter_by(comment_id=comment.id, vote_type=-1).count() + # comment.upvotes = g.db.query(CommentVote).options(lazyload('*')).filter_by(comment_id=comment.id, vote_type=1).count() + # comment.downvotes = g.db.query(CommentVote).options(lazyload('*')).filter_by(comment_id=comment.id, vote_type=-1).count() # g.db.add(comment) post.preloaded_comments = comments @@ -285,7 +285,7 @@ def edit_post(pid, v): fragment='') check_url = urlunparse(check_url) - badlink = g.db.query(BadLink).filter( + badlink = g.db.query(BadLink).options(lazyload('*')).filter( literal(check_url).contains( BadLink.link)).first() if badlink: @@ -379,7 +379,7 @@ def edit_post(pid, v): soup = BeautifulSoup(body_html, features="html.parser") for mention in soup.find_all("a", href=re.compile("^/@(\w+)")): username = mention["href"].split("@")[1] - user = g.db.query(User).filter_by(username=username).first() + user = g.db.query(User).options(lazyload('*')).filter_by(username=username).first() if user and not v.any_block_exists(user) and user.id != v.id: notify_users.add(user) for x in notify_users: send_notification(NOTIFICATIONS_ACCOUNT, x, f"@{v.username} has mentioned you: https://{site}{p.permalink}") @@ -622,7 +622,7 @@ def submit_post(v): url = url.replace(".png", "_d.png").replace(".jpg", "_d.jpg").replace(".jpeg", "_d.jpeg") if "_d." in url: url += "?maxwidth=9999" - repost = g.db.query(Submission).join(Submission.submission_aux).filter( + repost = g.db.query(Submission).join(Submission.submission_aux).options(lazyload('*')).filter( SubmissionAux.url.ilike(url), Submission.deleted_utc == 0, Submission.is_banned == False @@ -665,7 +665,7 @@ def submit_post(v): body = request.form.get("body", "") # check for duplicate - dup = g.db.query(Submission).join(Submission.submission_aux).filter( + dup = g.db.query(Submission).join(Submission.submission_aux).options(lazyload('*')).filter( Submission.author_id == v.id, Submission.deleted_utc == 0, @@ -843,7 +843,7 @@ def submit_post(v): fragment='') check_url = urlunparse(check_url) - badlink = g.db.query(BadLink).filter( + badlink = g.db.query(BadLink).options(lazyload('*')).filter( literal(check_url).contains( BadLink.link)).first() if badlink: @@ -966,7 +966,7 @@ def submit_post(v): soup = BeautifulSoup(body_html, features="html.parser") for mention in soup.find_all("a", href=re.compile("^/@(\w+)")): username = mention["href"].split("@")[1] - user = g.db.query(User).filter_by(username=username).first() + user = g.db.query(User).options(lazyload('*')).filter_by(username=username).first() if user and not v.any_block_exists(user) and user.id != v.id: notify_users.add(user) for x in notify_users: send_notification(NOTIFICATIONS_ACCOUNT, x, f"@{v.username} has mentioned you: https://{site}{new_post.permalink}") @@ -1150,7 +1150,7 @@ def undelete_post_pid(pid, v): @validate_formkey def toggle_comment_nsfw(cid, v): - comment = g.db.query(Comment).filter_by(id=cid).first() + comment = g.db.query(Comment).options(lazyload('*')).filter_by(id=cid).first() if not comment.author_id == v.id and not v.admin_level >= 3: abort(403) comment.over_18 = not comment.over_18 g.db.add(comment) @@ -1209,7 +1209,7 @@ def unsave_post(pid, v): post=get_post(pid) - save=g.db.query(SaveRelationship).filter_by(user_id=v.id, submission_id=post.id, type=1).first() + save=g.db.query(SaveRelationship).options(lazyload('*')).filter_by(user_id=v.id, submission_id=post.id, type=1).first() if save: g.db.delete(save) diff --git a/files/routes/reporting.py b/files/routes/reporting.py index 60ecc9e64..8b075216f 100644 --- a/files/routes/reporting.py +++ b/files/routes/reporting.py @@ -12,7 +12,7 @@ def api_flag_post(pid, v): post = get_post(pid) if v: - existing = g.db.query(Flag).filter_by(user_id=v.id, post_id=post.id).first() + existing = g.db.query(Flag).options(lazyload('*')).filter_by(user_id=v.id, post_id=post.id).first() if existing: return "", 409 @@ -43,7 +43,7 @@ def api_flag_comment(cid, v): comment = get_comment(cid) if v: - existing = g.db.query(CommentFlag).filter_by( + existing = g.db.query(CommentFlag).options(lazyload('*')).filter_by( user_id=v.id, comment_id=comment.id).first() if existing: return "", 409 @@ -75,9 +75,9 @@ def remove_report(report_fn, v): return {"error": "go outside"}, 403 if report_fn.startswith('c'): - report = g.db.query(CommentFlag).filter_by(id=int(report_fn.lstrip('c'))).first() + report = g.db.query(CommentFlag).options(lazyload('*')).filter_by(id=int(report_fn.lstrip('c'))).first() elif report_fn.startswith('p'): - report = g.db.query(Flag).filter_by(id=int(report_fn.lstrip('p'))).first() + report = g.db.query(Flag).options(lazyload('*')).filter_by(id=int(report_fn.lstrip('p'))).first() else: return {"error": "Invalid report ID"}, 400 diff --git a/files/routes/search.py b/files/routes/search.py index 7a826eb4e..483489d2f 100644 --- a/files/routes/search.py +++ b/files/routes/search.py @@ -150,7 +150,7 @@ def searchlisting(criteria, v=None, page=1, t="None", sort="top", b=None): def searchcommentlisting(criteria, v=None, page=1, t="None", sort="top"): - comments = g.db.query(Comment).options(lazyload('*')).filter(Comment.parent_submission != None).join(Comment.comment_aux) + comments = g.db.query(Comment).options(lazyload('*')).options(lazyload('*')).filter(Comment.parent_submission != None).join(Comment.comment_aux) if 'q' in criteria: words=criteria['q'].split() @@ -278,7 +278,7 @@ def searchusers(v): term=term.replace('\\','') term=term.replace('_','\_') - users=g.db.query(User).filter(User.username.ilike(f'%{term}%')) + users=g.db.query(User).options(lazyload('*')).filter(User.username.ilike(f'%{term}%')) users=users.order_by(User.username.ilike(term).desc(), User.stored_subscriber_count.desc()) diff --git a/files/routes/settings.py b/files/routes/settings.py index 3bf55d0b2..dc78ccb2a 100644 --- a/files/routes/settings.py +++ b/files/routes/settings.py @@ -417,7 +417,7 @@ def settings_security_post(v): return redirect("/settings/security?error=That email is already yours!") # check to see if email is in use - existing = g.db.query(User).filter(User.id != v.id, + existing = g.db.query(User).options(lazyload('*')).filter(User.id != v.id, func.lower(User.email) == new_email.lower()).first() if existing: return redirect("/settings/security?error=" + @@ -652,7 +652,7 @@ def settings_block_user(v): - existing = g.db.query(Notification).filter_by(blocksender=v.id, user_id=user.id).first() + existing = g.db.query(Notification).options(lazyload('*')).filter_by(blocksender=v.id, user_id=user.id).first() if not existing: send_block_notif(v.id, user.id, f"@{v.username} has blocked you!") if v.admin_level == 1: return {"message": f"@{user.username} banned!"} @@ -679,7 +679,7 @@ def settings_unblock_user(v): - existing = g.db.query(Notification).filter_by(unblocksender=v.id, user_id=user.id).first() + existing = g.db.query(Notification).options(lazyload('*')).filter_by(unblocksender=v.id, user_id=user.id).first() if not existing: send_unblock_notif(v.id, user.id, f"@{v.username} has unblocked you!") if v.admin_level == 1: return {"message": f"@{user.username} unbanned!"} @@ -759,7 +759,7 @@ def settings_name_change(v): v=v, error=f"Username `{new_name}` is already in use.") - v=g.db.query(User).with_for_update().options(lazyload('*')).filter_by(id=v.id).first() + v=g.db.query(User).with_for_update().options(lazyload('*')).options(lazyload('*')).filter_by(id=v.id).first() v.username=new_name v.name_changed_utc=int(time.time()) @@ -778,7 +778,7 @@ def settings_name_change(v): def settings_song_change(v): song=request.form.get("song").strip() - if song == "" and v.song and path.isfile(f"/songs/{v.song}.mp3") and g.db.query(User).filter_by(song=v.song).count() == 1: + if song == "" and v.song and path.isfile(f"/songs/{v.song}.mp3") and g.db.query(User).options(lazyload('*')).filter_by(song=v.song).count() == 1: os.remove(f"/songs/{v.song}.mp3") v.song=None g.db.add(v) @@ -821,7 +821,7 @@ def settings_song_change(v): error=f"Duration of the video must not exceed 10 minutes.") - if v.song and path.isfile(f"/songs/{v.song}.mp3") and g.db.query(User).filter_by(song=v.song).count() == 1: + if v.song and path.isfile(f"/songs/{v.song}.mp3") and g.db.query(User).options(lazyload('*')).filter_by(song=v.song).count() == 1: os.remove(f"/songs/{v.song}.mp3") ydl_opts = { diff --git a/files/routes/static.py b/files/routes/static.py index 8c3226035..f90a548db 100644 --- a/files/routes/static.py +++ b/files/routes/static.py @@ -33,28 +33,28 @@ def participation_stats(v): day = now - 86400 data = {"valid_users": g.db.query(User).count(), - "private_users": g.db.query(User).filter_by(is_private=True).count(), - "banned_users": g.db.query(User).filter(User.is_banned > 0).count(), - "verified_email_users": g.db.query(User).filter_by(is_activated=True).count(), + "private_users": g.db.query(User).options(lazyload('*')).filter_by(is_private=True).count(), + "banned_users": g.db.query(User).options(lazyload('*')).filter(User.is_banned > 0).count(), + "verified_email_users": g.db.query(User).options(lazyload('*')).filter_by(is_activated=True).count(), "total_coins": g.db.query(func.sum(User.coins)).scalar(), - "signups_last_24h": g.db.query(User).filter(User.created_utc > day).count(), + "signups_last_24h": g.db.query(User).options(lazyload('*')).filter(User.created_utc > day).count(), "total_posts": g.db.query(Submission).count(), "posting_users": g.db.query(Submission.author_id).distinct().count(), - "listed_posts": g.db.query(Submission).filter_by(is_banned=False).filter(Submission.deleted_utc == 0).count(), - "removed_posts": g.db.query(Submission).filter_by(is_banned=True).count(), - "deleted_posts": g.db.query(Submission).filter(Submission.deleted_utc > 0).count(), - "posts_last_24h": g.db.query(Submission).filter(Submission.created_utc > day).count(), + "listed_posts": g.db.query(Submission).options(lazyload('*')).filter_by(is_banned=False).filter(Submission.deleted_utc == 0).count(), + "removed_posts": g.db.query(Submission).options(lazyload('*')).filter_by(is_banned=True).count(), + "deleted_posts": g.db.query(Submission).options(lazyload('*')).filter(Submission.deleted_utc > 0).count(), + "posts_last_24h": g.db.query(Submission).options(lazyload('*')).filter(Submission.created_utc > day).count(), "total_comments": g.db.query(Comment).count(), "commenting_users": g.db.query(Comment.author_id).distinct().count(), - "removed_comments": g.db.query(Comment).filter_by(is_banned=True).count(), - "deleted_comments": g.db.query(Comment).filter(Comment.deleted_utc>0).count(), - "comments_last_24h": g.db.query(Comment).filter(Comment.created_utc > day).count(), + "removed_comments": g.db.query(Comment).options(lazyload('*')).filter_by(is_banned=True).count(), + "deleted_comments": g.db.query(Comment).options(lazyload('*')).filter(Comment.deleted_utc>0).count(), + "comments_last_24h": g.db.query(Comment).options(lazyload('*')).filter(Comment.created_utc > day).count(), "post_votes": g.db.query(Vote).count(), "post_voting_users": g.db.query(Vote.user_id).distinct().count(), "comment_votes": g.db.query(CommentVote).count(), "comment_voting_users": g.db.query(CommentVote.user_id).distinct().count(), "total_awards": g.db.query(AwardRelationship).count(), - "awards_given": g.db.query(AwardRelationship).filter(or_(AwardRelationship.submission_id != None, AwardRelationship.comment_id != None)).count() + "awards_given": g.db.query(AwardRelationship).options(lazyload('*')).filter(or_(AwardRelationship.submission_id != None, AwardRelationship.comment_id != None)).count() } @@ -89,7 +89,7 @@ def patrons(v): @app.get("/badmins") @auth_desired def admins(v): - admins = g.db.query(User).filter_by(admin_level=6).order_by(User.coins.desc()).all() + admins = g.db.query(User).options(lazyload('*')).filter_by(admin_level=6).order_by(User.coins.desc()).all() return render_template("admins.html", v=v, admins=admins) @app.get("/log") @@ -100,7 +100,7 @@ def log(v): page=int(request.args.get("page",1)) if v and v.admin_level == 6: actions = g.db.query(ModAction).order_by(ModAction.id.desc()).offset(25 * (page - 1)).limit(26).all() - else: actions=g.db.query(ModAction).filter(ModAction.kind!="shadowban", ModAction.kind!="unshadowban", ModAction.kind!="club", ModAction.kind!="unclub").order_by(ModAction.id.desc()).offset(25*(page-1)).limit(26).all() + else: actions=g.db.query(ModAction).options(lazyload('*')).filter(ModAction.kind!="shadowban", ModAction.kind!="unshadowban", ModAction.kind!="club", ModAction.kind!="unclub").order_by(ModAction.id.desc()).offset(25*(page-1)).limit(26).all() next_exists=len(actions)==26 actions=actions[:25] @@ -116,7 +116,7 @@ def log_item(id, v): try: id = int(id, 36) except: abort(404) - action=g.db.query(ModAction).filter_by(id=id).first() + action=g.db.query(ModAction).options(lazyload('*')).filter_by(id=id).first() if not action: abort(404) @@ -228,7 +228,7 @@ def blocks(v): def banned(v): - users = [x for x in g.db.query(User).filter(User.is_banned > 0, User.unban_utc == 0).all()] + users = [x for x in g.db.query(User).options(lazyload('*')).filter(User.is_banned > 0, User.unban_utc == 0).all()] return render_template("banned.html", v=v, users=users) @app.get("/formatting") diff --git a/files/routes/users.py b/files/routes/users.py index cdd514405..c7314a824 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -77,16 +77,16 @@ def steal(v): @app.get("/rentoids") @auth_desired def rentoids(v): - users = g.db.query(User).filter(User.rent_utc > 0).all() + users = g.db.query(User).options(lazyload('*')).filter(User.rent_utc > 0).all() return render_template("rentoids.html", v=v, users=users) @app.get("/thiefs") @auth_desired def thiefs(v): - successful = g.db.query(User).filter(User.steal_utc > 0).all() - failed = g.db.query(User).filter(User.fail_utc > 0).all() - failed2 = g.db.query(User).filter(User.fail2_utc > 0).all() + successful = g.db.query(User).options(lazyload('*')).filter(User.steal_utc > 0).all() + failed = g.db.query(User).options(lazyload('*')).filter(User.fail_utc > 0).all() + failed2 = g.db.query(User).options(lazyload('*')).filter(User.fail2_utc > 0).all() return render_template("thiefs.html", v=v, successful=successful, failed=failed, failed2=failed2) @@ -175,7 +175,7 @@ def get_profilecss(username): def songs(id): try: id = int(id) except: return "", 400 - user = g.db.query(User).filter_by(id=id).first() + user = g.db.query(User).options(lazyload('*')).filter_by(id=id).first() return redirect(f"/song/{user.song}.mp3") @app.get("/song/") @@ -196,7 +196,7 @@ def subscribe(v, post_id): @app.post("/unsubscribe/") @auth_required def unsubscribe(v, post_id): - sub=g.db.query(Subscription).filter_by(user_id=v.id, submission_id=post_id).first() + sub=g.db.query(Subscription).options(lazyload('*')).filter_by(user_id=v.id, submission_id=post_id).first() g.db.delete(sub) g.db.commit() return {"message": "Post unsubscribed!"} @@ -214,7 +214,7 @@ def message2(v, username): message = message.replace("\n", "\n\n").replace("\n\n\n\n\n\n", "\n\n").replace("\n\n\n\n", "\n\n").replace("\n\n\n", "\n\n") # check existing - existing = g.db.query(Comment).join(CommentAux).filter(Comment.author_id == v.id, + existing = g.db.query(Comment).join(CommentAux).options(lazyload('*')).filter(Comment.author_id == v.id, Comment.sentto == user.id, CommentAux.body == message, ).options(contains_eager(Comment.comment_aux)).first() @@ -254,7 +254,7 @@ def messagereply(v): message = message.replace("\n", "\n\n").replace("\n\n\n\n\n\n", "\n\n").replace("\n\n\n\n", "\n\n").replace("\n\n\n", "\n\n") # check existing - existing = g.db.query(Comment).join(CommentAux).filter(Comment.author_id == v.id, + existing = g.db.query(Comment).join(CommentAux).options(lazyload('*')).filter(Comment.author_id == v.id, Comment.sentto == user.id, CommentAux.body == message, ).options(contains_eager(Comment.comment_aux)).first() @@ -382,7 +382,7 @@ def u_username(username, v=None): # viewers if v and u.id != v.id: - view = g.db.query(ViewerRelationship).filter( + view = g.db.query(ViewerRelationship).options(lazyload('*')).filter( and_( ViewerRelationship.viewer_id == v.id, ViewerRelationship.user_id == u.id @@ -434,7 +434,7 @@ def u_username(username, v=None): # If page 1, check for sticky if page == 1: sticky = [] - sticky = g.db.query(Submission).filter_by(is_pinned=True, author_id=u.id).all() + sticky = g.db.query(Submission).options(lazyload('*')).filter_by(is_pinned=True, author_id=u.id).all() if sticky: for p in sticky: ids = [p.id] + ids @@ -595,15 +595,15 @@ def follow_user(username, v): if target.id==v.id: return {"error": "You can't follow yourself!"}, 400 # check for existing follow - if g.db.query(Follow).filter_by(user_id=v.id, target_id=target.id).first(): return {"message": "User followed!"} + if g.db.query(Follow).options(lazyload('*')).filter_by(user_id=v.id, target_id=target.id).first(): return {"message": "User followed!"} new_follow = Follow(user_id=v.id, target_id=target.id) g.db.add(new_follow) - target.stored_subscriber_count = g.db.query(Follow).filter_by(target_id=target.id).count() + target.stored_subscriber_count = g.db.query(Follow).options(lazyload('*')).filter_by(target_id=target.id).count() g.db.add(target) - existing = g.db.query(Notification).filter_by(followsender=v.id, user_id=target.id).first() + existing = g.db.query(Notification).options(lazyload('*')).filter_by(followsender=v.id, user_id=target.id).first() if not existing: send_follow_notif(v.id, target.id, f"@{v.username} has followed you!") g.db.commit() @@ -617,15 +617,15 @@ def unfollow_user(username, v): target = get_user(username) # check for existing follow - follow = g.db.query(Follow).filter_by(user_id=v.id, target_id=target.id).first() + follow = g.db.query(Follow).options(lazyload('*')).filter_by(user_id=v.id, target_id=target.id).first() if not follow: return {"message": "User unfollowed!"} g.db.delete(follow) - target.stored_subscriber_count = g.db.query(Follow).filter_by(target_id=target.id).count() + target.stored_subscriber_count = g.db.query(Follow).options(lazyload('*')).filter_by(target_id=target.id).count() g.db.add(target) - existing = g.db.query(Notification).filter_by(unfollowsender=v.id, user_id=target.id).first() + existing = g.db.query(Notification).options(lazyload('*')).filter_by(unfollowsender=v.id, user_id=target.id).first() if not existing: send_unfollow_notif(v.id, target.id, f"@{v.username} has unfollowed you!") g.db.commit() diff --git a/files/routes/votes.py b/files/routes/votes.py index 3be315e7a..fd11176a2 100644 --- a/files/routes/votes.py +++ b/files/routes/votes.py @@ -69,7 +69,7 @@ def api_vote_post(post_id, new, v): post = get_post(post_id) # check for existing vote - existing = g.db.query(Vote).filter_by(user_id=v.id, submission_id=post.id).first() + existing = g.db.query(Vote).options(lazyload('*')).filter_by(user_id=v.id, submission_id=post.id).first() if existing and existing.vote_type == new: return "", 204 @@ -96,8 +96,8 @@ def api_vote_post(post_id, new, v): ) g.db.add(vote) - post.upvotes = g.db.query(Vote).filter_by(submission_id=post.id, vote_type=1).count() - post.downvotes = g.db.query(Vote).filter_by(submission_id=post.id, vote_type=-1).count() + post.upvotes = g.db.query(Vote).options(lazyload('*')).filter_by(submission_id=post.id, vote_type=1).count() + post.downvotes = g.db.query(Vote).options(lazyload('*')).filter_by(submission_id=post.id, vote_type=-1).count() g.db.add(post) g.db.commit() return "", 204 @@ -121,7 +121,7 @@ def api_vote_comment(comment_id, new, v): comment = get_comment(comment_id) # check for existing vote - existing = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id).first() + existing = g.db.query(CommentVote).options(lazyload('*')).filter_by(user_id=v.id, comment_id=comment.id).first() if existing and existing.vote_type == new: return "", 204 @@ -149,8 +149,8 @@ def api_vote_comment(comment_id, new, v): g.db.add(vote) - comment.upvotes = g.db.query(CommentVote).filter_by(comment_id=comment.id, vote_type=1).count() - comment.downvotes = g.db.query(CommentVote).filter_by(comment_id=comment.id, vote_type=-1).count() + comment.upvotes = g.db.query(CommentVote).options(lazyload('*')).filter_by(comment_id=comment.id, vote_type=1).count() + comment.downvotes = g.db.query(CommentVote).options(lazyload('*')).filter_by(comment_id=comment.id, vote_type=-1).count() g.db.add(comment) g.db.commit() return "", 204 \ No newline at end of file