Add user creation and update functionality; refactor user template and helpers

This commit is contained in:
Yaro Kasear 2025-07-07 15:36:15 -05:00
parent f4369348c5
commit 26e55b9a3e
6 changed files with 134 additions and 24 deletions

View file

@ -19,9 +19,7 @@ inventory_headers = {
}
checked_box = '''
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-check2" viewBox="0 0 16 16">
<path d="M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0"/>
</svg>
<i class="bi bi-check2"></i>
'''
unchecked_box = ''

View file

@ -11,15 +11,10 @@ from ..utils.load import eager_load_room_relationships
@main.route('/settings', methods=['GET', 'POST'])
def settings():
if request.method == 'POST':
print("⚠️⚠️⚠️ POST /settings reached! ⚠️⚠️⚠️")
form = request.form
print("📝 Raw form payload:", form)
try:
state = json.loads(form['formState'])
import pprint
print("🧠 Parsed state:")
pprint.pprint(state, indent=2, width=120)
except Exception:
flash("Invalid form state submitted. JSON decode failed.", "danger")
traceback.print_exc()
@ -58,12 +53,9 @@ def settings():
function_map=function_map
)
print("✅ COMMIT executed.")
flash("Changes saved.", "success")
except Exception as e:
print("❌ COMMIT FAILED ❌")
traceback.print_exc()
flash(f"Error saving changes: {e}", "danger")
return redirect(url_for('main.settings'))

View file

@ -1,4 +1,4 @@
from flask import render_template
from flask import render_template, request, jsonify
from . import main
from .helpers import ACTIVE_STATUSES, user_headers, inventory_headers, worklog_headers
@ -63,4 +63,61 @@ def user(id):
worklog=worklog,
worklog_headers=filtered_worklog_headers,
worklog_rows=[{"id": log.id, "cells": [fn(log) for fn in filtered_worklog_headers.values()]} for log in worklog]
)
)
@main.route("/user/new", methods=["GET"])
def new_user():
rooms = eager_load_room_relationships(db.session.query(Room)).all()
users = eager_load_user_relationships(db.session.query(User)).all()
user = User(
active=True
)
return render_template(
"user.html",
title="New User",
user=user,
users=users,
rooms=rooms
)
@main.route("/api/user", methods=["POST"])
def create_user():
try:
data = request.get_json(force=True)
new_user = User.from_dict(data)
db.session.add(new_user)
db.session.commit()
return jsonify({"success": True, "id": new_user.id}), 201
except Exception as e:
db.session.rollback()
return jsonify({"success": False, "error": str(e)}), 400
@main.route("/api/user/<int:id>", methods=["PUT"])
def update_user(id):
try:
data = request.get_json(force=True)
user = db.session.query(User).get(id)
if not user:
return jsonify({"success": False, "error": f"User with ID {id} not found."}), 404
user.staff = bool(data.get("staff", user.staff))
user.active = bool(data.get("active", user.active))
user.last_name = data.get("last_name", user.last_name)
user.first_name = data.get("first_name", user.first_name)
user.location_id = data.get("location_id", user.location_id)
user.supervisor_id = data.get("supervisor_id", user.supervisor_id)
db.session.commit()
return jsonify({"success": True, "id": user.id}), 200
except Exception as e:
db.session.rollback()
return jsonify({"success": False, "error": str(e)}), 400