From 4abf75edc03ff4e2e5a4002e82d2b63897d7bace Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Mon, 12 Sep 2022 11:52:07 +0200 Subject: [PATCH] add error codes --- files/routes/admin.py | 16 ++++++++-------- files/routes/awards.py | 2 +- files/routes/casino.py | 28 ++++++++++++++-------------- files/routes/comments.py | 8 ++++---- files/routes/discord.py | 2 +- files/routes/hats.py | 14 +++++++------- files/routes/lottery.py | 2 +- files/routes/polls.py | 8 ++++---- files/routes/posts.py | 4 ++-- files/routes/reporting.py | 14 +++++++------- files/routes/search.py | 10 +++++----- files/routes/settings.py | 6 +++--- files/routes/subs.py | 2 +- files/routes/users.py | 12 ++++++------ 14 files changed, 64 insertions(+), 64 deletions(-) diff --git a/files/routes/admin.py b/files/routes/admin.py index df34b50fe..8f409fb17 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -194,7 +194,7 @@ def remove_admin(v, username): @admin_level_required(3) def distribute(v, option_id): autojanny = get_account(AUTOJANNY_ID) - if autojanny.coins == 0: return {"error": "@AutoJanny has 0 coins"} + if autojanny.coins == 0: return {"error": "@AutoJanny has 0 coins"}, 400 try: option_id = int(option_id) except: abort(400) @@ -310,7 +310,7 @@ def club_allow(v, username): if not u: abort(404) - if u.admin_level >= v.admin_level: return {"error": "noob"} + if u.admin_level >= v.admin_level: return {"error": "noob"}, 400 u.club_allowed = True g.db.add(u) @@ -337,7 +337,7 @@ def club_ban(v, username): if not u: abort(404) - if u.admin_level >= v.admin_level: return {"error": "noob"} + if u.admin_level >= v.admin_level: return {"error": "noob"}, 400 u.club_allowed = False @@ -497,7 +497,7 @@ def purge_cache(v): g.db.add(ma) if response == "": return {"message": "Cache purged!"} - return {"error": "Failed to purge cache."} + return {"error": "Failed to purge cache."}, 400 @app.post("/admin/under_attack") @@ -514,7 +514,7 @@ def under_attack(v): response = str(requests.patch(f'https://api.cloudflare.com/client/v4/zones/{CF_ZONE}/settings/security_level', headers=CF_HEADERS, data='{"value":"high"}', timeout=5)) if response == "": return {"message": "Under attack mode disabled!"} - return {"error": "Failed to disable under attack mode."} + return {"error": "Failed to disable under attack mode."}, 400 else: ma = ModAction( kind="enable_under_attack", @@ -524,7 +524,7 @@ def under_attack(v): response = str(requests.patch(f'https://api.cloudflare.com/client/v4/zones/{CF_ZONE}/settings/security_level', headers=CF_HEADERS, data='{"value":"under_attack"}', timeout=5)) if response == "": return {"message": "Under attack mode enabled!"} - return {"error": "Failed to enable under attack mode."} + return {"error": "Failed to enable under attack mode."}, 400 @app.get("/admin/badge_grant") @admin_level_required(2) @@ -1156,7 +1156,7 @@ def approve_post(post_id, v): post = get_post(post_id) if post.author.id == v.id and post.author.agendaposter and AGENDAPOSTER_PHRASE not in post.body.lower() and post.sub != 'chudrama': - return {"error": "You can't bypass the chud award!"} + return {"error": "You can't bypass the chud award!"}, 400 if not post: abort(400) @@ -1351,7 +1351,7 @@ def approve_comment(c_id, v): if not comment: abort(404) if comment.author.id == v.id and comment.author.agendaposter and AGENDAPOSTER_PHRASE not in comment.body.lower() and comment.post.sub != 'chudrama': - return {"error": "You can't bypass the chud award!"} + return {"error": "You can't bypass the chud award!"}, 400 if comment.is_banned: ma=ModAction( diff --git a/files/routes/awards.py b/files/routes/awards.py index 9c0608b88..8f7b91ccd 100644 --- a/files/routes/awards.py +++ b/files/routes/awards.py @@ -460,7 +460,7 @@ def admin_userawards_post(v): for key, value in notify_awards.items(): note += f"{value} {AWARDS[key]['title']}, " - if len(note) > 500: return {"error": "You're giving too many awards at the same time!"} + if len(note) > 500: return {"error": "You're giving too many awards at the same time!"}, 400 ma=ModAction( kind="grant_awards", diff --git a/files/routes/casino.py b/files/routes/casino.py index 5a361a8e4..cec14ee71 100644 --- a/files/routes/casino.py +++ b/files/routes/casino.py @@ -58,34 +58,34 @@ def casino_game_feed(v, game): @limiter.limit("100/minute;2000/hour;12000/day") @auth_required def pull_slots(v): - if v.rehab: return {"error": "You are under Rehab award effect!"} + if v.rehab: return {"error": "You are under Rehab award effect!"}, 400 try: wager = int(request.values.get("wager")) except: - return {"error": "Invalid wager."} + return {"error": "Invalid wager."}, 400 try: currency = request.values.get("currency") except: - return {"error": "Invalid currency (expected 'dramacoin' or 'marseybux')."} + return {"error": "Invalid currency (expected 'dramacoin' or 'marseybux')."}, 400 if (currency == "dramacoin" and wager > v.coins) or (currency == "marseybux" and wager > v.procoins): - return {"error": f"Not enough {currency} to make that bet."} + return {"error": f"Not enough {currency} to make that bet."}, 400 success, game_state = casino_slot_pull(v, wager, currency) if success: return {"game_state": game_state, "gambler": {"coins": v.coins, "procoins": v.procoins}} else: - return {"error": f"Wager must be more than 5 {currency}."} + return {"error": f"Wager must be more than 5 {currency}."}, 400 @app.post("/casino/twentyone/deal") @limiter.limit("100/minute;2000/hour;12000/day") @auth_required def blackjack_deal_to_player(v): - if v.rehab: return {"error": "You are under Rehab award effect!"} + if v.rehab: return {"error": "You are under Rehab award effect!"}, 400 try: wager = int(request.values.get("wager")) @@ -103,53 +103,53 @@ def blackjack_deal_to_player(v): @limiter.limit("100/minute;2000/hour;12000/day") @auth_required def blackjack_player_hit(v): - if v.rehab: return {"error": "You are under Rehab award effect!"} + if v.rehab: return {"error": "You are under Rehab award effect!"}, 400 try: state = dispatch_action(v, BlackjackAction.HIT) feed = get_game_feed('blackjack') return {"success": True, "state": state, "feed": feed, "gambler": {"coins": v.coins, "procoins": v.procoins}} except: - return {"error": "Unable to hit."} + return {"error": "Unable to hit."}, 400 @app.post("/casino/twentyone/stay") @limiter.limit("100/minute;2000/hour;12000/day") @auth_required def blackjack_player_stay(v): - if v.rehab: return {"error": "You are under Rehab award effect!"} + if v.rehab: return {"error": "You are under Rehab award effect!"}, 400 try: state = dispatch_action(v, BlackjackAction.STAY) feed = get_game_feed('blackjack') return {"success": True, "state": state, "feed": feed, "gambler": {"coins": v.coins, "procoins": v.procoins}} except: - return {"error": "Unable to stay."} + return {"error": "Unable to stay."}, 400 @app.post("/casino/twentyone/double-down") @limiter.limit("100/minute;2000/hour;12000/day") @auth_required def blackjack_player_doubled_down(v): - if v.rehab: return {"error": "You are under Rehab award effect!"} + if v.rehab: return {"error": "You are under Rehab award effect!"}, 400 try: state = dispatch_action(v, BlackjackAction.DOUBLE_DOWN) feed = get_game_feed('blackjack') return {"success": True, "state": state, "feed": feed, "gambler": {"coins": v.coins, "procoins": v.procoins}} except: - return {"error": "Unable to double down."} + return {"error": "Unable to double down."}, 400 @app.post("/casino/twentyone/buy-insurance") @limiter.limit("100/minute;2000/hour;12000/day") @auth_required def blackjack_player_bought_insurance(v): - if v.rehab: return {"error": "You are under Rehab award effect!"} + if v.rehab: return {"error": "You are under Rehab award effect!"}, 400 try: state = dispatch_action(v, BlackjackAction.BUY_INSURANCE) feed = get_game_feed('blackjack') return {"success": True, "state": state, "feed": feed, "gambler": {"coins": v.coins, "procoins": v.procoins}} except: - return {"error": "Unable to buy insurance."} + return {"error": "Unable to buy insurance."}, 400 diff --git a/files/routes/comments.py b/files/routes/comments.py index 44acfb957..f154d3a16 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -69,7 +69,7 @@ def post_pid_comment_cid(cid, pid=None, anything=None, v=None, sub=None): post = get_post(pid, v=v) if post.over_18 and not (v and v.over_18) and not session.get('over_18', 0) >= int(time.time()): - if request.headers.get("Authorization"): return {'error': 'This content is not suitable for some users and situations.'} + if request.headers.get("Authorization"): return {"error": 'This content is not suitable for some users and situations.'}, 403 else: return render_template("errors/nsfw.html", v=v) try: context = min(int(request.values.get("context", 0)), 8) @@ -149,7 +149,7 @@ def comment(v): if sub and v.exiled_from(sub): return {"error": f"You're exiled from /h/{sub}"}, 403 if sub in ('furry','vampire','racist','femboy') and not v.client and not v.house.lower().startswith(sub): - return {"error": f"You need to be a member of House {sub.capitalize()} to comment in /h/{sub}"} + return {"error": f"You need to be a member of House {sub.capitalize()} to comment in /h/{sub}"}, 400 if parent_post.club and not (v and (v.paid_dues or v.id == parent_post.author_id)): abort(403) @@ -196,7 +196,7 @@ def comment(v): oldname = f'/images/{time.time()}'.replace('.','') + '.webp' file.save(oldname) image = process_image(oldname) - if image == "": return {"error":"Image upload failed"} + if image == "": return {"error":"Image upload failed"}, 400 if v.admin_level > 2 and level == 1: if parent_post.id == SIDEBAR_THREAD: li = sorted(os.listdir(f'files/assets/images/{SITE_NAME}/sidebar'), @@ -796,7 +796,7 @@ def unpin_comment(cid, v): if v.id != comment.post.author_id: abort(403) if not comment.stickied.endswith(" (OP)"): - return {"error": "You can only unpin comments you have pinned!"} + return {"error": "You can only unpin comments you have pinned!"}, 400 comment.stickied = None g.db.add(comment) diff --git a/files/routes/discord.py b/files/routes/discord.py index ba9129145..d481e23a2 100644 --- a/files/routes/discord.py +++ b/files/routes/discord.py @@ -9,7 +9,7 @@ import requests @is_not_permabanned def join_discord(v): - if v.shadowbanned: return {"error": "Internal server error"} + if v.shadowbanned: return {"error": "Internal server error"}, 400 now=int(time.time()) diff --git a/files/routes/hats.py b/files/routes/hats.py index a4e3499cf..823005d82 100644 --- a/files/routes/hats.py +++ b/files/routes/hats.py @@ -38,13 +38,13 @@ def buy_hat(v, hat_id): if not FEATURES['HATS']: abort(404) try: hat_id = int(hat_id) - except: return {"error": "Hat not found!"} + except: return {"error": "Hat not found!"}, 400 hat = g.db.query(HatDef).filter_by(submitter_id=None, id=hat_id).one_or_none() - if not hat: return {"error": "Hat not found!"} + if not hat: return {"error": "Hat not found!"}, 400 existing = g.db.query(Hat).filter_by(user_id=v.id, hat_id=hat.id).one_or_none() - if existing: return {"error": "You already own this hat!"} + if existing: return {"error": "You already own this hat!"}, 400 if request.values.get("mb"): if v.procoins < hat.price: return {"error": "Not enough marseybux."}, 400 @@ -85,10 +85,10 @@ def equip_hat(v, hat_id): if not FEATURES['HATS']: abort(404) try: hat_id = int(hat_id) - except: return {"error": "Hat not found!"} + except: return {"error": "Hat not found!"}, 400 hat = g.db.query(Hat).filter_by(hat_id=hat_id, user_id=v.id).one_or_none() - if not hat: return {"error": "You don't own this hat!"} + if not hat: return {"error": "You don't own this hat!"}, 400 hat.equipped = True g.db.add(hat) @@ -101,10 +101,10 @@ def unequip_hat(v, hat_id): if not FEATURES['HATS']: abort(404) try: hat_id = int(hat_id) - except: return {"error": "Hat not found!"} + except: return {"error": "Hat not found!"}, 400 hat = g.db.query(Hat).filter_by(hat_id=hat_id, user_id=v.id).one_or_none() - if not hat: return {"error": "You don't own this hat!"} + if not hat: return {"error": "You don't own this hat!"}, 400 hat.equipped = False g.db.add(hat) diff --git a/files/routes/lottery.py b/files/routes/lottery.py index ab0da75a4..dfb87d207 100644 --- a/files/routes/lottery.py +++ b/files/routes/lottery.py @@ -29,7 +29,7 @@ def lottery_start(v): @casino_required def lottery_buy(v): try: quantity = int(request.values.get("quantity")) - except: return {"error": "Invalid ticket quantity."} + except: return {"error": "Invalid ticket quantity."}, 400 success, message = purchase_lottery_tickets(v, quantity) lottery, participants = get_active_lottery_stats() diff --git a/files/routes/polls.py b/files/routes/polls.py index b681215d9..0f3d7acf3 100644 --- a/files/routes/polls.py +++ b/files/routes/polls.py @@ -19,10 +19,10 @@ def vote_option(option_id, v): sub = option.post.sub if sub in ('furry','vampire','racist','femboy') and not v.house.lower().startswith(sub): - return {"error": f"You need to be a member of House {sub.capitalize()} to vote on polls in /h/{sub}"} + return {"error": f"You need to be a member of House {sub.capitalize()} to vote on polls in /h/{sub}"}, 400 if option.exclusive == 2: - if v.coins < 200: return {"error": "You don't have 200 coins!"} + if v.coins < 200: return {"error": "You don't have 200 coins!"}, 400 v.coins -= 200 g.db.add(v) autojanny = get_account(AUTOJANNY_ID) @@ -35,7 +35,7 @@ def vote_option(option_id, v): SubmissionOptionVote.submission_id==option.submission_id, SubmissionOption.exclusive==option.exclusive).one_or_none() if vote: - if option.exclusive == 2: return {"error": "You already voted on this bet!"} + if option.exclusive == 2: return {"error": "You already voted on this bet!"}, 400 g.db.delete(vote) existing = g.db.query(SubmissionOptionVote).filter_by(option_id=option_id, user_id=v.id).one_or_none() @@ -85,7 +85,7 @@ def vote_option_comment(option_id, v): sub = option.comment.post.sub if sub in ('furry','vampire','racist','femboy') and not v.house.lower().startswith(sub): - return {"error": f"You need to be a member of House {sub.capitalize()} to vote on polls in /h/{sub}"} + return {"error": f"You need to be a member of House {sub.capitalize()} to vote on polls in /h/{sub}"}, 400 if option.exclusive: vote = g.db.query(CommentOptionVote).join(CommentOption).filter( diff --git a/files/routes/posts.py b/files/routes/posts.py index 9d67fb8ad..d83e727a8 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -1241,7 +1241,7 @@ def pin_post(post_id, v): post = get_post(post_id) if post: - if v.id != post.author_id: return {"error": "Only the post author's can do that!"} + if v.id != post.author_id: return {"error": "Only the post author's can do that!"}, 400 post.is_pinned = not post.is_pinned g.db.add(post) @@ -1249,7 +1249,7 @@ def pin_post(post_id, v): if post.is_pinned: return {"message": "Post pinned!"} else: return {"message": "Post unpinned!"} - return {"error": "Post not found!"} + return {"error": "Post not found!"}, 400 extensions = ( diff --git a/files/routes/reporting.py b/files/routes/reporting.py index 588a49873..213134390 100644 --- a/files/routes/reporting.py +++ b/files/routes/reporting.py @@ -21,13 +21,13 @@ def flag_post(pid, v): send_repeatable_notification(CARP_ID, f"reports on {post.permalink}") if v.is_muted: - return {"error": "You are forbidden from making reports."} + return {"error": "You are forbidden from making reports."}, 400 reason = reason[:100] reason = filter_emojis_only(reason) - if len(reason) > 350: return {"error": "Too long."} + if len(reason) > 350: return {"error": "Too long."}, 400 if reason.startswith('!') and (v.admin_level > 1 or post.sub and v.mods(post.sub)): post.flair = reason[1:] @@ -47,16 +47,16 @@ def flag_post(pid, v): sub_to = g.db.query(Sub).filter_by(name=sub_to).one_or_none() sub_to = sub_to.name if sub_to else None - if sub_from == sub_to: abort(400) + if sub_from == sub_to: {"error": f"Post is already in /h/{sub_to}"}, 400 if post.author.exiled_from(sub_to): - return {"error": f"User is exiled from this {HOLE_NAME}!"} + return {"error": f"User is exiled from this {HOLE_NAME}!"}, 400 if sub_to in ('furry','vampire','racist','femboy') and not v.client and not post.author.house.lower().startswith(sub_to): if v.id == post.author_id: - return {"error": f"You need to be a member of House {sub.capitalize()} to post in /h/{sub}"} + return {"error": f"You need to be a member of House {sub.capitalize()} to post in /h/{sub}"}, 403 else: - return {"error": f"@{post.author.username} needs to be a member of House {sub.capitalize()} for their post to be moved to /h/{sub}"} + return {"error": f"@{post.author.username} needs to be a member of House {sub.capitalize()} for their post to be moved to /h/{sub}"}, 400 post.sub = sub_to g.db.add(post) @@ -108,7 +108,7 @@ def flag_comment(cid, v): reason = filter_emojis_only(reason) - if len(reason) > 350: return {"error": "Too long."} + if len(reason) > 350: return {"error": "Too long."}, 400 flag = CommentFlag(comment_id=comment.id, user_id=v.id, reason=reason) diff --git a/files/routes/search.py b/files/routes/search.py index f039e4a6d..0444d3b66 100644 --- a/files/routes/search.py +++ b/files/routes/search.py @@ -72,10 +72,10 @@ def searchposts(v): if 'author' in criteria: posts = posts.filter(Submission.ghost == False) author = get_user(criteria['author']) - if not author: return {"error": "User not found"} + if not author: return {"error": "User not found"}, 400 if author.is_private and author.id != v.id and v.admin_level < 2 and not v.eye: if request.headers.get("Authorization"): - return {"error": f"@{author.username}'s profile is private; You can't use the 'author' syntax on them"} + return {"error": f"@{author.username}'s profile is private; You can't use the 'author' syntax on them"}, 400 return render_template("search.html", v=v, query=query, @@ -202,17 +202,17 @@ def searchcomments(v): if 'post' in criteria: try: post = int(criteria['post']) - except: return {"error": f"Post with id {post} does not exist."} + except: return {"error": f"Post with id {post} does not exist."}, 400 comments = comments.filter(Comment.parent_submission == post) if 'author' in criteria: comments = comments.filter(Comment.ghost == False) author = get_user(criteria['author']) - if not author: return {"error": "User not found"} + if not author: return {"error": "User not found"}, 400 if author.is_private and author.id != v.id and v.admin_level < 2 and not v.eye: if request.headers.get("Authorization"): - return {"error": f"@{author.username}'s profile is private; You can't use the 'author' syntax on them"} + return {"error": f"@{author.username}'s profile is private; You can't use the 'author' syntax on them"}, 400 return render_template("search_comments.html", v=v, query=query, total=0, page=page, comments=[], sort=sort, t=t, next_exists=False, error=f"@{author.username}'s profile is private; You can't use the 'author' syntax on them.") diff --git a/files/routes/settings.py b/files/routes/settings.py index c6c72d70e..5763c93f0 100644 --- a/files/routes/settings.py +++ b/files/routes/settings.py @@ -258,7 +258,7 @@ def settings_profile_post(v): if theme: if theme in {"4chan","classic","classic_dark","coffee","dark","dramblr","light","midnight","transparent","tron","win98"}: if theme == "transparent" and not v.background: - return {"error": "You need to set a background to use the transparent theme!"} + return {"error": "You need to set a background to use the transparent theme!"}, 400 v.theme = theme if theme == "win98": v.themecolor = "30409f" updated = True @@ -581,12 +581,12 @@ def settings_css_get(v): @limiter.limit("1/second;30/minute;200/hour;1000/day", key_func=lambda:f'{SITE}-{session.get("lo_user")}') @auth_required def settings_css(v): - if v.agendaposter: return {"error": "Agendapostered users can't edit css!"} + if v.agendaposter: return {"error": "Agendapostered users can't edit css!"}, 400 css = request.values.get("css").strip().replace('\\', '').strip()[:4000] if '