kitchen sink formatting pass

This commit is contained in:
justcool393 2023-04-02 01:14:40 -05:00 committed by Ben Rog-Wilhelm
parent 0e1a01eb75
commit 3c056d3410
14 changed files with 56 additions and 109 deletions

View file

@ -197,13 +197,11 @@ class User(CreatedBase):
@lazy
def any_block_exists(self, other):
return g.db.query(UserBlock).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()
def validate_2fa(self, token):
x = pyotp.TOTP(self.mfa_secret)
return x.verify(token, valid_window=1)

View file

@ -302,7 +302,6 @@ def get_comment_trees_eager(
query_filter_callable: Callable[[Query], Query],
sort: str="old",
v: Optional[User]=None) -> tuple[list[Comment], defaultdict[Comment, list[Comment]]]:
if v:
votes = g.db.query(CommentVote).filter_by(user_id=v.id).subquery()
blocking = v.blocking.subquery()

View file

@ -59,6 +59,7 @@ def remove_admin(v, username):
g.db.commit()
return {"message": "Admin removed!"}
@app.post("/@<username>/delete_note/<id>")
@limiter.exempt
@admin_level_required(2)
@ -70,11 +71,11 @@ def delete_note(v,username,id):
'success':True, 'message': 'Note deleted', 'note': id
}), 200)
@app.post("/@<username>/create_note")
@limiter.exempt
@admin_level_required(2)
def create_note(v,username):
def result(msg,succ,note):
return make_response(jsonify({
'success':succ, 'message': msg, 'note': note
@ -111,6 +112,7 @@ def create_note(v,username):
return result('Note saved',True,note.json())
@app.post("/@<username>/revert_actions")
@limiter.exempt
@admin_level_required(3)
@ -159,11 +161,11 @@ def revert_actions(v, username):
g.db.commit()
return {"message": "Admin actions reverted!"}
@app.post("/@<username>/club_allow")
@limiter.exempt
@admin_level_required(2)
def club_allow(v, username):
u = get_user(username, v=v)
if not u: abort(404)
@ -188,11 +190,11 @@ def club_allow(v, username):
g.db.commit()
return {"message": f"@{username} has been allowed into the {CC_TITLE}!"}
@app.post("/@<username>/club_ban")
@limiter.exempt
@admin_level_required(2)
def club_ban(v, username):
u = get_user(username, v=v)
if not u: abort(404)
@ -309,7 +311,6 @@ def update_filter_status(v):
@limiter.exempt
@admin_level_required(2)
def image_posts_listing(v):
try: page = int(request.values.get('page', 1))
except: page = 1
@ -348,7 +349,6 @@ def reported_posts(v):
@limiter.exempt
@admin_level_required(2)
def reported_comments(v):
page = max(1, int(request.values.get("page", 1)))
listing = g.db.query(Comment
@ -583,7 +583,6 @@ def badge_remove_post(v):
@limiter.exempt
@admin_level_required(2)
def users_list(v):
try: page = int(request.values.get("page", 1))
except: page = 1
@ -628,7 +627,6 @@ def loggedout_list(v):
@limiter.exempt
@admin_level_required(2)
def alt_votes_get(v):
u1 = request.values.get("u1")
u2 = request.values.get("u2")
@ -762,7 +760,6 @@ def admin_link_accounts(v):
@limiter.exempt
@admin_level_required(2)
def admin_removed(v):
try: page = int(request.values.get("page", 1))
except: page = 1
@ -790,7 +787,6 @@ def admin_removed(v):
@limiter.exempt
@admin_level_required(2)
def admin_removed_comments(v):
try: page = int(request.values.get("page", 1))
except: page = 1
@ -927,7 +923,6 @@ def unverify(user_id, v):
@limiter.exempt
@admin_level_required(2)
def admin_title_change(user_id, v):
user = g.db.query(User).filter_by(id=user_id).one_or_none()
new_name=request.values.get("title").strip()[:256]
@ -1052,7 +1047,6 @@ def ban_user(user_id, v):
@limiter.exempt
@admin_level_required(2)
def unban_user(user_id, v):
user = g.db.query(User).filter_by(id=user_id).one_or_none()
if not user or not user.is_banned: abort(400)
@ -1089,7 +1083,6 @@ def unban_user(user_id, v):
@limiter.exempt
@admin_level_required(2)
def ban_post(post_id, v):
post = g.db.query(Submission).filter_by(id=post_id).one_or_none()
if not post:
@ -1127,7 +1120,6 @@ def ban_post(post_id, v):
@limiter.exempt
@admin_level_required(2)
def unban_post(post_id, v):
post = g.db.query(Submission).filter_by(id=post_id).one_or_none()
if not post:
@ -1161,7 +1153,6 @@ def unban_post(post_id, v):
@limiter.exempt
@admin_level_required(1)
def api_distinguish_post(post_id, v):
post = g.db.query(Submission).filter_by(id=post_id).one_or_none()
if not post: abort(404)
@ -1194,7 +1185,6 @@ def api_distinguish_post(post_id, v):
@limiter.exempt
@admin_level_required(2)
def sticky_post(post_id, v):
post = g.db.query(Submission).filter_by(id=post_id).one_or_none()
if post and not post.stickied:
pins = g.db.query(Submission.id).filter(Submission.stickied != None, Submission.is_banned == False).count()
@ -1397,7 +1387,6 @@ def admin_dump_cache(v):
@limiter.exempt
@admin_level_required(3)
def admin_banned_domains(v):
banned_domains = g.db.query(BannedDomain).all()
return render_template("admin/banned_domains.html", v=v, banned_domains=banned_domains)
@ -1405,7 +1394,6 @@ def admin_banned_domains(v):
@limiter.exempt
@admin_level_required(3)
def admin_toggle_ban_domain(v):
domain=request.values.get("domain", "").strip()
if not domain: abort(400)
@ -1440,7 +1428,6 @@ def admin_toggle_ban_domain(v):
@limiter.exempt
@admin_level_required(2)
def admin_nuke_user(v):
user=get_user(request.values.get("user"))
for post in g.db.query(Submission).filter_by(author_id=user.id).all():
@ -1475,7 +1462,6 @@ def admin_nuke_user(v):
@limiter.exempt
@admin_level_required(2)
def admin_nunuke_user(v):
user=get_user(request.values.get("user"))
for post in g.db.query(Submission).filter_by(author_id=user.id).all():

View file

@ -103,7 +103,6 @@ def disconnect(v):
@socketio.on('typing')
@auth_required
def typing_indicator(data, v):
if data and v.username not in typing: typing.append(v.username)
elif not data and v.username in typing: typing.remove(v.username)

View file

@ -404,7 +404,6 @@ def unpin_comment(cid, v):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required
def save_comment(cid, v):
comment=get_comment(cid)
save=g.db.query(CommentSaveRelationship).filter_by(user_id=v.id, comment_id=comment.id).one_or_none()
@ -421,7 +420,6 @@ def save_comment(cid, v):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required
def unsave_comment(cid, v):
comment=get_comment(cid)
save=g.db.query(CommentSaveRelationship).filter_by(user_id=v.id, comment_id=comment.id).one_or_none()

View file

@ -167,7 +167,6 @@ def me(v):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required
def logout(v):
session.pop("session_id", None)
session.pop("lo_user", None)
@ -209,13 +208,14 @@ def sign_up_get(v):
error = request.values.get("error")
return render_template("sign_up.html",
formkey=formkey,
now=now,
ref_user=ref_user,
hcaptcha=HCAPTCHA_SITEKEY,
error=error
)
return render_template(
"sign_up.html",
formkey=formkey,
now=now,
ref_user=ref_user,
hcaptcha=HCAPTCHA_SITEKEY,
error=error
)
@app.post("/signup")
@ -364,7 +364,6 @@ def get_forgot():
@app.post("/forgot")
@limiter.limit("1/second;30/minute;200/hour;1000/day")
def post_forgot():
username = request.values.get("username")
if not username: abort(400)
@ -397,7 +396,6 @@ def post_forgot():
@app.get("/reset")
def get_reset():
user_id = request.values.get("id")
timestamp = int(request.values.get("time",0))
@ -476,7 +474,6 @@ def post_reset(v):
@app.get("/lost_2fa")
@auth_desired
def lost_2fa(v):
return render_template(
"lost_2fa.html",
v=v
@ -485,7 +482,6 @@ def lost_2fa(v):
@app.post("/request_2fa_disable")
@limiter.limit("1/second;6/minute;200/hour;1000/day")
def request_2fa_disable():
username=request.values.get("username")
user=get_user(username, graceful=True)
if not user or not user.email or not user.mfa_secret:
@ -523,7 +519,6 @@ def request_2fa_disable():
@app.get("/reset_2fa")
def reset_2fa():
now=int(time.time())
t = request.values.get("t")
if not t: abort(400)

View file

@ -22,7 +22,6 @@ def authorize_prompt(v):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required
def authorize(v):
client_id = request.values.get("client_id")
application = g.db.query(OauthApp).filter_by(client_id=client_id).one_or_none()
if not application: return {"oauth_error": "Invalid `client_id`"}, 401
@ -44,7 +43,6 @@ def authorize(v):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@is_not_permabanned
def request_api_keys(v):
new_app = OauthApp(
app_name=request.values.get('name').replace('<','').replace('>',''),
redirect_uri=request.values.get('redirect_uri'),
@ -85,7 +83,6 @@ def request_api_keys(v):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required
def delete_oauth_app(v, aid):
aid = int(aid)
app = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
@ -105,7 +102,6 @@ def delete_oauth_app(v, aid):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@is_not_permabanned
def edit_oauth_app(v, aid):
aid = int(aid)
app = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
@ -126,7 +122,6 @@ def edit_oauth_app(v, aid):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@admin_level_required(3)
def admin_app_approve(v, aid):
app = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
user = app.author
@ -160,7 +155,6 @@ def admin_app_approve(v, aid):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@admin_level_required(2)
def admin_app_revoke(v, aid):
app = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
if app:
for auth in g.db.query(ClientAuth).filter_by(oauth_client=app.id).all(): g.db.delete(auth)
@ -185,7 +179,6 @@ def admin_app_revoke(v, aid):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@admin_level_required(2)
def admin_app_reject(v, aid):
app = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
if app:
@ -251,7 +244,6 @@ def admin_app_id_comments(v, aid):
@app.get("/admin/apps")
@admin_level_required(2)
def admin_apps_list(v):
apps = g.db.query(OauthApp).order_by(OauthApp.id.desc()).all()
return render_template("admin/apps.html", v=v, apps=apps)
@ -261,7 +253,6 @@ def admin_apps_list(v):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required
def reroll_oauth_tokens(aid, v):
aid = aid
a = g.db.query(OauthApp).filter_by(id=aid).one_or_none()

View file

@ -24,7 +24,6 @@ from files.helpers.strings import sql_ilike_clean
from files.helpers.wrappers import *
from files.routes.importstar import *
titleheaders = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36"
}
@ -36,7 +35,6 @@ MAX_URL_LENGTH = 2048
@app.post("/toggle_club/<pid>")
@auth_required
def toggle_club(pid, v):
post = get_post(pid)
if post.author_id != v.id and v.admin_level < 2: abort(403)

View file

@ -10,7 +10,6 @@ from files.helpers.wrappers import *
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required
def api_flag_post(pid, v):
post = get_post(pid)
reason = request.values.get("reason", "").strip()[:100]
reason = filter_emojis_only(reason)
@ -41,7 +40,6 @@ def api_flag_post(pid, v):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required
def api_flag_comment(cid, v):
comment = get_comment(cid)
reason = request.values.get("reason", "").strip()[:100]
reason = filter_emojis_only(reason)

View file

@ -403,7 +403,6 @@ def settings_security_post(v):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required
def settings_log_out_others(v):
submitted_password = request.values.get("password", "").strip()
if not v.verifyPass(submitted_password):
@ -575,7 +574,6 @@ def settings_content_get(v):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@is_not_permabanned
def settings_name_change(v):
new_name=request.values.get("name").strip()
if new_name==v.username:
@ -613,7 +611,6 @@ def settings_name_change(v):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required
def settings_title_change(v):
if v.flairchanged: abort(403)
new_name=request.values.get("title").strip()[:100].replace("𒐪","")

View file

@ -52,8 +52,6 @@ def support(v):
@auth_desired
@cache.memoize(timeout=86400, make_name=make_name)
def participation_stats(v):
day = int(time.time()) - 86400
week = int(time.time()) - 604800
@ -64,38 +62,40 @@ def participation_stats(v):
active_users = set(posters) | set(commenters) | set(voters) | set(commentvoters)
stats = {"marseys": g.db.query(Marsey.name).count(),
"users": g.db.query(User.id).count(),
"private users": g.db.query(User.id).filter_by(is_private=True).count(),
"banned users": g.db.query(User.id).filter(User.is_banned > 0).count(),
"verified email users": g.db.query(User.id).filter_by(is_activated=True).count(),
"coins in circulation": g.db.query(func.sum(User.coins)).scalar(),
"total shop sales": g.db.query(func.sum(User.coins_spent)).scalar(),
"signups last 24h": g.db.query(User.id).filter(User.created_utc > day).count(),
"total posts": g.db.query(Submission.id).count(),
"posting users": g.db.query(Submission.author_id).distinct().count(),
"listed posts": g.db.query(Submission.id).filter_by(is_banned=False).filter(Submission.deleted_utc == 0).count(),
"removed posts (by admins)": g.db.query(Submission.id).filter_by(is_banned=True).count(),
"deleted posts (by author)": g.db.query(Submission.id).filter(Submission.deleted_utc > 0).count(),
"posts last 24h": g.db.query(Submission.id).filter(Submission.created_utc > day).count(),
"total comments": g.db.query(Comment.id).filter(Comment.author_id.notin_((AUTOJANNY_ID,NOTIFICATIONS_ID))).count(),
"commenting users": g.db.query(Comment.author_id).distinct().count(),
"removed comments (by admins)": g.db.query(Comment.id).filter_by(is_banned=True).count(),
"deleted comments (by author)": g.db.query(Comment.id).filter(Comment.deleted_utc > 0).count(),
"comments last_24h": g.db.query(Comment.id).filter(Comment.created_utc > day, Comment.author_id.notin_((AUTOJANNY_ID,NOTIFICATIONS_ID))).count(),
"post votes": g.db.query(Vote.submission_id).count(),
"post voting users": g.db.query(Vote.user_id).distinct().count(),
"comment votes": g.db.query(CommentVote.comment_id).count(),
"comment voting users": g.db.query(CommentVote.user_id).distinct().count(),
"total upvotes": g.db.query(Vote.submission_id).filter_by(vote_type=1).count() + g.db.query(CommentVote.comment_id).filter_by(vote_type=1).count(),
"total downvotes": g.db.query(Vote.submission_id).filter_by(vote_type=-1).count() + g.db.query(CommentVote.comment_id).filter_by(vote_type=-1).count(),
"total awards": g.db.query(AwardRelationship.id).count(),
"awards given": g.db.query(AwardRelationship.id).filter(or_(AwardRelationship.submission_id != None, AwardRelationship.comment_id != None)).count(),
"users who posted, commented, or voted in the past 7 days": len(active_users),
}
g.db.commit()
users: Query = g.db.query(User.id)
submissions: Query = g.db.query(Submission.id)
comments: Query = g.db.query(Comment.id)
stats = {
"marseys": g.db.query(Marsey.name).count(),
"users": users.count(),
"private users": users.filter_by(is_private=True).count(),
"banned users": users.filter(User.is_banned > 0).count(),
"verified email users": users.filter_by(is_activated=True).count(),
"coins in circulation": g.db.query(func.sum(User.coins)).scalar(),
"total shop sales": g.db.query(func.sum(User.coins_spent)).scalar(),
"signups last 24h": users.filter(User.created_utc > day).count(),
"total posts": submissions.count(),
"posting users": g.db.query(Submission.author_id).distinct().count(),
"listed posts": submissions.filter_by(is_banned=False).filter(Submission.deleted_utc == 0).count(),
"removed posts (by admins)": submissions.filter_by(is_banned=True).count(),
"deleted posts (by author)": submissions.filter(Submission.deleted_utc > 0).count(),
"posts last 24h": submissions.filter(Submission.created_utc > day).count(),
"total comments": comments.filter(Comment.author_id.notin_((AUTOJANNY_ID,NOTIFICATIONS_ID))).count(),
"commenting users": g.db.query(Comment.author_id).distinct().count(),
"removed comments (by admins)": comments.filter_by(is_banned=True).count(),
"deleted comments (by author)": comments.filter(Comment.deleted_utc > 0).count(),
"comments last_24h": comments.filter(Comment.created_utc > day, Comment.author_id.notin_((AUTOJANNY_ID,NOTIFICATIONS_ID))).count(),
"post votes": g.db.query(Vote.submission_id).count(),
"post voting users": g.db.query(Vote.user_id).distinct().count(),
"comment votes": g.db.query(CommentVote.comment_id).count(),
"comment voting users": g.db.query(CommentVote.user_id).distinct().count(),
"total upvotes": g.db.query(Vote.submission_id).filter_by(vote_type=1).count() + g.db.query(CommentVote.comment_id).filter_by(vote_type=1).count(),
"total downvotes": g.db.query(Vote.submission_id).filter_by(vote_type=-1).count() + g.db.query(CommentVote.comment_id).filter_by(vote_type=-1).count(),
"total awards": g.db.query(AwardRelationship.id).count(),
"awards given": g.db.query(AwardRelationship.id).filter(or_(AwardRelationship.submission_id != None, AwardRelationship.comment_id != None)).count(),
"users who posted, commented, or voted in the past 7 days": len(active_users),
}
return render_template("admin/content_stats.html", v=v, title="Content Statistics", data=stats)
@ -110,6 +110,7 @@ def weekly_chart():
f = send_file(file)
return f
@app.get("/daily_chart")
def daily_chart():
file = cached_chart(kind="daily", site=SITE)
@ -151,7 +152,7 @@ def cached_chart(kind, site):
posts_chart = plt.subplot2grid((30, 20), (10, 0), rowspan=6, colspan=30)
comments_chart = plt.subplot2grid((30, 20), (20, 0), rowspan=6, colspan=30)
signup_chart.grid(), posts_chart.grid(), comments_chart.grid()
_ = signup_chart.grid(), posts_chart.grid(), comments_chart.grid()
signup_chart.plot(
daily_times,
@ -207,7 +208,6 @@ def admins(v):
@app.get("/modlog")
@auth_desired
def log(v):
try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1
@ -249,7 +249,6 @@ def log(v):
@app.get("/log/<id>")
@auth_desired
def log_item(v, id):
try: id = int(id)
except: abort(404)
@ -288,7 +287,7 @@ def submit_contact(v: Optional[User]):
email = request.values.get("email")
if not body: abort(400)
header = "This message has been sent automatically to all admins via [/contact](/contact)\n"
header = "This message has been sent automatically to all admins via [/contact](/contact)\n"
if not email:
email = ""
else:
@ -305,12 +304,13 @@ def submit_contact(v: Optional[User]):
html += f'<img data-bs-target="#expandImageModal" data-bs-toggle="modal" onclick="expandDesktopImage(this.src)" class="img" src="{url}" loading="lazy">'
else: abort(400, "Image files only")
new_comment = Comment(author_id=v.id if v else NOTIFICATIONS_ID,
parent_submission=None,
level=1,
body_html=html,
sentto=MODMAIL_ID,
)
new_comment = Comment(
author_id=v.id if v else NOTIFICATIONS_ID,
parent_submission=None,
level=1,
body_html=html,
sentto=MODMAIL_ID,
)
g.db.add(new_comment)
g.db.flush()
new_comment.top_comment_id = new_comment.id
@ -400,14 +400,12 @@ def blocks(v):
@app.get("/banned")
@auth_desired
def banned(v):
users = [x for x in g.db.query(User).filter(User.is_banned > 0, User.unban_utc == 0).all()]
return render_template("banned.html", v=v, users=users)
@app.get("/formatting")
@auth_desired
def formatting(v):
return render_template("formatting.html", v=v)
@app.get("/service-worker.js")
@ -417,7 +415,6 @@ def serviceworker():
@app.get("/settings/security")
@auth_required
def settings_security(v):
return render_template("settings_security.html",
v=v,
mfa_secret=pyotp.random_base32() if not v.mfa_secret else None

View file

@ -482,7 +482,6 @@ def message2(v, username):
@limiter.limit("1/second;6/minute;50/hour;200/day")
@auth_required
def messagereply(v):
message = request.values.get("body", "").strip()[:MESSAGE_BODY_LENGTH_MAXIMUM].strip()
if not message and not request.files.get("file"): abort(400, "Message is empty!")
@ -594,7 +593,6 @@ def mfa_qr(secret, v):
@app.get("/is_available/<name>")
def api_is_available(name):
name=name.strip()
if len(name)<3 or len(name)>25:
@ -807,7 +805,6 @@ def u_user_id_info(id, v=None):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required
def follow_user(username, v):
target = get_user(username)
if target.id==v.id: abort(400, "You can't follow yourself!")
@ -900,7 +897,6 @@ def user_profile_uid(id:int):
@app.get("/@<username>/pic")
@limiter.exempt
def user_profile_name(username:str):
name = f"/@{username}/pic"
path = cache.get(name)
tout = 5 * 60 # 5 min
@ -926,7 +922,6 @@ def user_profile_name(username:str):
@app.get("/@<username>/saved/posts")
@auth_required
def saved_posts(v, username):
page=int(request.values.get("page",1))
ids=v.saved_idlist(page=page)
@ -950,7 +945,6 @@ def saved_posts(v, username):
@app.get("/@<username>/saved/comments")
@auth_required
def saved_comments(v, username):
page=int(request.values.get("page",1))
ids=v.saved_comment_idlist(page=page)

View file

@ -56,7 +56,6 @@ def admin_vote_info_get(v):
@limiter.limit("5/second;60/minute;600/hour;1000/day")
@is_not_permabanned
def api_vote_post(post_id, new, v):
# make sure this account is not a bot
if request.headers.get("Authorization"): abort(403)
@ -124,7 +123,6 @@ def api_vote_post(post_id, new, v):
@limiter.limit("5/second;60/minute;600/hour;1000/day")
@is_not_permabanned
def api_vote_comment(comment_id, new, v):
# make sure this account is not a bot
if request.headers.get("Authorization"): abort(403)

View file

@ -95,7 +95,6 @@ class SessionProfiler:
reporter = StreamReporter()
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
if path_callback is not None: