Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /nfraxlab_svc-infra /src /svc_infra /notifications /mixin.py
| """SQLAlchemy mixin for notification models.""" | |
| from __future__ import annotations | |
| from datetime import datetime | |
| from uuid import UUID | |
| from sqlalchemy import DateTime, String, Text, func | |
| from sqlalchemy.dialects.postgresql import JSONB | |
| from sqlalchemy.orm import Mapped, declared_attr, mapped_column | |
| from svc_infra.db.sql.types import GUID | |
| class NotificationMixin: | |
| """SQLAlchemy mixin providing notification fields. | |
| Applications extend this mixin to create their notification table, | |
| adding any app-specific fields they need. | |
| Example: | |
| from svc_infra.notifications import NotificationMixin | |
| from your_app.models import Base | |
| class Notification(Base, NotificationMixin): | |
| __tablename__ = "notifications" | |
| # App-specific fields | |
| workspace_id: Mapped[UUID | None] = mapped_column( | |
| GUID(), | |
| ForeignKey("workspaces.id", ondelete="CASCADE"), | |
| nullable=True, | |
| index=True, | |
| ) | |
| Recommended indexes: | |
| - (user_id, read_at IS NULL) partial index for unread queries | |
| - (user_id, created_at DESC) for listing | |
| - (type) for filtering by notification type | |
| """ | |
| def id(cls) -> Mapped[UUID]: | |
| """Primary key.""" | |
| return mapped_column(GUID(), primary_key=True) | |
| def user_id(cls) -> Mapped[UUID]: | |
| """Target user for this notification.""" | |
| return mapped_column(GUID(), nullable=False, index=True) | |
| def type(cls) -> Mapped[str]: | |
| """Notification type (e.g., 'workspace_invite', 'budget_warning').""" | |
| return mapped_column(String(100), nullable=False, index=True) | |
| def title(cls) -> Mapped[str]: | |
| """Short title displayed prominently.""" | |
| return mapped_column(String(255), nullable=False) | |
| def body(cls) -> Mapped[str]: | |
| """Longer description text.""" | |
| return mapped_column(Text, nullable=False) | |
| def data(cls) -> Mapped[dict]: | |
| """Arbitrary JSON data for the notification.""" | |
| return mapped_column(JSONB, nullable=False, server_default="{}") | |
| def action_url(cls) -> Mapped[str | None]: | |
| """URL to navigate when notification is clicked.""" | |
| return mapped_column(String(500), nullable=True) | |
| def read_at(cls) -> Mapped[datetime | None]: | |
| """Timestamp when notification was read (null if unread).""" | |
| return mapped_column(DateTime(timezone=True), nullable=True) | |
| def created_at(cls) -> Mapped[datetime]: | |
| """Timestamp when notification was created.""" | |
| return mapped_column( | |
| DateTime(timezone=True), | |
| nullable=False, | |
| server_default=func.now(), | |
| ) | |
Xet Storage Details
- Size:
- 2.88 kB
- Xet hash:
- 2176725f4a3e2f32966983a0f4bc61f15b3abe6ba4c6be557fb84f2bb4ce3e58
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.