Getting hybrid property support.
This commit is contained in:
parent
b68dbfc7ae
commit
506713c748
3 changed files with 39 additions and 15 deletions
|
|
@ -1,6 +1,7 @@
|
|||
from typing import List, Optional
|
||||
|
||||
from sqlalchemy import Boolean, DateTime, ForeignKey, Index, Integer, Unicode
|
||||
from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, String, Unicode, case, cast, func
|
||||
from sqlalchemy.ext.hybrid import hybrid_property
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from sqlalchemy.sql import expression as sql
|
||||
|
||||
|
|
@ -57,3 +58,22 @@ class Inventory(Base, CRUDMixin):
|
|||
parts.append(f"location={repr(self.location.identifier)}")
|
||||
|
||||
return f"<Inventory({', '.join(parts)})>"
|
||||
|
||||
@hybrid_property
|
||||
def label(self):
|
||||
if self.name:
|
||||
return f"Name: {self.name}"
|
||||
if self.barcode:
|
||||
return f"Barcode: {self.barcode}"
|
||||
if self.serial:
|
||||
return f"Serial: {self.serial}"
|
||||
return f"ID: {self.id}"
|
||||
|
||||
@label.expression
|
||||
def label(cls):
|
||||
return case(
|
||||
(cls.name.isnot(None) & (cls.name != ''), func.concat("Name: ", cls.name)),
|
||||
(cls.barcode.isnot(None) & (cls.barcode != ''), func.concat("Barcode: ", cls.barcode)),
|
||||
(cls.serial.isnot(None) & (cls.serial != ''), func.concat("Serial: ", cls.serial)),
|
||||
else_=func.concat("ID: ", cast(cls.id, String)),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ def init_index_routes(app):
|
|||
"start_time",
|
||||
"contact.last_name",
|
||||
"contact.first_name",
|
||||
"work_item.name",
|
||||
"work_item.label",
|
||||
"work_item.device_type.description"
|
||||
],
|
||||
"sort": "start_time"
|
||||
|
|
@ -32,7 +32,7 @@ def init_index_routes(app):
|
|||
{"field": "start_time", "label": "Start", "format": "date"},
|
||||
{"field": "contact.last_name", "label": "Contact",
|
||||
"link": {"endpoint": "user.get_item", "params": {"id": "{contact.id}"}}},
|
||||
{"field": "work_item.name", "label": "Work Item",
|
||||
{"field": "work_item.label", "label": "Work Item",
|
||||
"link": {"endpoint": "inventory.get_item", "params": {"id": "{work_item.id}"}}},
|
||||
{"field": "work_item.device_type.description", "label": "Device Type"}
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue