diff --git a/files/classes/clients.py b/files/classes/clients.py index 9256effc8..552d729d0 100644 --- a/files/classes/clients.py +++ b/files/classes/clients.py @@ -39,7 +39,7 @@ class OauthApp(Base): @lazy def idlist(self, page=1, **kwargs): - posts = g.db.query(Submission.id).options(lazyload('*')).options(lazyload('*')).filter_by(app_id=self.id) + posts = g.db.query(Submission.id).options(lazyload('*')).filter_by(app_id=self.id) posts=posts.order_by(Submission.created_utc.desc()) @@ -50,7 +50,7 @@ class OauthApp(Base): @lazy def comments_idlist(self, page=1, **kwargs): - posts = g.db.query(Comment.id).options(lazyload('*')).options(lazyload('*')).filter_by(app_id=self.id) + posts = g.db.query(Comment.id).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 db0a1dacb..3fffddfa8 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -241,7 +241,7 @@ class User(Base): if self.shadowbanned and not (v and (v.admin_level >= 3 or v.id == self.id)): return [] - submissions = g.db.query(Submission).options(lazyload('*')).options(lazyload('*')).filter_by(author_id=self.id, is_pinned=False) + submissions = g.db.query(Submission).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) @@ -547,7 +547,7 @@ class User(Base): @lazy def saved_idlist(self, page=1): - posts = g.db.query(Submission.id).options(lazyload('*')).options(lazyload('*')).filter_by(is_banned=False, deleted_utc=0) + posts = g.db.query(Submission.id).options(lazyload('*')).filter_by(is_banned=False, deleted_utc=0) saved = [x[0] for x in g.db.query(SaveRelationship.submission_id).options(lazyload('*')).filter(SaveRelationship.user_id == self.id).all()] posts = posts.filter(Submission.id.in_(saved)) @@ -572,7 +572,7 @@ class User(Base): @lazy def saved_comment_idlist(self, page=1): - comments = g.db.query(Comment.id).options(lazyload('*')).options(lazyload('*')).filter_by(is_banned=False, deleted_utc=0) + comments = g.db.query(Comment.id).options(lazyload('*')).filter_by(is_banned=False, deleted_utc=0) saved = [x[0] for x in g.db.query(SaveRelationship.submission_id).options(lazyload('*')).filter(SaveRelationship.user_id == self.id).all()] comments = comments.filter(Comment.id.in_(saved)) diff --git a/files/routes/admin.py b/files/routes/admin.py index 7230b8d0f..563823707 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -38,14 +38,14 @@ def revert_actions(v, username): user = get_user(username) if not user: abort(404) - 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() + 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() for item in items: item.is_banned = False item.removed_by = None g.db.add(item) - users = g.db.query(User).options(lazyload('*')).options(lazyload('*')).filter_by(is_banned=user.id).all() + users = g.db.query(User).options(lazyload('*')).filter_by(is_banned=user.id).all() for user in users: user.is_banned = 0 user.unban_utc = 0 @@ -620,7 +620,7 @@ def admin_removed(v): page = int(request.values.get("page", 1)) - ids = g.db.query(Submission.id).options(lazyload('*')).options(lazyload('*')).filter_by(is_banned=True).order_by( + ids = g.db.query(Submission.id).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] @@ -831,7 +831,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('*')).options(lazyload('*')).filter_by(id=user.id).first() + user=g.db.query(User).with_for_update().options(lazyload('*')).filter_by(id=user.id).first() user.customtitle=new_name user.flairchanged = bool(request.values.get("locked")) g.db.add(user) diff --git a/files/routes/front.py b/files/routes/front.py index 83e2db895..6ec6501a7 100644 --- a/files/routes/front.py +++ b/files/routes/front.py @@ -249,7 +249,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('*')).options(lazyload('*')).filter_by(is_banned=False, private=False,).filter(Submission.deleted_utc == 0) + posts = g.db.query(Submission).options(lazyload('*')).filter_by(is_banned=False, private=False,).filter(Submission.deleted_utc == 0) if v and v.admin_level == 0: blocking = [x[0] for x in g.db.query( @@ -366,7 +366,7 @@ def comment_idlist(page=1, v=None, nsfw=False, sort="new", t="all", **kwargs): posts = posts.subquery() - comments = g.db.query(Comment).options(lazyload('*')).options(lazyload('*')).filter(Comment.parent_submission.notin_(cc_idlist)) + comments = g.db.query(Comment).options(lazyload('*')).filter(Comment.parent_submission.notin_(cc_idlist)) if v and v.admin_level <= 3: blocking = [x[0] for x in g.db.query( diff --git a/files/routes/search.py b/files/routes/search.py index 725ea3b39..6b1fcffd8 100644 --- a/files/routes/search.py +++ b/files/routes/search.py @@ -31,7 +31,33 @@ def searchparse(text): return criteria -def searchlisting(criteria, v=None, page=1, t="None", sort="top", b=None): + + + +@app.get("/search/posts") +@auth_desired +def searchposts(v): + + + query = request.values.get("q", '').strip() + + page = max(1, int(request.values.get("page", 1))) + + sort = request.values.get("sort", "top").lower() + t = request.values.get('t', 'all').lower() + + criteria=searchparse(query) + + + + + + + + + + + posts = g.db.query(Submission).options( lazyload('*') @@ -146,12 +172,58 @@ def searchlisting(criteria, v=None, page=1, t="None", sort="top", b=None): secondrange = firstrange+26 posts = posts[firstrange:secondrange] - return total, [x.id for x in posts] + ids = [x.id for x in posts] -def searchcommentlisting(criteria, v=None, page=1, t="None", sort="top"): - comments = g.db.query(Comment).options(lazyload('*')).options(lazyload('*')).filter(Comment.parent_submission != None).join(Comment.comment_aux) + + next_exists = (len(ids) > 25) + ids = ids[:25] + + posts = get_posts(ids, v=v) + + if v and v.admin_level>3 and "domain" in criteria: + domain=criteria['domain'] + domain_obj=get_domain(domain) + else: + domain=None + domain_obj=None + + if request.headers.get("Authorization"): return {"data":[x.json for x in posts]} + else: return render_template("search.html", + v=v, + query=query, + total=total, + page=page, + listing=posts, + sort=sort, + t=t, + next_exists=next_exists, + domain=domain, + domain_obj=domain_obj + ) + +@app.get("/search/comments") +@auth_desired +def searchcomments(v): + + + query = request.values.get("q", '').strip() + + try: page = max(1, int(request.values.get("page", 1))) + except: page = 1 + + sort = request.values.get("sort", "top").lower() + t = request.values.get('t', 'all').lower() + + criteria=searchparse(query) + + + + + + + comments = g.db.query(Comment).filter(Comment.parent_submission != None).join(Comment.comment_aux) if 'q' in criteria: words=criteria['q'].split() @@ -197,64 +269,14 @@ def searchcommentlisting(criteria, v=None, page=1, t="None", sort="top"): firstrange = 25 * (page - 1) secondrange = firstrange+26 comments = comments[firstrange:secondrange] - return total, [x.id for x in comments] - -@app.get("/search/posts") -@auth_desired -def searchposts(v): + ids = [x.id for x in comments] - query = request.values.get("q", '').strip() - - page = max(1, int(request.values.get("page", 1))) - - sort = request.values.get("sort", "top").lower() - t = request.values.get('t', 'all').lower() - - criteria=searchparse(query) - total, ids = searchlisting(criteria, v=v, page=page, t=t, sort=sort) - - next_exists = (len(ids) > 25) - ids = ids[:25] - - posts = get_posts(ids, v=v) - - if v and v.admin_level>3 and "domain" in criteria: - domain=criteria['domain'] - domain_obj=get_domain(domain) - else: - domain=None - domain_obj=None - - if request.headers.get("Authorization"): return {"data":[x.json for x in posts]} - else: return render_template("search.html", - v=v, - query=query, - total=total, - page=page, - listing=posts, - sort=sort, - t=t, - next_exists=next_exists, - domain=domain, - domain_obj=domain_obj - ) - -@app.get("/search/comments") -@auth_desired -def searchcomments(v): - query = request.values.get("q", '').strip() - try: page = max(1, int(request.values.get("page", 1))) - except: page = 1 - sort = request.values.get("sort", "top").lower() - t = request.values.get('t', 'all').lower() - criteria=searchparse(query) - total, ids = searchcommentlisting(criteria, v=v, page=page, t=t, sort=sort) next_exists = (len(ids) > 25) ids = ids[:25] diff --git a/files/routes/settings.py b/files/routes/settings.py index b0d9ed216..df45b32d0 100644 --- a/files/routes/settings.py +++ b/files/routes/settings.py @@ -743,7 +743,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('*')).options(lazyload('*')).filter_by(id=v.id).first() + v=g.db.query(User).with_for_update().options(lazyload('*')).filter_by(id=v.id).first() v.username=new_name v.name_changed_utc=int(time.time())