Files
rengine/shared/models/notification.py
T
Yogesh Ojha c6c5c61389 Refactor project and tag management, enhance error handling, and improve UI components
- Updated project slug generation to ensure uniqueness by checking active projects.
- Modified project retrieval functions to only fetch active projects.
- Introduced unique slug generation for tags associated with specific projects.
- Enhanced user authentication flow with improved error handling and session management.
- Added session expiration handling in the API client and SSE client for better user experience.
- Improved UI components for better accessibility and user feedback, including copy buttons and activity drawers.
- Refactored notification handling to ensure proper session management and state reset on logout.
- Removed unused enrichment components to streamline the codebase.
- Updated notification model to simplify JSONB handling and ensure proper expiration logic.
2026-05-23 14:33:19 +05:30

81 lines
2.2 KiB
Python

from datetime import datetime, timedelta
from pydantic import BaseModel, Field, field_validator
from sqlalchemy import Text
from sqlalchemy.dialects.postgresql import JSONB
from sqlmodel import Column, SQLModel
from sqlmodel import Field as SQLField
from shared.enums.notification import NotificationSeverity, NotificationType
from shared.utils.datetime import utc_now
MAX_URL_LENGTH = 500
class NotificationMetadata(BaseModel):
url: str | None = None
open_new_tab: bool = False
scan_id: str | None = None
target_id: str | None = None
action_label: str | None = Field(default=None, max_length=50)
@field_validator("url")
@classmethod
def validate_url(cls, v: str | None) -> str | None:
if v is None:
return v
if (
not v.startswith("/")
and not v.startswith("http://")
and not v.startswith("https://")
):
msg = "URL must be relative (start with /) or absolute (http/https)"
raise ValueError(msg)
if len(v) > MAX_URL_LENGTH:
msg = f"URL too long (max {MAX_URL_LENGTH} characters)"
raise ValueError(msg)
return v
class NotificationBase(SQLModel):
type: NotificationType
severity: NotificationSeverity
title: str = SQLField(max_length=200)
message: str = SQLField(sa_column=Column(Text))
class Notification(NotificationBase, table=True):
__tablename__ = "notifications"
id: int = SQLField(default=None, primary_key=True, index=True)
notification_metadata: dict = SQLField(
default_factory=dict, sa_column=Column(JSONB)
)
is_read: bool = SQLField(default=False, index=True)
created_at: datetime = SQLField(default_factory=utc_now, index=True)
expires_at: datetime = SQLField(
default_factory=lambda: utc_now() + timedelta(days=7),
index=True,
)
class NotificationCreate(NotificationBase):
notification_metadata: NotificationMetadata | None = None
class NotificationRead(NotificationBase):
id: int
notification_metadata: dict
is_read: bool
created_at: datetime
expires_at: datetime
class NotificationMarkRead(BaseModel):
notification_id: int
class NotificationStats(BaseModel):
total: int
unread: int