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:
parent
543494120c
commit
eddaa69158
5 changed files with 46 additions and 47 deletions
|
@ -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()}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue