summaryrefslogtreecommitdiff
path: root/pgweb/mailqueue/admin.py
blob: 99129923016d11956beb575771be4977f9d6b620 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from django.contrib import admin

from email.parser import Parser
from email import policy

from .models import QueuedMail


class QueuedMailAdmin(admin.ModelAdmin):
    model = QueuedMail
    readonly_fields = ('parsed_content', )
    list_display = ('pk', 'sender', 'receiver', 'sendat')

    def parsed_content(self, obj):
        # We only try to parse the *first* piece, because we assume
        # all our emails are trivial.
        try:
            parser = Parser(policy=policy.default)
            msg = parser.parsestr(obj.fullmsg)
            return msg.get_body(preferencelist=('plain', )).get_payload(decode=True).decode('utf8')
        except Exception as e:
            return "Failed to get body: %s" % e

    parsed_content.short_description = 'Parsed mail'


admin.site.register(QueuedMail, QueuedMailAdmin)