Refactor sync_from_state method in Room model; update parameter types for section_map and function_map to improve clarity and consistency

This commit is contained in:
Yaro Kasear 2025-06-25 11:34:45 -05:00
parent 543494120c
commit eddaa69158
5 changed files with 46 additions and 47 deletions

View file

@ -50,9 +50,10 @@ class Area(db.Model):
# Record real (non-temp) IDs
try:
parsed_id = int(raw_id)
if parsed_id >= 0:
seen_ids.add(parsed_id)
if raw_id is not None:
parsed_id = int(raw_id)
if parsed_id >= 0:
seen_ids.add(parsed_id)
except (ValueError, TypeError):
pass

View file

@ -33,7 +33,7 @@ class Brand(db.Model):
submitted_clean = []
seen_ids = set()
temp_id_map = {}
for item in submitted_items:
if not isinstance(item, dict):
continue
@ -42,24 +42,25 @@ class Brand(db.Model):
if not name:
continue
submitted_clean.append({"id": raw_id, "name": name})
try:
parsed_id = int(raw_id)
if parsed_id >= 0:
seen_ids.add(parsed_id)
if raw_id:
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_ids = set(existing_by_id.keys())
print(f"Existing brand IDs: {existing_ids}")
print(f"Submitted brand IDs: {seen_ids}")
for entry in submitted_clean:
submitted_id = entry.get("id")
name = entry["name"]
if is_temp_id(submitted_id):
obj = cls(name=name)
db.session.add(obj)
@ -71,17 +72,17 @@ class Brand(db.Model):
parsed_id = int(submitted_id)
except (ValueError, TypeError):
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:
db.session.delete(existing_by_id[id_to_remove])
print(f"🗑️ Removing brand ID {id_to_remove}")
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

View file

@ -36,35 +36,36 @@ class Item(db.Model):
submitted_clean = []
seen_ids = set()
temp_id_map = {}
for item in submitted_items:
if not isinstance(item, dict):
continue
name = str(item.get("name", "")).strip()
raw_id = item.get("id")
if not name:
continue
try:
parsed_id = int(raw_id)
if parsed_id >= 0:
seen_ids.add(parsed_id)
if raw_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_ids = set(existing_by_id.keys())
print(f"Existing item IDs: {existing_ids}")
print(f"Submitted item IDs: {seen_ids}")
for entry in submitted_clean:
submitted_id = entry["id"]
description = entry["description"]
if is_temp_id(submitted_id):
obj = cls(description=description)
db.session.add(obj)
@ -77,12 +78,12 @@ class Item(db.Model):
if obj and obj.description != description:
print(f"✏️ Updating type {obj.id}: '{obj.description}''{description}'")
obj.description = description
for id_to_remove in existing_ids - seen_ids:
obj = existing_by_id[id_to_remove]
db.session.delete(obj)
print(f"🗑️ Removing type ID {id_to_remove}")
id_map = {
**{str(i): i for i in seen_ids},
**{str(temp): real for temp, real in temp_id_map.items()}

View file

@ -33,35 +33,36 @@ class RoomFunction(db.Model):
submitted_clean = []
seen_ids = set()
temp_id_map = {}
for item in submitted_items:
if not isinstance(item, dict):
continue
name = str(item.get("name", "")).strip()
raw_id = item.get("id")
if not name:
continue
try:
parsed_id = int(raw_id)
if parsed_id >= 0:
seen_ids.add(parsed_id)
if raw_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_ids = set(existing_by_id.keys())
print(f"Existing function IDs: {existing_ids}")
print(f"Submitted function IDs: {seen_ids}")
for entry in submitted_clean:
submitted_id = entry.get("id")
description = entry["description"]
if is_temp_id(submitted_id):
obj = cls(description=description)
db.session.add(obj)
@ -74,12 +75,12 @@ class RoomFunction(db.Model):
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:
obj = existing_by_id[id_to_remove]
db.session.delete(obj)
print(f"🗑️ Removing function ID {id_to_remove}")
id_map = {
**{str(i): i for i in seen_ids},
**{str(temp): real for temp, real in temp_id_map.items()}

View file

@ -46,12 +46,7 @@ class Room(db.Model):
}
@classmethod
def sync_from_state(
cls,
submitted_rooms: list[dict],
section_map: dict[int, int],
function_map: dict[int, int]
):
def sync_from_state(cls, submitted_rooms: list[dict], section_map: dict[str, int], function_map: dict[str, int]) -> None:
"""
Syncs the Rooms table with the submitted room list.
Resolves foreign keys using section_map and function_map.