alpha v0.938
This commit is contained in:
1
routes/__init__.py
Normal file
1
routes/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# Route modules package.
|
||||
BIN
routes/__pycache__/__init__.cpython-314.pyc
Normal file
BIN
routes/__pycache__/__init__.cpython-314.pyc
Normal file
Binary file not shown.
BIN
routes/__pycache__/projectors.cpython-314.pyc
Normal file
BIN
routes/__pycache__/projectors.cpython-314.pyc
Normal file
Binary file not shown.
280
routes/projectors.py
Normal file
280
routes/projectors.py
Normal file
@@ -0,0 +1,280 @@
|
||||
from flask import flash, redirect, render_template, request, url_for
|
||||
|
||||
|
||||
def _normalize_board_fields(
|
||||
kit_type,
|
||||
projector_model,
|
||||
projector_serial,
|
||||
board_model,
|
||||
board_serial,
|
||||
):
|
||||
if kit_type != "board":
|
||||
return projector_model, projector_serial, board_model, board_serial
|
||||
if not board_model and projector_model:
|
||||
board_model = projector_model
|
||||
if not projector_model and board_model:
|
||||
projector_model = board_model
|
||||
if not board_serial and projector_serial:
|
||||
board_serial = projector_serial
|
||||
if not projector_serial and board_serial:
|
||||
projector_serial = board_serial
|
||||
return projector_model, projector_serial, board_model, board_serial
|
||||
|
||||
|
||||
def register_projector_routes(
|
||||
app,
|
||||
*,
|
||||
get_conn,
|
||||
login_required,
|
||||
admin_required,
|
||||
log_equipment_movement,
|
||||
projector_kit_types,
|
||||
):
|
||||
@app.route("/projectors/update_cabinet", methods=["POST"])
|
||||
@admin_required
|
||||
def projectors_update_cabinet():
|
||||
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"))
|
||||
|
||||
@app.route("/projectors")
|
||||
@login_required
|
||||
def projectors():
|
||||
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()
|
||||
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,
|
||||
)
|
||||
|
||||
@app.route("/projectors/add", methods=["POST"])
|
||||
@admin_required
|
||||
def projectors_add():
|
||||
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"))
|
||||
|
||||
@app.route("/projectors/edit", methods=["POST"])
|
||||
@admin_required
|
||||
def projectors_edit():
|
||||
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"))
|
||||
|
||||
@app.route("/projectors/delete", methods=["POST"])
|
||||
@admin_required
|
||||
def projectors_delete():
|
||||
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"))
|
||||
Reference in New Issue
Block a user