MAJOR: Restructured backend Flask application moved all non-routes into utils, renamed routes to not prefix get, created middleware.py to replace utils.py within blueprints
This commit is contained in:
75
web_server/utils/utils.py
Normal file
75
web_server/utils/utils.py
Normal file
@@ -0,0 +1,75 @@
|
||||
from database.database import Database
|
||||
from typing import Optional, List
|
||||
from re import match
|
||||
|
||||
def get_all_categories() -> Optional[List[dict]]:
|
||||
"""
|
||||
Returns all possible streaming categories
|
||||
"""
|
||||
with Database() as db:
|
||||
all_categories = db.fetchall("SELECT * FROM categories")
|
||||
|
||||
return all_categories
|
||||
|
||||
def get_all_tags() -> Optional[List[dict]]:
|
||||
"""
|
||||
Returns all possible streaming tags
|
||||
"""
|
||||
with Database() as db:
|
||||
all_tags = db.fetchall("SELECT * FROM tags")
|
||||
|
||||
return all_tags
|
||||
|
||||
def get_most_popular_category() -> Optional[List[dict]]:
|
||||
"""
|
||||
Returns the most popular category based on live stream viewers
|
||||
"""
|
||||
with Database() as db:
|
||||
category = db.fetchone("""
|
||||
SELECT categories.category_id, categories.category_name
|
||||
FROM streams
|
||||
JOIN categories ON streams.category_id = categories.category_id
|
||||
WHERE streams.isLive = 1
|
||||
GROUP BY categories.category_name
|
||||
ORDER BY SUM(streams.num_viewers) DESC
|
||||
LIMIT 1;
|
||||
""")
|
||||
|
||||
return category
|
||||
|
||||
def sanitize(user_input: str, input_type="username") -> str:
|
||||
"""
|
||||
Sanitizes user input based on the specified input type.
|
||||
|
||||
`input_type`: The type of input to sanitize (e.g., 'username', 'email', 'password').
|
||||
"""
|
||||
# Strip leading and trailing whitespace
|
||||
sanitised_input = user_input.strip()
|
||||
|
||||
# Define allowed patterns and length constraints for each type
|
||||
rules = {
|
||||
"username": {
|
||||
"pattern": r"^[a-zA-Z0-9_]+$", # Alphanumeric + underscores
|
||||
"min_length": 3,
|
||||
"max_length": 50,
|
||||
},
|
||||
"email": {
|
||||
"pattern": r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$", # Standard email regex
|
||||
"min_length": 5,
|
||||
"max_length": 128,
|
||||
},
|
||||
"password": {
|
||||
"pattern": r"^[\S]+$", # Non-whitespace characters only
|
||||
"min_length": 8,
|
||||
"max_length": 256,
|
||||
},
|
||||
}
|
||||
|
||||
# Get the validation rules for the specified type
|
||||
r = rules.get(input_type)
|
||||
if not r or \
|
||||
not (r["min_length"] <= len(sanitised_input) <= r["max_length"]) or \
|
||||
not match(r["pattern"], sanitised_input):
|
||||
raise ValueError("Unaccepted character or length in input")
|
||||
|
||||
return sanitised_input
|
||||
Reference in New Issue
Block a user