wcag_AI_validation/UI/dependences_ui/utils.py

170 lines
4.5 KiB
Python

import hashlib
import json
import os
import gradio as gr
# File to store user credentials
USERS_FILE = "users.json"
def load_users():
"""Load users from JSON file"""
if os.path.exists(USERS_FILE):
with open(USERS_FILE, "r") as f:
return json.load(f)
return {}
def save_users(users):
"""Save users to JSON file"""
with open(USERS_FILE, "w") as f:
json.dump(users, f)
def hash_password(password):
"""Hash password using SHA-256"""
return hashlib.sha256(password.encode()).hexdigest()
def associate_user_with_manager(users, user_assignment_manager):
user_list = users.keys()
print(f"registering--Associating users with manager: {list(user_list)}")
user_assignment_manager.register_active_users(list(user_list))
def register_user(username, password, confirm_password, user_assignment_manager):
"""Register a new user"""
if not username or not password:
return "", "Username and password cannot be empty!", None
if password != confirm_password:
return "", "Passwords do not match!", None
if len(password) < 6:
return "", "Password must be at least 6 characters long!", None
users = load_users()
if username in users:
return "", "Username already exists!", None
users[username] = hash_password(password)
save_users(users)
try:
associate_user_with_manager(users, user_assignment_manager)
except Exception as e:
print(f"Error associating user with manager: {e}")
return "", f"✅ Registration successful! You can now login.", None
def login_user(username, password, state):
"""Validate user login"""
if not username or not password:
return (
"Please enter both username and password!",
"",
state,
gr.update(visible=True),
gr.update(visible=False),
gr.update(visible=False),
gr.update(open=True),
)
users = load_users()
if username not in users:
return (
"Invalid username or password!",
"",
state,
gr.update(visible=True),
gr.update(visible=False),
gr.update(visible=False),
gr.update(open=True),
)
if users[username] != hash_password(password):
return (
"Invalid username or password!",
"",
state,
gr.update(visible=True),
gr.update(visible=False),
gr.update(visible=False),
gr.update(open=True),
)
# Login successful
state = {"logged_in": True, "username": username}
return (
f"✅ Welcome back, {username}!",
"",
state,
gr.update(visible=False),
gr.update(visible=True),
gr.update(visible=True),
gr.update(open=False),
)
def logout_user(state):
"""Logout current user"""
state = {"logged_in": False, "username": None}
return (
"Logged out successfully!",
state,
gr.update(visible=True),
gr.update(visible=False),
gr.update(visible=False),
)
def protected_content(state):
"""Content only accessible to logged-in users"""
if state.get("logged_in"):
return f"You are logged as {state.get('username')}\n"
return "Please login to access this content."
def get_user_assessments_done(connection_db, username):
"""
it returns:
{
"https://example.com/page1": [1, 3, 5],
"https://example.com/page2": [2, 4, 6],
}
"""
cursor = connection_db.cursor()
username = json.dumps({"username": username}, ensure_ascii=False)
cursor.execute(
"""
SELECT page_url, json_output_data
FROM wcag_user_assessments
WHERE user = ? AND insert_type = ?
ORDER BY page_url
""",
(username, "wcag_user_llm_alttext_assessments"),
)
rows = cursor.fetchall()
assessment_done = {} # dict: {page_url: sorted list of image numbers}
for row in rows:
page_url = row[0]
data = json.loads(row[1])
image_numbers = {int(item["Image #"]) for item in data} # set to deduplicate
if page_url not in assessment_done:
assessment_done[page_url] = image_numbers
else:
assessment_done[page_url].update(
image_numbers
) # merge if url appears multiple times
# Convert sets to sorted lists
assessment_done = {url: sorted(imgs) for url, imgs in assessment_done.items()}
return assessment_done