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:
parent
347baaa12e
commit
b2a418982e
1 changed files with 13 additions and 11 deletions
24
routes.py
24
routes.py
|
@ -457,37 +457,40 @@ def settings():
|
|||
name = room.get('name', '').strip()
|
||||
raw_section = room.get('section_id')
|
||||
raw_function = room.get('function_id')
|
||||
|
||||
|
||||
if not name:
|
||||
print(f"⚠️ Skipping room at index {idx} due to missing name.")
|
||||
continue
|
||||
|
||||
|
||||
try:
|
||||
section_id = int(raw_section) if raw_section else None
|
||||
function_id = int(raw_function) if raw_function else None
|
||||
|
||||
section_id = int(raw_section) if raw_section is not None else None
|
||||
function_id = int(raw_function) if raw_function is not None else None
|
||||
|
||||
# Resolve negative or unmapped IDs
|
||||
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)
|
||||
if not section_id:
|
||||
raise ValueError(f"Unresolved section: {section_name}")
|
||||
|
||||
|
||||
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)
|
||||
if not function_id:
|
||||
raise ValueError(f"Unresolved function: {function_name}")
|
||||
|
||||
|
||||
print(f"🏗️ Creating room '{name}' with section ID {section_id} and function ID {function_id}")
|
||||
new_room = Room(name=name, area_id=section_id, function_id=function_id) # type: ignore
|
||||
db.session.add(new_room)
|
||||
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to process room at index {idx}: {e}")
|
||||
traceback.print_exc()
|
||||
flash(f"Error processing room '{name}': {e}", "danger")
|
||||
|
||||
|
||||
# === 4. Commit changes ===
|
||||
try:
|
||||
print("🚀 Attempting commit...")
|
||||
|
@ -509,4 +512,3 @@ def settings():
|
|||
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()
|
||||
return render_template('settings.html', title="Settings", brands=brands, types=types, sections=sections, functions=functions, rooms=rooms)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue