Refactor brand and item models to introduce 'identifier' property for consistency; update dropdown rendering in templates to utilize new properties and improve input handling.

This commit is contained in:
Yaro Kasear 2025-07-22 13:04:31 -05:00
parent 8631e4082c
commit 9242ce6eab
5 changed files with 52 additions and 58 deletions

View file

@ -23,13 +23,13 @@ class Brand(ValidatableMixin, db.Model):
def __repr__(self):
return f"<Brand(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]:
submitted_clean = []
@ -93,7 +93,7 @@ class Brand(ValidatableMixin, db.Model):
if not isinstance(item, dict):
errors.append(f"Area entry #{index + 1} is not a valid object.")
continue
name = item.get('name')
if not name or not str(name).strip():
errors.append(f"Area entry #{index + 1} is missing a name.")
@ -107,3 +107,7 @@ class Brand(ValidatableMixin, db.Model):
errors.append(f"Area entry #{index + 1} has invalid ID: {raw_id}")
return errors
@property
def identifier(self) -> str:
return self.name if self.name else f"ID: {self.id}"

View file

@ -42,6 +42,7 @@ class Inventory(db.Model, ImageAttachable):
item: Mapped['Item'] = relationship('Item', back_populates='inventory')
work_logs: Mapped[List['WorkLog']] = relationship('WorkLog', back_populates='work_item')
image: Mapped[Optional['Image']] = relationship('Image', back_populates='inventory', passive_deletes=True)
type: Mapped[Optional['Item']] = relationship('Item', back_populates='inventory')
def __init__(self, timestamp: datetime.datetime, condition: str, type_id: Optional[int] = None,
name: Optional[str] = None, serial: Optional[str] = None,

View file

@ -1,6 +1,6 @@
from typing import List, Optional, TYPE_CHECKING
if TYPE_CHECKING:
from .inventory import Inventory
from .inventory import Inventory
from sqlalchemy import Identity, Integer, Unicode
from sqlalchemy.orm import Mapped, mapped_column, relationship
@ -20,18 +20,21 @@ class Item(ValidatableMixin, db.Model):
def __init__(self, description: Optional[str] = None, category: Optional[str] = None):
self.description = description
self.category = category
def __repr__(self):
return f"<Item(id={self.id}, description={repr(self.description)}, category={repr(self.category)})>"
return f"<Item(id={self.id}, description={repr(self.description)})>"
def serialize(self):
return {
'id': self.id,
'name': self.description,
'category': self.category
}
@property
def identifier(self):
return self.description if self.description else f"Item {self.id}"
@classmethod
def sync_from_state(cls, submitted_items: list[dict]) -> dict[str, int]:
submitted_clean = []
@ -84,7 +87,7 @@ class Item(ValidatableMixin, db.Model):
**{str(temp): real for temp, real in temp_id_map.items()}
}
return id_map
@classmethod
def validate_state(cls, submitted_items: list[dict]) -> list[str]:
errors = []
@ -93,7 +96,7 @@ class Item(ValidatableMixin, db.Model):
if not isinstance(item, dict):
errors.append(f"Area entry #{index + 1} is not a valid object.")
continue
name = item.get('name')
if not name or not str(name).strip():
errors.append(f"Area entry #{index + 1} is missing a name.")