Enhance model constructors; add optional parameters for improved initialization across multiple modelsOh
This commit is contained in:
parent
acacf39f8e
commit
8162038f40
10 changed files with 90 additions and 13 deletions
|
@ -15,6 +15,9 @@ class Area(db.Model):
|
|||
|
||||
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)})>"
|
||||
|
||||
|
|
|
@ -15,6 +15,9 @@ class Brand(db.Model):
|
|||
|
||||
inventory: Mapped[List['Inventory']] = relationship('Inventory', back_populates='brand')
|
||||
|
||||
def __init__(self, name: Optional[str] = None):
|
||||
self.name = name
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Brand(id={self.id}, name={repr(self.name)})>"
|
||||
|
||||
|
|
|
@ -39,6 +39,25 @@ class Inventory(db.Model):
|
|||
item: Mapped['Item'] = relationship('Item', back_populates='inventory')
|
||||
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):
|
||||
parts = [f"id={self.id}"]
|
||||
|
||||
|
|
|
@ -16,6 +16,10 @@ class Item(db.Model):
|
|||
|
||||
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):
|
||||
return f"<Item(id={self.id}, description={repr(self.description)}, category={repr(self.category)})>"
|
||||
|
||||
|
|
|
@ -15,6 +15,9 @@ class RoomFunction(db.Model):
|
|||
|
||||
rooms: Mapped[List['Room']] = relationship('Room', back_populates='room_function')
|
||||
|
||||
def __init__(self, description: Optional[str] = None):
|
||||
self.description = description
|
||||
|
||||
def __repr__(self):
|
||||
return f"<RoomFunction(id={self.id}, description={repr(self.description)})>"
|
||||
|
||||
|
|
|
@ -23,6 +23,11 @@ class Room(db.Model):
|
|||
inventory: Mapped[List['Inventory']] = relationship('Inventory', 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):
|
||||
return f"<Room(id={self.id}, room={repr(self.name)}, area_id={self.area_id}, function_id={self.function_id})>"
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ class User(db.Model):
|
|||
active: Mapped[Optional[bool]] = mapped_column("Active", Boolean, server_default=text('((0))'))
|
||||
last_name: Mapped[Optional[str]] = mapped_column("Last", 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: Mapped[Optional['User']] = relationship('User', remote_side='User.id', back_populates='subordinates')
|
||||
|
@ -30,6 +30,16 @@ class User(db.Model):
|
|||
@property
|
||||
def full_name(self) -> str:
|
||||
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):
|
||||
return f"<User(id={self.id}, first_name={repr(self.first_name)}, last_name={repr(self.last_name)}, " \
|
||||
|
|
|
@ -28,6 +28,19 @@ class WorkLog(db.Model):
|
|||
work_item: Mapped[Optional['Inventory']] = relationship('Inventory', 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):
|
||||
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}, " \
|
||||
|
|
25
routes.py
25
routes.py
|
@ -407,23 +407,28 @@ def settings():
|
|||
if mapper is not None:
|
||||
db.session.flush()
|
||||
mapper[clean] = new_obj.id
|
||||
|
||||
|
||||
def resolve_id(raw_id, fallback_list, id_map, label):
|
||||
try:
|
||||
resolved_id = int(raw_id)
|
||||
except (TypeError, ValueError):
|
||||
resolved_id = None
|
||||
|
||||
if resolved_id is not None and resolved_id >= 0:
|
||||
return resolved_id
|
||||
|
||||
index = abs(resolved_id + 1) if resolved_id is not None else 0
|
||||
raise ValueError(f"{label.title()} ID was not a valid integer: {raw_id}")
|
||||
|
||||
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
|
||||
else:
|
||||
raise ValueError(f"{label.title()} ID {resolved_id} not found in known {label}s.")
|
||||
|
||||
# It's a negative ID = created on frontend. Resolve from fallback list
|
||||
index = abs(resolved_id + 1)
|
||||
try:
|
||||
entry = fallback_list[index]
|
||||
key = entry["name"] if isinstance(entry, dict) else str(entry).strip()
|
||||
except (IndexError, KeyError, TypeError):
|
||||
raise ValueError(f"Invalid {label} index or format at index {index}: {entry}")
|
||||
|
||||
except (IndexError, KeyError, TypeError) as e:
|
||||
raise ValueError(f"Invalid {label} index or format at index {index}: {entry if entry else 'UNKNOWN'}") from e
|
||||
|
||||
final_id = id_map.get(key)
|
||||
if not final_id:
|
||||
raise ValueError(f"Unresolved {label}: {key}")
|
||||
|
|
|
@ -190,8 +190,12 @@ submit_button=True
|
|||
|
||||
saveButton.addEventListener('click', () => {
|
||||
const name = document.getElementById('roomName').value.trim();
|
||||
const section = document.getElementById('roomSection').value;
|
||||
const func = document.getElementById('roomFunction').value;
|
||||
const sectionVal = document.getElementById('roomSection').value;
|
||||
const funcVal = document.getElementById('roomFunction').value;
|
||||
|
||||
const section = sectionVal !== "" ? parseInt(sectionVal) : null;
|
||||
const func = funcVal !== "" ? parseInt(funcVal) : null;
|
||||
|
||||
|
||||
if (!name) {
|
||||
alert('Please enter a room name.');
|
||||
|
@ -208,6 +212,14 @@ submit_button=True
|
|||
|
||||
// Add to select box visibly
|
||||
const option = ComboBoxWidget.createOption(name);
|
||||
|
||||
if (section !== null) {
|
||||
option.dataset.sectionId = section;
|
||||
}
|
||||
if (func !== null) {
|
||||
option.dataset.functionId = func;
|
||||
}
|
||||
|
||||
roomList.appendChild(option);
|
||||
ComboBoxWidget.sortOptions(roomList);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue