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:
Yaro Kasear 2025-06-25 11:23:33 -05:00
parent 7833c4828b
commit 543494120c
9 changed files with 170 additions and 96 deletions

View file

@ -38,45 +38,60 @@ class Area(db.Model):
submitted_clean = [] submitted_clean = []
seen_ids = set() seen_ids = set()
temp_id_map = {} temp_id_map = {}
for item in submitted_items: for item in submitted_items:
if not isinstance(item, dict): if not isinstance(item, dict):
continue continue
name = str(item.get("name", "")).strip() name = str(item.get("name", "")).strip()
raw_id = item.get("id")
if not name: if not name:
continue continue
submitted_clean.append({"id": item.get("id"), "name": name}) submitted_clean.append({"id": raw_id, "name": name})
if isinstance(item.get("id"), int) and item["id"] >= 0:
seen_ids.add(item["id"]) # Record real (non-temp) IDs
try:
parsed_id = int(raw_id)
if parsed_id >= 0:
seen_ids.add(parsed_id)
except (ValueError, TypeError):
pass
existing_query = db.session.query(cls) existing_query = db.session.query(cls)
existing_by_id = {area.id: area for area in existing_query.all()} existing_by_id = {area.id: area for area in existing_query.all()}
existing_ids = set(existing_by_id.keys()) existing_ids = set(existing_by_id.keys())
print(f"Existing area IDs: {existing_ids}") print(f"Existing area IDs: {existing_ids}")
print(f"Submitted area IDs: {seen_ids}") print(f"Submitted area IDs: {seen_ids}")
for entry in submitted_clean: for entry in submitted_clean:
submitted_id = entry.get("id") submitted_id_raw = entry.get("id")
submitted_name = entry["name"] submitted_name = entry["name"]
if is_temp_id(submitted_id): if is_temp_id(submitted_id_raw):
new_area = cls(name=submitted_name) new_area = cls(name=submitted_name)
db.session.add(new_area) db.session.add(new_area)
db.session.flush() # Get the real ID db.session.flush() # Get the real ID
temp_id_map[submitted_id] = new_area.id temp_id_map[submitted_id_raw] = new_area.id
print(f" Adding area: {submitted_name}") print(f" Adding area: {submitted_name}")
elif submitted_id in existing_by_id: else:
area = existing_by_id[submitted_id] try:
if area.name != submitted_name: submitted_id = int(submitted_id_raw)
print(f"✏️ Updating area {area.id}: '{area.name}''{submitted_name}'") except (ValueError, TypeError):
area.name = submitted_name continue # Skip malformed ID
if submitted_id in existing_by_id:
area = existing_by_id[submitted_id]
if area.name != submitted_name:
print(f"✏️ Updating area {area.id}: '{area.name}''{submitted_name}'")
area.name = submitted_name
for existing_id in existing_ids - seen_ids: for existing_id in existing_ids - seen_ids:
area = existing_by_id[existing_id] area = existing_by_id[existing_id]
db.session.delete(area) db.session.delete(area)
print(f"🗑️ Removing area: {area.name}") print(f"🗑️ Removing area: {area.name}")
return temp_id_map
id_map = {
**{str(i): i for i in seen_ids}, # "1" → 1
**{str(temp): real for temp, real in temp_id_map.items()} # "temp-1" → 5
}
return id_map

View file

@ -38,12 +38,18 @@ class Brand(db.Model):
if not isinstance(item, dict): if not isinstance(item, dict):
continue continue
name = str(item.get("name", "")).strip() name = str(item.get("name", "")).strip()
raw_id = item.get("id")
if not name: if not name:
continue continue
submitted_clean.append({"id": item.get("id"), "name": name}) submitted_clean.append({"id": raw_id, "name": name})
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
existing_by_id = {b.id: b for b in db.session.query(cls).all()} existing_by_id = {b.id: b for b in db.session.query(cls).all()}
existing_ids = set(existing_by_id.keys()) existing_ids = set(existing_by_id.keys())
@ -60,16 +66,26 @@ class Brand(db.Model):
db.session.flush() db.session.flush()
temp_id_map[submitted_id] = obj.id temp_id_map[submitted_id] = obj.id
print(f" Adding brand: {name}") print(f" Adding brand: {name}")
elif submitted_id in existing_by_id: else:
obj = existing_by_id[submitted_id] try:
if obj.name != name: parsed_id = int(submitted_id)
print(f"✏️ Updating brand {obj.id}: '{obj.name}''{name}'") except (ValueError, TypeError):
obj.name = name continue
if parsed_id in existing_by_id:
obj = existing_by_id[parsed_id]
if obj.name != name:
print(f"✏️ Updating brand {obj.id}: '{obj.name}''{name}'")
obj.name = name
for id_to_remove in existing_ids - seen_ids: for id_to_remove in existing_ids - seen_ids:
db.session.delete(existing_by_id[id_to_remove]) db.session.delete(existing_by_id[id_to_remove])
print(f"🗑️ Removing brand ID {id_to_remove}") print(f"🗑️ Removing brand ID {id_to_remove}")
return temp_id_map id_map = {
**{str(i): i for i in seen_ids}, # "1" → 1
**{str(temp): real for temp, real in temp_id_map.items()} # "temp-1" → 5
}
return id_map

View file

@ -40,12 +40,20 @@ class Item(db.Model):
for item in submitted_items: for item in submitted_items:
if not isinstance(item, dict): if not isinstance(item, dict):
continue continue
description = str(item.get("name", "")).strip() name = str(item.get("name", "")).strip()
if not description: raw_id = item.get("id")
if not name:
continue continue
submitted_clean.append({"id": item.get("id"), "description": description})
if isinstance(item.get("id"), int) and item["id"] >= 0: try:
seen_ids.add(item["id"]) 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 = {t.id: t for t in db.session.query(cls).all()} existing_by_id = {t.id: t for t in db.session.query(cls).all()}
existing_ids = set(existing_by_id.keys()) existing_ids = set(existing_by_id.keys())
@ -54,7 +62,7 @@ class Item(db.Model):
print(f"Submitted item IDs: {seen_ids}") print(f"Submitted item IDs: {seen_ids}")
for entry in submitted_clean: for entry in submitted_clean:
submitted_id = entry.get("id") submitted_id = entry["id"]
description = entry["description"] description = entry["description"]
if is_temp_id(submitted_id): if is_temp_id(submitted_id):
@ -63,15 +71,20 @@ class Item(db.Model):
db.session.flush() db.session.flush()
temp_id_map[submitted_id] = obj.id temp_id_map[submitted_id] = obj.id
print(f" Adding type: {description}") print(f" Adding type: {description}")
elif submitted_id in existing_by_id: elif isinstance(submitted_id, int) or submitted_id.isdigit():
obj = existing_by_id[submitted_id] submitted_id_int = int(submitted_id)
if obj.description != description: obj = existing_by_id.get(submitted_id_int)
if obj and obj.description != description:
print(f"✏️ Updating type {obj.id}: '{obj.description}''{description}'") print(f"✏️ Updating type {obj.id}: '{obj.description}''{description}'")
obj.description = description obj.description = description
for id_to_remove in existing_ids - seen_ids: 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 type ID {id_to_remove}") print(f"🗑️ Removing type 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

View file

@ -37,12 +37,20 @@ class RoomFunction(db.Model):
for item in submitted_items: for item in submitted_items:
if not isinstance(item, dict): if not isinstance(item, dict):
continue continue
description = str(item.get("name", "")).strip() name = str(item.get("name", "")).strip()
if not description: raw_id = item.get("id")
if not name:
continue continue
submitted_clean.append({"id": item.get("id"), "description": description})
if isinstance(item.get("id"), int) and item["id"] >= 0: try:
seen_ids.add(item["id"]) 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_by_id = {f.id: f for f in db.session.query(cls).all()}
existing_ids = set(existing_by_id.keys()) existing_ids = set(existing_by_id.keys())
@ -60,15 +68,20 @@ class RoomFunction(db.Model):
db.session.flush() db.session.flush()
temp_id_map[submitted_id] = obj.id temp_id_map[submitted_id] = obj.id
print(f" Adding function: {description}") print(f" Adding function: {description}")
elif submitted_id in existing_by_id: elif isinstance(submitted_id, int) or submitted_id.isdigit():
obj = existing_by_id[submitted_id] submitted_id_int = int(submitted_id)
if obj.description != description: obj = existing_by_id.get(submitted_id_int)
if obj and obj.description != description:
print(f"✏️ Updating function {obj.id}: '{obj.description}''{description}'") print(f"✏️ Updating function {obj.id}: '{obj.description}''{description}'")
obj.description = description obj.description = description
for id_to_remove in existing_ids - seen_ids: 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}") 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

