This commit is contained in:
Jonas Winkler 2023-02-21 18:37:08 +01:00
parent a37177703c
commit 1189df1fe6
4 changed files with 97 additions and 8 deletions

View File

@ -3,6 +3,7 @@ from django.contrib import admin
from django.utils.translation import gettext_lazy as _
from paperless_mail.models import MailAccount
from paperless_mail.models import MailRule
from paperless_mail.models import ProcessedMail
class MailAccountAdminForm(forms.ModelForm):
@ -105,5 +106,31 @@ class MailRuleAdmin(admin.ModelAdmin):
ordering = ["order"]
class ProcessedMailAdmin(admin.ModelAdmin):
class Meta:
model = ProcessedMail
fields = "__all__"
list_display = ("rule", "processed", "status", "subject", "received")
ordering = ["-processed"]
readonly_fields = [
"owner",
"processed",
"received",
"status",
"subject",
"error",
"uid",
"folder",
"rule",
]
list_filter = ("status",)
admin.site.register(MailAccount, MailAccountAdmin)
admin.site.register(MailRule, MailRuleAdmin)
admin.site.register(ProcessedMail, ProcessedMailAdmin)

View File

@ -1,3 +1,4 @@
import datetime
import itertools
import os
import re
@ -153,6 +154,8 @@ def apply_mail_action(
result: str,
rule_id: int,
message_uid: str,
message_subject: str,
message_date: datetime.datetime,
):
rule = MailRule.objects.get(pk=rule_id)
account = MailAccount.objects.get(pk=rule.account.pk)
@ -171,17 +174,23 @@ def apply_mail_action(
action.post_consume(M, message_uid, rule.action_parameter)
ProcessedMail.objects.create(
owner=rule.owner,
rule=rule,
folder=rule.folder,
uid=message_uid,
subject=message_subject,
received=message_date,
status="SUCCESS",
)
except Exception as e:
ProcessedMail.objects.create(
owner=rule.owner,
rule=rule,
folder=rule.folder,
uid=message_uid,
subject=message_subject,
received=message_date,
status="FAILED",
error=traceback.format_exc(),
)
@ -189,13 +198,23 @@ def apply_mail_action(
@shared_task
def error_callback(request, exc, tb, rule_id: int, message_uid: str):
def error_callback(
request,
exc,
tb,
rule_id: int,
message_uid: str,
message_subject: str,
message_date: datetime.datetime,
):
rule = MailRule.objects.get(pk=rule_id)
ProcessedMail.objects.create(
rule=rule,
folder=rule.folder,
uid=message_uid,
subject=message_subject,
received=message_date,
status="FAILED",
error=traceback.format_exc(),
)
@ -204,14 +223,21 @@ def error_callback(request, exc, tb, rule_id: int, message_uid: str):
def queue_consumption_tasks(
consume_tasks: list[Signature],
rule: MailRule,
message_uid: str,
message: MailMessage,
):
mail_action_task = apply_mail_action.s(
rule_id=rule.pk,
message_uid=message_uid,
message_uid=message.uid,
message_subject=message.subject,
message_date=message.date,
)
chord(header=consume_tasks, body=mail_action_task).on_error(
error_callback.s(rule_id=rule.pk, message_uid=message_uid),
error_callback.s(
rule_id=rule.pk,
message_uid=message.uid,
message_subject=message.subject,
message_date=message.date,
),
).delay()
@ -605,7 +631,7 @@ class MailAccountHandler(LoggingMixin):
f"by paperless",
)
queue_consumption_tasks(consume_tasks, rule, message.uid)
queue_consumption_tasks(consume_tasks, rule, message)
return processed_attachments
@ -663,7 +689,7 @@ class MailAccountHandler(LoggingMixin):
override_owner_id=rule.owner.id if rule.owner else None,
)
queue_consumption_tasks([consume_task], rule, message.uid)
queue_consumption_tasks([consume_task], rule, message)
processed_elements = 1
return processed_elements

View File

@ -1,8 +1,9 @@
# Generated by Django 4.1.5 on 2023-02-21 12:48
# Generated by Django 4.1.5 on 2023-02-21 17:15
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
class Migration(migrations.Migration):
@ -27,6 +28,14 @@ class Migration(migrations.Migration):
),
("folder", models.CharField(max_length=256, verbose_name="folder")),
("uid", models.CharField(max_length=256, verbose_name="folder")),
("subject", models.CharField(max_length=256, verbose_name="subject")),
("received", models.DateTimeField(verbose_name="received")),
(
"processed",
models.DateTimeField(
default=django.utils.timezone.now, verbose_name="processed"
),
),
("status", models.CharField(max_length=256, verbose_name="status")),
(
"error",

View File

@ -1,5 +1,6 @@
import documents.models as document_models
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
@ -223,6 +224,7 @@ class ProcessedMail(document_models.ModelWithOwner):
null=False,
blank=False,
on_delete=models.CASCADE,
editable=False,
)
folder = models.CharField(
@ -230,13 +232,36 @@ class ProcessedMail(document_models.ModelWithOwner):
null=False,
blank=False,
max_length=256,
editable=False,
)
uid = models.CharField(
_("folder"),
_("uid"),
null=False,
blank=False,
max_length=256,
editable=False,
)
subject = models.CharField(
_("subject"),
null=False,
blank=False,
max_length=256,
editable=False,
)
received = models.DateTimeField(
_("received"),
null=False,
blank=False,
editable=False,
)
processed = models.DateTimeField(
_("processed"),
default=timezone.now,
editable=False,
)
status = models.CharField(
@ -244,10 +269,12 @@ class ProcessedMail(document_models.ModelWithOwner):
null=False,
blank=False,
max_length=256,
editable=False,
)
error = models.TextField(
_("error"),
null=True,
blank=True,
editable=False,
)