2026-06-04 00:06:17 +00:00
|
|
|
"""Contact model: people at accounts (or standalone contacts)."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-06-10 21:24:24 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2026-06-04 00:06:17 +00:00
|
|
|
|
|
|
|
|
from sqlalchemy import ForeignKey, String
|
|
|
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
|
|
|
|
|
|
from app.models.base import Base, OrgScopedMixin, SoftDeleteMixin, TimestampMixin
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from app.models.account import Account
|
|
|
|
|
from app.models.activity import Activity
|
|
|
|
|
from app.models.note import Note
|
|
|
|
|
from app.models.tag_link import TagLink
|
2026-06-10 21:24:24 +00:00
|
|
|
from app.models.user import User
|
2026-06-04 00:06:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Contact(Base, TimestampMixin, SoftDeleteMixin, OrgScopedMixin):
|
|
|
|
|
__tablename__ = "contacts"
|
|
|
|
|
|
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
|
|
|
first_name: Mapped[str] = mapped_column(String(128), nullable=False)
|
|
|
|
|
last_name: Mapped[str] = mapped_column(String(128), nullable=False, index=True)
|
2026-06-10 21:24:24 +00:00
|
|
|
email: Mapped[str | None] = mapped_column(String(255), nullable=True, index=True)
|
|
|
|
|
phone: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
|
|
|
|
account_id: Mapped[int | None] = mapped_column(
|
2026-06-04 00:06:17 +00:00
|
|
|
ForeignKey("accounts.id"), nullable=True, index=True
|
|
|
|
|
)
|
2026-06-10 21:24:24 +00:00
|
|
|
owner_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False, index=True)
|
2026-06-04 00:06:17 +00:00
|
|
|
|
|
|
|
|
# Relationships
|
2026-06-10 21:24:24 +00:00
|
|
|
account: Mapped[Account | None] = relationship(
|
2026-06-04 00:06:17 +00:00
|
|
|
"Account", back_populates="contacts", lazy="selectin"
|
|
|
|
|
)
|
2026-06-10 21:24:24 +00:00
|
|
|
owner: Mapped[User] = relationship("User", foreign_keys=[owner_id], lazy="joined")
|
|
|
|
|
activities: Mapped[list[Activity]] = relationship(
|
2026-06-04 00:06:17 +00:00
|
|
|
"Activity", back_populates="contact", lazy="selectin"
|
|
|
|
|
)
|
2026-06-10 21:24:24 +00:00
|
|
|
notes: Mapped[list[Note]] = relationship(
|
2026-06-04 00:06:17 +00:00
|
|
|
"Note",
|
|
|
|
|
primaryjoin="and_(Contact.id==foreign(Note.parent_id), Note.parent_type=='contact')",
|
|
|
|
|
viewonly=True,
|
|
|
|
|
lazy="selectin",
|
|
|
|
|
)
|
2026-06-10 21:24:24 +00:00
|
|
|
tags: Mapped[list[TagLink]] = relationship(
|
2026-06-04 00:06:17 +00:00
|
|
|
"TagLink",
|
|
|
|
|
primaryjoin="and_(Contact.id==foreign(TagLink.parent_id), TagLink.parent_type=='contact')",
|
|
|
|
|
viewonly=True,
|
|
|
|
|
lazy="selectin",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return f"<Contact id={self.id} {self.first_name} {self.last_name!r}>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ["Contact"]
|