61 lines
2.0 KiB
Python
61 lines
2.0 KiB
Python
"""Reference lists and the items in them."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from sqlalchemy import (
|
|
Boolean,
|
|
Column,
|
|
DateTime,
|
|
ForeignKey,
|
|
Integer,
|
|
String,
|
|
func,
|
|
)
|
|
from sqlalchemy.dialects.postgresql import JSONB, UUID
|
|
from sqlalchemy.orm import relationship
|
|
|
|
from app.config.database import Base
|
|
|
|
|
|
class LookupList(Base):
|
|
__tablename__ = "lookup_lists"
|
|
|
|
id = Column(UUID(as_uuid=True), primary_key=True,
|
|
server_default=func.gen_random_uuid())
|
|
tenant_id = Column(UUID(as_uuid=True), ForeignKey("tenants.id", ondelete="CASCADE"))
|
|
code = Column(String(60), nullable=False)
|
|
name = Column(String(150), nullable=False)
|
|
description = Column(String(500))
|
|
allows_custom_items = Column(Boolean, nullable=False, default=True)
|
|
is_active = Column(Boolean, nullable=False, default=True)
|
|
created_at = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
|
|
|
items = relationship("LookupItem", back_populates="list", lazy="raise",
|
|
cascade="all, delete-orphan")
|
|
|
|
@property
|
|
def is_platform(self) -> bool:
|
|
return self.tenant_id is None
|
|
|
|
|
|
class LookupItem(Base):
|
|
__tablename__ = "lookup_items"
|
|
|
|
id = Column(UUID(as_uuid=True), primary_key=True,
|
|
server_default=func.gen_random_uuid())
|
|
tenant_id = Column(UUID(as_uuid=True), ForeignKey("tenants.id", ondelete="CASCADE"))
|
|
list_id = Column(UUID(as_uuid=True),
|
|
ForeignKey("lookup_lists.id", ondelete="CASCADE"), nullable=False)
|
|
code = Column(String(60), nullable=False)
|
|
label = Column(String(200), nullable=False)
|
|
metadata_json = Column(JSONB)
|
|
sort_order = Column(Integer, nullable=False, default=0)
|
|
is_active = Column(Boolean, nullable=False, default=True)
|
|
created_at = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
|
|
|
list = relationship("LookupList", back_populates="items", lazy="raise")
|
|
|
|
@property
|
|
def is_platform(self) -> bool:
|
|
return self.tenant_id is None
|