Refactor entity synchronization logic in Area, Brand, Item, RoomFunction, and Room models; improve ID handling and streamline foreign key resolution in settings
This commit is contained in:
parent
7833c4828b
commit
543494120c
9 changed files with 170 additions and 96 deletions
|
@ -37,12 +37,20 @@ class RoomFunction(db.Model):
|
|||
for item in submitted_items:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
description = str(item.get("name", "")).strip()
|
||||
if not description:
|
||||
name = str(item.get("name", "")).strip()
|
||||
raw_id = item.get("id")
|
||||
|
||||
if not name:
|
||||
continue
|
||||
submitted_clean.append({"id": item.get("id"), "description": description})
|
||||
if isinstance(item.get("id"), int) and item["id"] >= 0:
|
||||
seen_ids.add(item["id"])
|
||||
|
||||
try:
|
||||
parsed_id = int(raw_id)
|
||||
if parsed_id >= 0:
|
||||
seen_ids.add(parsed_id)
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
submitted_clean.append({"id": raw_id, "description": name})
|
||||
|
||||
existing_by_id = {f.id: f for f in db.session.query(cls).all()}
|
||||
existing_ids = set(existing_by_id.keys())
|
||||
|
@ -60,15 +68,20 @@ class RoomFunction(db.Model):
|
|||
db.session.flush()
|
||||
temp_id_map[submitted_id] = obj.id
|
||||
print(f"➕ Adding function: {description}")
|
||||
elif submitted_id in existing_by_id:
|
||||
obj = existing_by_id[submitted_id]
|
||||
if obj.description != description:
|
||||
elif isinstance(submitted_id, int) or submitted_id.isdigit():
|
||||
submitted_id_int = int(submitted_id)
|
||||
obj = existing_by_id.get(submitted_id_int)
|
||||
if obj and obj.description != description:
|
||||
print(f"✏️ Updating function {obj.id}: '{obj.description}' → '{description}'")
|
||||
obj.description = description
|
||||
|
||||
for id_to_remove in existing_ids - seen_ids:
|
||||
db.session.delete(existing_by_id[id_to_remove])
|
||||
obj = existing_by_id[id_to_remove]
|
||||
db.session.delete(obj)
|
||||
print(f"🗑️ Removing function ID {id_to_remove}")
|
||||
|
||||
return temp_id_map
|
||||
|
||||
id_map = {
|
||||
**{str(i): i for i in seen_ids},
|
||||
**{str(temp): real for temp, real in temp_id_map.items()}
|
||||
}
|
||||
return id_map
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue