dedup page defining

master
Aevann 2023-05-05 08:23:59 +03:00
parent f3ec963708
commit d9aeb1f111
11 changed files with 49 additions and 81 deletions

View File

@ -371,3 +371,7 @@ def get_error():
def get_msg():
return request.values.get("msg")
def get_page():
try: return max(int(request.values.get("page", 1)), 1)
except: return 1

View File

@ -322,8 +322,7 @@ def image_posts_listing(v):
@admin_level_required(PERMS['POST_COMMENT_MODERATION'])
def reported_posts(v):
try: page = max(1, int(request.values.get("page", 1)))
except: abort(400, "Invalid page input!")
page = get_page()
listing = g.db.query(Submission).options(load_only(Submission.id)).filter_by(
is_approved=None,
@ -347,8 +346,7 @@ def reported_posts(v):
@admin_level_required(PERMS['POST_COMMENT_MODERATION'])
def reported_comments(v):
try: page = max(1, int(request.values.get("page", 1)))
except: abort(400, "Invalid page input!")
page = get_page()
listing = g.db.query(Comment
).filter_by(
@ -738,9 +736,7 @@ def admin_delink_relink_alt(v:User, username, other):
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@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)
page = get_page()
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) > PAGE_SIZE
@ -761,8 +757,7 @@ def admin_removed(v):
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@admin_level_required(PERMS['POST_COMMENT_MODERATION'])
def admin_removed_comments(v):
try: page = int(request.values.get("page", 1))
except: page = 1
page = get_page()
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 + 1).all()
ids=[x[0] for x in ids]

View File

@ -13,8 +13,7 @@ from files.__main__ import app
@app.get('/rss/<sort>/<t>')
@limiter.limit(DEFAULT_RATELIMIT)
def feeds_user(sort='hot', t='all'):
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
page = get_page()
ids, next_exists, size = frontlist(
sort=sort,

View File

@ -23,8 +23,7 @@ def front_all(v, sub=None, subdomain=None):
if (request.path.startswith('/h/') or request.path.startswith('/s/')) and not sub: abort(404)
try: page = max(int(request.values.get("page", 1)), 1)
except: abort(400)
page = get_page()
if v:
defaultsorting = v.defaultsorting
@ -213,8 +212,7 @@ def comment_idlist(v=None, page=1, sort="new", t="day", gt=0, lt=0):
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@auth_required
def all_comments(v:User):
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
page = get_page()
sort=request.values.get("sort", "new")
t=request.values.get("t", "hour")

View File

@ -16,8 +16,7 @@ def hats(v:User):
sort = request.values.get("sort")
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
page = get_page()
if SITE == 'rdrama.net':
hats = g.db.query(HatDef, User).join(HatDef.author)
@ -159,8 +158,7 @@ def hat_owners(v:User, hat_id):
try: hat_id = int(hat_id)
except: abort(404, "Hat not found!")
try: page = int(request.values.get("page", 1))
except: page = 1
page = get_page()
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()]

View File

@ -58,8 +58,7 @@ def unread(v):
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@admin_level_required(PERMS['VIEW_MODMAIL'])
def notifications_modmail(v):
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
page = get_page()
comments = g.db.query(Comment).filter_by(
sentto=MODMAIL_ID,
@ -89,8 +88,7 @@ def notifications_modmail(v):
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@auth_required
def notifications_messages(v:User):
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
page = get_page()
# All of these queries are horrible. For whomever comes here after me,
# PLEASE just turn DMs into their own table and get them out of
@ -160,8 +158,7 @@ def notifications_messages(v:User):
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@auth_required
def notifications_posts(v:User):
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
page = get_page()
listing = g.db.query(Submission).filter(
or_(
@ -208,8 +205,7 @@ def notifications_posts(v:User):
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@auth_required
def notifications_modactions(v:User):
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
page = get_page()
if v.admin_level >= PERMS['NOTIFICATIONS_MODERATOR_ACTIONS']:
cls = ModAction
@ -256,8 +252,7 @@ def notifications_modactions(v:User):
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@auth_required
def notifications_reddit(v:User):
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
page = get_page()
if not v.can_view_offsitementions: abort(403)
@ -296,8 +291,7 @@ def notifications_reddit(v:User):
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@auth_required
def notifications(v:User):
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
page = get_page()
if v.admin_level < PERMS['USER_SHADOWBAN']:
unread_and_inaccessible = g.db.query(Notification).join(Notification.comment).join(Comment.author).filter(

View File

@ -252,7 +252,9 @@ def admin_app_id_posts(v, aid):
oauth = g.db.get(OauthApp, aid)
if not oauth: abort(404)
pids=oauth.idlist(g.db, page=int(request.values.get("page",1)))
page = get_page()
pids=oauth.idlist(g.db, page=page)
next_exists=len(pids)==101
pids=pids[:100]
@ -277,7 +279,9 @@ def admin_app_id_comments(v, aid):
oauth = g.db.get(OauthApp, aid)
if not oauth: abort(404)
cids=oauth.comments_idlist(g.db, page=int(request.values.get("page",1)))
page = get_page()
cids=oauth.comments_idlist(g.db, page=page)
next_exists=len(cids)==101
cids=cids[:100]

View File

@ -53,8 +53,7 @@ def searchposts(v:User):
if not query:
abort(403, "Empty searches aren't allowed!")
try: page = max(1, int(request.values.get("page", 1)))
except: abort(400, "Invalid page input!")
page = get_page()
sort = request.values.get("sort", "new").lower()
t = request.values.get('t', 'all').lower()
@ -184,8 +183,7 @@ def searchcomments(v:User):
if not query:
abort(403, "Empty searches aren't allowed!")
try: page = max(1, int(request.values.get("page", 1)))
except: abort(400, "Invalid page input!")
page = get_page()
sort = request.values.get("sort", "new").lower()
t = request.values.get('t', 'all').lower()
@ -288,8 +286,7 @@ def searchmessages(v:User):
if not query:
abort(403, "Empty searches aren't allowed!")
try: page = max(1, int(request.values.get("page", 1)))
except: abort(400, "Invalid page input!")
page = get_page()
sort = request.values.get("sort", "new").lower()
t = request.values.get('t', 'all').lower()
@ -378,8 +375,7 @@ def searchusers(v:User):
if not query:
abort(403, "Empty searches aren't allowed!")
try: page = max(1, int(request.values.get("page", 1)))
except: abort(400, "Invalid page input!")
page = get_page()
users = g.db.query(User)

View File

@ -65,8 +65,7 @@ def marseys(v:User):
else: # implied sort == "usage"
marseys = marseys.order_by(Emoji.count.desc(), User.username)
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
page = get_page()
if request.path != "/marseys/all":
marseys = marseys.offset(PAGE_SIZE*(page-1)).limit(PAGE_SIZE)
@ -161,8 +160,7 @@ def admins(v:User):
@auth_required
def log(v:User):
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
page = get_page()
admin = request.values.get("admin")
if admin: admin_id = get_id(admin)
@ -391,8 +389,7 @@ def transfers(v:User):
comments = g.db.query(Comment).filter(Comment.author_id == AUTOJANNY_ID, Comment.parent_submission == None, Comment.body_html.like("%</a> has transferred %"))
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
page = get_page()
next_exists = comments.count()
comments = comments.order_by(Comment.id.desc()).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE).all()

View File

@ -853,8 +853,7 @@ def hole_log(v:User, sub):
sub = get_sub_by_name(sub)
if not User.can_see(v, sub):
abort(403)
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
page = get_page()
mod = request.values.get("mod")
if mod: mod_id = get_id(mod)

View File

@ -36,8 +36,7 @@ def upvoters_downvoters(v, username, uid, cls, vote_cls, vote_dir, template, sta
except:
abort(404)
try: page = max(1, int(request.values.get("page", 1)))
except: abort(400, "Invalid page input!")
page = get_page()
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()
@ -95,8 +94,7 @@ def upvoting_downvoting(v, username, uid, cls, vote_cls, vote_dir, template, sta
except:
abort(404)
try: page = max(1, int(request.values.get("page", 1)))
except: abort(400, "Invalid page input!")
page = get_page()
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()
@ -149,8 +147,7 @@ def user_voted(v, username, cls, vote_cls, template, standalone):
if not u.is_visible_to(v): abort(403)
if not (v.id == u.id or v.admin_level >= PERMS['USER_VOTERS_VISIBLE']): abort(403)
try: page = max(1, int(request.values.get("page", 1)))
except: abort(400, "Invalid page input!")
page = get_page()
listing = g.db.query(cls).join(vote_cls).filter(
cls.ghost == False,
@ -271,8 +268,7 @@ def all_upvoters_downvoters(v:User, username:str, vote_dir:int, is_who_simps_hat
name2 = f'Who @{username} {simps_haters}' if is_who_simps_hates else f"@{username}'s {simps_haters}"
try: page = int(request.values.get("page", 1))
except: page = 1
page = get_page()
users = users[PAGE_SIZE * (page-1):]
next_exists = (len(users) > PAGE_SIZE)
@ -732,8 +728,7 @@ def followers(v:User, username:str):
if not (v.id == u.id or v.admin_level >= PERMS['USER_FOLLOWS_VISIBLE']):
abort(403)
try: page = int(request.values.get("page", 1))
except: page = 1
page = get_page()
users = g.db.query(Follow, User).join(Follow, Follow.target_id == u.id) \
.filter(Follow.user_id == User.id) \
@ -752,8 +747,7 @@ def followers(v:User, username:str):
def blockers(v:User, username:str):
u = get_user(username, v=v)
try: page = int(request.values.get("page", 1))
except: page = 1
page = get_page()
users = g.db.query(UserBlock, User).join(UserBlock, UserBlock.target_id == u.id) \
.filter(UserBlock.user_id == User.id) \
@ -774,8 +768,7 @@ def following(v:User, username:str):
if not (v.id == u.id or v.admin_level >= PERMS['USER_FOLLOWS_VISIBLE']):
abort(403)
try: page = int(request.values.get("page", 1))
except: page = 1
page = get_page()
users = g.db.query(User).join(Follow, Follow.user_id == u.id) \
.filter(Follow.target_id == User.id) \
@ -794,8 +787,7 @@ def following(v:User, username:str):
def visitors(v:User, username:str):
u = get_user(username, v=v)
try: page = int(request.values.get("page", 1))
except: page = 1
page = get_page()
views = g.db.query(ViewerRelationship).filter_by(user_id=u.id)
next_exists = views.count()
@ -835,8 +827,7 @@ def u_username_wall(v:Optional[User], username:str):
else: view = ViewerRelationship(viewer_id=v.id, user_id=u.id)
g.db.add(view)
try: page = max(int(request.values.get("page", "1")), 1)
except: page = 1
page = get_page()
if v:
comments, output = get_comments_v_properties(v, None, Comment.wall_user_id == u.id)
@ -941,8 +932,7 @@ def u_username(v:Optional[User], username:str):
sort = request.values.get("sort", "new")
t = request.values.get("t", "all")
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
page = get_page()
ids, next_exists = userpagelisting(u, v=v, page=page, sort=sort, t=t)
@ -1010,8 +1000,7 @@ def u_username_comments(username, v=None):
else: view = ViewerRelationship(viewer_id=v.id, user_id=u.id)
g.db.add(view)
try: page = max(int(request.values.get("page", "1")), 1)
except: page = 1
page = get_page()
sort=request.values.get("sort","new")
t=request.values.get("t","all")
@ -1210,8 +1199,7 @@ def get_saves_and_subscribes(v, template, relationship_cls, page:int, standalone
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@auth_required
def saved_posts(v:User, username):
try: page = max(1, int(request.values.get("page", 1)))
except: abort(400, "Invalid page input!")
page = get_page()
return get_saves_and_subscribes(v, "userpage/submissions.html", SaveRelationship, page, False)
@ -1220,8 +1208,7 @@ def saved_posts(v:User, username):
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@auth_required
def saved_comments(v:User, username):
try: page = max(1, int(request.values.get("page", 1)))
except: abort(400, "Invalid page input!")
page = get_page()
return get_saves_and_subscribes(v, "userpage/comments.html", CommentSaveRelationship, page, True)
@ -1230,8 +1217,7 @@ def saved_comments(v:User, username):
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@auth_required
def subscribed_posts(v:User, username):
try: page = max(1, int(request.values.get("page", 1)))
except: abort(400, "Invalid page input!")
page = get_page()
return get_saves_and_subscribes(v, "userpage/submissions.html", Subscription, page, False)
@ -1287,8 +1273,7 @@ def bid_list(v:User, bid):
try: bid = int(bid)
except: abort(400)
try: page = int(request.values.get("page", 1))
except: page = 1
page = get_page()
users = g.db.query(User).join(User.badges).filter(Badge.badge_id==bid).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE + 1).all()
@ -1457,8 +1442,7 @@ def settings_claim_rewards(v:User):
@auth_required
def users_list(v):
try: page = int(request.values.get("page", 1))
except: page = 1
page = get_page()
users = g.db.query(User).order_by(User.id.desc()).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE + 1).all()