From c2b1f50099b025892ed24937f40d550e64fefcb8 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Fri, 17 Sep 2021 10:55:55 +0200 Subject: [PATCH] df --- files/helpers/alerts.py | 15 ++++++--------- files/helpers/wrappers.py | 3 +-- files/mail/mail.py | 1 + files/routes/admin.py | 26 ++++++++++++-------------- files/routes/awards.py | 3 +-- files/routes/oauth.py | 4 +--- files/routes/posts.py | 17 ++++++++--------- files/routes/users.py | 3 ++- 8 files changed, 32 insertions(+), 40 deletions(-) diff --git a/files/helpers/alerts.py b/files/helpers/alerts.py index bc81e63ca..dd81fc493 100644 --- a/files/helpers/alerts.py +++ b/files/helpers/alerts.py @@ -7,7 +7,7 @@ from .sanitize import * from .const import * -def send_notification(vid, user, text, db=None): +def send_notification(vid, user, text): # for when working outside request context if isinstance(user, int): @@ -15,9 +15,6 @@ def send_notification(vid, user, text, db=None): else: uid = user.id - if not db: - db = g.db - text = text.replace('r/', 'r\/').replace('u/', 'u\/') text = text.replace("\n", "\n\n").replace("\n\n\n\n\n\n", "\n\n").replace("\n\n\n\n", "\n\n").replace("\n\n\n", "\n\n") text_html = CustomRenderer().render(mistletoe.Document(text)) @@ -28,20 +25,20 @@ def send_notification(vid, user, text, db=None): parent_submission=None, distinguish_level=6, ) - db.add(new_comment) + g.db.add(new_comment) - db.flush() + g.db.flush() new_aux = CommentAux(id=new_comment.id, body=text, body_html=text_html, ) - db.add(new_aux) + g.db.add(new_aux) notif = Notification(comment_id=new_comment.id, user_id=uid) - db.add(notif) - db.commit() + g.db.add(notif) + g.db.commit() def send_follow_notif(vid, user, text): diff --git a/files/helpers/wrappers.py b/files/helpers/wrappers.py index 507adee0c..8e31ef2c0 100644 --- a/files/helpers/wrappers.py +++ b/files/helpers/wrappers.py @@ -92,15 +92,14 @@ def check_ban_evade(v): g.db.add(ma) except: pass - g.db.flush() try: abort(403) except Exception as e: print(e) else: v.ban_evade +=1 g.db.add(v) - g.db.flush() + g.db.commit() diff --git a/files/mail/mail.py b/files/mail/mail.py index f0ca51c20..ac5f5291b 100644 --- a/files/mail/mail.py +++ b/files/mail/mail.py @@ -96,5 +96,6 @@ def activate(v): g.db.add(mail_badge) g.db.add(user) + g.db.commit() return render_template("message_success.html", v=v, title="Email verified.", message=f"Your email {email} has been verified. Thank you.") diff --git a/files/routes/admin.py b/files/routes/admin.py index 12baabaec..40475a8f0 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -42,7 +42,7 @@ def revert_actions(v, username): for user in users: user.unban() - g.db.commit() + g.db.commit() return {"message": "Admin actions reverted!"} @app.post("/@/club_allow") @@ -97,7 +97,7 @@ def make_admin(v, username): if not user: abort(404) user.admin_level = 6 g.db.add(user) - g.db.commit() + g.db.commit() return {"message": "User has been made admin!"} @@ -109,7 +109,7 @@ def remove_admin(v, username): if not user: abort(404) user.admin_level = 0 g.db.add(user) - g.db.commit() + g.db.commit() return {"message": "Admin removed!"} @@ -121,7 +121,7 @@ def make_fake_admin(v, username): if not user: abort(404) user.admin_level = 1 g.db.add(user) - g.db.commit() + g.db.commit() return {"message": "User has been made fake admin!"} @@ -133,7 +133,7 @@ def remove_fake_admin(v, username): if not user: abort(404) user.admin_level = 0 g.db.add(user) - g.db.commit() + g.db.commit() return {"message": "Fake admin removed!"} @@ -436,7 +436,7 @@ def badge_grant_post(v): g.db.add(user) - g.db.commit() + g.db.commit() return redirect("/admin/badge_grant") @@ -720,7 +720,6 @@ def agendaposter(user_id, v): note = note ) g.db.add(ma) - g.db.flush() if user.agendaposter: if not user.has_badge(26): @@ -891,8 +890,6 @@ def ban_user(user_id, v): ) g.db.add(ma) - g.db.commit() - if 'reason' in request.args: if reason.startswith("/post/"): post = reason.split("/post/")[1].split("/")[0] @@ -904,8 +901,11 @@ def ban_user(user_id, v): comment = get_comment(comment) comment.bannedfor = True g.db.add(comment) + g.db.commit() return {"message": f"@{user.username} was banned!"} - else: return redirect(user.url) + else: + g.db.commit() + return redirect(user.url) @app.post("/unban_user/") @@ -937,10 +937,8 @@ def unban_user(user_id, v): g.db.commit() - if "@" in request.referrer: - return redirect(user.url) - else: - return {"message": f"@{user.username} was unbanned!"} + if "@" in request.referrer: return redirect(user.url) + else: return {"message": f"@{user.username} was unbanned!"} @app.post("/ban_post/") @admin_level_required(3) diff --git a/files/routes/awards.py b/files/routes/awards.py index 9a6034194..f6d103ff5 100644 --- a/files/routes/awards.py +++ b/files/routes/awards.py @@ -252,8 +252,7 @@ def award_post(pid, v): send_notification(NOTIFICATIONS_ACCOUNT, post.author, msg) - if kind in ACTIONS: - ACTIONS[kind](post=post) + if kind in ACTIONS: ACTIONS[kind](post=post) post.author.received_award_count += 1 g.db.add(post.author) diff --git a/files/routes/oauth.py b/files/routes/oauth.py index 76c37208b..7704da789 100644 --- a/files/routes/oauth.py +++ b/files/routes/oauth.py @@ -130,12 +130,11 @@ def admin_app_revoke(v, aid): if app.id: for auth in g.db.query(ClientAuth).options(lazyload('*')).filter_by(oauth_client=app.id).all(): g.db.delete(auth) - g.db.flush() send_notification(NOTIFICATIONS_ACCOUNT, app.author, f"Your application `{app.app_name}` has been revoked.") g.db.delete(app) - g.db.commit() + g.db.commit() return {"message": f"App revoked"} @@ -149,7 +148,6 @@ def admin_app_reject(v, aid): for auth in g.db.query(ClientAuth).options(lazyload('*')).filter_by(oauth_client=app.id).all(): g.db.delete(auth) - g.db.flush() send_notification(NOTIFICATIONS_ACCOUNT, app.author, f"Your application `{app.app_name}` has been rejected.") g.db.delete(app) diff --git a/files/routes/posts.py b/files/routes/posts.py index 94080eea2..8d21c6b3b 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -448,7 +448,7 @@ def filter_title(title): IMGUR_KEY = environ.get("IMGUR_KEY", "").strip() -def check_processing_thread(v, post, link, db): +def check_processing_thread(v, post, link): image_id = link.split('/')[-1].rstrip('.mp4') headers = {"Authorization": f"Client-ID {IMGUR_KEY}"} @@ -463,16 +463,15 @@ def check_processing_thread(v, post, link, db): status = req.json()['data']['processing']['status'] if status == 'completed': post.processing = False - db.add(post) + g.db.add(post) send_notification( NOTIFICATIONS_ACCOUNT, v, - f"Your video has finished processing and your [post](/post/{post.id}) is now live.", - db=db + f"Your video has finished processing and your [post](/post/{post.id}) is now live." ) - db.commit() + g.db.commit() break # just in case elif status == 'failed': @@ -933,7 +932,7 @@ def submit_post(v): post_url += 'mp4' new_post.url = post_url new_post.processing = True - gevent.spawn(check_processing_thread, v.id, new_post, post_url, g.db) + gevent.spawn(check_processing_thread, v.id, new_post, post_url) except UploadException as e: if request.headers.get("Authorization"): return { @@ -1211,8 +1210,8 @@ def unsave_post(pid, v): save=g.db.query(SaveRelationship).options(lazyload('*')).filter_by(user_id=v.id, submission_id=post.id, type=1).first() - if save: g.db.delete(save) - - g.db.commit() + if save: + g.db.delete(save) + g.db.commit() return {"message": "Post unsaved!"} diff --git a/files/routes/users.py b/files/routes/users.py index c7314a824..89b651575 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -131,11 +131,12 @@ def transfer_coins(v, username): receiver.coins += amount g.db.add(receiver) g.db.add(v) - g.db.commit() transfer_message = f"🤑 [@{v.username}]({v.url}) has gifted you {amount} {app.config['COINS_NAME']}!" send_notification(NOTIFICATIONS_ACCOUNT, receiver, transfer_message) + g.db.commit() + return {"message": f"{app.config['COINS_NAME']} transferred!"}