c3lf-system-3/core/mail/models.py

55 lines
2 KiB
Python
Raw Permalink Normal View History

2024-01-07 19:42:31 +00:00
import random
from django.db import models
from django_softdelete.models import SoftDeleteModel
from core.settings import MAIL_DOMAIN, ACTIVE_SPAM_TRAINING
2024-06-26 16:42:56 +00:00
from authentication.models import ExtendedUser
from core.settings import MAIL_DOMAIN
2024-01-09 21:42:47 +00:00
from files.models import AbstractFile
2024-01-07 19:42:31 +00:00
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)
2024-11-08 21:23:52 +00:00
raw_file = models.FileField(upload_to='raw_mail/')
2024-01-07 19:42:31 +00:00
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()
def train_spam(self):
2024-11-18 01:01:23 +00:00
if ACTIVE_SPAM_TRAINING and self.raw_file.path:
import subprocess
path = self.raw_file.path
subprocess.run(["rspamc", "learn_spam", path])
def train_ham(self):
2024-11-18 01:01:23 +00:00
if ACTIVE_SPAM_TRAINING and self.raw_file.path:
import subprocess
path = self.raw_file.path
subprocess.run(["rspamc", "learn_ham", path])
2024-01-07 19:42:31 +00:00
class EventAddress(models.Model):
id = models.AutoField(primary_key=True)
event = models.ForeignKey(Event, models.SET_NULL, null=True, related_name='addresses')
2024-11-06 06:25:10 +00:00
address = models.CharField(max_length=255, unique=True)
2024-01-09 21:42:47 +00:00
class EmailAttachment(AbstractFile):
email = models.ForeignKey(Email, models.CASCADE, related_name='attachments', null=True)
name = models.CharField(max_length=255)