Refactor inventory model relationships to replace 'item' with 'type' for improved clarity and consistency
This commit is contained in:
parent
193c2aec70
commit
da60b971f8
5 changed files with 8 additions and 9 deletions
|
@ -39,7 +39,7 @@ class Inventory(db.Model, ImageAttachable):
|
||||||
location: Mapped[Optional['Room']] = relationship('Room', back_populates='inventory')
|
location: Mapped[Optional['Room']] = relationship('Room', back_populates='inventory')
|
||||||
owner = relationship('User', back_populates='inventory')
|
owner = relationship('User', back_populates='inventory')
|
||||||
brand: Mapped[Optional['Brand']] = relationship('Brand', back_populates='inventory')
|
brand: Mapped[Optional['Brand']] = relationship('Brand', back_populates='inventory')
|
||||||
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')
|
||||||
image: Mapped[Optional['Image']] = relationship('Image', back_populates='inventory', passive_deletes=True)
|
image: Mapped[Optional['Image']] = relationship('Image', back_populates='inventory', passive_deletes=True)
|
||||||
type: Mapped[Optional['Item']] = relationship('Item', back_populates='inventory')
|
type: Mapped[Optional['Item']] = relationship('Item', back_populates='inventory')
|
||||||
|
@ -68,8 +68,8 @@ class Inventory(db.Model, ImageAttachable):
|
||||||
if self.name:
|
if self.name:
|
||||||
parts.append(f"name={repr(self.name)}")
|
parts.append(f"name={repr(self.name)}")
|
||||||
|
|
||||||
if self.item:
|
if self.type:
|
||||||
parts.append(f"item={repr(self.item.description)}")
|
parts.append(f"item={repr(self.type.description)}")
|
||||||
|
|
||||||
if self.notes:
|
if self.notes:
|
||||||
parts.append(f"notes={repr(self.notes)}")
|
parts.append(f"notes={repr(self.notes)}")
|
||||||
|
|
|
@ -18,7 +18,7 @@ class Item(ValidatableMixin, 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):
|
def __init__(self, description: Optional[str] = None):
|
||||||
self.description = description
|
self.description = description
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
@ -27,8 +27,7 @@ class Item(ValidatableMixin, db.Model):
|
||||||
def serialize(self):
|
def serialize(self):
|
||||||
return {
|
return {
|
||||||
'id': self.id,
|
'id': self.id,
|
||||||
'name': self.description,
|
'name': self.description
|
||||||
'category': self.category
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -18,7 +18,7 @@ inventory_headers = {
|
||||||
"Bar Code": lambda i: {"text": i.barcode},
|
"Bar Code": lambda i: {"text": i.barcode},
|
||||||
"Brand": lambda i: {"text": i.brand.name} if i.brand else {"text": None},
|
"Brand": lambda i: {"text": i.brand.name} if i.brand else {"text": None},
|
||||||
"Model": lambda i: {"text": i.model},
|
"Model": lambda i: {"text": i.model},
|
||||||
"Item Type": lambda i: {"text": i.item.description} if i.item else {"text": None},
|
"Item Type": lambda i: {"text": i.type.description} if i.type else {"text": None},
|
||||||
"Shared?": lambda i: {"text": i.shared, "type": "bool", "html": checked_box if i.shared else unchecked_box},
|
"Shared?": lambda i: {"text": i.shared, "type": "bool", "html": checked_box if i.shared else unchecked_box},
|
||||||
"Owner": lambda i: {"text": i.owner.identifier, "url": url_for("main.user", id=i.owner.id)} if i.owner else {"text": None},
|
"Owner": lambda i: {"text": i.owner.identifier, "url": url_for("main.user", id=i.owner.id)} if i.owner else {"text": None},
|
||||||
"Location": lambda i: {"text": i.location.identifier} if i.location else {"Text": None},
|
"Location": lambda i: {"text": i.location.identifier} if i.location else {"Text": None},
|
||||||
|
|
|
@ -224,7 +224,7 @@ def get_inventory_csv():
|
||||||
case "owner":
|
case "owner":
|
||||||
return item.owner.identifier
|
return item.owner.identifier
|
||||||
case "type":
|
case "type":
|
||||||
return item.item.description
|
return item.type.description
|
||||||
case _:
|
case _:
|
||||||
return getattr(item, col, "")
|
return getattr(item, col, "")
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|
|
@ -12,7 +12,7 @@ def eager_load_inventory_relationships(query):
|
||||||
return query.options(
|
return query.options(
|
||||||
joinedload(Inventory.owner),
|
joinedload(Inventory.owner),
|
||||||
joinedload(Inventory.brand),
|
joinedload(Inventory.brand),
|
||||||
joinedload(Inventory.item),
|
joinedload(Inventory.type),
|
||||||
selectinload(Inventory.location).selectinload(Room.room_function)
|
selectinload(Inventory.location).selectinload(Room.room_function)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue