replace "g.db.query" on pkeys with the much more efficient "g.db.get"

remotes/1693045480750635534/spooky-22
Aevann1 2022-06-18 02:57:23 +02:00
parent c42cb538aa
commit f60db1598d
14 changed files with 53 additions and 61 deletions

View File

@ -70,7 +70,7 @@ class Comment(Base):
@property
@lazy
def top_comment(self):
return g.db.query(Comment).filter_by(id=self.top_comment_id).one_or_none()
return g.db.get(Comment, self.top_comment_id)
@lazy
def flags(self, v):
@ -206,7 +206,7 @@ class Comment(Base):
if self.level == 1: return self.post
else: return g.db.query(Comment).get(self.parent_comment_id)
else: return g.db.get(Comment, self.parent_comment_id)
@property
@lazy

View File

@ -353,7 +353,7 @@ class User(Base):
@lazy
def banned_by(self):
if not self.is_suspended: return None
return g.db.query(User).filter_by(id=self.is_banned).one_or_none()
return g.db.get(User, self.is_banned)
def has_badge(self, badge_id):
return g.db.query(Badge).filter_by(user_id=self.id, badge_id=badge_id).one_or_none()

View File

@ -91,7 +91,7 @@ def get_account(id, v=None):
try: id = int(id)
except: abort(404)
user = g.db.query(User).filter_by(id = id).one_or_none()
user = g.db.get(User, id)
if not user: abort(404)
@ -148,9 +148,7 @@ def get_post(i, v=None, graceful=False):
x.voted = items[1] or 0
x.is_blocking = items[2] or 0
else:
items = g.db.query(
Submission
).filter(Submission.id == i).one_or_none()
items = g.db.get(Submission, i)
if not items:
if graceful: return None
else: abort(404)
@ -206,7 +204,7 @@ def get_comment(i, v=None, graceful=False):
if v:
comment=g.db.query(Comment).filter(Comment.id == i).one_or_none()
comment=g.db.get(Comment, i)
if not comment and not graceful: abort(404)
@ -230,7 +228,7 @@ def get_comment(i, v=None, graceful=False):
comment.voted = vt.vote_type if vt else 0
else:
comment = g.db.query(Comment).filter(Comment.id == i).one_or_none()
comment = g.db.get(Comment, i)
if not comment and not graceful:abort(404)
return comment

View File

@ -23,7 +23,7 @@ def get_logged_in_user():
lo_user = session.get("lo_user")
if lo_user:
id = int(lo_user)
v = g.db.query(User).filter_by(id=id).one_or_none()
v = g.db.get(User, id)
if v:
nonce = session.get("login_nonce", 0)
if nonce < v.login_nonce or v.id != id: abort(401)

View File

@ -40,11 +40,6 @@ def give_monthly_marseybux_task():
send_repeatable_notification(u.id, f"@Snappy has given you {procoins} Marseybux for the month of {month}! You can use them to buy awards in the [shop](/shop).")
else: u.patron = 0
if SITE == 'pcmemes.net':
u = g.db.query(User).filter_by(id=KIPPY_ID).one()
u.procoins += 50000
g.db.add(u)
ma = ModAction(
kind="monthly",
user_id=v.id
@ -263,13 +258,13 @@ def remove_admin(v, username):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@admin_level_required(3)
def distribute(v, comment):
autobetter = g.db.query(User).filter_by(id=AUTOBETTER_ID).one_or_none()
autobetter = g.db.get(User, AUTOBETTER_ID)
if autobetter.coins == 0: return {"error": "@AutoBetter has 0 coins"}
try: comment = int(comment)
except: abort(400)
post = g.db.query(Comment.parent_submission).filter_by(id=comment).one_or_none()[0]
post = g.db.query(Submission).filter_by(id=post).one_or_none()
post = g.db.get(Submission, post)
pool = 0
for option in post.bet_options: pool += option.upvotes
@ -851,7 +846,7 @@ def admin_link_accounts(v):
g.db.add(ma)
g.db.commit()
return redirect(f"/admin/alt_votes?u1={g.db.query(User).get(u1).username}&u2={g.db.query(User).get(u2).username}")
return redirect(f"/admin/alt_votes?u1={g.db.get(User, u1).username}&u2={g.db.get(User, u2).username}")
@app.get("/admin/removed/posts")
@ -909,7 +904,7 @@ def admin_removed_comments(v):
@app.post("/agendaposter/<user_id>")
@admin_level_required(2)
def agendaposter(user_id, v):
user = g.db.query(User).filter_by(id=user_id).one_or_none()
user = g.db.get(User, user_id)
days = request.values.get("days")
if not days: days = 30.0
@ -944,7 +939,7 @@ def agendaposter(user_id, v):
@app.post("/unagendaposter/<user_id>")
@admin_level_required(2)
def unagendaposter(user_id, v):
user = g.db.query(User).filter_by(id=user_id).one_or_none()
user = g.db.get(User, user_id)
user.agendaposter = 0
g.db.add(user)
@ -974,7 +969,7 @@ def unagendaposter(user_id, v):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@admin_level_required(2)
def shadowban(user_id, v):
user = g.db.query(User).filter_by(id=user_id).one_or_none()
user = g.db.get(User, user_id)
if user.admin_level != 0: abort(403)
user.shadowbanned = v.username
g.db.add(user)
@ -1001,7 +996,7 @@ def shadowban(user_id, v):
@limiter.limit("1/second;30/minute;200/hour;1000/day")
@admin_level_required(2)
def unshadowban(user_id, v):
user = g.db.query(User).filter_by(id=user_id).one_or_none()
user = g.db.get(User, user_id)
user.shadowbanned = None
user.ban_evade = 0
g.db.add(user)
@ -1029,7 +1024,7 @@ def unshadowban(user_id, v):
@admin_level_required(2)
def admin_title_change(user_id, v):
user = g.db.query(User).filter_by(id=user_id).one_or_none()
user = g.db.get(User, user_id)
if CARP_ID > 0 and user.id == CARP_ID:
abort(403)
@ -1039,7 +1034,7 @@ def admin_title_change(user_id, v):
user.customtitleplain=new_name
new_name = filter_emojis_only(new_name)
user=g.db.query(User).filter_by(id=user.id).one_or_none()
user=g.db.get(User, user.id)
user.customtitle=new_name
if request.values.get("locked"): user.flairchanged = int(time.time()) + 2629746
else:
@ -1068,7 +1063,7 @@ def admin_title_change(user_id, v):
@admin_level_required(2)
def ban_user(user_id, v):
user = g.db.query(User).filter_by(id=user_id).one_or_none()
user = g.db.get(User, user_id)
if not user: abort(404)
@ -1141,7 +1136,7 @@ def ban_user(user_id, v):
@admin_level_required(2)
def unban_user(user_id, v):
user = g.db.query(User).filter_by(id=user_id).one_or_none()
user = g.db.get(User, user_id)
if not user or not user.is_banned: abort(400)
@ -1179,7 +1174,7 @@ def unban_user(user_id, v):
@admin_level_required(2)
def ban_post(post_id, v):
post = g.db.query(Submission).filter_by(id=post_id).one_or_none()
post = g.db.get(Submission, post_id)
if not post:
abort(400)
@ -1220,7 +1215,7 @@ def ban_post(post_id, v):
@admin_level_required(2)
def unban_post(post_id, v):
post = g.db.query(Submission).filter_by(id=post_id).one_or_none()
post = g.db.get(Submission, post_id)
if post.author.agendaposter and AGENDAPOSTER_PHRASE not in post.body.lower():
return {"error": "You can't bypass the chud award!"}
@ -1259,7 +1254,7 @@ def unban_post(post_id, v):
@admin_level_required(1)
def api_distinguish_post(post_id, v):
post = g.db.query(Submission).filter_by(id=post_id).one_or_none()
post = g.db.get(Submission, post_id)
if not post: abort(404)
@ -1291,7 +1286,7 @@ def api_distinguish_post(post_id, v):
@admin_level_required(2)
def sticky_post(post_id, v):
post = g.db.query(Submission).filter_by(id=post_id).one_or_none()
post = g.db.get(Submission, post_id)
if post and not post.stickied:
pins = g.db.query(Submission).filter(Submission.stickied != None, Submission.is_banned == False).count()
if pins >= PIN_LIMIT:
@ -1320,7 +1315,7 @@ def sticky_post(post_id, v):
@admin_level_required(2)
def unsticky_post(post_id, v):
post = g.db.query(Submission).filter_by(id=post_id).one_or_none()
post = g.db.get(Submission, post_id)
if post and post.stickied:
if post.stickied.endswith('(pin award)'): return {"error": "Can't unpin award pins!"}, 403
@ -1399,7 +1394,7 @@ def unsticky_comment(cid, v):
@admin_level_required(2)
def api_ban_comment(c_id, v):
comment = g.db.query(Comment).filter_by(id=c_id).one_or_none()
comment = g.db.get(Comment, c_id)
if not comment:
abort(404)
@ -1426,7 +1421,7 @@ def api_ban_comment(c_id, v):
@admin_level_required(2)
def api_unban_comment(c_id, v):
comment = g.db.query(Comment).filter_by(id=c_id).one_or_none()
comment = g.db.get(Comment, c_id)
if not comment: abort(404)
if comment.author.agendaposter and AGENDAPOSTER_PHRASE not in comment.body.lower():

View File

@ -107,8 +107,8 @@ def buy(v, award):
@is_not_permabanned
def award_thing(v, thing_type, id):
if thing_type == 'post': thing = g.db.query(Submission).filter_by(id=id).one_or_none()
else: thing = g.db.query(Comment).filter_by(id=id).one_or_none()
if thing_type == 'post': thing = g.db.get(Submission, id)
else: thing = g.db.get(Comment, id)
if not thing: return {"error": f"That {thing_type} doesn't exist."}, 404

View File

@ -513,7 +513,7 @@ def api_comment(v):
g.db.add(c2)
longpostbot = g.db.query(User).filter_by(id = LONGPOSTBOT_ID).one_or_none()
longpostbot = g.db.get(User, LONGPOSTBOT_ID)
longpostbot.comment_count += 1
longpostbot.coins += 1
g.db.add(longpostbot)
@ -588,7 +588,7 @@ def api_comment(v):
g.db.add(c4)
zozbot = g.db.query(User).filter_by(id = ZOZBOT_ID).one_or_none()
zozbot = g.db.get(User, ZOZBOT_ID)
zozbot.comment_count += 3
zozbot.coins += 3
g.db.add(zozbot)

View File

@ -257,7 +257,7 @@ def sign_up_post(v):
args = {"error": error}
if request.values.get("referred_by"):
user = g.db.query(User).filter_by(id=request.values.get("referred_by")).one_or_none()
user = g.db.get(User, request.values.get("referred_by"))
if user: args["ref"] = user.username
return redirect(f"/signup?{urlencode(args)}")
@ -311,9 +311,8 @@ def sign_up_post(v):
ref_id = int(request.values.get("referred_by", 0))
id_1 = g.db.query(User).filter_by(id=9).count()
users_count = g.db.query(User).count()
if id_1 == 0 and users_count == 8:
if users_count == 8:
admin_level=3
session["history"] = []
else: admin_level=0
@ -334,7 +333,7 @@ def sign_up_post(v):
g.db.add(new_user)
if ref_id:
ref_user = g.db.query(User).filter_by(id=ref_id).one_or_none()
ref_user = g.db.get(User, ref_id)
if ref_user:
badge_grant(user=ref_user, badge_id=10)
@ -421,7 +420,7 @@ def get_reset():
title="Password reset link expired",
error="That password reset link has expired.")
user = g.db.query(User).filter_by(id=user_id).one_or_none()
user = g.db.get(User, user_id)
if not user: abort(400)
@ -461,7 +460,7 @@ def post_reset(v):
title="Password reset expired",
error="That password reset form has expired.")
user = g.db.query(User).filter_by(id=user_id).one_or_none()
user = g.db.get(User, user_id)
if not validate_hash(f"{user_id}+{timestamp}+reset+{user.login_nonce}", token):
abort(400)

View File

@ -88,7 +88,7 @@ def request_api_keys(v):
def delete_oauth_app(v, aid):
aid = int(aid)
app = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
app = g.db.get(OauthApp, aid)
if app.author_id != v.id: abort(403)
@ -109,7 +109,7 @@ def delete_oauth_app(v, aid):
def edit_oauth_app(v, aid):
aid = int(aid)
app = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
app = g.db.get(OauthApp, aid)
if app.author_id != v.id: abort(403)
@ -129,7 +129,7 @@ def edit_oauth_app(v, aid):
@admin_level_required(3)
def admin_app_approve(v, aid):
app = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
app = g.db.get(OauthApp, aid)
user = app.author
app.client_id = secrets.token_urlsafe(64)[:64]
@ -163,7 +163,7 @@ def admin_app_approve(v, aid):
@admin_level_required(2)
def admin_app_revoke(v, aid):
app = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
app = g.db.get(OauthApp, aid)
if app:
for auth in g.db.query(ClientAuth).filter_by(oauth_client=app.id).all(): g.db.delete(auth)
@ -188,7 +188,7 @@ def admin_app_revoke(v, aid):
@admin_level_required(2)
def admin_app_reject(v, aid):
app = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
app = g.db.get(OauthApp, aid)
if app:
for auth in g.db.query(ClientAuth).filter_by(oauth_client=app.id).all(): g.db.delete(auth)
@ -215,7 +215,7 @@ def admin_app_id(v, aid):
aid=aid
oauth = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
oauth = g.db.get(OauthApp, aid)
pids=oauth.idlist(page=int(request.values.get("page",1)))
@ -237,7 +237,7 @@ def admin_app_id_comments(v, aid):
aid=aid
oauth = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
oauth = g.db.get(OauthApp, aid)
cids=oauth.comments_idlist(page=int(request.values.get("page",1)),
)
@ -274,7 +274,7 @@ def reroll_oauth_tokens(aid, v):
aid = aid
a = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
a = g.db.get(OauthApp, aid)
if a.author_id != v.id: abort(403)

View File

@ -425,7 +425,7 @@ def morecomments(v, cid):
else: dump.append(comment)
comments = output
else:
c = g.db.query(Comment).filter_by(id=cid).one_or_none()
c = g.db.get(Comment, cid)
comments = c.replies
if comments: p = comments[0].post
@ -603,11 +603,11 @@ def thumbnail_thread(pid):
else:
return f"{post_url}{'/' if not post_url.endswith('/') else ''}{fragment_url}"
post = db.query(Submission).filter_by(id=pid).one_or_none()
post = db.get(Submission, pid)
if not post or not post.url:
time.sleep(5)
post = db.query(Submission).filter_by(id=pid).one_or_none()
post = db.get(Submission, pid)
if not post or not post.url: return
@ -1160,7 +1160,7 @@ def submit_post(v, sub=None):
n = Notification(comment_id=c_jannied.id, user_id=v.id)
g.db.add(n)
snappy = g.db.query(User).get(SNAPPY_ID)
snappy = g.db.get(User, SNAPPY_ID)
if not (post.sub and g.db.query(Exile.user_id).filter_by(user_id=SNAPPY_ID, sub=post.sub).one_or_none()):
if post.sub == 'dankchristianmemes' or post.sub == 'truth':
@ -1346,7 +1346,7 @@ def undelete_post_pid(pid, v):
@app.post("/toggle_comment_nsfw/<cid>")
@auth_required
def toggle_comment_nsfw(cid, v):
comment = g.db.query(Comment).filter_by(id=cid).one_or_none()
comment = g.db.get(Comment, cid)
if comment.author_id != v.id and not v.admin_level > 1:
abort(403)
@ -1426,7 +1426,7 @@ def unsave_post(pid, v):
@auth_required
def api_pin_post(post_id, v):
post = g.db.query(Submission).filter_by(id=post_id).one_or_none()
post = g.db.get(Submission, post_id)
if post:
if v.id != post.author_id: return {"error": "Only the post author's can do that!"}
post.is_pinned = not post.is_pinned

View File

@ -781,7 +781,7 @@ def settings_name_change(v):
v=v,
error=f"Username `{new_name}` is already in use.")
v=g.db.query(User).filter_by(id=v.id).one_or_none()
v=g.db.get(User, v.id)
v.username=new_name
v.name_changed_utc=int(time.time())

View File

@ -158,7 +158,7 @@ def log_item(id, v):
try: id = int(id)
except: abort(404)
action=g.db.query(ModAction).filter_by(id=id).one_or_none()
action=g.db.get(ModAction, id)
if not action: abort(404)

View File

@ -244,7 +244,7 @@ def remove_mod(v, sub):
try: uid = int(uid)
except: abort(400)
user = g.db.query(User).filter_by(id=uid).one_or_none()
user = g.db.get(User, uid)
if not user: abort(404)

View File

@ -254,7 +254,7 @@ def bet(comment_id, v):
v.coins -= 200
g.db.add(v)
autobetter = g.db.query(User).filter_by(id=AUTOBETTER_ID).one_or_none()
autobetter = g.db.get(User, AUTOBETTER_ID)
autobetter.coins += 200
g.db.add(autobetter)