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 # Record real (non-temp) IDs
try: try:
parsed_id = int(raw_id) if raw_id is not None:
if parsed_id >= 0: parsed_id = int(raw_id)
seen_ids.add(parsed_id) if parsed_id >= 0:
seen_ids.add(parsed_id)
except (ValueError, TypeError): except (ValueError, TypeError):
pass pass

View file

@ -33,7 +33,7 @@ class Brand(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
@ -42,24 +42,25 @@ class Brand(db.Model):
if not name: if not name:
continue continue
submitted_clean.append({"id": raw_id, "name": name}) submitted_clean.append({"id": raw_id, "name": name})
try: try:
parsed_id = int(raw_id) if raw_id:
if parsed_id >= 0: parsed_id = int(raw_id)
seen_ids.add(parsed_id) if parsed_id >= 0:
seen_ids.add(parsed_id)
except (ValueError, TypeError): except (ValueError, TypeError):
pass 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())
print(f"Existing brand IDs: {existing_ids}") print(f"Existing brand IDs: {existing_ids}")
print(f"Submitted brand IDs: {seen_ids}") print(f"Submitted brand IDs: {seen_ids}")
for entry in submitted_clean: for entry in submitted_clean:
submitted_id = entry.get("id") submitted_id = entry.get("id")
name = entry["name"] name = entry["name"]
if is_temp_id(submitted_id): if is_temp_id(submitted_id):
obj = cls(name=name) obj = cls(name=name)
db.session.add(obj) db.session.add(obj)
@ -71,17 +72,17 @@ class Brand(db.Model):
parsed_id = int(submitted_id) parsed_id = int(submitted_id)
except (ValueError, TypeError): except (ValueError, TypeError):
continue continue
if parsed_id in existing_by_id: if parsed_id in existing_by_id:
obj = existing_by_id[parsed_id] obj = existing_by_id[parsed_id]
if obj.name != name: if obj.name != name:
print(f"✏️ Updating brand {obj.id}: '{obj.name}''{name}'") print(f"✏️ Updating brand {obj.id}: '{obj.name}''{name}'")
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}")
id_map = { id_map = {
**{str(i): i for i in seen_ids}, # "1" → 1 **{str(i): i for i in seen_ids}, # "1" → 1
**{str(temp): real for temp, real in temp_id_map.items()} # "temp-1" → 5 **{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 = [] 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") raw_id = item.get("id")
if not name: if not name:
continue continue
try: try:
parsed_id = int(raw_id) if raw_id:
if parsed_id >= 0: parsed_id = int(raw_id)
seen_ids.add(parsed_id) if parsed_id >= 0:
seen_ids.add(parsed_id)
except (ValueError, TypeError): except (ValueError, TypeError):
pass pass
submitted_clean.append({"id": raw_id, "description": name}) 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())
print(f"Existing item IDs: {existing_ids}") print(f"Existing item IDs: {existing_ids}")
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["id"] submitted_id = entry["id"]
description = entry["description"] description = entry["description"]
if is_temp_id(submitted_id): if is_temp_id(submitted_id):
obj = cls(description=description) obj = cls(description=description)
db.session.add(obj) db.session.add(obj)
@ -77,12 +78,12 @@ class Item(db.Model):
if obj and obj.description != description: 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:
obj = existing_by_id[id_to_remove] obj = existing_by_id[id_to_remove]
db.session.delete(obj) db.session.delete(obj)
print(f"🗑️ Removing type ID {id_to_remove}") print(f"🗑️ Removing type ID {id_to_remove}")
id_map = { id_map = {
**{str(i): i for i in seen_ids}, **{str(i): i for i in seen_ids},
**{str(temp): real for temp, real in temp_id_map.items()} **{str(temp): real for temp, real in temp_id_map.items()}

View file

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

View file

@ -46,12 +46,7 @@ class Room(db.Model):
} }
@classmethod @classmethod
def sync_from_state( def sync_from_state(cls, submitted_rooms: list[dict], section_map: dict[str, int], function_map: dict[str, int]) -> None:
cls,
submitted_rooms: list[dict],
section_map: dict[int, int],
function_map: dict[int, int]
):
""" """
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.