Refactor room creation logic; improve handling of section and function IDs, enhance error reporting, and streamline code for better readability

This commit is contained in:
Yaro Kasear 2025-06-23 10:22:39 -05:00
parent 347baaa12e
commit b2a418982e

View file

@ -463,18 +463,20 @@ def settings():
continue continue
try: try:
section_id = int(raw_section) if raw_section else None section_id = int(raw_section) if raw_section is not None else None
function_id = int(raw_function) if raw_function else None function_id = int(raw_function) if raw_function is not None else None
# Resolve negative or unmapped IDs # Resolve negative or unmapped IDs
if section_id is None or section_id < 0: if section_id is None or section_id < 0:
section_name = state['sections'][abs(section_id + 1)] section_idx = abs(section_id + 1) if section_id is not None else 0
section_name = state['sections'][section_idx]
section_id = section_map.get(section_name) section_id = section_map.get(section_name)
if not section_id: if not section_id:
raise ValueError(f"Unresolved section: {section_name}") raise ValueError(f"Unresolved section: {section_name}")
if function_id is None or function_id < 0: if function_id is None or function_id < 0:
function_name = state['functions'][abs(function_id + 1)] function_idx = abs(function_id + 1) if function_id is not None else 0
function_name = state['functions'][function_idx]
function_id = function_map.get(function_name) function_id = function_map.get(function_name)
if not function_id: if not function_id:
raise ValueError(f"Unresolved function: {function_name}") raise ValueError(f"Unresolved function: {function_name}")
@ -488,6 +490,7 @@ def settings():
traceback.print_exc() traceback.print_exc()
flash(f"Error processing room '{name}': {e}", "danger") flash(f"Error processing room '{name}': {e}", "danger")
# === 4. Commit changes === # === 4. Commit changes ===
try: try:
print("🚀 Attempting commit...") print("🚀 Attempting commit...")
@ -509,4 +512,3 @@ def settings():
functions = db.session.query(RoomFunction.id, RoomFunction.description.label("name")).order_by(RoomFunction.description).all() functions = db.session.query(RoomFunction.id, RoomFunction.description.label("name")).order_by(RoomFunction.description).all()
rooms = eager_load_room_relationships(db.session.query(Room).order_by(Room.name)).all() rooms = eager_load_room_relationships(db.session.query(Room).order_by(Room.name)).all()
return render_template('settings.html', title="Settings", brands=brands, types=types, sections=sections, functions=functions, rooms=rooms) return render_template('settings.html', title="Settings", brands=brands, types=types, sections=sections, functions=functions, rooms=rooms)