57 lines
2.4 KiB
Python
57 lines
2.4 KiB
Python
from typing import Optional, TYPE_CHECKING, List
|
|
if TYPE_CHECKING:
|
|
from .areas import Area
|
|
from .inventory import Inventory
|
|
from .users import User
|
|
|
|
from .room_functions import RoomFunction
|
|
from crudkit import CrudMixin
|
|
from sqlalchemy import ForeignKey, Identity, Integer, Unicode, func, select, literal
|
|
from sqlalchemy.ext.hybrid import hybrid_property
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from . import db
|
|
|
|
class Room(db.Model, CrudMixin):
|
|
__tablename__ = 'rooms'
|
|
|
|
id: Mapped[int] = mapped_column(Integer, Identity(start=1, increment=1), primary_key=True)
|
|
name: Mapped[Optional[str]] = mapped_column(Unicode(255), nullable=True)
|
|
area_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("area.id"), nullable=True, index=True)
|
|
function_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("room_function.id"), nullable=True, index=True)
|
|
|
|
area: Mapped[Optional['Area']] = relationship('Area', back_populates='rooms')
|
|
room_function: Mapped[Optional['RoomFunction']] = relationship('RoomFunction', back_populates='rooms')
|
|
inventory: Mapped[List['Inventory']] = relationship('Inventory', back_populates='location')
|
|
users: Mapped[List['User']] = relationship('User', back_populates='location')
|
|
|
|
ui_eagerload = tuple()
|
|
ui_extra_attrs = ('area_id', 'function_id')
|
|
|
|
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})>"
|
|
|
|
@hybrid_property
|
|
def identifier(self):
|
|
name = self.name or ""
|
|
function = self.room_function.description if self.room_function else ""
|
|
return f"{name} - {function}".strip(" -")
|
|
|
|
@identifier.expression
|
|
def identifier(cls):
|
|
rf_desc = (
|
|
select(RoomFunction.description)
|
|
.where(RoomFunction.id == cls.function_id)
|
|
.correlate(cls)
|
|
.scalar_subquery()
|
|
)
|
|
return func.concat(
|
|
func.coalesce(cls.name, ''), # left part
|
|
literal(' - '), # separator
|
|
func.coalesce(rf_desc, '') # right part via correlated subquery
|
|
)
|