Update: remove unused imports, added better comments and refactored for all blueprints

This commit is contained in:
white
2025-01-27 12:49:42 +00:00
parent 3dc44da69a
commit 4e9fa011fa
8 changed files with 96 additions and 74 deletions

View File

@@ -3,11 +3,16 @@ from functools import wraps
from re import match
def logged_in_user():
"""
Validator to make sure a user is logged in.
"""
g.user = session.get("username", None)
g.admin = session.get("username", None)
def login_required(view):
"""add at start of routes where users need to be logged in to access"""
"""
Add at start of routes where users need to be logged in to access.
"""
@wraps(view)
def wrapped_view(*args, **kwargs):
if g.user is None:
@@ -16,7 +21,9 @@ def login_required(view):
return wrapped_view
def admin_required(view):
"""add at start of routes where admins need to be logged in to access"""
"""
Add at start of routes where admins need to be logged in to access.
"""
@wraps(view)
def wrapped_view(*args, **kwargs):
if g.admin != "admin":
@@ -24,8 +31,6 @@ def admin_required(view):
return view(*args, **kwargs)
return wrapped_view
import re
def sanitizer(user_input: str, input_type="username") -> str:
"""
Sanitizes user input based on the specified input type.
@@ -58,7 +63,7 @@ def sanitizer(user_input: str, input_type="username") -> str:
r = rules.get(input_type)
if not r or \
not (r["min_length"] <= len(sanitised_input) <= r["max_length"]) or \
not re.match(r["pattern"], sanitised_input):
not match(r["pattern"], sanitised_input):
raise ValueError("Unaccepted character or length in input")
return sanitised_input