Implement sync_from_state method for Area, Brand, Item, RoomFunction, and Room models; streamline entity management in settings
This commit is contained in:
parent
87fa623cde
commit
8a5c5db9e0
11 changed files with 204 additions and 70 deletions
|
@ -45,3 +45,60 @@ class Room(db.Model):
|
|||
'function_id': self.function_id
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def sync_from_state(cls, submitted_rooms: list[dict], section_map: dict, function_map: dict, section_fallbacks: list, function_fallbacks: list):
|
||||
"""
|
||||
Syncs the Rooms table with the submitted room list.
|
||||
Resolves foreign keys using section_map and function_map.
|
||||
"""
|
||||
|
||||
def resolve_id(raw_id, fallback_list, id_map, label):
|
||||
try:
|
||||
resolved = int(raw_id)
|
||||
if resolved >= 0:
|
||||
if resolved in id_map.values():
|
||||
return resolved
|
||||
raise ValueError(f"ID {resolved} not found in {label}.")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
index = abs(resolved + 1)
|
||||
try:
|
||||
entry = fallback_list[index]
|
||||
key = entry.get("name") if isinstance(entry, dict) else str(entry).strip()
|
||||
final_id = id_map.get(key)
|
||||
if final_id is None:
|
||||
raise ValueError(f"ID for {key} not found in {label}.")
|
||||
return final_id
|
||||
except Exception as e:
|
||||
raise ValueError(f"Failed to resolve {label} ID: {e}") from e
|
||||
|
||||
existing_query = db.session.query(cls)
|
||||
existing = {room.name: room for room in existing_query.all()}
|
||||
|
||||
submitted = {
|
||||
str(room.get("name", "")).strip(): room
|
||||
for room in submitted_rooms
|
||||
if isinstance(room, dict) and str(room.get("name", "")).strip()
|
||||
}
|
||||
|
||||
existing_names = set(existing.keys())
|
||||
submitted_names = set(submitted.keys())
|
||||
|
||||
print(f"Existing rooms: {existing_names}")
|
||||
print(f"Submitted rooms: {submitted_names}")
|
||||
print(f"Rooms to add: {submitted_names - existing_names}")
|
||||
print(f"Rooms to remove: {existing_names - submitted_names}")
|
||||
|
||||
for name in submitted_names - existing_names:
|
||||
room_data = submitted[name]
|
||||
area_id = resolve_id(room_data.get("section_id", -1), section_fallbacks, section_map, "section") if room_data.get("section_id") is not None else None
|
||||
function_id = resolve_id(room_data.get("function_id", -1), function_fallbacks, function_map, "function") if room_data.get("function_id") is not None else None
|
||||
new_room = cls(name=name, area_id=area_id, function_id=function_id)
|
||||
db.session.add(new_room)
|
||||
print(f"➕ Adding room: {new_room}")
|
||||
|
||||
for name in existing_names - submitted_names:
|
||||
room_to_remove = existing[name]
|
||||
db.session.delete(room_to_remove)
|
||||
print(f"🗑️ Removing room: {room_to_remove}")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue