import random from django.db import models from django_softdelete.models import SoftDeleteModel from authentication.models import ExtendedUser from core.settings import MAIL_DOMAIN from files.models import AbstractFile from inventory.models import Event from tickets.models import IssueThread class Email(SoftDeleteModel): id = models.AutoField(primary_key=True) timestamp = models.DateTimeField(auto_now_add=True) body = models.TextField() subject = models.CharField(max_length=255) sender = models.CharField(max_length=255) recipient = models.CharField(max_length=255) reference = models.CharField(max_length=255, null=True, unique=True) in_reply_to = models.CharField(max_length=255, null=True) raw = models.TextField() issue_thread = models.ForeignKey(IssueThread, models.SET_NULL, null=True, related_name='emails') event = models.ForeignKey(Event, models.SET_NULL, null=True) def save(self, *args, **kwargs): super().save(*args, **kwargs) if not self.reference: self.reference = f'<{random.randint(0, 1000000000):09}@{MAIL_DOMAIN}>' self.save() class EventAddress(models.Model): id = models.AutoField(primary_key=True) event = models.ForeignKey(Event, models.SET_NULL, null=True) address = models.CharField(max_length=255) class EmailAttachment(AbstractFile): email = models.ForeignKey(Email, models.CASCADE, related_name='attachments', null=True) name = models.CharField(max_length=255) class UserNotificationChannel(models.Model): user = models.ForeignKey(ExtendedUser, models.CASCADE) channel_type = models.CharField(choices=[('telegram', 'telegram'), ('email', 'email')], max_length=255) channel_target = models.CharField(max_length=255) event_filter = models.CharField(max_length=255) active = models.BooleanField(default=True) created = models.DateTimeField(auto_now_add=True) def validate_constraints(self, exclude=None): # TODO: email -> emailaddress, telegram -> chatid return True