Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /notifications /models.py
| """ | |
| Notification Models for Zen-AI-Pentest | |
| Database models for user notification settings and notification history. | |
| """ | |
| from datetime import datetime, timezone | |
| from typing import Optional | |
| from sqlalchemy import ( | |
| JSON, | |
| Boolean, | |
| Column, | |
| DateTime, | |
| ForeignKey, | |
| Index, | |
| Integer, | |
| String, | |
| Text, | |
| ) | |
| from sqlalchemy.orm import relationship | |
| from database.models import Base | |
| class NotificationSettings(Base): | |
| """User Notification Settings - Per-User Configuration""" | |
| __tablename__ = "notification_settings" | |
| id = Column(Integer, primary_key=True, index=True) | |
| user_id = Column(Integer, ForeignKey("users.id"), unique=True, nullable=False, index=True) | |
| created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc)) | |
| updated_at = Column(DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc)) | |
| # Slack Configuration | |
| slack_enabled = Column(Boolean, default=False) | |
| slack_webhook_url = Column(String(500), nullable=True) | |
| slack_channel = Column(String(100), nullable=True) | |
| # Discord Configuration | |
| discord_enabled = Column(Boolean, default=False) | |
| discord_webhook_url = Column(String(500), nullable=True) | |
| # Email Configuration | |
| email_enabled = Column(Boolean, default=False) | |
| email_address = Column(String(255), nullable=True) | |
| # Telegram Configuration | |
| telegram_enabled = Column(Boolean, default=False) | |
| telegram_bot_token = Column(String(255), nullable=True) | |
| telegram_chat_id = Column(String(100), nullable=True) | |
| # Generic Webhook Configuration | |
| webhook_enabled = Column(Boolean, default=False) | |
| webhook_url = Column(String(500), nullable=True) | |
| webhook_headers = Column(JSON, default=dict) # Custom headers | |
| # Event Subscriptions | |
| notify_on_scan_start = Column(Boolean, default=False) | |
| notify_on_scan_complete = Column(Boolean, default=True) | |
| notify_on_critical = Column(Boolean, default=True) | |
| notify_on_high = Column(Boolean, default=True) | |
| notify_on_medium = Column(Boolean, default=False) | |
| notify_on_system_alert = Column(Boolean, default=True) | |
| notify_on_report_ready = Column(Boolean, default=True) | |
| # Rate Limiting (per user) | |
| rate_limit_per_minute = Column(Integer, default=10) | |
| # Relationship | |
| user = relationship("User", backref="notification_settings") | |
| __table_args__ = (Index("ix_notification_settings_user", "user_id"),) | |
| def to_dict(self) -> dict: | |
| """Convert to dictionary (masks sensitive data)""" | |
| return { | |
| "id": self.id, | |
| "user_id": self.user_id, | |
| "slack": { | |
| "enabled": self.slack_enabled, | |
| "channel": self.slack_channel, | |
| "configured": bool(self.slack_webhook_url), | |
| }, | |
| "discord": { | |
| "enabled": self.discord_enabled, | |
| "configured": bool(self.discord_webhook_url), | |
| }, | |
| "email": { | |
| "enabled": self.email_enabled, | |
| "address": self.email_address, | |
| "configured": bool(self.email_address), | |
| }, | |
| "telegram": { | |
| "enabled": self.telegram_enabled, | |
| "configured": bool(self.telegram_bot_token and self.telegram_chat_id), | |
| }, | |
| "webhook": { | |
| "enabled": self.webhook_enabled, | |
| "url": self._mask_url(self.webhook_url) if self.webhook_url else None, | |
| "configured": bool(self.webhook_url), | |
| }, | |
| "subscriptions": { | |
| "scan_start": self.notify_on_scan_start, | |
| "scan_complete": self.notify_on_scan_complete, | |
| "critical": self.notify_on_critical, | |
| "high": self.notify_on_high, | |
| "medium": self.notify_on_medium, | |
| "system_alert": self.notify_on_system_alert, | |
| "report_ready": self.notify_on_report_ready, | |
| }, | |
| "rate_limit": self.rate_limit_per_minute, | |
| "updated_at": self.updated_at.isoformat() if self.updated_at else None, | |
| } | |
| def _mask_url(self, url: Optional[str]) -> str: | |
| """Mask sensitive parts of URL""" | |
| if not url: | |
| return "" | |
| # Show only first 20 chars and domain | |
| from urllib.parse import urlparse | |
| parsed = urlparse(url) | |
| return f"{parsed.scheme}://{parsed.netloc}/****" | |
| class NotificationHistory(Base): | |
| """History of sent notifications""" | |
| __tablename__ = "notification_history" | |
| id = Column(Integer, primary_key=True, index=True) | |
| user_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True) | |
| # Notification details | |
| channel = Column(String(50), nullable=False, index=True) # slack, discord, email, etc. | |
| event_type = Column(String(50), nullable=False, index=True) # scan_complete, critical_finding, etc. | |
| status = Column(String(20), nullable=False, index=True) # sent, failed, retrying | |
| # Content | |
| title = Column(String(255)) | |
| message = Column(Text) | |
| payload = Column(JSON, default=dict) # Full payload sent | |
| # Response tracking | |
| response_status = Column(String(50)) # HTTP status or error message | |
| retry_count = Column(Integer, default=0) | |
| # Timestamps | |
| created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc), index=True) | |
| sent_at = Column(DateTime) | |
| # Error details | |
| error_message = Column(Text) | |
| __table_args__ = ( | |
| Index("ix_notification_history_user_created", "user_id", "created_at"), | |
| Index("ix_notification_history_status_created", "status", "created_at"), | |
| ) | |
| def to_dict(self) -> dict: | |
| """Convert to dictionary""" | |
| return { | |
| "id": self.id, | |
| "user_id": self.user_id, | |
| "channel": self.channel, | |
| "event_type": self.event_type, | |
| "status": self.status, | |
| "title": self.title, | |
| "message": self.message, | |
| "response_status": self.response_status, | |
| "retry_count": self.retry_count, | |
| "created_at": self.created_at.isoformat() if self.created_at else None, | |
| "sent_at": self.sent_at.isoformat() if self.sent_at else None, | |
| "error_message": self.error_message, | |
| } | |
| class NotificationTemplate(Base): | |
| """Custom notification templates""" | |
| __tablename__ = "notification_templates" | |
| id = Column(Integer, primary_key=True, index=True) | |
| user_id = Column(Integer, ForeignKey("users.id"), nullable=True) # NULL = global | |
| # Template identification | |
| name = Column(String(100), nullable=False) | |
| event_type = Column(String(50), nullable=False, index=True) | |
| channel = Column(String(50), nullable=False) # slack, discord, email, telegram, webhook, all | |
| # Template content (Jinja2) | |
| subject_template = Column(String(500)) # For email | |
| body_template = Column(Text, nullable=False) | |
| # Styling | |
| color = Column(String(20)) # For Slack/Discord (hex or named color) | |
| # Metadata | |
| is_default = Column(Boolean, default=False) | |
| is_enabled = Column(Boolean, default=True) | |
| created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc)) | |
| updated_at = Column(DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc)) | |
| __table_args__ = ( | |
| Index("ix_notification_templates_event", "event_type", "channel"), | |
| Index("ix_notification_templates_user", "user_id", "name"), | |
| ) | |
| def to_dict(self) -> dict: | |
| """Convert to dictionary""" | |
| return { | |
| "id": self.id, | |
| "name": self.name, | |
| "event_type": self.event_type, | |
| "channel": self.channel, | |
| "subject_template": self.subject_template, | |
| "body_template": self.body_template, | |
| "color": self.color, | |
| "is_default": self.is_default, | |
| "is_enabled": self.is_enabled, | |
| "created_at": self.created_at.isoformat() if self.created_at else None, | |
| "updated_at": self.updated_at.isoformat() if self.updated_at else None, | |
| } | |
Xet Storage Details
- Size:
- 8.18 kB
- Xet hash:
- 16a190de12f8c39ef3367878137ab49575fcfa6fdc39761bb8e78f6555eb0b57
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.