42 lines
1.7 KiB
Python
42 lines
1.7 KiB
Python
from typing import Optional, TYPE_CHECKING, List
|
|
if TYPE_CHECKING:
|
|
from .areas import Area
|
|
from .room_functions import RoomFunction
|
|
from .inventory import Inventory
|
|
from .users import User
|
|
|
|
from sqlalchemy import ForeignKey, Identity, Integer, Unicode
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from . import db
|
|
|
|
class Room(db.Model):
|
|
__tablename__ = 'Rooms'
|
|
|
|
id: Mapped[int] = mapped_column("ID", Integer, Identity(start=1, increment=1), primary_key=True)
|
|
name: Mapped[Optional[str]] = mapped_column("Room", Unicode(255), nullable=True)
|
|
area_id: Mapped[Optional[int]] = mapped_column("Area", Integer, ForeignKey("Areas.ID"))
|
|
function_id: Mapped[Optional[int]] = mapped_column("Function", Integer, ForeignKey("Room Functions.ID"))
|
|
|
|
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')
|
|
|
|
def __repr__(self):
|
|
return f"<Room(id={self.id}, room={repr(self.name)}, area_id={self.area_id}, function_id={self.function_id})>"
|
|
|
|
@property
|
|
def full_name(self):
|
|
name = self.name or ""
|
|
func = self.room_function.description if self.room_function else ""
|
|
return f"{name} - {func}".strip(" -")
|
|
|
|
def serialize(self):
|
|
return {
|
|
'id': self.id,
|
|
'name': self.name,
|
|
'area_id': self.area_id,
|
|
'function_id': self.function_id
|
|
}
|
|
|