04/22 Upgraded code commit
This commit is contained in:
@@ -427,18 +427,21 @@ def get_website_by_id(website_id: int):
|
||||
conn.close()
|
||||
|
||||
|
||||
def create_website(admin_id, name, url, check_type, note, credentials: list):
|
||||
def create_website(admin_id, name, url, check_type, note, credentials: list,
|
||||
visibility: str = "all", assigned_user_ids: list = None):
|
||||
"""
|
||||
credentials: list of dicts with keys: username, password, label
|
||||
check_type: 'daily' or 'weekly'
|
||||
check_type: 'daily' or 'weekly'
|
||||
visibility: 'all' (all users) or 'assigned' (only website_users)
|
||||
"""
|
||||
conn = None
|
||||
try:
|
||||
conn = get_connection()
|
||||
cur = conn.cursor()
|
||||
cur.execute(
|
||||
"INSERT INTO websites (name, url, check_type, note, created_by) VALUES (%s,%s,%s,%s,%s)",
|
||||
(name, url, check_type, note, admin_id)
|
||||
"INSERT INTO websites (name, url, check_type, visibility, note, created_by) "
|
||||
"VALUES (%s,%s,%s,%s,%s,%s)",
|
||||
(name, url, check_type, visibility, note, admin_id)
|
||||
)
|
||||
conn.commit()
|
||||
new_id = cur.lastrowid
|
||||
@@ -451,17 +454,29 @@ def create_website(admin_id, name, url, check_type, note, credentials: list):
|
||||
log_action(admin_id, "ADD_CREDENTIAL", "website_credentials", new_id,
|
||||
f"Added credential label='{cred.get('label','')}' "
|
||||
f"user='{cred['username']}' for website '{name}'.")
|
||||
|
||||
# Assign specific users if visibility='assigned'
|
||||
if visibility == "assigned" and assigned_user_ids:
|
||||
for uid in assigned_user_ids:
|
||||
cur.execute(
|
||||
"INSERT IGNORE INTO website_users (website_id, user_id) VALUES (%s,%s)",
|
||||
(new_id, uid)
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
cur.close()
|
||||
log_action(admin_id, "CREATE_WEBSITE", "websites", new_id,
|
||||
f"Created website '{name}' check_type='{check_type}'.")
|
||||
f"Created website '{name}' check_type='{check_type}' "
|
||||
f"visibility='{visibility}'.")
|
||||
return new_id
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
|
||||
def update_website(admin_id, website_id, name, url, check_type, note, credentials: list):
|
||||
def update_website(admin_id, website_id, name, url, check_type, note,
|
||||
credentials: list, visibility: str = "all",
|
||||
assigned_user_ids: list = None):
|
||||
conn = None
|
||||
try:
|
||||
conn = get_connection()
|
||||
@@ -475,8 +490,9 @@ def update_website(admin_id, website_id, name, url, check_type, note, credential
|
||||
old_creds = {(r["username"], r["label"] or "") for r in cur.fetchall()}
|
||||
|
||||
cur.execute(
|
||||
"UPDATE websites SET name=%s, url=%s, check_type=%s, note=%s WHERE id=%s",
|
||||
(name, url, check_type, note, website_id)
|
||||
"UPDATE websites SET name=%s, url=%s, check_type=%s, "
|
||||
"visibility=%s, note=%s WHERE id=%s",
|
||||
(name, url, check_type, visibility, note, website_id)
|
||||
)
|
||||
# Replace credentials
|
||||
cur.execute("DELETE FROM website_credentials WHERE website_id=%s", (website_id,))
|
||||
@@ -488,6 +504,15 @@ def update_website(admin_id, website_id, name, url, check_type, note, credential
|
||||
)
|
||||
new_creds.add((cred["username"], cred.get("label", "") or ""))
|
||||
|
||||
# Replace assigned users
|
||||
cur.execute("DELETE FROM website_users WHERE website_id=%s", (website_id,))
|
||||
if visibility == "assigned" and assigned_user_ids:
|
||||
for uid in assigned_user_ids:
|
||||
cur.execute(
|
||||
"INSERT IGNORE INTO website_users (website_id, user_id) VALUES (%s,%s)",
|
||||
(website_id, uid)
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
@@ -504,7 +529,8 @@ def update_website(admin_id, website_id, name, url, check_type, note, credential
|
||||
f"from website id={website_id}.")
|
||||
|
||||
log_action(admin_id, "UPDATE_WEBSITE", "websites", website_id,
|
||||
f"Updated website id={website_id} check_type='{check_type}'.")
|
||||
f"Updated website id={website_id} check_type='{check_type}' "
|
||||
f"visibility='{visibility}'.")
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
@@ -544,6 +570,30 @@ def get_website_credentials(website_id: int):
|
||||
conn.close()
|
||||
|
||||
|
||||
def get_website_assigned_users(website_id: int):
|
||||
"""Return users explicitly assigned to a website (visibility='assigned')."""
|
||||
conn = None
|
||||
try:
|
||||
conn = get_connection()
|
||||
cur = conn.cursor(dictionary=True)
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT u.id, u.username, u.full_name
|
||||
FROM website_users wu
|
||||
JOIN users u ON u.id = wu.user_id
|
||||
WHERE wu.website_id = %s
|
||||
ORDER BY u.username
|
||||
""",
|
||||
(website_id,)
|
||||
)
|
||||
rows = cur.fetchall()
|
||||
cur.close()
|
||||
return rows
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
|
||||
# ─── Shift Check CRUD ─────────────────────────────────────────────────────────
|
||||
|
||||
def get_today_checks(user_id: int):
|
||||
@@ -617,7 +667,7 @@ def get_today_checks(user_id: int):
|
||||
(user_id, user_id, user_id)
|
||||
)
|
||||
else:
|
||||
# Legacy fallback: show all active websites (original behaviour)
|
||||
# Legacy fallback: show active websites, filtered by visibility
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT
|
||||
@@ -638,6 +688,13 @@ def get_today_checks(user_id: int):
|
||||
AND sc.user_id = %s
|
||||
AND DATE(sc.checked_at) = CURDATE()
|
||||
WHERE w.is_active = 1
|
||||
AND (
|
||||
w.visibility = 'all'
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM website_users wu
|
||||
WHERE wu.website_id = w.id AND wu.user_id = %s
|
||||
)
|
||||
)
|
||||
AND (
|
||||
w.check_type = 'daily'
|
||||
OR (
|
||||
@@ -652,7 +709,7 @@ def get_today_checks(user_id: int):
|
||||
)
|
||||
ORDER BY w.name
|
||||
""",
|
||||
(user_id, user_id)
|
||||
(user_id, user_id, user_id)
|
||||
)
|
||||
|
||||
rows = cur.fetchall()
|
||||
|
||||
Reference in New Issue
Block a user