feat(#357): Paket 6 — Contact-Model ins ContactsPlugin (physischer Move + PEP-562-Lazy-Re-Export-Bruecke, ALEMBIC_OWNED_TABLES gegen Schema-Dual-Ownership, outbox-Vorbestands-Fix in models/__init__)
Check Cross-Plugin Imports / check (push) Has been cancelled
Check Cross-Plugin Imports / check (push) Has been cancelled
This commit is contained in:
+20
-1
@@ -8,7 +8,11 @@ from app.models.backup import Backup
|
||||
from app.models.bank_account import BankAccount
|
||||
from app.models.compliance import ComplianceIncident
|
||||
from app.models.consumer_inbox import ConsumerInbox
|
||||
from app.models.contact import Contact, ContactPerson
|
||||
|
||||
# Contact/ContactPerson: lazy via package __getattr__ (Paket 6) — the physical
|
||||
# model lives in app.plugins.builtins.contacts.models; importing the plugin
|
||||
# framework while app.models is still initializing caused a proven circular
|
||||
# ImportError (app.core.auth -> app.models.session -> ... -> app.plugins).
|
||||
from app.models.contact_folder import ContactFolder
|
||||
from app.models.contact_merge import ContactMergeHistory
|
||||
from app.models.currency import Currency
|
||||
@@ -18,6 +22,7 @@ from app.models.entity_permission import EntityPermission
|
||||
from app.models.entity_policy import EntityPolicy
|
||||
from app.models.group import Group, UserGroup
|
||||
from app.models.notification import Notification, NotificationPreference, NotificationType
|
||||
from app.models.outbox import EventOutbox, OutboxDelivery
|
||||
from app.models.owned_mixin import OwnedMixin
|
||||
from app.models.permission_delegation import PermissionDelegation
|
||||
from app.models.permission_template import PermissionTemplate
|
||||
@@ -83,3 +88,17 @@ from app.models.workspace import ( # noqa: F401
|
||||
WorkspaceUser,
|
||||
WorkspaceWidget,
|
||||
)
|
||||
|
||||
|
||||
# ── Lazy Contact re-export (Paket 6, #357) ──────────────────────────────────
|
||||
# The physical home of Contact/ContactPerson is the ContactsPlugin
|
||||
# (app.plugins.builtins.contacts.models). Resolving them lazily via package
|
||||
# __getattr__ keeps ``from app.models import *`` (alembic/env.py) working
|
||||
# while avoiding a plugin-framework import during app.models initialization
|
||||
# (proven circular ImportError, see app/models/contact.py).
|
||||
def __getattr__(name: str):
|
||||
if name in {"Contact", "ContactPerson"}:
|
||||
from app.models.contact import Contact, ContactPerson
|
||||
|
||||
return {"Contact": Contact, "ContactPerson": ContactPerson}[name]
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
|
||||
+31
-248
@@ -1,258 +1,41 @@
|
||||
"""Unified Contact model — company or person, with inline addresses.
|
||||
"""Contact model - backwards-compatibility re-export (Paket 6, #357).
|
||||
|
||||
Based on Rentman's contact model: a single table with type field
|
||||
('company' or 'person'). ContactPerson is a 1:N child for
|
||||
ansprechpartner (company employees / contact persons).
|
||||
The physical home of Contact/ContactPerson moved to the ContactsPlugin:
|
||||
app/plugins.builtins.contacts.models
|
||||
|
||||
This module re-exports both classes lazily (PEP 562 ``__getattr__``) so every
|
||||
existing import keeps working - ``from app.models.contact import Contact``
|
||||
resolves at attribute-access time:
|
||||
- alembic/env.py (``from app.models import *`` -> Base.metadata stays
|
||||
complete; Autogenerate never sees the tables as removed)
|
||||
- Core services (worker.py, jobs.py, address_service.py, ...)
|
||||
- 19 test files and scripts
|
||||
|
||||
Why LAZY and not a top-level import: app/models/__init__.py is imported very
|
||||
early (app.core.auth imports app.models.session). A top-level plugin import
|
||||
here would pull in app.plugins -> registry -> service_container -> cache ->
|
||||
app.core.auth while app.core.auth is still initializing -> circular ImportError
|
||||
(proven in the Paket 6 red run). With PEP 562 the plugin framework is only
|
||||
touched when Contact is actually accessed, long after app.models finished
|
||||
initializing - every entry order is cycle-free.
|
||||
|
||||
The cross-plugin checker (scripts/check_cross_plugin_imports.py) lists this
|
||||
file in EXEMPT_PATHS: the re-export is the deliberate, documented bridge -
|
||||
the plugin OWNS the model; the core only mirrors it for import stability.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from decimal import Decimal
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import (
|
||||
Computed,
|
||||
DateTime,
|
||||
Float,
|
||||
ForeignKey,
|
||||
Index,
|
||||
Numeric,
|
||||
String,
|
||||
Text,
|
||||
UniqueConstraint,
|
||||
)
|
||||
from sqlalchemy.dialects.postgresql import JSONB, TSVECTOR
|
||||
from sqlalchemy.dialects.postgresql import UUID as PGUUID
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.core.db import Base, TenantMixin
|
||||
from app.models.owned_mixin import OwnedMixin
|
||||
_EXPORTS = {"Contact", "ContactPerson"}
|
||||
|
||||
|
||||
class Contact(Base, TenantMixin, OwnedMixin):
|
||||
"""Unified contact entity — can be a company or a person.
|
||||
def __getattr__(name: str):
|
||||
if name in _EXPORTS:
|
||||
from app.plugins.builtins.contacts.models import Contact, ContactPerson
|
||||
|
||||
type='company': name is the company name, firstname/surname empty.
|
||||
type='person': firstname/surname are the person's name, name empty.
|
||||
Both types can have contactpersons (1:N) and inline addresses
|
||||
(mailing, visit, invoice).
|
||||
"""
|
||||
|
||||
__tablename__ = "contacts"
|
||||
indexed_at: Mapped[Any] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
__table_args__ = (
|
||||
UniqueConstraint("tenant_id", "code", name="uq_contacts_tenant_code"),
|
||||
UniqueConstraint("tenant_id", "accounting_code", name="uq_contacts_tenant_accounting_code"),
|
||||
Index("ix_contacts_tenant_deleted", "tenant_id", "deleted_at"),
|
||||
Index("ix_contacts_tenant_type", "tenant_id", "type"),
|
||||
Index("ix_contacts_tenant_name", "tenant_id", "name"),
|
||||
Index("ix_contacts_tenant_displayname", "tenant_id", "displayname"),
|
||||
Index("ix_contacts_email", "email_1"),
|
||||
Index("ix_contacts_code", "code"),
|
||||
Index("ix_contacts_search_vec", "search_tsv", postgresql_using="gin"),
|
||||
)
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||
)
|
||||
|
||||
# ── Identity & Type ──
|
||||
type: Mapped[str] = mapped_column(String(20), nullable=False, default="company") # 'company' or 'person'
|
||||
displayname: Mapped[str] = mapped_column(String(255), nullable=False, default="")
|
||||
|
||||
# ── Lifecycle Status (state machine: lead → qualified → customer → inactive) ──
|
||||
status: Mapped[str] = mapped_column(String(30), nullable=False, default="lead", index=True)
|
||||
name: Mapped[str | None] = mapped_column(String(255), nullable=True) # company name
|
||||
firstname: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
surname: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
suffix: Mapped[str | None] = mapped_column(String(50), nullable=True) # name prefix (Dr., Prof.)
|
||||
ext_name_line: Mapped[str | None] = mapped_column(String(255), nullable=True) # additional name line / subtitle
|
||||
gender: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
|
||||
# ── Customer / Accounting ──
|
||||
code: Mapped[str | None] = mapped_column(String(100), nullable=True) # customer number
|
||||
accounting_code: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
vendor_accounting_code: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
|
||||
# ── Mailing Address (inline) ──
|
||||
mailing_street: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
mailing_number: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
mailing_unit_number: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
mailing_district: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
mailing_extra_address_line: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
mailing_postalcode: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
mailing_city: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
mailing_state: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
mailing_country: Mapped[str | None] = mapped_column(String(2), nullable=True)
|
||||
|
||||
# ── Visit Address (inline) ──
|
||||
visit_street: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
visit_number: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
visit_unit_number: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
visit_district: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
visit_extra_address_line: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
visit_postalcode: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
visit_city: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
visit_state: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
|
||||
# ── Invoice Address (inline) ──
|
||||
invoice_street: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
invoice_number: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
invoice_unit_number: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
invoice_district: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
invoice_extra_address_line: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
invoice_postalcode: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
invoice_city: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
invoice_state: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
invoice_country: Mapped[str | None] = mapped_column(String(2), nullable=True)
|
||||
|
||||
# ── General country ──
|
||||
country: Mapped[str | None] = mapped_column(String(2), nullable=True)
|
||||
|
||||
# ── Communication ──
|
||||
phone_1: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
phone_2: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
email_1: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
email_2: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
website: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
||||
|
||||
# ── Financial & Tax ──
|
||||
vat_code: Mapped[str | None] = mapped_column(String(50), nullable=True) # USt-IdNr.
|
||||
fiscal_code: Mapped[str | None] = mapped_column(String(50), nullable=True) # Steuernummer
|
||||
commerce_code: Mapped[str | None] = mapped_column(String(100), nullable=True) # Handelsregister
|
||||
purchase_number: Mapped[str | None] = mapped_column(String(100), nullable=True) # Bestellnummer
|
||||
bic: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
bank_account: Mapped[str | None] = mapped_column(String(50), nullable=True) # IBAN
|
||||
|
||||
# ── Discounts ──
|
||||
discount_crew: Mapped[Decimal] = mapped_column(Numeric(5, 2), nullable=False, default=0)
|
||||
discount_transport: Mapped[Decimal] = mapped_column(Numeric(5, 2), nullable=False, default=0)
|
||||
discount_rental: Mapped[Decimal] = mapped_column(Numeric(5, 2), nullable=False, default=0)
|
||||
discount_sale: Mapped[Decimal] = mapped_column(Numeric(5, 2), nullable=False, default=0)
|
||||
discount_subrent: Mapped[Decimal] = mapped_column(Numeric(5, 2), nullable=False, default=0)
|
||||
discount_total: Mapped[Decimal] = mapped_column(Numeric(5, 2), nullable=False, default=0)
|
||||
|
||||
# ── Geo ──
|
||||
latitude: Mapped[float | None] = mapped_column(Float, nullable=True)
|
||||
longitude: Mapped[float | None] = mapped_column(Float, nullable=True)
|
||||
|
||||
# ── Notes & Warnings ──
|
||||
projectnote: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
projectnote_title: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
contact_warning: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
tags: Mapped[str | None] = mapped_column(String(500), nullable=True) # comma-separated
|
||||
image: Mapped[str | None] = mapped_column(Text, nullable=True) # logo/image URL or base64
|
||||
|
||||
# ── Default contact persons (self-referential via contactpersons table) ──
|
||||
default_person_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("contactpersons.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
admin_contactperson_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("contactpersons.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
|
||||
# ── Folder assignment ──
|
||||
folder_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
PGUUID(as_uuid=True),
|
||||
ForeignKey("contact_folders.id", ondelete="SET NULL"),
|
||||
nullable=True,
|
||||
index=True,
|
||||
)
|
||||
|
||||
# ── Custom fields ──
|
||||
custom: Mapped[dict | None] = mapped_column(JSONB, nullable=True, default=dict)
|
||||
|
||||
# ── FTS ──
|
||||
search_tsv: Mapped[Any] = mapped_column(
|
||||
TSVECTOR,
|
||||
Computed(
|
||||
"to_tsvector('german', coalesce(name, '') || ' ' || coalesce(displayname, '') || ' ' || coalesce(firstname, '') || ' ' || coalesce(surname, '') || ' ' || coalesce(email_1, '') || ' ' || coalesce(email_2, '') || ' ' || coalesce(code, '') || ' ' || coalesce(phone_1, '') || ' ' || coalesce(phone_2, '') || ' ' || coalesce(mailing_city, '') || ' ' || coalesce(mailing_postalcode, '') || ' ' || coalesce(tags, ''))",
|
||||
persisted=True,
|
||||
),
|
||||
nullable=True,
|
||||
)
|
||||
|
||||
# ── Embedding (pgvector, 768-dim) ──
|
||||
from pgvector.sqlalchemy import Vector
|
||||
embedding: Mapped[Any | None] = mapped_column(
|
||||
Vector(768), nullable=True, default=None
|
||||
)
|
||||
|
||||
# ── Audit ──
|
||||
created_by: Mapped[uuid.UUID | None] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
updated_by: Mapped[uuid.UUID | None] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
|
||||
# ── Relationships ──
|
||||
contact_persons: Mapped[list[ContactPerson]] = relationship(
|
||||
back_populates="contact", cascade="all, delete-orphan", foreign_keys="ContactPerson.contact_id"
|
||||
)
|
||||
return {"Contact": Contact, "ContactPerson": ContactPerson}[name]
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
|
||||
|
||||
class ContactPerson(Base, TenantMixin, OwnedMixin):
|
||||
"""Ansprechpartner — 1:N child of a Contact.
|
||||
|
||||
Represents a person working at / associated with a company contact.
|
||||
Has its own address and communication fields.
|
||||
"""
|
||||
|
||||
__tablename__ = "contactpersons"
|
||||
__table_args__ = (
|
||||
Index("ix_contactpersons_tenant_deleted", "tenant_id", "deleted_at"),
|
||||
Index("ix_contactpersons_contact", "contact_id"),
|
||||
Index("ix_contactpersons_email", "email"),
|
||||
)
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||
)
|
||||
|
||||
# ── Parent contact ──
|
||||
contact_id: Mapped[uuid.UUID] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("contacts.id", ondelete="CASCADE"), nullable=False
|
||||
)
|
||||
|
||||
# ── Name ──
|
||||
displayname: Mapped[str] = mapped_column(String(255), nullable=False, default="")
|
||||
firstname: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
middle_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
lastname: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
function: Mapped[str | None] = mapped_column(String(255), nullable=True) # position/role
|
||||
|
||||
# ── Communication ──
|
||||
phone: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
mobilephone: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
email: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
|
||||
# ── Own address ──
|
||||
street: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
number: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
postalcode: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
city: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
state: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
country: Mapped[str | None] = mapped_column(String(2), nullable=True)
|
||||
|
||||
# ── Other ──
|
||||
tags: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
||||
custom: Mapped[dict | None] = mapped_column(JSONB, nullable=True, default=dict)
|
||||
|
||||
# ── Audit ──
|
||||
created_by: Mapped[uuid.UUID | None] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
updated_by: Mapped[uuid.UUID | None] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
|
||||
# ── Relationship ──
|
||||
contact: Mapped[Contact] = relationship(
|
||||
back_populates="contact_persons", foreign_keys=[contact_id]
|
||||
)
|
||||
|
||||
|
||||
# Keep old names for backward compat during migration
|
||||
|
||||
def __dir__() -> list[str]:
|
||||
return sorted(_EXPORTS | {"__getattr__", "__dir__"})
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
"""Unified Contact model - company or person, with inline addresses.
|
||||
|
||||
Plugin-owned since Paket 6 (#357): this module is the physical home of the
|
||||
Contact/ContactPerson ORM models. app/models/contact.py re-exports them
|
||||
for backwards compatibility (Alembic env.py, Core services, tests).
|
||||
|
||||
Based on Rentman's contact model: a single table with type field
|
||||
('company' or 'person'). ContactPerson is a 1:N child for
|
||||
ansprechpartner (company employees / contact persons).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from decimal import Decimal
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import (
|
||||
Computed,
|
||||
DateTime,
|
||||
Float,
|
||||
ForeignKey,
|
||||
Index,
|
||||
Numeric,
|
||||
String,
|
||||
Text,
|
||||
UniqueConstraint,
|
||||
)
|
||||
from sqlalchemy.dialects.postgresql import JSONB, TSVECTOR
|
||||
from sqlalchemy.dialects.postgresql import UUID as PGUUID
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.core.db import Base, TenantMixin
|
||||
from app.models.owned_mixin import OwnedMixin
|
||||
|
||||
|
||||
class Contact(Base, TenantMixin, OwnedMixin):
|
||||
"""Unified contact entity — can be a company or a person.
|
||||
|
||||
type='company': name is the company name, firstname/surname empty.
|
||||
type='person': firstname/surname are the person's name, name empty.
|
||||
Both types can have contactpersons (1:N) and inline addresses
|
||||
(mailing, visit, invoice).
|
||||
"""
|
||||
|
||||
__tablename__ = "contacts"
|
||||
indexed_at: Mapped[Any] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
__table_args__ = (
|
||||
UniqueConstraint("tenant_id", "code", name="uq_contacts_tenant_code"),
|
||||
UniqueConstraint("tenant_id", "accounting_code", name="uq_contacts_tenant_accounting_code"),
|
||||
Index("ix_contacts_tenant_deleted", "tenant_id", "deleted_at"),
|
||||
Index("ix_contacts_tenant_type", "tenant_id", "type"),
|
||||
Index("ix_contacts_tenant_name", "tenant_id", "name"),
|
||||
Index("ix_contacts_tenant_displayname", "tenant_id", "displayname"),
|
||||
Index("ix_contacts_email", "email_1"),
|
||||
Index("ix_contacts_code", "code"),
|
||||
Index("ix_contacts_search_vec", "search_tsv", postgresql_using="gin"),
|
||||
)
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||
)
|
||||
|
||||
# ── Identity & Type ──
|
||||
type: Mapped[str] = mapped_column(String(20), nullable=False, default="company") # 'company' or 'person'
|
||||
displayname: Mapped[str] = mapped_column(String(255), nullable=False, default="")
|
||||
|
||||
# ── Lifecycle Status (state machine: lead → qualified → customer → inactive) ──
|
||||
status: Mapped[str] = mapped_column(String(30), nullable=False, default="lead", index=True)
|
||||
name: Mapped[str | None] = mapped_column(String(255), nullable=True) # company name
|
||||
firstname: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
surname: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
suffix: Mapped[str | None] = mapped_column(String(50), nullable=True) # name prefix (Dr., Prof.)
|
||||
ext_name_line: Mapped[str | None] = mapped_column(String(255), nullable=True) # additional name line / subtitle
|
||||
gender: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
|
||||
# ── Customer / Accounting ──
|
||||
code: Mapped[str | None] = mapped_column(String(100), nullable=True) # customer number
|
||||
accounting_code: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
vendor_accounting_code: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
|
||||
# ── Mailing Address (inline) ──
|
||||
mailing_street: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
mailing_number: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
mailing_unit_number: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
mailing_district: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
mailing_extra_address_line: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
mailing_postalcode: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
mailing_city: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
mailing_state: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
mailing_country: Mapped[str | None] = mapped_column(String(2), nullable=True)
|
||||
|
||||
# ── Visit Address (inline) ──
|
||||
visit_street: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
visit_number: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
visit_unit_number: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
visit_district: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
visit_extra_address_line: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
visit_postalcode: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
visit_city: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
visit_state: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
|
||||
# ── Invoice Address (inline) ──
|
||||
invoice_street: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
invoice_number: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
invoice_unit_number: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
invoice_district: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
invoice_extra_address_line: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
invoice_postalcode: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
invoice_city: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
invoice_state: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
invoice_country: Mapped[str | None] = mapped_column(String(2), nullable=True)
|
||||
|
||||
# ── General country ──
|
||||
country: Mapped[str | None] = mapped_column(String(2), nullable=True)
|
||||
|
||||
# ── Communication ──
|
||||
phone_1: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
phone_2: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
email_1: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
email_2: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
website: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
||||
|
||||
# ── Financial & Tax ──
|
||||
vat_code: Mapped[str | None] = mapped_column(String(50), nullable=True) # USt-IdNr.
|
||||
fiscal_code: Mapped[str | None] = mapped_column(String(50), nullable=True) # Steuernummer
|
||||
commerce_code: Mapped[str | None] = mapped_column(String(100), nullable=True) # Handelsregister
|
||||
purchase_number: Mapped[str | None] = mapped_column(String(100), nullable=True) # Bestellnummer
|
||||
bic: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
bank_account: Mapped[str | None] = mapped_column(String(50), nullable=True) # IBAN
|
||||
|
||||
# ── Discounts ──
|
||||
discount_crew: Mapped[Decimal] = mapped_column(Numeric(5, 2), nullable=False, default=0)
|
||||
discount_transport: Mapped[Decimal] = mapped_column(Numeric(5, 2), nullable=False, default=0)
|
||||
discount_rental: Mapped[Decimal] = mapped_column(Numeric(5, 2), nullable=False, default=0)
|
||||
discount_sale: Mapped[Decimal] = mapped_column(Numeric(5, 2), nullable=False, default=0)
|
||||
discount_subrent: Mapped[Decimal] = mapped_column(Numeric(5, 2), nullable=False, default=0)
|
||||
discount_total: Mapped[Decimal] = mapped_column(Numeric(5, 2), nullable=False, default=0)
|
||||
|
||||
# ── Geo ──
|
||||
latitude: Mapped[float | None] = mapped_column(Float, nullable=True)
|
||||
longitude: Mapped[float | None] = mapped_column(Float, nullable=True)
|
||||
|
||||
# ── Notes & Warnings ──
|
||||
projectnote: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
projectnote_title: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
contact_warning: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
tags: Mapped[str | None] = mapped_column(String(500), nullable=True) # comma-separated
|
||||
image: Mapped[str | None] = mapped_column(Text, nullable=True) # logo/image URL or base64
|
||||
|
||||
# ── Default contact persons (self-referential via contactpersons table) ──
|
||||
default_person_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("contactpersons.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
admin_contactperson_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("contactpersons.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
|
||||
# ── Folder assignment ──
|
||||
folder_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
PGUUID(as_uuid=True),
|
||||
ForeignKey("contact_folders.id", ondelete="SET NULL"),
|
||||
nullable=True,
|
||||
index=True,
|
||||
)
|
||||
|
||||
# ── Custom fields ──
|
||||
custom: Mapped[dict | None] = mapped_column(JSONB, nullable=True, default=dict)
|
||||
|
||||
# ── FTS ──
|
||||
search_tsv: Mapped[Any] = mapped_column(
|
||||
TSVECTOR,
|
||||
Computed(
|
||||
"to_tsvector('german', coalesce(name, '') || ' ' || coalesce(displayname, '') || ' ' || coalesce(firstname, '') || ' ' || coalesce(surname, '') || ' ' || coalesce(email_1, '') || ' ' || coalesce(email_2, '') || ' ' || coalesce(code, '') || ' ' || coalesce(phone_1, '') || ' ' || coalesce(phone_2, '') || ' ' || coalesce(mailing_city, '') || ' ' || coalesce(mailing_postalcode, '') || ' ' || coalesce(tags, ''))",
|
||||
persisted=True,
|
||||
),
|
||||
nullable=True,
|
||||
)
|
||||
|
||||
# ── Embedding (pgvector, 768-dim) ──
|
||||
from pgvector.sqlalchemy import Vector
|
||||
embedding: Mapped[Any | None] = mapped_column(
|
||||
Vector(768), nullable=True, default=None
|
||||
)
|
||||
|
||||
# ── Audit ──
|
||||
created_by: Mapped[uuid.UUID | None] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
updated_by: Mapped[uuid.UUID | None] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
|
||||
# ── Relationships ──
|
||||
contact_persons: Mapped[list[ContactPerson]] = relationship(
|
||||
back_populates="contact", cascade="all, delete-orphan", foreign_keys="ContactPerson.contact_id"
|
||||
)
|
||||
|
||||
|
||||
class ContactPerson(Base, TenantMixin, OwnedMixin):
|
||||
"""Ansprechpartner — 1:N child of a Contact.
|
||||
|
||||
Represents a person working at / associated with a company contact.
|
||||
Has its own address and communication fields.
|
||||
"""
|
||||
|
||||
__tablename__ = "contactpersons"
|
||||
__table_args__ = (
|
||||
Index("ix_contactpersons_tenant_deleted", "tenant_id", "deleted_at"),
|
||||
Index("ix_contactpersons_contact", "contact_id"),
|
||||
Index("ix_contactpersons_email", "email"),
|
||||
)
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||
)
|
||||
|
||||
# ── Parent contact ──
|
||||
contact_id: Mapped[uuid.UUID] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("contacts.id", ondelete="CASCADE"), nullable=False
|
||||
)
|
||||
|
||||
# ── Name ──
|
||||
displayname: Mapped[str] = mapped_column(String(255), nullable=False, default="")
|
||||
firstname: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
middle_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
lastname: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
function: Mapped[str | None] = mapped_column(String(255), nullable=True) # position/role
|
||||
|
||||
# ── Communication ──
|
||||
phone: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
mobilephone: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
email: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
|
||||
# ── Own address ──
|
||||
street: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
number: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
postalcode: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
city: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
state: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
country: Mapped[str | None] = mapped_column(String(2), nullable=True)
|
||||
|
||||
# ── Other ──
|
||||
tags: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
||||
custom: Mapped[dict | None] = mapped_column(JSONB, nullable=True, default=dict)
|
||||
|
||||
# ── Audit ──
|
||||
created_by: Mapped[uuid.UUID | None] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
updated_by: Mapped[uuid.UUID | None] = mapped_column(
|
||||
PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True
|
||||
)
|
||||
|
||||
# ── Relationship ──
|
||||
contact: Mapped[Contact] = relationship(
|
||||
back_populates="contact_persons", foreign_keys=[contact_id]
|
||||
)
|
||||
|
||||
|
||||
# Keep old names for backward compat during migration
|
||||
|
||||
Reference in New Issue
Block a user