use get_user whenever possible instead of repeating code

master
Aevann 2023-09-27 00:57:33 +03:00
parent 88d77fd6c1
commit c3870505ea
3 changed files with 10 additions and 30 deletions

View File

@ -377,15 +377,11 @@ def post_forgot():
if not email_regex.fullmatch(email):
return render_template("login/forgot_password.html", error="Invalid email!"), 400
username = username.lstrip('@').replace('\\', '').replace('_', '\_').replace('%', '').strip()
user = get_user(username, graceful=True)
email = email.replace('\\', '').replace('_', '\_').replace('%', '').strip()
user = g.db.query(User).filter(
User.username.ilike(username),
User.email.ilike(email)).one_or_none()
if user:
if user and user.email.lower() == email.lower():
now = int(time.time())
token = generate_hash(f"{user.id}+{now}+forgot+{user.login_nonce}")
url = f"{SITE_FULL}/reset?id={user.id}&time={now}&token={token}"

View File

@ -732,7 +732,7 @@ def settings_name_change(v):
new_name = request.values.get("name").strip()
if new_name==v.username:
if new_name == v.username:
abort(400, "You didn't change anything")
if v.patron:
@ -743,17 +743,9 @@ def settings_name_change(v):
if not used_regex.fullmatch(new_name):
abort(400, "This isn't a valid username.")
search_name = new_name.replace('\\', '').replace('_','\_').replace('%','')
existing = get_user(new_name, graceful=True)
x = g.db.query(User).filter(
or_(
User.username.ilike(search_name),
User.original_username.ilike(search_name),
User.prelock_username.ilike(search_name),
)
).one_or_none()
if x and x.id != v.id:
if existing and existing.id != v.id:
abort(400, f"Username `{new_name}` is already in use.")
v.username = new_name

View File

@ -710,20 +710,12 @@ def is_available(name):
name = name.strip()
if len(name)<3 or len(name)>25:
return {name:False}
if len(name) < 3 or len(name) > 25:
return {name: False}
name2 = name.replace('\\', '').replace('_','\_').replace('%','')
existing = get_user(name, graceful=True)
x = g.db.query(User).filter(
or_(
User.username.ilike(name2),
User.original_username.ilike(name2),
User.prelock_username.ilike(name2),
)
).one_or_none()
if x:
if existing:
return {name: False}
else:
return {name: True}