alpha v0.0977
This commit is contained in:
@@ -1,38 +1,56 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from flask import flash, redirect, render_template, request, url_for
|
||||
from flask import flash, jsonify, redirect, render_template, request, url_for
|
||||
|
||||
|
||||
def computers_manage_icon_label(*, get_conn, clean_text, safe_next_url):
|
||||
computer_id_raw = clean_text(request.form.get("computer_id"))
|
||||
icon_label = clean_text(request.form.get("icon_label"))
|
||||
next_url = safe_next_url(request.form.get("next"), fallback="index")
|
||||
if not computer_id_raw.isdigit():
|
||||
payload = request.get_json(silent=True) if request.is_json else request.form
|
||||
payload = payload or {}
|
||||
source_type = clean_text(payload.get("source_type")) or "computer"
|
||||
source_id_raw = clean_text(payload.get("source_id") or payload.get("computer_id"))
|
||||
icon_label = clean_text(payload.get("icon_label"))[:120]
|
||||
next_url = safe_next_url(payload.get("next"), fallback="index")
|
||||
|
||||
source_settings = {
|
||||
"computer": ("computers", "computer_manage_icons", "computer_id"),
|
||||
"client_test": ("client_test_devices", "client_manage_icons", "client_id"),
|
||||
}
|
||||
if source_type not in source_settings or not source_id_raw.isdigit():
|
||||
if request.is_json:
|
||||
return jsonify(ok=False, error="invalid computer source"), 400
|
||||
return redirect(next_url)
|
||||
|
||||
computer_id = int(computer_id_raw)
|
||||
source_id = int(source_id_raw)
|
||||
source_table, labels_table, source_column = source_settings[source_type]
|
||||
conn = get_conn()
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT 1 FROM computers WHERE id=%s", (computer_id,))
|
||||
cur.execute(f"SELECT 1 FROM {source_table} WHERE id=%s", (source_id,))
|
||||
exists = cur.fetchone() is not None
|
||||
if not exists:
|
||||
conn.close()
|
||||
if request.is_json:
|
||||
return jsonify(ok=False, error="computer not found"), 404
|
||||
return redirect(next_url)
|
||||
|
||||
if icon_label:
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO computer_manage_icons (computer_id, icon_label)
|
||||
f"""
|
||||
INSERT INTO {labels_table} ({source_column}, icon_label)
|
||||
VALUES (%s, %s)
|
||||
ON CONFLICT (computer_id)
|
||||
ON CONFLICT ({source_column})
|
||||
DO UPDATE SET icon_label = EXCLUDED.icon_label
|
||||
""",
|
||||
(computer_id, icon_label),
|
||||
(source_id, icon_label),
|
||||
)
|
||||
else:
|
||||
cur.execute("DELETE FROM computer_manage_icons WHERE computer_id=%s", (computer_id,))
|
||||
cur.execute(
|
||||
f"DELETE FROM {labels_table} WHERE {source_column}=%s",
|
||||
(source_id,),
|
||||
)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
if request.is_json:
|
||||
return jsonify(ok=True, icon_label=icon_label)
|
||||
return redirect(next_url)
|
||||
|
||||
|
||||
@@ -301,4 +319,3 @@ def computers_delete(*, get_conn):
|
||||
conn.close()
|
||||
flash("Компьютер удалён")
|
||||
return redirect(url_for("computers"))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user