Files
celtic-life-science/app/chemists/models.py
Shubhankar45 0d30c58bf0 Server backup 2026-11-07 of uncommitted work in projects/pharma-web-celtic
Snapshot of the working tree on srv1071960 (branch main), taken during
server cleanup. .env files, node_modules, builds, logs and media were excluded.
2026-11-07 05:43:12 +00:00

70 lines
2.8 KiB
Python

"""Chemist master data and chemist → MR assignment (same shape as doctors)."""
import uuid
from datetime import date
from sqlalchemy import Boolean, Date, ForeignKey, Index, String, UniqueConstraint, text
from sqlalchemy.dialects.postgresql import UUID as PGUUID
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.common.enums import MasterApproval
from app.database.base import Base, TimestampMixin, UUIDMixin
class Chemist(Base, UUIDMixin, TimestampMixin):
__tablename__ = "chemist"
__table_args__ = (
UniqueConstraint(
"headquarters_id", "name", "shop_address", name="uq_chemist_hq_name_shop"
),
)
headquarters_id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), ForeignKey("headquarters.id", ondelete="RESTRICT"), nullable=False
)
name: Mapped[str] = mapped_column(String(200), nullable=False)
shop_address: Mapped[str] = mapped_column(String(500), nullable=False)
contact_number: Mapped[str | None] = mapped_column(String(20))
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
#: PENDING until the Area Business Manager approves one an MR added; only APPROVED ones are listed or visited.
approval_status: Mapped[str] = mapped_column(
String(10), default=MasterApproval.APPROVED.value, server_default=MasterApproval.APPROVED.value, nullable=False
)
#: The MR who asked for it (None for one the administrator created).
requested_by_user_id: Mapped[uuid.UUID | None] = mapped_column(
PGUUID(as_uuid=True), ForeignKey("user.id", ondelete="SET NULL")
)
rejection_reason: Mapped[str | None] = mapped_column(String(500))
requested_by: Mapped["User | None"] = relationship( # noqa: F821
lazy="selectin", foreign_keys=[requested_by_user_id]
)
@property
def requested_by_name(self) -> str | None:
return self.requested_by.full_name if self.requested_by else None
assignments: Mapped[list["ChemistAssignment"]] = relationship(back_populates="chemist")
class ChemistAssignment(Base, UUIDMixin, TimestampMixin):
__tablename__ = "chemist_assignment"
__table_args__ = (
Index(
"uq_active_chemist_assignment",
"chemist_id",
unique=True,
postgresql_where=text("effective_to IS NULL"),
),
)
chemist_id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), ForeignKey("chemist.id", ondelete="CASCADE"), nullable=False
)
mr_user_id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), ForeignKey("user.id", ondelete="RESTRICT"), nullable=False
)
effective_from: Mapped[date] = mapped_column(Date, nullable=False)
effective_to: Mapped[date | None] = mapped_column(Date)
chemist: Mapped["Chemist"] = relationship(back_populates="assignments")