From 5a6349c5d3d8bc9a47866901d9c0a0d5fce8910a Mon Sep 17 00:00:00 2001 From: jedi Date: Sat, 9 Nov 2024 01:00:53 +0100 Subject: [PATCH] train spam on state change to 'closed_spam' --- core/mail/migrations/0006_email_raw_file.py | 6 ++-- .../tickets/migrations/0011_train_old_spam.py | 31 +++++++++++++++++++ core/tickets/models.py | 2 ++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 core/tickets/migrations/0011_train_old_spam.py diff --git a/core/mail/migrations/0006_email_raw_file.py b/core/mail/migrations/0006_email_raw_file.py index 1288bcf..4086af8 100644 --- a/core/mail/migrations/0006_email_raw_file.py +++ b/core/mail/migrations/0006_email_raw_file.py @@ -4,7 +4,6 @@ from django.db import migrations, models class Migration(migrations.Migration): - dependencies = [ ('mail', '0005_alter_eventaddress_event'), ] @@ -13,8 +12,9 @@ class Migration(migrations.Migration): Email = apps.get_model('mail', 'Email') for email in Email.objects.all(): raw_content = email.raw - email.raw_file = ContentFile(raw_content) - email.raw = None + path = "mail_{}".format(email.id) + if len(raw_content): + email.raw_file.save(path, ContentFile(raw_content)) email.save() operations = [ diff --git a/core/tickets/migrations/0011_train_old_spam.py b/core/tickets/migrations/0011_train_old_spam.py new file mode 100644 index 0000000..206cbb4 --- /dev/null +++ b/core/tickets/migrations/0011_train_old_spam.py @@ -0,0 +1,31 @@ +# Generated by Django 4.2.7 on 2024-06-23 02:17 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [ + ('mail', '0006_email_raw_file'), + ('tickets', '0010_issuethread_event_itemrelation_and_more'), + ] + + def train_old_mails(apps, schema_editor): + from tickets.models import IssueThread + for t in IssueThread.objects.all(): + try: + state = t.state + i = 0 + for e in t.emails.all(): + if e.raw_file: + if state == 'closed_spam' and i == 0: + e.train_spam() + else: + e.train_ham() + i += 1 + except: + pass + + operations = [ + migrations.RunPython(train_old_mails), + ] diff --git a/core/tickets/models.py b/core/tickets/models.py index db427fe..aff5d6c 100644 --- a/core/tickets/models.py +++ b/core/tickets/models.py @@ -60,6 +60,8 @@ class IssueThread(SoftDeleteModel): if self.state == value: return self.state_changes.create(state=value) + if value == 'closed_spam' and self.emails.exists(): + self.emails.first().train_spam() @property def assigned_to(self):