From 3a6dfdf2debd90c4c05daf483d7a00a99a7fc16d Mon Sep 17 00:00:00 2001 From: justcool393 Date: Fri, 28 Oct 2022 22:20:48 -0500 Subject: [PATCH] listings: constantify PAGE_SIZE as 25 magic numbers are bad, do better next time --- files/classes/user.py | 3 +-- files/helpers/const.py | 5 ++-- files/routes/admin.py | 50 +++++++++++++---------------------- files/routes/front.py | 13 +++------ files/routes/hats.py | 7 +++-- files/routes/notifications.py | 24 ++++++++--------- files/routes/search.py | 21 +++++++-------- files/routes/static.py | 12 ++++----- files/routes/subs.py | 2 +- files/routes/users.py | 36 ++++++++++++------------- 10 files changed, 75 insertions(+), 98 deletions(-) diff --git a/files/classes/user.py b/files/classes/user.py index 5d9e7eda3..6965a1cb1 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -458,7 +458,6 @@ class User(Base): @cache.memoize(timeout=86400) def userpagelisting(self, site=None, v=None, page=1, sort="new", t="all"): - if self.shadowbanned and not (v and v.can_see_shadowbanned): return [] posts = g.db.query(Submission.id).filter_by(author_id=self.id, is_pinned=False, is_banned=False) @@ -471,7 +470,7 @@ class User(Base): posts = sort_objects(sort, posts, Submission, include_shadowbanned=(v and v.can_see_shadowbanned)) - posts = posts.offset(25 * (page - 1)).limit(26).all() + posts = posts.offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE+1).all() return [x[0] for x in posts] diff --git a/files/helpers/const.py b/files/helpers/const.py index b0ceef982..a07d1103e 100644 --- a/files/helpers/const.py +++ b/files/helpers/const.py @@ -152,8 +152,6 @@ DISCORD_CHANGELOG_CHANNEL_IDS = [1022232469606498324] WPD_CHANNEL_ID = 1013990963846332456 PIN_AWARD_TEXT = " (pin award)" -LEADERBOARD_LIMIT = 25 - ################################################################################ ### SITE SPECIFIC CONSTANTS ################################################################################ @@ -400,6 +398,9 @@ MAX_VIDEO_SIZE_MB_PATRON = 64 ANTISPAM_BYPASS_IDS = () +PAGE_SIZE = 25 +LEADERBOARD_LIMIT = PAGE_SIZE + if SITE == 'rdrama.net': FEATURES['PRONOUNS'] = True FEATURES['HOUSES'] = True diff --git a/files/routes/admin.py b/files/routes/admin.py index c5e04062e..38b1c6ff5 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -365,11 +365,11 @@ def image_posts_listing(v): posts = g.db.query(Submission).order_by(Submission.id.desc()) - firstrange = 25 * (page - 1) - secondrange = firstrange+26 + firstrange = PAGE_SIZE * (page - 1) + secondrange = firstrange + PAGE_SIZE + 1 posts = [x.id for x in posts if x.is_image][firstrange:secondrange] - next_exists = (len(posts) > 25) - posts = get_posts(posts[:25], v=v) + next_exists = (len(posts) > PAGE_SIZE) + posts = get_posts(posts[:PAGE_SIZE], v=v) return render_template("admin/image_posts.html", v=v, listing=posts, next_exists=next_exists, page=page, sort="new") @@ -384,11 +384,11 @@ def reported_posts(v): is_approved=None, is_banned=False, deleted_utc=0 - ).join(Submission.flags).order_by(Submission.id.desc()).offset(25 * (page - 1)).limit(26) + ).join(Submission.flags).order_by(Submission.id.desc()).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE+1) listing = [p.id for p in listing] - next_exists = len(listing) > 25 - listing = listing[:25] + next_exists = len(listing) > PAGE_SIZE + listing = listing[:PAGE_SIZE] listing = get_posts(listing, v=v) @@ -407,11 +407,11 @@ def reported_comments(v): is_approved=None, is_banned=False, deleted_utc=0 - ).join(Comment.flags).order_by(Comment.id.desc()).offset(25 * (page - 1)).limit(26).all() + ).join(Comment.flags).order_by(Comment.id.desc()).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE+1).all() listing = [c.id for c in listing] - next_exists = len(listing) > 25 - listing = listing[:25] + next_exists = len(listing) > PAGE_SIZE + listing = listing[:PAGE_SIZE] listing = get_comments(listing, v=v) @@ -628,10 +628,10 @@ def users_list(v): try: page = int(request.values.get("page", 1)) except: page = 1 - users = g.db.query(User).order_by(User.id.desc()).offset(25 * (page - 1)).limit(26).all() + users = g.db.query(User).order_by(User.id.desc()).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE + 1).all() - next_exists = (len(users) > 25) - users = users[:25] + next_exists = (len(users) > PAGE_SIZE) + users = users[:PAGE_SIZE] return render_template("user_cards.html", v=v, @@ -781,19 +781,13 @@ def admin_link_accounts(v): @app.get("/admin/removed/posts") @admin_level_required(PERMS['POST_COMMENT_MODERATION']) def admin_removed(v): - try: page = int(request.values.get("page", 1)) except: page = 1 - if page < 1: abort(400) - - ids = g.db.query(Submission.id).join(Submission.author).filter(or_(Submission.is_banned==True, User.shadowbanned != None)).order_by(Submission.id.desc()).offset(25 * (page - 1)).limit(26).all() - + ids = g.db.query(Submission.id).join(Submission.author).filter(or_(Submission.is_banned==True, User.shadowbanned != None)).order_by(Submission.id.desc()).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE + 1).all() ids=[x[0] for x in ids] - - next_exists = len(ids) > 25 - - ids = ids[:25] + next_exists = len(ids) > PAGE_SIZE + ids = ids[:PAGE_SIZE] posts = get_posts(ids, v=v) @@ -808,20 +802,14 @@ def admin_removed(v): @app.get("/admin/removed/comments") @admin_level_required(PERMS['POST_COMMENT_MODERATION']) def admin_removed_comments(v): - try: page = int(request.values.get("page", 1)) except: page = 1 - ids = g.db.query(Comment.id).join(Comment.author).filter(or_(Comment.is_banned==True, User.shadowbanned != None)).order_by(Comment.id.desc()).offset(25 * (page - 1)).limit(26).all() - + ids = g.db.query(Comment.id).join(Comment.author).filter(or_(Comment.is_banned==True, User.shadowbanned != None)).order_by(Comment.id.desc()).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE).all() ids=[x[0] for x in ids] - - next_exists = len(ids) > 25 - - ids = ids[:25] - + next_exists = len(ids) > PAGE_SIZE + ids = ids[:PAGE_SIZE] comments = get_comments(ids, v=v) - return render_template("admin/removed_comments.html", v=v, listing=comments, diff --git a/files/routes/front.py b/files/routes/front.py index 5ec23171d..5ef45fe03 100644 --- a/files/routes/front.py +++ b/files/routes/front.py @@ -139,7 +139,7 @@ def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, ccmode="false" include_shadowbanned=(v and v.can_see_shadowbanned)) if v: size = v.frontsize or 0 - else: size = 25 + else: size = PAGE_SIZE posts = posts.offset(size * (page - 1)).limit(size+1).all() @@ -201,8 +201,6 @@ def random_user(v): @app.get("/comments") @auth_required def all_comments(v): - - try: page = max(int(request.values.get("page", 1)), 1) except: page = 1 @@ -214,7 +212,6 @@ def all_comments(v): try: lt=int(request.values.get("before", 0)) except: lt=0 - idlist = comment_idlist(v=v, page=page, sort=sort, @@ -225,10 +222,8 @@ def all_comments(v): ) comments = get_comments(idlist, v=v) - - next_exists = len(idlist) > 25 - - idlist = idlist[:25] + next_exists = len(idlist) > PAGE_SIZE + idlist = idlist[:PAGE_SIZE] if v.client: return {"data": [x.json for x in comments]} return render_template("home_comments.html", v=v, sort=sort, t=t, page=page, comments=comments, standalone=True, next_exists=next_exists) @@ -260,5 +255,5 @@ def comment_idlist(v=None, page=1, sort="new", t="all", gt=0, lt=0, site=None): comments = sort_objects(sort, comments, Comment, include_shadowbanned=(v and v.can_see_shadowbanned)) - comments = comments.offset(25 * (page - 1)).limit(26).all() + comments = comments.offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE + 1).all() return [x[0] for x in comments] diff --git a/files/routes/hats.py b/files/routes/hats.py index e61a24fa2..2025f3f7e 100644 --- a/files/routes/hats.py +++ b/files/routes/hats.py @@ -113,17 +113,16 @@ def unequip_hat(v, hat_id): @app.get("/hat_owners/") @auth_required def hat_owners(v, hat_id): - try: hat_id = int(hat_id) except: abort(400) try: page = int(request.values.get("page", 1)) except: page = 1 - users = [x[1] for x in g.db.query(Hat, User).join(Hat.owners).filter(Hat.hat_id == hat_id).offset(25 * (page - 1)).limit(26).all()] + users = [x[1] for x in g.db.query(Hat, User).join(Hat.owners).filter(Hat.hat_id == hat_id).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE+1).all()] - next_exists = (len(users) > 25) - users = users[:25] + next_exists = (len(users) > PAGE_SIZE) + users = users[:PAGE_SIZE] return render_template("user_cards.html", v=v, diff --git a/files/routes/notifications.py b/files/routes/notifications.py index a1b37008d..9a3d6e05d 100644 --- a/files/routes/notifications.py +++ b/files/routes/notifications.py @@ -41,9 +41,9 @@ def notifications_modmail(v): try: page = max(int(request.values.get("page", 1)), 1) except: page = 1 - comments = g.db.query(Comment).filter(Comment.sentto==2).order_by(Comment.id.desc()).offset(25*(page-1)).limit(26).all() - next_exists = (len(comments) > 25) - listing = comments[:25] + comments = g.db.query(Comment).filter(Comment.sentto==2).order_by(Comment.id.desc()).offset(PAGE_SIZE*(page-1)).limit(PAGE_SIZE+1).all() + next_exists = (len(comments) > PAGE_SIZE) + listing = comments[:PAGE_SIZE] g.db.commit() @@ -92,7 +92,7 @@ def notifications_messages(v): message_threads = message_threads.join(thread_order, thread_order.c.top_comment_id == Comment.top_comment_id) message_threads = message_threads.order_by(thread_order.c.created_utc.desc()) \ - .offset(25*(page-1)).limit(26).all() + .offset(PAGE_SIZE*(page-1)).limit(PAGE_SIZE+1).all() # Clear notifications (used for unread indicator only) for all user messages. notifs_unread_row = g.db.query(Notification.comment_id).join(Comment).filter( @@ -147,7 +147,7 @@ def notifications_posts(v): Submission.author_id != v.id, Submission.ghost == False, Submission.author_id.notin_(v.userblocks) - ).order_by(Submission.created_utc.desc()).offset(25 * (page - 1)).limit(26).all()] + ).order_by(Submission.created_utc.desc()).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE+1).all()] next_exists = (len(listing) > 25) listing = listing[:25] @@ -177,10 +177,10 @@ def notifications_modactions(v): try: page = max(int(request.values.get("page", 1)), 1) except: page = 1 - listing = g.db.query(ModAction).filter(ModAction.user_id != v.id).order_by(ModAction.id.desc()).offset(25*(page-1)).limit(26).all() + listing = g.db.query(ModAction).filter(ModAction.user_id != v.id).order_by(ModAction.id.desc()).offset(PAGE_SIZE*(page-1)).limit(PAGE_SIZE+1).all() - next_exists = len(listing) > 25 - listing = listing[:25] + next_exists = len(listing) > PAGE_SIZE + listing = listing[:PAGE_SIZE] for ma in listing: ma.unread = ma.created_utc > v.last_viewed_log_notifs @@ -258,13 +258,11 @@ def notifications(v): or_(Comment.sentto == None, Comment.sentto == 2), ).order_by(Notification.created_utc.desc()) - comments = comments.offset(25 * (page - 1)).limit(26).all() - - next_exists = (len(comments) > 25) - comments = comments[:25] + comments = comments.offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE+1).all() + next_exists = (len(comments) > PAGE_SIZE) + comments = comments[:PAGE_SIZE] cids = [x[0].id for x in comments] - comms = get_comments(cids, v=v) listing = [] diff --git a/files/routes/search.py b/files/routes/search.py index 9b05f7b05..0aa3b6dd5 100644 --- a/files/routes/search.py +++ b/files/routes/search.py @@ -147,15 +147,12 @@ def searchposts(v): total = posts.count() - posts = posts.offset(25 * (page - 1)).limit(26).all() + posts = posts.offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE+1).all() ids = [x[0] for x in posts] - - - - next_exists = (len(ids) > 25) - ids = ids[:25] + next_exists = (len(ids) > PAGE_SIZE) + ids = ids[:PAGE_SIZE] posts = get_posts(ids, v=v) @@ -252,12 +249,12 @@ def searchcomments(v): total = comments.count() - comments = comments.offset(25 * (page - 1)).limit(26).all() + comments = comments.offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE+1).all() ids = [x[0] for x in comments] - next_exists = (len(ids) > 25) - ids = ids[:25] + next_exists = (len(ids) > PAGE_SIZE) + ids = ids[:PAGE_SIZE] comments = get_comments(ids, v=v) @@ -291,9 +288,9 @@ def searchusers(v): total=users.count() - users = users.offset(25 * (page-1)).limit(26).all() - next_exists=(len(users)>25) - users=users[:25] + users = users.offset(PAGE_SIZE * (page-1)).limit(PAGE_SIZE+1).all() + next_exists=(len(users)>PAGE_SIZE) + users=users[:PAGE_SIZE] if v.client: return {"data": [x.json for x in users]} return render_template("search_users.html", v=v, query=query, total=total, page=page, users=users, sort=sort, t=t, next_exists=next_exists) diff --git a/files/routes/static.py b/files/routes/static.py index f96344b83..8d27a3d74 100644 --- a/files/routes/static.py +++ b/files/routes/static.py @@ -158,10 +158,10 @@ def log(v): types = types2 if kind: actions = actions.filter_by(kind=kind) - actions = actions.order_by(ModAction.id.desc()).offset(25*(page-1)).limit(26).all() + actions = actions.order_by(ModAction.id.desc()).offset(PAGE_SIZE*(page-1)).limit(PAGE_SIZE+1).all() - next_exists=len(actions)>25 - actions=actions[:25] + next_exists=len(actions) > PAGE_SIZE + actions=actions[:PAGE_SIZE] admins = [x[0] for x in g.db.query(User.username).filter(User.admin_level >= PERMS['ADMIN_MOP_VISIBLE']).order_by(User.username).all()] return render_template("log.html", v=v, admins=admins, types=types, admin=admin, type=kind, actions=actions, next_exists=next_exists, page=page) @@ -397,9 +397,9 @@ def transfers(v): try: page = max(int(request.values.get("page", 1)), 1) except: page = 1 - comments = comments.offset(25 * (page - 1)).limit(26).all() - next_exists = len(comments) > 25 - comments = comments[:25] + comments = comments.offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE + 1).all() + next_exists = len(comments) > PAGE_SIZE + comments = comments[:PAGE_SIZE] if v.client: return {"data": [x.json for x in comments]} diff --git a/files/routes/subs.py b/files/routes/subs.py index 8a3fbfe20..15ed3dbf6 100644 --- a/files/routes/subs.py +++ b/files/routes/subs.py @@ -737,7 +737,7 @@ def hole_log(v, sub): types = types2 if kind: actions = actions.filter_by(kind=kind) - actions = actions.order_by(SubAction.id.desc()).offset(25*(page-1)).limit(26).all() + actions = actions.order_by(SubAction.id.desc()).offset(PAGE_SIZE*(page-1)).limit(PAGE_SIZE+1).all() next_exists=len(actions)>25 actions=actions[:25] diff --git a/files/routes/users.py b/files/routes/users.py index e791fbd7e..c2d671645 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -35,11 +35,11 @@ def upvoters_downvoters(v, username, uid, cls, vote_cls, vote_dir, template, sta page = max(1, int(request.values.get("page", 1))) - listing = g.db.query(cls).join(vote_cls).filter(cls.ghost == False, cls.is_banned == False, cls.deleted_utc == 0, vote_cls.vote_type==vote_dir, cls.author_id==id, vote_cls.user_id==uid).order_by(cls.created_utc.desc()).offset(25 * (page - 1)).limit(26).all() + listing = g.db.query(cls).join(vote_cls).filter(cls.ghost == False, cls.is_banned == False, cls.deleted_utc == 0, vote_cls.vote_type==vote_dir, cls.author_id==id, vote_cls.user_id==uid).order_by(cls.created_utc.desc()).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE + 1).all() listing = [p.id for p in listing] - next_exists = len(listing) > 25 - listing = listing[:25] + next_exists = len(listing) > PAGE_SIZE + listing = listing[:PAGE_SIZE] if cls == Submission: listing = get_posts(listing, v=v) @@ -85,11 +85,11 @@ def upvoting_downvoting(v, username, uid, cls, vote_cls, vote_dir, template, sta page = max(1, int(request.values.get("page", 1))) - listing = g.db.query(cls).join(vote_cls).filter(cls.ghost == False, cls.is_banned == False, cls.deleted_utc == 0, vote_cls.vote_type==vote_dir, vote_cls.user_id==id, cls.author_id==uid).order_by(cls.created_utc.desc()).offset(25 * (page - 1)).limit(26).all() + listing = g.db.query(cls).join(vote_cls).filter(cls.ghost == False, cls.is_banned == False, cls.deleted_utc == 0, vote_cls.vote_type==vote_dir, vote_cls.user_id==id, cls.author_id==uid).order_by(cls.created_utc.desc()).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE + 1).all() listing = [p.id for p in listing] - next_exists = len(listing) > 25 - listing = listing[:25] + next_exists = len(listing) > PAGE_SIZE + listing = listing[:PAGE_SIZE] if cls == Submission: listing = get_posts(listing, v=v) @@ -137,11 +137,11 @@ def user_voted(v, username, cls, vote_cls, vote_dir, template, standalone): cls.author_id != u.id, vote_cls.user_id == u.id, vote_cls.vote_type == vote_dir - ).order_by(cls.created_utc.desc()).offset(25 * (page - 1)).limit(26).all() + ).order_by(cls.created_utc.desc()).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE + 1).all() listing = [p.id for p in listing] - next_exists = len(listing) > 25 - listing = listing[:25] + next_exists = len(listing) > PAGE_SIZE + listing = listing[:PAGE_SIZE] if cls == Submission: listing = get_posts(listing, v=v) elif cls == Comment: @@ -218,7 +218,7 @@ def all_upvoters_downvoters(v, username, vote_dir, is_who_simps_hates): name2 = f'Who @{username} {simps_haters}' if is_who_simps_hates else f'@{username} biggest {simps_haters}' - return render_template("voters.html", v=v, users=users[:25], pos=pos, name=vote_name, name2=name2, total=total) + return render_template("voters.html", v=v, users=users[:PAGE_SIZE], pos=pos, name=vote_name, name2=name2, total=total) @app.get("/@/upvoters") @auth_required @@ -719,8 +719,8 @@ def u_username(username, v=None): ids = u.userpagelisting(site=SITE, v=v, page=page, sort=sort, t=t) - next_exists = (len(ids) > 25) - ids = ids[:25] + next_exists = (len(ids) > PAGE_SIZE) + ids = ids[:PAGE_SIZE] if page == 1: sticky = [] @@ -815,11 +815,11 @@ def u_username_comments(username, v=None): comments = sort_objects(sort, comments, Comment, include_shadowbanned=(v and v.can_see_shadowbanned)) - comments = comments.offset(25 * (page - 1)).limit(26).all() + comments = comments.offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE+1).all() ids = [x.id for x in comments] - next_exists = (len(ids) > 25) - ids = ids[:25] + next_exists = (len(ids) > PAGE_SIZE) + ids = ids[:PAGE_SIZE] listing = get_comments(ids, v=v) @@ -1047,10 +1047,10 @@ def bid_list(v, bid): try: page = int(request.values.get("page", 1)) except: page = 1 - users = g.db.query(User).join(User.badges).filter(Badge.badge_id==bid).offset(25 * (page - 1)).limit(26).all() + users = g.db.query(User).join(User.badges).filter(Badge.badge_id==bid).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE + 1).all() - next_exists = (len(users) > 25) - users = users[:25] + next_exists = (len(users) > PAGE_SIZE) + users = users[:PAGE_SIZE] return render_template("user_cards.html", v=v,