Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /models /notification_model.py
| """ | |
| Notification Model for Zen AI Pentest | |
| This module provides data models for notification management including | |
| dataclasses for notifications, notification types/channels, and status tracking. | |
| """ | |
| from dataclasses import dataclass, field | |
| from datetime import datetime, timezone | |
| from enum import Enum | |
| from typing import Any, Dict, List, Optional | |
| class NotificationType(Enum): | |
| """Types of notifications that can be sent.""" | |
| INFO = "info" | |
| WARNING = "warning" | |
| ERROR = "error" | |
| CRITICAL = "critical" | |
| SUCCESS = "success" | |
| class NotificationChannel(Enum): | |
| """Available notification channels.""" | |
| EMAIL = "email" | |
| SLACK = "slack" | |
| DISCORD = "discord" | |
| TELEGRAM = "telegram" | |
| WEBHOOK = "webhook" | |
| SMS = "sms" | |
| IN_APP = "in_app" | |
| class NotificationStatus(Enum): | |
| """Status of a notification.""" | |
| PENDING = "pending" | |
| QUEUED = "queued" | |
| SENDING = "sending" | |
| SENT = "sent" | |
| DELIVERED = "delivered" | |
| FAILED = "failed" | |
| RETRYING = "retrying" | |
| CANCELLED = "cancelled" | |
| class NotificationAttachment: | |
| """Attachment for a notification.""" | |
| filename: str | |
| content: bytes | |
| mime_type: str = "application/octet-stream" | |
| class Notification: | |
| """Main notification dataclass.""" | |
| id: str | |
| title: str | |
| message: str | |
| notification_type: NotificationType = NotificationType.INFO | |
| channels: List[NotificationChannel] = field(default_factory=list) | |
| status: NotificationStatus = NotificationStatus.PENDING | |
| recipient: Optional[str] = None | |
| created_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc)) | |
| sent_at: Optional[datetime] = None | |
| delivered_at: Optional[datetime] = None | |
| retry_count: int = 0 | |
| max_retries: int = 3 | |
| metadata: Dict[str, Any] = field(default_factory=dict) | |
| attachments: List[NotificationAttachment] = field(default_factory=list) | |
| error_message: Optional[str] = None | |
| def __post_init__(self): | |
| """Validate notification after initialization.""" | |
| if not self.title: | |
| raise ValueError("Title cannot be empty") | |
| if not self.message: | |
| raise ValueError("Message cannot be empty") | |
| def mark_as_sent(self) -> None: | |
| """Mark notification as sent.""" | |
| self.status = NotificationStatus.SENT | |
| self.sent_at = datetime.now(timezone.utc) | |
| def mark_as_delivered(self) -> None: | |
| """Mark notification as delivered.""" | |
| self.status = NotificationStatus.DELIVERED | |
| self.delivered_at = datetime.now(timezone.utc) | |
| def mark_as_failed(self, error: str) -> None: | |
| """Mark notification as failed with error message.""" | |
| self.status = NotificationStatus.FAILED | |
| self.error_message = error | |
| def increment_retry(self) -> bool: | |
| """Increment retry count and return if more retries are allowed.""" | |
| self.retry_count += 1 | |
| if self.retry_count < self.max_retries: | |
| self.status = NotificationStatus.RETRYING | |
| return True | |
| return False | |
| def to_dict(self) -> Dict[str, Any]: | |
| """Convert notification to dictionary.""" | |
| return { | |
| "id": self.id, | |
| "title": self.title, | |
| "message": self.message, | |
| "type": self.notification_type.value, | |
| "channels": [ch.value for ch in self.channels], | |
| "status": self.status.value, | |
| "recipient": self.recipient, | |
| "created_at": (self.created_at.isoformat() if self.created_at else None), | |
| "sent_at": self.sent_at.isoformat() if self.sent_at else None, | |
| "delivered_at": (self.delivered_at.isoformat() if self.delivered_at else None), | |
| "retry_count": self.retry_count, | |
| "max_retries": self.max_retries, | |
| "metadata": self.metadata, | |
| "error_message": self.error_message, | |
| } | |
| def is_delivered(self) -> bool: | |
| """Check if notification has been delivered.""" | |
| return self.status == NotificationStatus.DELIVERED | |
| def is_failed(self) -> bool: | |
| """Check if notification has failed.""" | |
| return self.status == NotificationStatus.FAILED | |
| def can_retry(self) -> bool: | |
| """Check if notification can be retried.""" | |
| return self.retry_count < self.max_retries and self.status in ( | |
| NotificationStatus.FAILED, | |
| NotificationStatus.RETRYING, | |
| ) | |
Xet Storage Details
- Size:
- 4.44 kB
- Xet hash:
- c93e6c2f5d5d1b68591a4e67fe28579702eaaddea1d79b366f941b68922030ca
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.