Implement sync_from_state methods for Area, Brand, Item, RoomFunction, and Room models; enhance entity management and foreign key resolution in settings
This commit is contained in:
parent
8a5c5db9e0
commit
7833c4828b
9 changed files with 359 additions and 186 deletions
30
routes.py
30
routes.py
|
@ -412,21 +412,30 @@ def settings():
|
|||
|
||||
try:
|
||||
with db.session.begin():
|
||||
Brand.sync_from_state(state.get("brands", []))
|
||||
Item.sync_from_state(state.get("types", []))
|
||||
Area.sync_from_state(state.get("sections", []))
|
||||
RoomFunction.sync_from_state(state.get("functions", []))
|
||||
# Sync each table and grab temp ID maps
|
||||
brand_map = Brand.sync_from_state(state.get("brands", []))
|
||||
type_map = Item.sync_from_state(state.get("types", []))
|
||||
section_map = Area.sync_from_state(state.get("sections", []))
|
||||
function_map = RoomFunction.sync_from_state(state.get("functions", []))
|
||||
|
||||
# Refresh maps after inserts
|
||||
section_map = {a.name: a.id for a in db.session.query(Area).all()}
|
||||
function_map = {f.description: f.id for f in db.session.query(RoomFunction).all()}
|
||||
# Fix up room foreign keys based on real IDs
|
||||
submitted_rooms = []
|
||||
for room in state.get("rooms", []):
|
||||
room = dict(room) # shallow copy
|
||||
sid = room.get("section_id")
|
||||
fid = room.get("function_id")
|
||||
if sid in section_map:
|
||||
room["section_id"] = section_map[sid]
|
||||
if fid in function_map:
|
||||
room["function_id"] = function_map[fid]
|
||||
submitted_rooms.append(room)
|
||||
|
||||
Room.sync_from_state(
|
||||
submitted_rooms=state.get("rooms", []),
|
||||
submitted_rooms=submitted_rooms,
|
||||
section_map=section_map,
|
||||
function_map=function_map,
|
||||
section_fallbacks=state.get("sections", []),
|
||||
function_fallbacks=state.get("functions", [])
|
||||
section_fallbacks=[{"id": v, "name": k} for k, v in section_map.items()],
|
||||
function_fallbacks=[{"id": v, "name": k} for k, v in function_map.items()]
|
||||
)
|
||||
|
||||
print("✅ COMMIT executed.")
|
||||
|
@ -454,3 +463,4 @@ def settings():
|
|||
functions=[f.serialize() for f in functions],
|
||||
rooms=[r.serialize() for r in rooms],
|
||||
)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue