Implement sync_from_state methods for Area, Brand, Item, RoomFunction, and Room models; enhance entity management and foreign key resolution in settings
This commit is contained in:
parent
8a5c5db9e0
commit
7833c4828b
9 changed files with 359 additions and 186 deletions
|
@ -6,6 +6,7 @@ from sqlalchemy import Identity, Integer, Unicode
|
|||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from . import db
|
||||
from ..temp import is_temp_id
|
||||
|
||||
class Brand(db.Model):
|
||||
__tablename__ = 'Brands'
|
||||
|
@ -28,31 +29,47 @@ class Brand(db.Model):
|
|||
}
|
||||
|
||||
@classmethod
|
||||
def sync_from_state(cls, submitted_items: list[dict]):
|
||||
"""
|
||||
Syncs the Brand table with the provided list of dictionaries.
|
||||
Adds new brands and removes brands that are not in the list.
|
||||
"""
|
||||
submitted = {
|
||||
str(item.get("name", "")).strip()
|
||||
for item in submitted_items
|
||||
if isinstance(item, dict) and str(item.get("name", "")).strip()
|
||||
}
|
||||
def sync_from_state(cls, submitted_items: list[dict]) -> dict[str, int]:
|
||||
submitted_clean = []
|
||||
seen_ids = set()
|
||||
temp_id_map = {}
|
||||
|
||||
existing_query = db.session.query(cls)
|
||||
existing = {brand.name: brand for brand in existing_query.all()}
|
||||
for item in submitted_items:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
name = str(item.get("name", "")).strip()
|
||||
if not name:
|
||||
continue
|
||||
submitted_clean.append({"id": item.get("id"), "name": name})
|
||||
if isinstance(item.get("id"), int) and item["id"] >= 0:
|
||||
seen_ids.add(item["id"])
|
||||
|
||||
existing_names = set(existing.keys())
|
||||
existing_by_id = {b.id: b for b in db.session.query(cls).all()}
|
||||
existing_ids = set(existing_by_id.keys())
|
||||
|
||||
print(f"Existing brands: {existing_names}")
|
||||
print(f"Submitted brands: {submitted}")
|
||||
print(f"Brands to add: {submitted - existing_names}")
|
||||
print(f"Brands to remove: {existing_names - submitted}")
|
||||
print(f"Existing brand IDs: {existing_ids}")
|
||||
print(f"Submitted brand IDs: {seen_ids}")
|
||||
|
||||
for name in submitted - existing_names:
|
||||
db.session.add(cls(name=name))
|
||||
print(f"➕ Adding brand: {name}")
|
||||
for entry in submitted_clean:
|
||||
submitted_id = entry.get("id")
|
||||
name = entry["name"]
|
||||
|
||||
for name in existing_names - submitted:
|
||||
db.session.delete(existing[name])
|
||||
print(f"🗑️ Removing brand: {name}")
|
||||
if is_temp_id(submitted_id):
|
||||
obj = cls(name=name)
|
||||
db.session.add(obj)
|
||||
db.session.flush()
|
||||
temp_id_map[submitted_id] = obj.id
|
||||
print(f"➕ Adding brand: {name}")
|
||||
elif submitted_id in existing_by_id:
|
||||
obj = existing_by_id[submitted_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}")
|
||||
|
||||
return temp_id_map
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue