Add docstrings to user database functions for improved documentation and clarity

This commit is contained in:
voldemort 2025-07-23 01:40:51 +07:00
parent 1ef842978a
commit 29be40ab95

View File

@ -1,9 +1,12 @@
"""Module to manage the user database."""
import sqlite3 import sqlite3
import os import os
import bcrypt import bcrypt
def create_user_database(): def create_user_database():
"""Create the user database."""
os.makedirs(f"{os.getcwd()}/databases/sql", exist_ok=True) os.makedirs(f"{os.getcwd()}/databases/sql", exist_ok=True)
with sqlite3.connect(f"{os.getcwd()}/databases/sql/users.db") as conn: with sqlite3.connect(f"{os.getcwd()}/databases/sql/users.db") as conn:
@ -21,6 +24,7 @@ def create_user_database():
def add_user(username, password, api_key): def add_user(username, password, api_key):
"""Add a user to the database."""
hashed_pw = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()) hashed_pw = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt())
with sqlite3.connect(f"{os.getcwd()}/databases/sql/users.db") as conn: with sqlite3.connect(f"{os.getcwd()}/databases/sql/users.db") as conn:
@ -37,6 +41,7 @@ def add_user(username, password, api_key):
def verify_user(username, password): def verify_user(username, password):
"""Verify a user."""
with sqlite3.connect(f"{os.getcwd()}/databases/sql/users.db") as conn: with sqlite3.connect(f"{os.getcwd()}/databases/sql/users.db") as conn:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute( cursor.execute(
@ -55,6 +60,7 @@ def verify_user(username, password):
def fetch_api_key(username): def fetch_api_key(username):
"""Fetch the API key for a user."""
with sqlite3.connect(f"{os.getcwd()}/databases/sql/users.db") as conn: with sqlite3.connect(f"{os.getcwd()}/databases/sql/users.db") as conn:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute( cursor.execute(
@ -69,7 +75,7 @@ def fetch_api_key(username):
def change_password(username, new_password): def change_password(username, new_password):
"""Change the password for a user."""
# Hash the new password # Hash the new password
new_hashed_pw = bcrypt.hashpw(new_password.encode("utf-8"), bcrypt.gensalt()) new_hashed_pw = bcrypt.hashpw(new_password.encode("utf-8"), bcrypt.gensalt())
@ -85,6 +91,7 @@ def change_password(username, new_password):
def change_api_key(username, new_api_key): def change_api_key(username, new_api_key):
"""Change the API key for a user."""
# Update the API key in the database # Update the API key in the database
with sqlite3.connect(f"{os.getcwd()}/databases/sql/users.db") as conn: with sqlite3.connect(f"{os.getcwd()}/databases/sql/users.db") as conn:
cursor = conn.cursor() cursor = conn.cursor()
@ -97,6 +104,7 @@ def change_api_key(username, new_api_key):
def fetch_styled_username(username): def fetch_styled_username(username):
"""Fetch the styled username for a user."""
with sqlite3.connect(f"{os.getcwd()}/databases/sql/users.db") as conn: with sqlite3.connect(f"{os.getcwd()}/databases/sql/users.db") as conn:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute( cursor.execute(
@ -112,6 +120,7 @@ def fetch_styled_username(username):
def fetch_username_by_api_key(api_key): def fetch_username_by_api_key(api_key):
"""Fetch the username for a user by API key."""
with sqlite3.connect(f"{os.getcwd()}/databases/sql/users.db") as conn: with sqlite3.connect(f"{os.getcwd()}/databases/sql/users.db") as conn:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute("SELECT Username FROM user_info WHERE API_Key = ?", (api_key,)) cursor.execute("SELECT Username FROM user_info WHERE API_Key = ?", (api_key,))