Files
Inv_web/handlers/projectors.py
2026-03-29 17:05:48 +03:00

270 lines
10 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from flask import flash, redirect, render_template, request, url_for
def projectors_update_cabinet(*, get_conn, log_equipment_movement):
pid = request.form.get("id", "").strip()
cabinet_id = request.form.get("cabinet_id", "").strip()
if not pid.isdigit():
flash("Некорректное устройство")
return redirect(request.referrer or url_for("index"))
cab_id = int(cabinet_id) if cabinet_id.isdigit() else None
conn = get_conn()
cur = conn.cursor()
cur.execute("SELECT inventory_number, cabinet_id FROM projectors WHERE id=%s", (int(pid),))
row = cur.fetchone()
inv_num = row[0] if row else None
old_cab_id = row[1] if row else None
cur.execute("UPDATE projectors SET cabinet_id=%s WHERE id=%s", (cab_id, int(pid)))
conn.commit()
conn.close()
if inv_num and old_cab_id != cab_id:
log_equipment_movement(inv_num, "projector", old_cab_id, cab_id)
flash("Расположение обновлено")
return redirect(request.referrer or url_for("index"))
def projectors_list(*, get_conn, projector_kit_types):
conn = get_conn()
cur = conn.cursor()
cur.execute(
"""
SELECT p.id, p.inventory_number, p.kit_type, p.brand, p.projector_model,
p.projector_serial, p.board_model, p.board_serial, p.projector_inventory_number,
p.board_inventory_number, p.computer_inventory_number,
p.computer_id, c.inventory_number,
c.brand, c.model, c.type, p.cabinet_id, cab.name AS cabinet_name,
p.date_in_operation, p.note, p.date_added
FROM projectors p
LEFT JOIN computers c ON c.id = p.computer_id
LEFT JOIN cabinets cab ON cab.id = p.cabinet_id
ORDER BY p.date_added DESC, p.id DESC
"""
)
rows = cur.fetchall()
total_items = len(rows)
kits_count = 0
projectors_count = 0
tv_count = 0
boards_count = 0
interactive_display_count = 0
for row in rows:
kit_type = (row[2] or "").strip().lower()
if kit_type == "kit":
kits_count += 1
projectors_count += 1
boards_count += 1
elif kit_type == "projector":
projectors_count += 1
elif kit_type == "board":
boards_count += 1
elif kit_type == "tv":
tv_count += 1
elif kit_type == "display":
interactive_display_count += 1
cur.execute("SELECT id, name FROM cabinets ORDER BY name")
cabinets = cur.fetchall()
cur.execute(
"""
SELECT id, inventory_number, brand, model, type
FROM computers
ORDER BY inventory_number, brand, model
"""
)
computers = cur.fetchall()
cur.execute(
"""
SELECT DISTINCT brand
FROM projectors
WHERE brand IS NOT NULL AND brand <> ''
ORDER BY brand
"""
)
projector_brands = [row[0] for row in cur.fetchall()]
conn.close()
return render_template(
"projectors.html",
rows=rows,
cabinets=cabinets,
computers=computers,
kit_types=projector_kit_types,
kit_type_labels=dict(projector_kit_types),
projector_brands=projector_brands,
total_items=total_items,
kits_count=kits_count,
projectors_count=projectors_count,
tv_count=tv_count,
boards_count=boards_count,
interactive_display_count=interactive_display_count,
)
def projectors_add(*, get_conn, projector_kit_types, normalize_board_fields):
inventory_number = request.form.get("inventory_number", "").strip()
kit_type = request.form.get("kit_type", "").strip()
brand = request.form.get("brand", "").strip()
projector_model = request.form.get("projector_model", "").strip()
projector_serial = request.form.get("projector_serial", "").strip()
board_model = request.form.get("board_model", "").strip()
board_serial = request.form.get("board_serial", "").strip()
projector_inventory_number = request.form.get("projector_inventory_number", "").strip()
board_inventory_number = request.form.get("board_inventory_number", "").strip()
computer_inventory_number = request.form.get("computer_inventory_number", "").strip()
computer_id = request.form.get("computer_id", "").strip()
cabinet_id = request.form.get("cabinet_id", "").strip()
date_in_operation = request.form.get("date_in_operation", "").strip()
note = request.form.get("note", "").strip()
valid_kit_types = {key for key, _ in projector_kit_types}
if not inventory_number:
flash("Укажите инвентарный номер")
return redirect(url_for("projectors"))
if kit_type not in valid_kit_types:
flash("Укажите тип комплекта")
return redirect(url_for("projectors"))
projector_model, projector_serial, board_model, board_serial = normalize_board_fields(
kit_type,
projector_model,
projector_serial,
board_model,
board_serial,
)
comp_id = int(computer_id) if computer_id.isdigit() else None
cab_id = int(cabinet_id) if cabinet_id.isdigit() else None
conn = get_conn()
cur = conn.cursor()
try:
cur.execute(
"""
INSERT INTO projectors (
inventory_number, kit_type, brand, projector_model, projector_serial,
board_model, board_serial, projector_inventory_number, board_inventory_number,
computer_inventory_number, computer_id, cabinet_id, date_in_operation, note
)
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
""",
(
inventory_number,
kit_type,
brand or None,
projector_model or None,
projector_serial or None,
board_model or None,
board_serial or None,
projector_inventory_number or None,
board_inventory_number or None,
computer_inventory_number or None,
comp_id,
cab_id,
date_in_operation or None,
note or None,
),
)
conn.commit()
flash("Устройство добавлено")
except Exception:
conn.rollback()
flash("Не удалось добавить устройство")
finally:
conn.close()
return redirect(url_for("projectors"))
def projectors_edit(*, get_conn, projector_kit_types, normalize_board_fields, log_equipment_movement):
pid = request.form.get("id", "").strip()
inventory_number = request.form.get("inventory_number", "").strip()
kit_type = request.form.get("kit_type", "").strip()
brand = request.form.get("brand", "").strip()
projector_model = request.form.get("projector_model", "").strip()
projector_serial = request.form.get("projector_serial", "").strip()
board_model = request.form.get("board_model", "").strip()
board_serial = request.form.get("board_serial", "").strip()
projector_inventory_number = request.form.get("projector_inventory_number", "").strip()
board_inventory_number = request.form.get("board_inventory_number", "").strip()
computer_inventory_number = request.form.get("computer_inventory_number", "").strip()
computer_id = request.form.get("computer_id", "").strip()
cabinet_id = request.form.get("cabinet_id", "").strip()
date_in_operation = request.form.get("date_in_operation", "").strip()
note = request.form.get("note", "").strip()
if not pid.isdigit():
flash("Некорректное устройство")
return redirect(url_for("projectors"))
if not inventory_number:
flash("Укажите инвентарный номер")
return redirect(url_for("projectors"))
valid_kit_types = {key for key, _ in projector_kit_types}
if kit_type not in valid_kit_types:
flash("Укажите тип комплекта")
return redirect(url_for("projectors"))
projector_model, projector_serial, board_model, board_serial = normalize_board_fields(
kit_type,
projector_model,
projector_serial,
board_model,
board_serial,
)
comp_id = int(computer_id) if computer_id.isdigit() else None
cab_id = int(cabinet_id) if cabinet_id.isdigit() else None
conn = get_conn()
cur = conn.cursor()
try:
cur.execute("SELECT cabinet_id FROM projectors WHERE id=%s", (int(pid),))
row = cur.fetchone()
old_cab_id = row[0] if row else None
cur.execute(
"""
UPDATE projectors
SET inventory_number=%s, kit_type=%s, brand=%s, projector_model=%s,
projector_serial=%s, board_model=%s, board_serial=%s, projector_inventory_number=%s,
board_inventory_number=%s, computer_inventory_number=%s,
computer_id=%s, cabinet_id=%s, date_in_operation=%s, note=%s
WHERE id=%s
""",
(
inventory_number,
kit_type,
brand or None,
projector_model or None,
projector_serial or None,
board_model or None,
board_serial or None,
projector_inventory_number or None,
board_inventory_number or None,
computer_inventory_number or None,
comp_id,
cab_id,
date_in_operation or None,
note or None,
int(pid),
),
)
conn.commit()
if old_cab_id != cab_id:
log_equipment_movement(inventory_number, "projector", old_cab_id, cab_id)
flash("Устройство обновлено")
except Exception:
conn.rollback()
flash("Не удалось обновить устройство")
finally:
conn.close()
return redirect(url_for("projectors"))
def projectors_delete(*, get_conn):
pid = request.form.get("id", "").strip()
if not pid.isdigit():
flash("Некорректное устройство")
return redirect(url_for("projectors"))
conn = get_conn()
cur = conn.cursor()
cur.execute("DELETE FROM projectors WHERE id=%s", (int(pid),))
conn.commit()
conn.close()
flash("Устройство удалено")
return redirect(url_for("projectors"))