View file

@ -49,36 +49,25 @@ class Room(db.Model):
def sync_from_state( def sync_from_state(
cls, cls,
submitted_rooms: list[dict], submitted_rooms: list[dict],
section_map: dict, section_map: dict[int, int],
function_map: dict, function_map: dict[int, int]
section_fallbacks: list,
function_fallbacks: list
): ):
""" """
Syncs the Rooms table with the submitted room list. Syncs the Rooms table with the submitted room list.
Resolves foreign keys using section_map and function_map. Resolves foreign keys using section_map and function_map.
Supports add, update, and delete. Supports add, update, and delete.
""" """
def resolve_fk(key, fk_map, label):
def resolve_fk(raw_id, fallback_list, id_map, label): # Print the fucking map so we can see what we're working with
if not raw_id: print(f"Resolving {label} ID: {key} using map: {fk_map}")
if key is None:
return None return None
key = str(key)
# 🛠 Handle SQLAlchemy model objects and dicts both if key.startswith("temp") or not key.isdigit():
name_entry = next( if key in fk_map:
(getattr(item, "name", None) or item.get("name") return fk_map[key]
for item in fallback_list raise ValueError(f"Unable to resolve {label} ID: {key}")
if str(getattr(item, "id", None) or item.get("id")) == str(raw_id)), return int(key) # It's already a real ID
None
)
if name_entry is None:
raise ValueError(f"Unable to resolve {label} ID: {raw_id}")
resolved_id = id_map.get(name_entry)
if resolved_id is None:
raise ValueError(f"{label.capitalize()} '{name_entry}' not found in ID map.")
return resolved_id
submitted_clean = [] submitted_clean = []
seen_ids = set() seen_ids = set()
@ -105,7 +94,7 @@ class Room(db.Model):
try: try:
seen_ids.add(int(rid)) seen_ids.add(int(rid))
except ValueError: except ValueError:
pass # It's an invalid non-temp string pass # Not valid? Not seen.
existing_query = db.session.query(cls) existing_query = db.session.query(cls)
existing_by_id = {room.id: room for room in existing_query.all()} existing_by_id = {room.id: room for room in existing_query.all()}
@ -118,8 +107,8 @@ class Room(db.Model):
rid = entry.get("id") rid = entry.get("id")
name = entry["name"] name = entry["name"]
resolved_section_id = resolve_fk(entry.get("section_id"), section_fallbacks, section_map, "section") resolved_section_id = resolve_fk(entry.get("section_id"), section_map, "section")
resolved_function_id = resolve_fk(entry.get("function_id"), function_fallbacks, function_map, "function") resolved_function_id = resolve_fk(entry.get("function_id"), function_map, "function")
if not rid or str(rid).startswith("room-"): if not rid or str(rid).startswith("room-"):
new_room = cls(name=name, area_id=resolved_section_id, function_id=resolved_function_id) new_room = cls(name=name, area_id=resolved_section_id, function_id=resolved_function_id)
@ -158,4 +147,3 @@ class Room(db.Model):
db.session.delete(room) db.session.delete(room)
print(f"🗑️ Removing room: {room.name}") print(f"🗑️ Removing room: {room.name}")

View file

@ -424,18 +424,23 @@ def settings():
room = dict(room) # shallow copy room = dict(room) # shallow copy
sid = room.get("section_id") sid = room.get("section_id")
fid = room.get("function_id") fid = room.get("function_id")
if sid in section_map:
room["section_id"] = section_map[sid] if sid is not None:
if fid in function_map: sid_key = str(sid)
room["function_id"] = function_map[fid] if sid_key in section_map:
room["section_id"] = section_map[sid_key]
if fid is not None:
fid_key = str(fid)
if fid_key in function_map:
room["function_id"] = function_map[fid_key]
submitted_rooms.append(room) submitted_rooms.append(room)
Room.sync_from_state( Room.sync_from_state(
submitted_rooms=submitted_rooms, submitted_rooms=submitted_rooms,
section_map=section_map, section_map=section_map,
function_map=function_map, function_map=function_map
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.") print("✅ COMMIT executed.")

View file

@ -74,7 +74,7 @@ const ComboBoxWidget = (() => {
removeBtn.disabled = selected.length === 0; removeBtn.disabled = selected.length === 0;
if (selected.length === 1) { if (selected.length === 1) {
input.value = selected[0].textContent; input.value = selected[0].textContent.trim();
currentlyEditing = selected[0]; currentlyEditing = selected[0];
addBtn.disabled = input.value.trim() === ''; addBtn.disabled = input.value.trim() === '';
} else { } else {

View file

@ -1,6 +1,6 @@
{% import "fragments/_icon_fragment.html" as icons %} {% import "fragments/_icon_fragment.html" as icons %}
{% macro render_combobox(id, options, label=none, placeholder=none, onAdd=none, onRemove=none, onEdit=none) %} {% macro render_combobox(id, options, label=none, placeholder=none, onAdd=none, onRemove=none, onEdit=none, data_attributes=none) %}
<!-- Combobox Widget Fragment --> <!-- Combobox Widget Fragment -->
{% if label %} {% if label %}
@ -18,7 +18,16 @@
</div> </div>
<select class="form-select border-top-0 rounded-top-0" id="{{ id }}-list" name="{{ id }}" size="10" multiple> <select class="form-select border-top-0 rounded-top-0" id="{{ id }}-list" name="{{ id }}" size="10" multiple>
{% for option in options %} {% for option in options %}
<option value="{{ option.id }}">{{ option.name }}</option> <option value="{{ option.id }}"
{% if data_attributes %}
{% for key, data_attr in data_attributes.items() %}
{% if option[key] is defined %}
data-{{ data_attr }}="{{ option[key] }}"
{% endif %}
{% endfor %}
{% endif %}>
{{ option.name }}
</option>
{% endfor %} {% endfor %}
</select> </select>
</div> </div>

View file

@ -84,7 +84,8 @@ submit_button=True
label='Rooms', label='Rooms',
placeholder='Add a new room', placeholder='Add a new room',
onAdd=room_editor, onAdd=room_editor,
onEdit=room_editor onEdit=room_editor,
data_attributes={'area_id': 'section-id', 'function_id': 'function-id'}
) }} ) }}
</div> </div>
</div> </div>
@ -154,17 +155,23 @@ submit_button=True
})); }));
} }
function sanitizeFk(val) {
return val && val !== "null" && val !== "" ? val : null;
}
const roomOptions = Array.from(document.getElementById('room-list').options); const roomOptions = Array.from(document.getElementById('room-list').options);
const rooms = roomOptions.map(opt => { const rooms = roomOptions.map(opt => {
const data = opt.dataset; const data = opt.dataset;
return { return {
id: opt.value || undefined, id: opt.value || undefined,
name: opt.textContent.trim(), name: opt.textContent.trim(),
section_id: data.sectionId ?? null, section_id: sanitizeFk(data.sectionId),
function_id: data.functionId ?? null, function_id: sanitizeFk(data.functionId),
}; };
}); });
return { return {
brands: extractOptions("brand"), brands: extractOptions("brand"),
types: extractOptions("type"), types: extractOptions("type"),
@ -181,8 +188,16 @@ submit_button=True
const form = document.getElementById('settingsForm'); const form = document.getElementById('settingsForm');
// Replace the whole submission logic with just JSON // Replace the whole submission logic with just JSON
form.addEventListener('submit', () => { form.addEventListener('submit', (event) => {
document.getElementById('formStateField').value = JSON.stringify(buildFormState()); event.preventDefault(); // 🚨 Stop form from leaving the building
try {
const state = buildFormState();
document.getElementById('formStateField').value = JSON.stringify(state);
form.submit(); // 🟢 Now it can go
} catch (err) {
alert("Form submission failed: " + err.message);
console.error("Failed to build form state:", err);
}
}); });
// Modal populates dropdowns fresh from the page every time it opens // Modal populates dropdowns fresh from the page every time it opens
@ -232,8 +247,8 @@ submit_button=True
existingOption.textContent = name; existingOption.textContent = name;
existingOption.value = idRaw; existingOption.value = idRaw;
existingOption.dataset.sectionId = sectionVal; existingOption.dataset.sectionId = sectionVal || "";
existingOption.dataset.functionId = funcVal; existingOption.dataset.functionId = funcVal || "";
ComboBoxWidget.sortOptions(roomList); ComboBoxWidget.sortOptions(roomList);