diff --git a/inventory/models/rooms.py b/inventory/models/rooms.py index b9ea03b..4081e87 100644 --- a/inventory/models/rooms.py +++ b/inventory/models/rooms.py @@ -6,7 +6,7 @@ if TYPE_CHECKING: from .users import User from sqlalchemy import ForeignKey, Identity, Integer, Unicode -from sqlalchemy.orm import Mapped, mapped_column, relationship +from sqlalchemy.orm import Mapped, mapped_column, relationship, joinedload, selectinload from . import db @@ -26,6 +26,22 @@ class Room(ValidatableMixin, db.Model): inventory: Mapped[List['Inventory']] = relationship('Inventory', back_populates='location') users: Mapped[List['User']] = relationship('User', back_populates='location') + ui_label_attr = 'name' + ui_eagerload = tuple() + ui_extra_attrs = ('area_id', 'function_id') + + @classmethod + def ui_update(cls, session, id_, payload): + print(payload) + obj = session.get(cls, id_) + if not obj: + return None + obj.name = payload.get("name", obj.name) + obj.area_id = payload.get("area_id", obj.area_id) + obj.function_id = payload.get("function_id", obj.function_id) + session.commit() + return obj + def __init__(self, name: Optional[str] = None, area_id: Optional[int] = None, function_id: Optional[int] = None): self.name = name self.area_id = area_id @@ -76,13 +92,13 @@ class Room(ValidatableMixin, db.Model): continue rid = room.get("id") - section_id = room.get("section_id") + area_id = room.get("area_id") function_id = room.get("function_id") submitted_clean.append({ "id": rid, "name": name, - "section_id": section_id, + "area_id": area_id, "function_id": function_id }) @@ -100,11 +116,11 @@ class Room(ValidatableMixin, db.Model): rid = entry.get("id") name = entry["name"] - resolved_section_id = resolve_fk(entry.get("section_id"), section_map, "section") + resolved_area_id = resolve_fk(entry.get("area_id"), section_map, "section") resolved_function_id = resolve_fk(entry.get("function_id"), function_map, "function") if not rid or str(rid).startswith("room-"): - new_room = cls(name=name, area_id=resolved_section_id, function_id=resolved_function_id) + new_room = cls(name=name, area_id=resolved_area_id, function_id=resolved_function_id) db.session.add(new_room) else: try: @@ -120,8 +136,8 @@ class Room(ValidatableMixin, db.Model): if room.name != name: room.name = name - if room.area_id != resolved_section_id: - room.area_id = resolved_section_id + if room.area_id != resolved_area_id: + room.area_id = resolved_area_id if room.function_id != resolved_function_id: room.function_id = resolved_function_id @@ -133,7 +149,7 @@ class Room(ValidatableMixin, db.Model): # Skip if a newly added room matches this one — likely duplicate if any( r["name"] == room.name and - resolve_fk(r["section_id"], section_map, "section") == room.area_id and + resolve_fk(r["area_id"], section_map, "section") == room.area_id and resolve_fk(r["function_id"], function_map, "function") == room.function_id for r in submitted_clean if r.get("id") is None or str(r.get("id")).startswith("room-") @@ -167,7 +183,7 @@ class Room(ValidatableMixin, db.Model): errors.append(f"{label} has an invalid ID: {raw_id}") # These fields are FK IDs, so we're just checking for valid formats here. - for fk_field, fk_label in [("section_id", "Section"), ("function_id", "Function")]: + for fk_field, fk_label in [("area_id", "Section"), ("function_id", "Function")]: fk_val = item.get(fk_field) if fk_val is None: @@ -181,3 +197,10 @@ class Room(ValidatableMixin, db.Model): errors.append(f"{label} has invalid {fk_label} ID: {fk_val}") return errors + +Room.ui_eagerload = ( + joinedload(Room.area), + joinedload(Room.room_function), + selectinload(Room.inventory), + selectinload(Room.users) +) \ No newline at end of file diff --git a/inventory/routes/settings.py b/inventory/routes/settings.py index 1cd1433..00cc98c 100644 --- a/inventory/routes/settings.py +++ b/inventory/routes/settings.py @@ -32,13 +32,13 @@ def settings(): submitted_rooms = [] for room in state.get("rooms", []): room = dict(room) # shallow copy - sid = room.get("section_id") + sid = room.get("area_id") fid = room.get("function_id") if sid is not None: sid_key = str(sid) if sid_key in section_map: - room["section_id"] = section_map[sid_key] + room["area_id"] = section_map[sid_key] if fid is not None: fid_key = str(fid) diff --git a/inventory/static/js/combobox.js b/inventory/static/js/combobox.js index e8bb099..03f4258 100644 --- a/inventory/static/js/combobox.js +++ b/inventory/static/js/combobox.js @@ -172,10 +172,15 @@ function ComboBox(cfg) { } else if (this.createUrl) { const data = await this._post(this.createUrl, { name }, true); const id = (data && data.id) ? data.id : ('temp-' + Math.random().toString(36).slice(2)); + + // add option optimistically const opt = document.createElement('option'); - opt.value = id; opt.textContent = name; + opt.value = id; opt.textContent = data?.name || name; this.$refs.list.appendChild(opt); this._sortOptions(); + + // ✅ NEW: tell the world we created something + this.$dispatch('combobox:item-created', { id, name: data?.name || name }); } this.query = ''; diff --git a/inventory/templates/fragments/_combobox_fragment.html b/inventory/templates/fragments/_combobox_fragment.html index 45cb626..3cd7720 100644 --- a/inventory/templates/fragments/_combobox_fragment.html +++ b/inventory/templates/fragments/_combobox_fragment.html @@ -79,7 +79,7 @@ create_url = none, edit_url = none, delete_url = none, refresh_url = none {% if refresh_url %} - {% set url = refresh_url ~ ('&' if '?' in refresh_url else '?') ~ 'view=option' %} + {% set url = refresh_url ~ ('&' if '?' in refresh_url else '?') ~ 'view=option&limit=0' %}