82 lines
2.8 KiB
Python
82 lines
2.8 KiB
Python
from typing import List, Optional, TYPE_CHECKING
|
||
if TYPE_CHECKING:
|
||
from .rooms import Room
|
||
|
||
from sqlalchemy import Identity, Integer, Unicode
|
||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||
|
||
from . import db
|
||
from ..temp import is_temp_id
|
||
|
||
class Area(db.Model):
|
||
__tablename__ = 'Areas'
|
||
|
||
id: Mapped[int] = mapped_column("ID", Integer, Identity(start=1, increment=1), primary_key=True)
|
||
name: Mapped[Optional[str]] = mapped_column("Area", Unicode(255), nullable=True)
|
||
|
||
rooms: Mapped[List['Room']] = relationship('Room', back_populates='area')
|
||
|
||
def __init__(self, name: Optional[str] = None):
|
||
self.name = name
|
||
|
||
def __repr__(self):
|
||
return f"<Area(id={self.id}, name={repr(self.name)})>"
|
||
|
||
def serialize(self):
|
||
return {
|
||
'id': self.id,
|
||
'name': self.name
|
||
}
|
||
|
||
@classmethod
|
||
def sync_from_state(cls, submitted_items: list[dict]) -> dict[str, int]:
|
||
"""
|
||
Syncs the Area table (aka 'sections') with the submitted list.
|
||
Supports add, update, and delete.
|
||
Also returns a mapping of temp IDs to real IDs for resolution.
|
||
"""
|
||
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()
|
||
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_query = db.session.query(cls)
|
||
existing_by_id = {area.id: area for area in existing_query.all()}
|
||
existing_ids = set(existing_by_id.keys())
|
||
|
||
print(f"Existing area IDs: {existing_ids}")
|
||
print(f"Submitted area IDs: {seen_ids}")
|
||
|
||
for entry in submitted_clean:
|
||
submitted_id = entry.get("id")
|
||
submitted_name = entry["name"]
|
||
|
||
if is_temp_id(submitted_id):
|
||
new_area = cls(name=submitted_name)
|
||
db.session.add(new_area)
|
||
db.session.flush() # Get the real ID
|
||
temp_id_map[submitted_id] = new_area.id
|
||
print(f"➕ Adding area: {submitted_name}")
|
||
elif submitted_id in existing_by_id:
|
||
area = existing_by_id[submitted_id]
|
||
if area.name != submitted_name:
|
||
print(f"✏️ Updating area {area.id}: '{area.name}' → '{submitted_name}'")
|
||
area.name = submitted_name
|
||
|
||
for existing_id in existing_ids - seen_ids:
|
||
area = existing_by_id[existing_id]
|
||
db.session.delete(area)
|
||
print(f"🗑️ Removing area: {area.name}")
|
||
|
||
return temp_id_map
|
||
|
||
|