remotes/1693045480750635534/spooky-22
Aevann1 2022-01-02 02:06:46 +02:00
parent 116d086a83
commit 00e5e1c048
18 changed files with 119 additions and 119 deletions

View File

@ -74,7 +74,7 @@ class Comment(Base):
@lazy
def poll_voted(self, v):
if v:
vote = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=self.id).first()
vote = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=self.id).one_or_none()
if vote: return vote.vote_type
else: return None
else: return None

View File

@ -182,7 +182,7 @@ class User(Base):
return len(self.referrals)
def is_blocking(self, target):
return g.db.query(UserBlock).filter_by(user_id=self.id, target_id=target.id).first()
return g.db.query(UserBlock).filter_by(user_id=self.id, target_id=target.id).one_or_none()
@property
@lazy
@ -193,7 +193,7 @@ class User(Base):
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()
UserBlock.user_id == other.id, UserBlock.target_id == self.id))).one_or_none()
def validate_2fa(self, token):
@ -287,10 +287,10 @@ 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).first()
return g.db.query(User).filter_by(id=self.is_banned).one_or_none()
def has_badge(self, badge_id):
return g.db.query(Badge).filter_by(user_id=self.id, badge_id=badge_id).first()
return g.db.query(Badge).filter_by(user_id=self.id, badge_id=badge_id).one_or_none()
def hash_password(self, password):
return generate_password_hash(
@ -422,7 +422,7 @@ class User(Base):
def has_follower(self, user):
return g.db.query(Follow).filter_by(target_id=self.id, user_id=user.id).first()
return g.db.query(Follow).filter_by(target_id=self.id, user_id=user.id).one_or_none()
@property
@lazy

View File

@ -30,11 +30,11 @@ def send_repeatable_notification(uid, text, autojanny=False):
if autojanny: author_id = AUTOJANNY_ID
else: author_id = NOTIFICATIONS_ID
existing_comment = g.db.query(Comment.id).filter_by(author_id=author_id, parent_submission=None, distinguish_level=6, body=text, created_utc=0).first()
existing_comment = g.db.query(Comment.id).filter_by(author_id=author_id, parent_submission=None, distinguish_level=6, body=text, created_utc=0).one_or_none()
if existing_comment:
cid = existing_comment[0]
existing_notif = g.db.query(Notification.id).filter_by(user_id=uid, comment_id=cid).first()
existing_notif = g.db.query(Notification.id).filter_by(user_id=uid, comment_id=cid).one_or_none()
if existing_notif: cid = create_comment(text, autojanny)
else: cid = create_comment(text, autojanny)
@ -53,14 +53,14 @@ def notif_comment(text, autojanny=False):
if autojanny: author_id = AUTOJANNY_ID
else: author_id = NOTIFICATIONS_ID
existing = g.db.query(Comment.id).filter_by(author_id=author_id, parent_submission=None, distinguish_level=6, body=text, created_utc=0).first()
existing = g.db.query(Comment.id).filter_by(author_id=author_id, parent_submission=None, distinguish_level=6, body=text, created_utc=0).one_or_none()
if existing: return existing[0]
else: return create_comment(text, autojanny)
def add_notif(cid, uid):
existing = g.db.query(Notification.id).filter_by(comment_id=cid, user_id=uid).first()
existing = g.db.query(Notification.id).filter_by(comment_id=cid, user_id=uid).one_or_none()
if not existing:
notif = Notification(comment_id=cid, user_id=uid)
g.db.add(notif)
@ -96,7 +96,7 @@ def NOTIFY_USERS(text, v):
soup = BeautifulSoup(text, features="html.parser")
for mention in soup.find_all("a", href=re.compile("^/@(\w+)")):
username = mention["href"].split("@")[1]
user = g.db.query(User).filter_by(username=username).first()
user = g.db.query(User).filter_by(username=username).one_or_none()
if user and not v.any_block_exists(user) and user.id != v.id: notify_users.add(user.id)
return notify_users

View File

@ -15,7 +15,7 @@ def get_id(username, v=None, graceful=False):
User.username.ilike(username),
User.original_username.ilike(username)
)
).first()
).one_or_none()
if not user:
if not graceful:
@ -43,7 +43,7 @@ def get_user(username, v=None, graceful=False):
User.username.ilike(username),
User.original_username.ilike(username)
)
).first()
).one_or_none()
if not user:
if not graceful: abort(404)
@ -60,7 +60,7 @@ def get_user(username, v=None, graceful=False):
UserBlock.target_id == v.id
)
)
).first()
).one_or_none()
user.is_blocking = block and block.user_id == v.id
user.is_blocked = block and block.target_id == v.id
@ -69,12 +69,12 @@ def get_user(username, v=None, graceful=False):
def get_account(id, v=None):
user = g.db.query(User).filter_by(id = id).first()
user = g.db.query(User).filter_by(id = id).one_or_none()
if not user:
try: id = int(str(id), 36)
except: abort(404)
user = g.db.query(User).filter_by(id = id).first()
user = g.db.query(User).filter_by(id = id).one_or_none()
if not user: abort(404)
if v:
@ -88,7 +88,7 @@ def get_account(id, v=None):
UserBlock.target_id == v.id
)
)
).first()
).one_or_none()
user.is_blocking = block and block.user_id == v.id
user.is_blocked = block and block.target_id == v.id
@ -120,7 +120,7 @@ def get_post(i, v=None, graceful=False):
isouter=True
)
items=items.first()
items=items.one_or_none()
if not items and not graceful:
abort(404)
@ -130,7 +130,7 @@ def get_post(i, v=None, graceful=False):
else:
items = g.db.query(
Submission
).filter(Submission.id == i).first()
).filter(Submission.id == i).one_or_none()
if not items and not graceful:
abort(404)
x=items
@ -201,16 +201,16 @@ def get_comment(i, v=None, graceful=False):
UserBlock.target_id == v.id
)
)
).first()
).one_or_none()
vts = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id)
vt = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id).first()
vt = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id).one_or_none()
comment.is_blocking = block and block.user_id == v.id
comment.is_blocked = block and block.target_id == v.id
comment.voted = vt.vote_type if vt else 0
else:
comment = g.db.query(Comment).filter(Comment.id == i).first()
comment = g.db.query(Comment).filter(Comment.id == i).one_or_none()
if not comment and not graceful:abort(404)
return comment

View File

@ -69,7 +69,7 @@ def activate(v):
if not validate_hash(f"{email}+{id}+{timestamp}", token):
abort(403)
user = g.db.query(User).filter_by(id=id).first()
user = g.db.query(User).filter_by(id=id).one_or_none()
if not user:
abort(404)

View File

@ -43,7 +43,7 @@ def grassed(v):
def distribute(v, comment):
try: comment = int(comment)
except: abort(400)
post = g.db.query(Comment).filter_by(id=comment).first().post
post = g.db.query(Comment).filter_by(id=comment).one_or_none().post
pool = 0
for option in post.bet_options: pool += option.upvotes
@ -58,7 +58,7 @@ def distribute(v, comment):
u.coins += coinsperperson
add_notif(cid, u.id)
autobetter = g.db.query(User).filter_by(id=AUTOBETTER_ID).first()
autobetter = g.db.query(User).filter_by(id=AUTOBETTER_ID).one_or_none()
autobetter.coins -= pool
if autobetter.coins < 0: return {"error": "Not enough coins in bool"}, 400
g.db.add(autobetter)
@ -190,7 +190,7 @@ def remove_meme_admin(v, username):
def monthly(v):
if request.host == 'rdrama.net' and v.id != 1: abort (403)
thing = g.db.query(AwardRelationship).order_by(AwardRelationship.id.desc()).first().id
thing = g.db.query(AwardRelationship).order_by(AwardRelationship.id.desc()).one_or_none().id
data = {'access_token': GUMROAD_TOKEN}
@ -625,7 +625,7 @@ def admin_removed_comments(v):
@admin_level_required(2)
@validate_formkey
def agendaposter(user_id, v):
user = g.db.query(User).filter_by(id=user_id).first()
user = g.db.query(User).filter_by(id=user_id).one_or_none()
if user.username == '911roofer': abort(403)
@ -682,7 +682,7 @@ def agendaposter(user_id, v):
@admin_level_required(2)
@validate_formkey
def shadowban(user_id, v):
user = g.db.query(User).filter_by(id=user_id).first()
user = g.db.query(User).filter_by(id=user_id).one_or_none()
if user.admin_level != 0: abort(403)
user.shadowbanned = v.username
g.db.add(user)
@ -708,7 +708,7 @@ def shadowban(user_id, v):
@admin_level_required(2)
@validate_formkey
def unshadowban(user_id, v):
user = g.db.query(User).filter_by(id=user_id).first()
user = g.db.query(User).filter_by(id=user_id).one_or_none()
if user.admin_level != 0: abort(403)
user.shadowbanned = None
user.ban_evade = 0
@ -735,7 +735,7 @@ def unshadowban(user_id, v):
@admin_level_required(2)
@validate_formkey
def verify(user_id, v):
user = g.db.query(User).filter_by(id=user_id).first()
user = g.db.query(User).filter_by(id=user_id).one_or_none()
user.verified = "Verified"
g.db.add(user)
@ -754,7 +754,7 @@ def verify(user_id, v):
@admin_level_required(2)
@validate_formkey
def unverify(user_id, v):
user = g.db.query(User).filter_by(id=user_id).first()
user = g.db.query(User).filter_by(id=user_id).one_or_none()
user.verified = None
g.db.add(user)
@ -775,7 +775,7 @@ def unverify(user_id, v):
@validate_formkey
def admin_title_change(user_id, v):
user = g.db.query(User).filter_by(id=user_id).first()
user = g.db.query(User).filter_by(id=user_id).one_or_none()
if user.admin_level != 0: abort(403)
@ -784,7 +784,7 @@ def admin_title_change(user_id, v):
user.customtitleplain=new_name
new_name = sanitize(new_name)
user=g.db.query(User).with_for_update().filter_by(id=user.id).first()
user=g.db.query(User).with_for_update().filter_by(id=user.id).one_or_none()
user.customtitle=new_name
if request.values.get("locked"): user.flairchanged = int(time.time()) + 2629746
g.db.add(user)
@ -809,7 +809,7 @@ def admin_title_change(user_id, v):
@validate_formkey
def ban_user(user_id, v):
user = g.db.query(User).filter_by(id=user_id).first()
user = g.db.query(User).filter_by(id=user_id).one_or_none()
if user.admin_level >= v.admin_level: abort(403)
@ -869,7 +869,7 @@ def ban_user(user_id, v):
@validate_formkey
def unban_user(user_id, v):
user = g.db.query(User).filter_by(id=user_id).first()
user = g.db.query(User).filter_by(id=user_id).one_or_none()
if not user:
abort(400)
@ -909,7 +909,7 @@ def unban_user(user_id, v):
@validate_formkey
def ban_post(post_id, v):
post = g.db.query(Submission).filter_by(id=post_id).first()
post = g.db.query(Submission).filter_by(id=post_id).one_or_none()
if not post:
abort(400)
@ -946,7 +946,7 @@ def ban_post(post_id, v):
@validate_formkey
def unban_post(post_id, v):
post = g.db.query(Submission).filter_by(id=post_id).first()
post = g.db.query(Submission).filter_by(id=post_id).one_or_none()
if not post:
abort(400)
@ -979,7 +979,7 @@ def unban_post(post_id, v):
@validate_formkey
def api_distinguish_post(post_id, v):
post = g.db.query(Submission).filter_by(id=post_id).first()
post = g.db.query(Submission).filter_by(id=post_id).one_or_none()
if not post:
abort(404)
@ -1100,7 +1100,7 @@ def unsticky_comment(cid, v):
@validate_formkey
def api_ban_comment(c_id, v):
comment = g.db.query(Comment).filter_by(id=c_id).first()
comment = g.db.query(Comment).filter_by(id=c_id).one_or_none()
if not comment:
abort(404)
@ -1124,7 +1124,7 @@ def api_ban_comment(c_id, v):
@validate_formkey
def api_unban_comment(c_id, v):
comment = g.db.query(Comment).filter_by(id=c_id).first()
comment = g.db.query(Comment).filter_by(id=c_id).one_or_none()
if not comment: abort(404)
if comment.author.agendaposter and 'trans lives matters' not in comment.body.lower():
@ -1193,7 +1193,7 @@ def admin_toggle_ban_domain(v):
reason=request.values.get("reason", "").strip()
d = g.db.query(BannedDomain).filter_by(domain=domain).first()
d = g.db.query(BannedDomain).filter_by(domain=domain).one_or_none()
if d:
g.db.delete(d)
ma = ModAction(

View File

@ -146,7 +146,7 @@ def buy(v, award):
if award == "lootbox":
send_repeatable_notification(995, f"@{v.username} bought a lootbox!")
for i in [1,2,3,4,5]:
thing = g.db.query(AwardRelationship).order_by(AwardRelationship.id.desc()).first().id
thing = g.db.query(AwardRelationship).order_by(AwardRelationship.id.desc()).one_or_none().id
thing += 1
award = random.choice(["snow", "gingerbread", "lights", "candycane", "fireplace"])
award = AwardRelationship(id=thing, user_id=v.id, kind=award)
@ -167,7 +167,7 @@ def buy(v, award):
send_notification(v.id, f"@AutoJanny has given you the following profile badge:\n\n![]({new_badge.path})\n\n{new_badge.name}")
else:
thing = g.db.query(AwardRelationship).order_by(AwardRelationship.id.desc()).first().id
thing = g.db.query(AwardRelationship).order_by(AwardRelationship.id.desc()).one_or_none().id
thing += 1
award = AwardRelationship(id=thing, user_id=v.id, kind=award)
g.db.add(award)
@ -200,12 +200,12 @@ def award_post(pid, v):
AwardRelationship.submission_id == None,
AwardRelationship.comment_id == None
)
).first()
).one_or_none()
if not post_award:
return {"error": "You don't have that award."}, 404
post = g.db.query(Submission).filter_by(id=pid).first()
post = g.db.query(Submission).filter_by(id=pid).one_or_none()
if not post:
return {"error": "That post doesn't exist."}, 404
@ -216,7 +216,7 @@ def award_post(pid, v):
AwardRelationship.user_id == v.id,
AwardRelationship.kind == kind
)
).first()
).one_or_none()
post_award.submission_id = post.id
g.db.add(post_award)
@ -374,12 +374,12 @@ def award_comment(cid, v):
AwardRelationship.submission_id == None,
AwardRelationship.comment_id == None
)
).first()
).one_or_none()
if not comment_award:
return {"error": "You don't have that award."}, 404
c = g.db.query(Comment).filter_by(id=cid).first()
c = g.db.query(Comment).filter_by(id=cid).one_or_none()
if not c:
return {"error": "That comment doesn't exist."}, 404
@ -390,7 +390,7 @@ def award_comment(cid, v):
AwardRelationship.user_id == v.id,
AwardRelationship.kind == kind
)
).first()
).one_or_none()
comment_award.comment_id = c.id
g.db.add(comment_award)
@ -547,7 +547,7 @@ def admin_userawards_post(v):
notify_awards = {}
latest = g.db.query(AwardRelationship).order_by(AwardRelationship.id.desc()).first()
latest = g.db.query(AwardRelationship).order_by(AwardRelationship.id.desc()).one_or_none()
thing = latest.id
for key, value in request.values.items():

View File

@ -43,7 +43,7 @@ def post_pid_comment_cid(cid, pid=None, anything=None, v=None):
comment = get_comment(cid, v=v)
if v and request.values.get("read"):
notif = g.db.query(Notification).filter_by(comment_id=cid, user_id=v.id, read=False).first()
notif = g.db.query(Notification).filter_by(comment_id=cid, user_id=v.id, read=False).one_or_none()
if notif:
notif.read = True
g.db.add(notif)
@ -230,7 +230,7 @@ def api_comment(v):
Comment.parent_comment_id == parent_comment_id,
Comment.parent_submission == parent_submission,
Comment.body_html == body_html
).first()
).one_or_none()
if existing: return {"error": f"You already made that comment: /comment/{existing.id}"}, 409
if parent.author.any_block_exists(v) and v.admin_level < 2: return {"error": "You can't reply to users who have blocked you, or users you have blocked."}, 403
@ -433,7 +433,7 @@ def api_comment(v):
g.db.add(c2)
longpostbot = g.db.query(User).filter_by(id = LONGPOSTBOT_ID).first()
longpostbot = g.db.query(User).filter_by(id = LONGPOSTBOT_ID).one_or_none()
longpostbot.comment_count += 1
longpostbot.coins += 1
g.db.add(longpostbot)
@ -510,7 +510,7 @@ def api_comment(v):
g.db.add(c4)
zozbot = g.db.query(User).filter_by(id = ZOZBOT_ID).first()
zozbot = g.db.query(User).filter_by(id = ZOZBOT_ID).one_or_none()
zozbot.comment_count += 3
zozbot.coins += 3
g.db.add(zozbot)
@ -788,7 +788,7 @@ def edit_comment(cid, v):
notify_users = NOTIFY_USERS(body_html, v)
for x in notify_users:
notif = g.db.query(Notification).filter_by(comment_id=c.id, user_id=x).first()
notif = g.db.query(Notification).filter_by(comment_id=c.id, user_id=x).one_or_none()
if not notif:
n = Notification(comment_id=c.id, user_id=x)
g.db.add(n)
@ -804,7 +804,7 @@ def edit_comment(cid, v):
@validate_formkey
def delete_comment(cid, v):
c = g.db.query(Comment).filter_by(id=cid).first()
c = g.db.query(Comment).filter_by(id=cid).one_or_none()
if not c: abort(404)
@ -826,7 +826,7 @@ def delete_comment(cid, v):
@validate_formkey
def undelete_comment(cid, v):
c = g.db.query(Comment).filter_by(id=cid).first()
c = g.db.query(Comment).filter_by(id=cid).one_or_none()
if not c:
abort(404)
@ -895,7 +895,7 @@ def save_comment(cid, v):
comment=get_comment(cid)
save=g.db.query(SaveRelationship).filter_by(user_id=v.id, comment_id=comment.id, type=2).first()
save=g.db.query(SaveRelationship).filter_by(user_id=v.id, comment_id=comment.id, type=2).one_or_none()
if not save:
new_save=SaveRelationship(user_id=v.id, comment_id=comment.id, type=2)
@ -914,7 +914,7 @@ def unsave_comment(cid, v):
comment=get_comment(cid)
save=g.db.query(SaveRelationship).filter_by(user_id=v.id, comment_id=comment.id, type=2).first()
save=g.db.query(SaveRelationship).filter_by(user_id=v.id, comment_id=comment.id, type=2).one_or_none()
if save:
g.db.delete(save)

View File

@ -97,7 +97,7 @@ def discord_redirect(v):
url=f"https://discord.com/api/guilds/{SERVER_ID}/members/{v.discord_id}"
requests.delete(url, headers=headers, timeout=5)
if g.db.query(User).filter(User.id!=v.id, User.discord_id==x["id"]).first():
if g.db.query(User).filter(User.id!=v.id, User.discord_id==x["id"]).one_or_none():
if not v or v.oldsite: template = ''
else: template = 'CHRISTMAS/'
return render_template(f"{template}message.html", title="Discord account already linked.", error="That Discord account is already in use by another user.", v=v)

View File

@ -389,7 +389,7 @@ def random_post(v):
total = x.count()
n = random.randint(1, total - 2)
post = x.offset(n).limit(1).first()
post = x.offset(n).limit(1).one_or_none()
return redirect(f"/post/{post.id}")
@cache.memoize(timeout=86400)

View File

@ -34,9 +34,9 @@ def check_for_alts(current_id):
if past_id == current_id: continue
check1 = g.db.query(Alt).filter_by(
user1=current_id, user2=past_id).first()
user1=current_id, user2=past_id).one_or_none()
check2 = g.db.query(Alt).filter_by(
user1=past_id, user2=current_id).first()
user1=past_id, user2=current_id).one_or_none()
if not check1 and not check2:
@ -49,25 +49,25 @@ def check_for_alts(current_id):
alts = g.db.query(Alt)
otheralts = alts.filter(or_(Alt.user1 == past_id, Alt.user2 == past_id, Alt.user1 == current_id, Alt.user2 == current_id)).all()
for a in otheralts:
existing = alts.filter_by(user1=a.user1, user2=past_id).first()
existing = alts.filter_by(user1=a.user1, user2=past_id).one_or_none()
if not existing:
new_alt = Alt(user1=a.user1, user2=past_id)
g.db.add(new_alt)
g.db.flush()
existing = alts.filter_by(user1=a.user1, user2=current_id).first()
existing = alts.filter_by(user1=a.user1, user2=current_id).one_or_none()
if not existing:
new_alt = Alt(user1=a.user1, user2=current_id)
g.db.add(new_alt)
g.db.flush()
existing = alts.filter_by(user1=a.user2, user2=past_id).first()
existing = alts.filter_by(user1=a.user2, user2=past_id).one_or_none()
if not existing:
new_alt = Alt(user1=a.user2, user2=past_id)
g.db.add(new_alt)
g.db.flush()
existing = alts.filter_by(user1=a.user2, user2=current_id).first()
existing = alts.filter_by(user1=a.user2, user2=current_id).one_or_none()
if not existing:
new_alt = Alt(user1=a.user2, user2=current_id)
g.db.add(new_alt)
@ -85,7 +85,7 @@ def login_post():
if not username: abort(400)
if "@" in username:
account = g.db.query(User).filter(
User.email.ilike(username)).first()
User.email.ilike(username)).one_or_none()
else:
account = get_user(username, graceful=True)
@ -180,7 +180,7 @@ def sign_up_get(v):
ref = request.values.get("ref", None)
if ref:
ref_user = g.db.query(User).filter(User.username.ilike(ref)).first()
ref_user = g.db.query(User).filter(User.username.ilike(ref)).one_or_none()
else:
ref_user = None
@ -249,7 +249,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")).first()
id=request.values.get("referred_by")).one_or_none()
if user:
args["ref"] = user.username
@ -366,7 +366,7 @@ def post_forgot():
user = g.db.query(User).filter(
User.username.ilike(username),
User.email.ilike(email)).first()
User.email.ilike(email)).one_or_none()
if user:
now = int(time.time())
@ -401,7 +401,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).first()
user = g.db.query(User).filter_by(id=user_id).one_or_none()
if not validate_hash(f"{user_id}+{timestamp}+forgot+{user.login_nonce}", token):
abort(400)
@ -443,7 +443,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).first()
user = g.db.query(User).filter_by(id=user_id).one_or_none()
if not validate_hash(f"{user_id}+{timestamp}+reset+{user.login_nonce}", token):
abort(400)

View File

@ -11,7 +11,7 @@ from sqlalchemy.orm import joinedload
@auth_required
def authorize_prompt(v):
client_id = request.values.get("client_id")
application = g.db.query(OauthApp).filter_by(client_id=client_id).first()
application = g.db.query(OauthApp).filter_by(client_id=client_id).one_or_none()
if not application: return {"oauth_error": "Invalid `client_id`"}, 401
if not v or v.oldsite: template = ''
else: template = 'CHRISTMAS/'
@ -25,7 +25,7 @@ def authorize_prompt(v):
def authorize(v):
client_id = request.values.get("client_id")
application = g.db.query(OauthApp).filter_by(client_id=client_id).first()
application = g.db.query(OauthApp).filter_by(client_id=client_id).one_or_none()
if not application: return {"oauth_error": "Invalid `client_id`"}, 401
access_token = secrets.token_urlsafe(128)[:128]
new_auth = ClientAuth(oauth_client = application.id, user_id = v.id, access_token=access_token)
@ -66,7 +66,7 @@ def request_api_keys(v):
def delete_oauth_app(v, aid):
aid = int(aid)
app = g.db.query(OauthApp).filter_by(id=aid).first()
app = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
if app.author_id != v.id: abort(403)
@ -87,7 +87,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).first()
app = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
if app.author_id != v.id: abort(403)
@ -108,7 +108,7 @@ def edit_oauth_app(v, aid):
@validate_formkey
def admin_app_approve(v, aid):
app = g.db.query(OauthApp).filter_by(id=aid).first()
app = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
user = app.author
app.client_id = secrets.token_urlsafe(64)[:64]
@ -143,7 +143,7 @@ def admin_app_approve(v, aid):
@validate_formkey
def admin_app_revoke(v, aid):
app = g.db.query(OauthApp).filter_by(id=aid).first()
app = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
if app.id:
for auth in g.db.query(ClientAuth).filter_by(oauth_client=app.id).all(): g.db.delete(auth)
@ -169,7 +169,7 @@ def admin_app_revoke(v, aid):
@validate_formkey
def admin_app_reject(v, aid):
app = g.db.query(OauthApp).filter_by(id=aid).first()
app = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
for auth in g.db.query(ClientAuth).filter_by(oauth_client=app.id).all(): g.db.delete(auth)
@ -198,7 +198,7 @@ def admin_app_id(v, aid):
oauth = g.db.query(OauthApp).options(
joinedload(
OauthApp.author)).filter_by(
id=aid).first()
id=aid).one_or_none()
pids=oauth.idlist(page=int(request.values.get("page",1)),
)
@ -226,7 +226,7 @@ def admin_app_id_comments(v, aid):
oauth = g.db.query(OauthApp).options(
joinedload(
OauthApp.author)).filter_by(
id=aid).first()
id=aid).one_or_none()
cids=oauth.comments_idlist(page=int(request.values.get("page",1)),
)
@ -267,7 +267,7 @@ def reroll_oauth_tokens(aid, v):
aid = aid
a = g.db.query(OauthApp).filter_by(id=aid).first()
a = g.db.query(OauthApp).filter_by(id=aid).one_or_none()
if a.author_id != v.id: abort(403)

View File

@ -383,7 +383,7 @@ def morecomments(v, cid):
else: dump.append(comment)
comments = output
else:
c = g.db.query(Comment).filter_by(id=cid).first()
c = g.db.query(Comment).filter_by(id=cid).one_or_none()
comments = c.replies
if not v or v.oldsite: template = ''
@ -596,11 +596,11 @@ def thumbnail_thread(pid):
db = db_session()
post = db.query(Submission).filter_by(id=pid).first()
post = db.query(Submission).filter_by(id=pid).one_or_none()
if not post or not post.url:
time.sleep(5)
post = db.query(Submission).filter_by(id=pid).first()
post = db.query(Submission).filter_by(id=pid).one_or_none()
if not post or not post.url: return
@ -776,7 +776,7 @@ def submit_post(v):
Submission.url.ilike(url),
Submission.deleted_utc == 0,
Submission.is_banned == False
).first()
).one_or_none()
if repost: return redirect(repost.permalink)
@ -849,7 +849,7 @@ def submit_post(v):
Submission.title == title,
Submission.url == url,
Submission.body == body
).first()
).one_or_none()
if dup: return redirect(dup.permalink)
@ -1167,7 +1167,7 @@ def submit_post(v):
g.db.add(c)
snappy = g.db.query(User).filter_by(id = SNAPPY_ID).first()
snappy = g.db.query(User).filter_by(id = SNAPPY_ID).one_or_none()
snappy.comment_count += 1
snappy.coins += 1
g.db.add(snappy)
@ -1238,7 +1238,7 @@ def undelete_post_pid(pid, v):
@validate_formkey
def toggle_comment_nsfw(cid, v):
comment = g.db.query(Comment).filter_by(id=cid).first()
comment = g.db.query(Comment).filter_by(id=cid).one_or_none()
if not comment.author_id == v.id and not v.admin_level > 1: abort(403)
comment.over_18 = not comment.over_18
g.db.add(comment)
@ -1282,7 +1282,7 @@ def save_post(pid, v):
post=get_post(pid)
save = g.db.query(SaveRelationship).filter_by(user_id=v.id, submission_id=post.id, type=1).first()
save = g.db.query(SaveRelationship).filter_by(user_id=v.id, submission_id=post.id, type=1).one_or_none()
if not save:
new_save=SaveRelationship(user_id=v.id, submission_id=post.id, type=1)
@ -1299,7 +1299,7 @@ def unsave_post(pid, v):
post=get_post(pid)
save = g.db.query(SaveRelationship).filter_by(user_id=v.id, submission_id=post.id, type=1).first()
save = g.db.query(SaveRelationship).filter_by(user_id=v.id, submission_id=post.id, type=1).one_or_none()
if save:
g.db.delete(save)
@ -1311,7 +1311,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).first()
post = g.db.query(Submission).filter_by(id=post_id).one_or_none()
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

@ -17,7 +17,7 @@ def api_flag_post(pid, v):
reason = request.values.get("reason", "").strip()[:100]
if not reason.startswith('!'):
existing = g.db.query(Flag.id).filter_by(user_id=v.id, post_id=post.id).first()
existing = g.db.query(Flag.id).filter_by(user_id=v.id, post_id=post.id).one_or_none()
if existing: return "", 409
reason = filter_emojis_only(reason)
@ -45,7 +45,7 @@ def api_flag_comment(cid, v):
comment = get_comment(cid)
if not v.shadowbanned:
existing = g.db.query(CommentFlag.id).filter_by( user_id=v.id, comment_id=comment.id).first()
existing = g.db.query(CommentFlag.id).filter_by( user_id=v.id, comment_id=comment.id).one_or_none()
if existing: return "", 409
reason = request.values.get("reason", "").strip()[:100]
@ -68,9 +68,9 @@ def api_flag_comment(cid, v):
def remove_report(report_fn, v):
if report_fn.startswith('c'):
report = g.db.query(CommentFlag).filter_by(id=int(report_fn.lstrip('c'))).first()
report = g.db.query(CommentFlag).filter_by(id=int(report_fn.lstrip('c'))).one_or_none()
elif report_fn.startswith('p'):
report = g.db.query(Flag).filter_by(id=int(report_fn.lstrip('p'))).first()
report = g.db.query(Flag).filter_by(id=int(report_fn.lstrip('p'))).one_or_none()
else:
return {"error": "Invalid report ID"}, 400

View File

@ -603,7 +603,7 @@ def settings_security_post(v):
return render_template("settings_security.html", v=v, error="That email is already yours!")
existing = g.db.query(User.id).filter(User.id != v.id,
func.lower(User.email) == new_email.lower()).first()
func.lower(User.email) == new_email.lower()).one_or_none()
if existing:
return render_template("settings_security.html", v=v, error="That email address is already in use.")
@ -960,7 +960,7 @@ def settings_name_change(v):
User.username.ilike(name),
User.original_username.ilike(name)
)
).first()
).one_or_none()
if x and x.id != v.id:
if not v or v.oldsite: template = ''
@ -969,7 +969,7 @@ def settings_name_change(v):
v=v,
error=f"Username `{new_name}` is already in use.")
v=g.db.query(User).with_for_update().filter_by(id=v.id).first()
v=g.db.query(User).with_for_update().filter_by(id=v.id).one_or_none()
v.username=new_name
v.name_changed_utc=int(time.time())

View File

@ -98,7 +98,7 @@ def cached_chart(days):
today_cutoff = calendar.timegm(midnight_this_morning)
if not days:
firstsignup = g.db.query(User.created_utc).filter(User.created_utc != 0).order_by(User.created_utc).first()[0] - 86400
firstsignup = g.db.query(User.created_utc).filter(User.created_utc != 0).order_by(User.created_utc).one_or_none()[0] - 86400
nowstamp = int(time.time())
days = int((nowstamp - firstsignup) / 86400)
@ -224,7 +224,7 @@ def log_item(id, v):
try: id = int(id, 36)
except: abort(404)
action=g.db.query(ModAction).filter_by(id=id).first()
action=g.db.query(ModAction).filter_by(id=id).one_or_none()
if not action:
abort(404)

View File

@ -206,7 +206,7 @@ def get_coins(v, username):
@is_not_banned
@validate_formkey
def transfer_coins(v, username):
receiver = g.db.query(User).filter_by(username=username).first()
receiver = g.db.query(User).filter_by(username=username).one_or_none()
if receiver is None: return {"error": "That user doesn't exist."}, 404
@ -220,7 +220,7 @@ def transfer_coins(v, username):
if not v.patron and not receiver.patron:
tax = math.ceil(amount*0.03)
tax_receiver = g.db.query(User).filter_by(id=TAX_RECEIVER_ID).first()
tax_receiver = g.db.query(User).filter_by(id=TAX_RECEIVER_ID).one_or_none()
tax_receiver.coins += tax
log_message = f"[@{v.username}]({v.url}) has transferred {amount} {app.config['COINS_NAME']} to [@{receiver.username}]({receiver.url})"
send_repeatable_notification(TAX_RECEIVER_ID, log_message)
@ -244,7 +244,7 @@ def transfer_coins(v, username):
@is_not_banned
@validate_formkey
def transfer_bux(v, username):
receiver = g.db.query(User).filter_by(username=username).first()
receiver = g.db.query(User).filter_by(username=username).one_or_none()
if not receiver: return {"error": "That user doesn't exist."}, 404
@ -329,7 +329,7 @@ def get_profilecss(username):
def songs(id):
try: id = int(id)
except: return "", 400
user = g.db.query(User).filter_by(id=id).first()
user = g.db.query(User).filter_by(id=id).one_or_none()
if user and user.song: return redirect(f"/static/song/{user.song}.mp3")
else: abort(404)
@ -357,7 +357,7 @@ def subscribe(v, post_id):
@auth_required
@validate_formkey
def unsubscribe(v, post_id):
sub=g.db.query(Subscription).filter_by(user_id=v.id, submission_id=post_id).first()
sub=g.db.query(Subscription).filter_by(user_id=v.id, submission_id=post_id).one_or_none()
if sub:
g.db.delete(sub)
g.db.commit()
@ -393,7 +393,7 @@ def message2(v, username):
existing = g.db.query(Comment.id).filter(Comment.author_id == v.id,
Comment.sentto == user.id,
Comment.body_html == text_html,
).first()
).one_or_none()
if existing: return redirect('/notifications?messages=true')
new_comment = Comment(author_id=v.id,
@ -504,7 +504,7 @@ def api_is_available(name, v):
User.username.ilike(name2),
User.original_username.ilike(name2)
)
).first()
).one_or_none()
if x:
return {name: False}
@ -584,7 +584,7 @@ def u_username(username, v=None):
ViewerRelationship.viewer_id == v.id,
ViewerRelationship.user_id == u.id
)
).first()
).one_or_none()
if view:
view.last_view_utc = g.timestamp
@ -806,7 +806,7 @@ def follow_user(username, v):
if target.id==v.id: return {"error": "You can't follow yourself!"}, 400
if g.db.query(Follow).filter_by(user_id=v.id, target_id=target.id).first(): return {"message": "User followed!"}
if g.db.query(Follow).filter_by(user_id=v.id, target_id=target.id).one_or_none(): return {"message": "User followed!"}
new_follow = Follow(user_id=v.id, target_id=target.id)
g.db.add(new_follow)
@ -831,7 +831,7 @@ def unfollow_user(username, v):
if target.fish: return {"error": "You can't unfollow this user!"}
follow = g.db.query(Follow).filter_by(user_id=v.id, target_id=target.id).first()
follow = g.db.query(Follow).filter_by(user_id=v.id, target_id=target.id).one_or_none()
if follow:
g.db.delete(follow)
@ -853,7 +853,7 @@ def unfollow_user(username, v):
def remove_follow(username, v):
target = get_user(username)
follow = g.db.query(Follow).filter_by(user_id=target.id, target_id=v.id).first()
follow = g.db.query(Follow).filter_by(user_id=target.id, target_id=v.id).one_or_none()
if not follow: return {"message": "Follower removed!"}
@ -953,7 +953,7 @@ def fp(v, fp):
users += g.db.query(User).filter(User.email == v.email, User.is_activated, User.id != v.id).all()
for u in users:
li = [v.id, u.id]
existing = g.db.query(Alt).filter(Alt.user1.in_(li), Alt.user2.in_(li)).first()
existing = g.db.query(Alt).filter(Alt.user1.in_(li), Alt.user2.in_(li)).one_or_none()
if existing: continue
new_alt = Alt(user1=v.id, user2=u.id)
g.db.add(new_alt)

View File

@ -81,7 +81,7 @@ def api_vote_post(post_id, new, v):
post = get_post(post_id)
existing = g.db.query(Vote).filter_by(user_id=v.id, submission_id=post.id).first()
existing = g.db.query(Vote).filter_by(user_id=v.id, submission_id=post.id).one_or_none()
if existing and existing.vote_type == new: return "", 204
@ -147,7 +147,7 @@ def api_vote_comment(comment_id, new, v):
if comment.author_id == AUTOBETTER_ID: return {"error": "forbidden."}, 403
existing = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id).first()
existing = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id).one_or_none()
if existing and existing.vote_type == new: return "", 204
@ -205,7 +205,7 @@ def api_vote_poll(comment_id, v):
comment_id = int(comment_id)
comment = get_comment(comment_id)
existing = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id).first()
existing = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id).one_or_none()
if existing and existing.vote_type == new: return "", 204
@ -239,7 +239,7 @@ def bet(comment_id, v):
comment_id = int(comment_id)
comment = get_comment(comment_id)
existing = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id).first()
existing = g.db.query(CommentVote).filter_by(user_id=v.id, comment_id=comment.id).one_or_none()
if existing: return "", 204
vote = CommentVote(user_id=v.id, vote_type=1, comment_id=comment.id)
@ -250,7 +250,7 @@ def bet(comment_id, v):
v.coins -= 200
g.db.add(v)
autobetter = g.db.query(User).filter_by(id=AUTOBETTER_ID).first()
autobetter = g.db.query(User).filter_by(id=AUTOBETTER_ID).one_or_none()
autobetter.coins += 200
g.db.add(autobetter)