from __future__ import annotations
from onegov.core.orm import Base, observes
from onegov.core.orm.mixins import ContentMixin, TimestampMixin
from onegov.core.utils import normalize_for_url
from sqlalchemy import Enum
from sqlalchemy.orm import mapped_column, Mapped
from uuid import uuid4, UUID
from typing import Literal, TypeAlias
[docs]
Medium: TypeAlias = Literal['phone', 'email', 'http']
[docs]
class GenericRecipient(Base, ContentMixin, TimestampMixin):
""" A generic recipient class with polymorphic support. """
[docs]
__tablename__ = 'generic_recipients'
#: the internal id of the recipient
[docs]
id: Mapped[UUID] = mapped_column(
primary_key=True,
default=uuid4
)
#: the recipient name
#: the order of the records (set to the normalized name + medium)
[docs]
order: Mapped[str] = mapped_column(index=True)
#: the medium over which notifications are sent
[docs]
medium: Mapped[Medium] = mapped_column(
Enum(
'phone',
'email',
'http',
name='recipient_medium'
)
)
#: the phone number, e-mail, url, etc. of the recipient (matches medium)
#: extra information associated with the address (say POST/GET for http)
#: the polymorphic recipient type
[docs]
type: Mapped[str] = mapped_column(default=lambda: 'generic')
[docs]
__mapper_args__ = {
'polymorphic_on': 'type',
'polymorphic_identity': 'generic'
}
@observes('name')
[docs]
def name_observer(self, name: str) -> None:
self.order = normalize_for_url(name)