from django.db import models from django.conf import settings from simple_history.models import HistoricalRecords class EmailConfiguration(models.Model): """ Email notification configuration. """ name = models.CharField( max_length=100, default='Default', help_text='Configuration name' ) is_enabled = models.BooleanField( default=True, help_text='Whether email notifications are enabled' ) # Recipients recipient_emails = models.TextField( help_text='Comma-separated list of recipient email addresses' ) # What to notify about notify_on_scrape_complete = models.BooleanField( default=True, help_text='Send email after each scraper job completes' ) notify_on_scrape_failure = models.BooleanField( default=True, help_text='Send email when scraper job fails' ) notify_on_sync_complete = models.BooleanField( default=False, help_text='Send email after CloudKit sync completes' ) notify_on_sync_failure = models.BooleanField( default=True, help_text='Send email when CloudKit sync fails' ) notify_on_new_reviews = models.BooleanField( default=True, help_text='Include review items in scrape notifications' ) # Thresholds min_games_for_notification = models.PositiveIntegerField( default=0, help_text='Minimum games changed to trigger notification (0 = always)' ) # Metadata created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) # Audit trail history = HistoricalRecords() class Meta: verbose_name = 'Email Configuration' verbose_name_plural = 'Email Configurations' def __str__(self): return self.name def get_recipients(self): """Return list of recipient emails.""" return [ email.strip() for email in self.recipient_emails.split(',') if email.strip() ] class EmailLog(models.Model): """ Log of sent email notifications. """ STATUS_CHOICES = [ ('sent', 'Sent'), ('failed', 'Failed'), ] configuration = models.ForeignKey( EmailConfiguration, on_delete=models.SET_NULL, null=True, blank=True, related_name='logs' ) subject = models.CharField(max_length=255) recipients = models.TextField( help_text='Comma-separated list of recipients' ) body_preview = models.TextField( blank=True, help_text='First 500 chars of email body' ) status = models.CharField( max_length=10, choices=STATUS_CHOICES ) error_message = models.TextField(blank=True) # Related objects scrape_job = models.ForeignKey( 'scraper.ScrapeJob', on_delete=models.SET_NULL, null=True, blank=True, related_name='email_logs' ) sync_job = models.ForeignKey( 'cloudkit.CloudKitSyncJob', on_delete=models.SET_NULL, null=True, blank=True, related_name='email_logs' ) # Metadata created_at = models.DateTimeField(auto_now_add=True) class Meta: ordering = ['-created_at'] verbose_name = 'Email Log' verbose_name_plural = 'Email Logs' def __str__(self): return f"{self.subject} ({self.status})"