diff --git a/models/areas.py b/models/areas.py index d95c394..e1572a4 100644 --- a/models/areas.py +++ b/models/areas.py @@ -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 diff --git a/models/brands.py b/models/brands.py index f58ee79..e972413 100644 --- a/models/brands.py +++ b/models/brands.py @@ -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 diff --git a/models/items.py b/models/items.py index d86d974..4a47bff 100644 --- a/models/items.py +++ b/models/items.py @@ -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()} diff --git a/models/room_functions.py b/models/room_functions.py index 02685f6..6a65b1f 100644 --- a/models/room_functions.py +++ b/models/room_functions.py @@ -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()} diff --git a/models/rooms.py b/models/rooms.py index 3badbd3..acc8fd3 100644 --- a/models/rooms.py +++ b/models/rooms.py @@ -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.