Enhance model constructors; add optional parameters for improved initialization across multiple modelsOh

This commit is contained in:
Yaro Kasear 2025-06-23 15:42:48 -05:00
parent acacf39f8e
commit 8162038f40
10 changed files with 90 additions and 13 deletions

View file

@ -15,6 +15,9 @@ class Area(db.Model):
rooms: Mapped[List['Room']] = relationship('Room', back_populates='area') rooms: Mapped[List['Room']] = relationship('Room', back_populates='area')
def __init__(self, name: Optional[str] = None):
self.name = name
def __repr__(self): def __repr__(self):
return f"<Area(id={self.id}, name={repr(self.name)})>" return f"<Area(id={self.id}, name={repr(self.name)})>"

View file

@ -15,6 +15,9 @@ class Brand(db.Model):
inventory: Mapped[List['Inventory']] = relationship('Inventory', back_populates='brand') inventory: Mapped[List['Inventory']] = relationship('Inventory', back_populates='brand')
def __init__(self, name: Optional[str] = None):
self.name = name
def __repr__(self): def __repr__(self):
return f"<Brand(id={self.id}, name={repr(self.name)})>" return f"<Brand(id={self.id}, name={repr(self.name)})>"

View file

@ -39,6 +39,25 @@ class Inventory(db.Model):
item: Mapped['Item'] = relationship('Item', back_populates='inventory') item: Mapped['Item'] = relationship('Item', back_populates='inventory')
work_logs: Mapped[List['WorkLog']] = relationship('WorkLog', back_populates='work_item') work_logs: Mapped[List['WorkLog']] = relationship('WorkLog', back_populates='work_item')
def __init__(self, timestamp: datetime.datetime, condition: str, needed: str, type_id: int,
inventory_name: Optional[str] = None, serial: Optional[str] = None,
model: Optional[str] = None, notes: Optional[str] = None, owner_id: Optional[int] = None,
brand_id: Optional[int] = None, location_id: Optional[str] = None, barcode: Optional[str] = None,
shared: bool = False):
self.timestamp = timestamp
self.condition = condition
self.needed = needed
self.type_id = type_id
self.inventory_name = inventory_name
self.serial = serial
self.model = model
self.notes = notes
self.owner_id = owner_id
self.brand_id = brand_id
self.location_id = location_id
self.barcode = barcode
self.shared = shared
def __repr__(self): def __repr__(self):
parts = [f"id={self.id}"] parts = [f"id={self.id}"]

View file

@ -16,6 +16,10 @@ class Item(db.Model):
inventory: Mapped[List['Inventory']] = relationship('Inventory', back_populates='item') inventory: Mapped[List['Inventory']] = relationship('Inventory', back_populates='item')
def __init__(self, description: Optional[str] = None, category: Optional[str] = None):
self.description = description
self.category = category
def __repr__(self): 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)}, category={repr(self.category)})>"

View file

@ -15,6 +15,9 @@ class RoomFunction(db.Model):
rooms: Mapped[List['Room']] = relationship('Room', back_populates='room_function') rooms: Mapped[List['Room']] = relationship('Room', back_populates='room_function')
def __init__(self, description: Optional[str] = None):
self.description = description
def __repr__(self): def __repr__(self):
return f"<RoomFunction(id={self.id}, description={repr(self.description)})>" return f"<RoomFunction(id={self.id}, description={repr(self.description)})>"

View file

@ -23,6 +23,11 @@ class Room(db.Model):
inventory: Mapped[List['Inventory']] = relationship('Inventory', back_populates='location') inventory: Mapped[List['Inventory']] = relationship('Inventory', back_populates='location')
users: Mapped[List['User']] = relationship('User', back_populates='location') users: Mapped[List['User']] = relationship('User', back_populates='location')
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
self.function_id = function_id
def __repr__(self): def __repr__(self):
return f"<Room(id={self.id}, room={repr(self.name)}, area_id={self.area_id}, function_id={self.function_id})>" return f"<Room(id={self.id}, room={repr(self.name)}, area_id={self.area_id}, function_id={self.function_id})>"

View file

@ -17,7 +17,7 @@ class User(db.Model):
active: Mapped[Optional[bool]] = mapped_column("Active", Boolean, server_default=text('((0))')) active: Mapped[Optional[bool]] = mapped_column("Active", Boolean, server_default=text('((0))'))
last_name: Mapped[Optional[str]] = mapped_column("Last", Unicode(255), nullable=True) last_name: Mapped[Optional[str]] = mapped_column("Last", Unicode(255), nullable=True)
first_name: Mapped[Optional[str]] = mapped_column("First", Unicode(255), nullable=True) first_name: Mapped[Optional[str]] = mapped_column("First", Unicode(255), nullable=True)
location_id: Mapped[Optional[str]] = mapped_column(ForeignKey("Rooms.ID")) location_id: Mapped[Optional[str]] = mapped_column(ForeignKey("Rooms.ID"), nullable=True)
supervisor_id: Mapped[Optional[int]] = mapped_column("Supervisor", Integer, ForeignKey("Users.ID")) supervisor_id: Mapped[Optional[int]] = mapped_column("Supervisor", Integer, ForeignKey("Users.ID"))
supervisor: Mapped[Optional['User']] = relationship('User', remote_side='User.id', back_populates='subordinates') supervisor: Mapped[Optional['User']] = relationship('User', remote_side='User.id', back_populates='subordinates')
@ -31,6 +31,16 @@ class User(db.Model):
def full_name(self) -> str: def full_name(self) -> str:
return f"{self.first_name or ''} {self.last_name or ''}".strip() return f"{self.first_name or ''} {self.last_name or ''}".strip()
def __init__(self, first_name: Optional[str] = None, last_name: Optional[str] = None,
location_id: Optional[int] = None, supervisor_id: Optional[int] = None,
staff: Optional[bool] = False, active: Optional[bool] = False):
self.first_name = first_name
self.last_name = last_name
self.location_id = location_id
self.supervisor_id = supervisor_id
self.staff = staff
self.active = active
def __repr__(self): def __repr__(self):
return f"<User(id={self.id}, first_name={repr(self.first_name)}, last_name={repr(self.last_name)}, " \ return f"<User(id={self.id}, first_name={repr(self.first_name)}, last_name={repr(self.last_name)}, " \
f"location={repr(self.location)}, staff={self.staff}, active={self.active})>" f"location={repr(self.location)}, staff={self.staff}, active={self.active})>"

View file

@ -28,6 +28,19 @@ class WorkLog(db.Model):
work_item: Mapped[Optional['Inventory']] = relationship('Inventory', back_populates='work_logs') work_item: Mapped[Optional['Inventory']] = relationship('Inventory', back_populates='work_logs')
contact: Mapped[Optional['User']] = relationship('User', back_populates='work_logs') contact: Mapped[Optional['User']] = relationship('User', back_populates='work_logs')
def __init__(self, start_time: Optional[datetime.datetime] = None, end_time: Optional[datetime.datetime] = None,
notes: Optional[str] = None, complete: Optional[bool] = False,
followup: Optional[bool] = False, contact_id: Optional[int] = None,
analysis: Optional[bool] = False, work_item_id: Optional[int] = None):
self.start_time = start_time
self.end_time = end_time
self.notes = notes
self.complete = complete
self.followup = followup
self.contact_id = contact_id
self.analysis = analysis
self.work_item_id = work_item_id
def __repr__(self): def __repr__(self):
return f"<WorkLog(id={self.id}, start_time={self.start_time}, end_time={self.end_time}, " \ return f"<WorkLog(id={self.id}, start_time={self.start_time}, end_time={self.end_time}, " \
f"notes={repr(self.notes)}, complete={self.complete}, followup={self.followup}, " \ f"notes={repr(self.notes)}, complete={self.complete}, followup={self.followup}, " \

View file

@ -412,17 +412,22 @@ def settings():
try: try:
resolved_id = int(raw_id) resolved_id = int(raw_id)
except (TypeError, ValueError): except (TypeError, ValueError):
resolved_id = None raise ValueError(f"{label.title()} ID was not a valid integer: {raw_id}")
if resolved_id is not None and resolved_id >= 0: if resolved_id >= 0:
# Try to validate this ID by checking if it appears in the map values
if resolved_id in id_map.values():
return resolved_id return resolved_id
else:
raise ValueError(f"{label.title()} ID {resolved_id} not found in known {label}s.")
index = abs(resolved_id + 1) if resolved_id is not None else 0 # It's a negative ID = created on frontend. Resolve from fallback list
index = abs(resolved_id + 1)
try: try:
entry = fallback_list[index] entry = fallback_list[index]
key = entry["name"] if isinstance(entry, dict) else str(entry).strip() key = entry["name"] if isinstance(entry, dict) else str(entry).strip()
except (IndexError, KeyError, TypeError): except (IndexError, KeyError, TypeError) as e:
raise ValueError(f"Invalid {label} index or format at index {index}: {entry}") raise ValueError(f"Invalid {label} index or format at index {index}: {entry if entry else 'UNKNOWN'}") from e
final_id = id_map.get(key) final_id = id_map.get(key)
if not final_id: if not final_id:

View file

@ -190,8 +190,12 @@ submit_button=True
saveButton.addEventListener('click', () => { saveButton.addEventListener('click', () => {
const name = document.getElementById('roomName').value.trim(); const name = document.getElementById('roomName').value.trim();
const section = document.getElementById('roomSection').value; const sectionVal = document.getElementById('roomSection').value;
const func = document.getElementById('roomFunction').value; const funcVal = document.getElementById('roomFunction').value;
const section = sectionVal !== "" ? parseInt(sectionVal) : null;
const func = funcVal !== "" ? parseInt(funcVal) : null;
if (!name) { if (!name) {
alert('Please enter a room name.'); alert('Please enter a room name.');
@ -208,6 +212,14 @@ submit_button=True
// Add to select box visibly // Add to select box visibly
const option = ComboBoxWidget.createOption(name); const option = ComboBoxWidget.createOption(name);
if (section !== null) {
option.dataset.sectionId = section;
}
if (func !== null) {
option.dataset.functionId = func;
}
roomList.appendChild(option); roomList.appendChild(option);
ComboBoxWidget.sortOptions(roomList); ComboBoxWidget.sortOptions(roomList);