2015-07-21 10:44:32 +00:00
|
|
|
import base64
|
|
|
|
import email
|
|
|
|
|
2015-05-26 12:59:16 +00:00
|
|
|
from django import forms
|
2015-05-04 19:52:53 +00:00
|
|
|
from django.contrib import admin
|
2021-04-21 12:27:18 +00:00
|
|
|
from django.urls import reverse
|
2015-05-19 13:27:04 +00:00
|
|
|
from django.db.models import Count
|
2015-05-07 19:00:02 +00:00
|
|
|
from django.shortcuts import redirect
|
2021-05-20 11:58:16 +00:00
|
|
|
from django.utils.html import format_html
|
|
|
|
from django.utils.safestring import mark_safe
|
2023-10-24 16:59:02 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2015-05-04 19:52:53 +00:00
|
|
|
|
2016-05-09 12:02:10 +00:00
|
|
|
from orchestra.admin import ExtendedModelAdmin
|
2015-05-07 19:00:02 +00:00
|
|
|
from orchestra.admin.utils import admin_link, admin_colored, admin_date, wrap_admin_view
|
|
|
|
from orchestra.contrib.tasks import task
|
2015-05-04 19:52:53 +00:00
|
|
|
|
2016-05-09 12:02:10 +00:00
|
|
|
from .actions import last
|
2015-05-07 19:00:02 +00:00
|
|
|
from .engine import send_pending
|
2015-05-04 19:52:53 +00:00
|
|
|
from .models import Message, SMTPLog
|
|
|
|
|
|
|
|
|
2015-07-21 10:44:32 +00:00
|
|
|
COLORS = {
|
2015-05-05 19:42:55 +00:00
|
|
|
Message.QUEUED: 'purple',
|
|
|
|
Message.SENT: 'green',
|
|
|
|
Message.DEFERRED: 'darkorange',
|
|
|
|
Message.FAILED: 'red',
|
|
|
|
SMTPLog.SUCCESS: 'green',
|
|
|
|
SMTPLog.FAILURE: 'red',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-26 13:05:02 +00:00
|
|
|
@admin.register(Message)
|
2016-05-09 12:02:10 +00:00
|
|
|
class MessageAdmin(ExtendedModelAdmin):
|
2015-05-04 19:52:53 +00:00
|
|
|
list_display = (
|
2015-05-26 12:59:16 +00:00
|
|
|
'display_subject', 'colored_state', 'priority', 'to_address', 'from_address',
|
2015-08-05 22:58:35 +00:00
|
|
|
'created_at_delta', 'display_retries', 'last_try_delta',
|
2015-05-04 19:52:53 +00:00
|
|
|
)
|
2015-05-05 19:42:55 +00:00
|
|
|
list_filter = ('state', 'priority', 'retries')
|
2016-05-09 12:02:10 +00:00
|
|
|
list_prefetch_related = ('logs',)
|
2015-08-31 11:58:59 +00:00
|
|
|
search_fields = ('to_address', 'from_address', 'subject',)
|
2015-07-21 10:44:32 +00:00
|
|
|
fieldsets = (
|
|
|
|
(None, {
|
2015-08-05 22:58:35 +00:00
|
|
|
'fields': ('state', 'priority', ('retries', 'last_try_delta', 'created_at_delta'),
|
2015-07-23 12:41:42 +00:00
|
|
|
'display_full_subject', 'display_from', 'display_to',
|
2015-07-21 10:44:32 +00:00
|
|
|
'display_content'),
|
|
|
|
}),
|
|
|
|
(_("Edit"), {
|
|
|
|
'classes': ('collapse',),
|
2015-07-23 12:41:42 +00:00
|
|
|
'fields': ('subject', 'from_address', 'to_address', 'content'),
|
2015-07-21 10:44:32 +00:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
readonly_fields = (
|
2015-08-05 22:58:35 +00:00
|
|
|
'retries', 'last_try_delta', 'created_at_delta', 'display_full_subject',
|
2015-07-23 12:41:42 +00:00
|
|
|
'display_to', 'display_from', 'display_content',
|
2015-07-21 10:44:32 +00:00
|
|
|
)
|
2015-07-23 12:41:42 +00:00
|
|
|
date_hierarchy = 'created_at'
|
2016-05-09 12:02:10 +00:00
|
|
|
change_view_actions = (last,)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-05-05 19:42:55 +00:00
|
|
|
colored_state = admin_colored('state', colors=COLORS)
|
|
|
|
created_at_delta = admin_date('created_at')
|
2015-08-05 22:58:35 +00:00
|
|
|
last_try_delta = admin_date('last_try')
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2024-01-26 13:05:02 +00:00
|
|
|
@admin.display(
|
|
|
|
description=_("Subject"),
|
|
|
|
ordering='subject',
|
|
|
|
)
|
2015-05-07 19:00:02 +00:00
|
|
|
def display_subject(self, instance):
|
2015-07-23 12:41:42 +00:00
|
|
|
subject = instance.subject
|
2015-08-05 22:58:35 +00:00
|
|
|
if len(subject) > 64:
|
2021-05-20 11:58:16 +00:00
|
|
|
return mark_safe(subject[:64] + '…')
|
2015-07-23 12:41:42 +00:00
|
|
|
return subject
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2024-01-26 13:05:02 +00:00
|
|
|
@admin.display(
|
|
|
|
description=_("Retries"),
|
|
|
|
ordering='retries',
|
|
|
|
)
|
2015-08-05 22:58:35 +00:00
|
|
|
def display_retries(self, instance):
|
|
|
|
num_logs = instance.logs__count
|
|
|
|
if num_logs == 1:
|
2015-05-07 19:00:02 +00:00
|
|
|
pk = instance.logs.all()[0].id
|
|
|
|
url = reverse('admin:mailer_smtplog_change', args=(pk,))
|
|
|
|
else:
|
|
|
|
url = reverse('admin:mailer_smtplog_changelist')
|
|
|
|
url += '?&message=%i' % instance.pk
|
2021-05-20 11:58:16 +00:00
|
|
|
return format_html('<a href="{}" onclick="return showAddAnotherPopup(this);">{}</a>', url, instance.retries)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2024-01-26 13:05:02 +00:00
|
|
|
@admin.display(
|
|
|
|
description=_("Content")
|
|
|
|
)
|
2015-07-21 10:44:32 +00:00
|
|
|
def display_content(self, instance):
|
|
|
|
part = email.message_from_string(instance.content)
|
|
|
|
payload = part.get_payload()
|
|
|
|
if isinstance(payload, list):
|
2015-09-23 12:22:32 +00:00
|
|
|
for cpart in payload:
|
|
|
|
cpayload = cpart.get_payload()
|
|
|
|
if cpart.get_content_type().startswith('text/'):
|
|
|
|
part = cpart
|
|
|
|
payload = cpayload
|
|
|
|
if cpart.get_content_type() == 'text/html':
|
|
|
|
payload = '<div style="padding-left:110px">%s</div>' % payload
|
|
|
|
# prioritize HTML
|
|
|
|
break
|
2015-07-21 10:44:32 +00:00
|
|
|
if part.get('Content-Transfer-Encoding') == 'base64':
|
|
|
|
payload = base64.b64decode(payload)
|
2015-09-23 12:22:32 +00:00
|
|
|
charset = part.get_charsets()[0]
|
|
|
|
if charset:
|
|
|
|
payload = payload.decode(charset)
|
2015-07-21 10:44:32 +00:00
|
|
|
if part.get_content_type() == 'text/plain':
|
2015-07-23 12:41:42 +00:00
|
|
|
payload = payload.replace('\n', '<br>').replace(' ', ' ')
|
2021-05-20 11:58:16 +00:00
|
|
|
return mark_safe(payload)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2024-01-26 13:05:02 +00:00
|
|
|
@admin.display(
|
|
|
|
description=_("Subject")
|
|
|
|
)
|
2015-07-21 10:44:32 +00:00
|
|
|
def display_full_subject(self, instance):
|
|
|
|
return instance.subject
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2024-01-26 13:05:02 +00:00
|
|
|
@admin.display(
|
|
|
|
description=_("From")
|
|
|
|
)
|
2015-07-23 12:41:42 +00:00
|
|
|
def display_from(self, instance):
|
2015-07-21 10:44:32 +00:00
|
|
|
return instance.from_address
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2024-01-26 13:05:02 +00:00
|
|
|
@admin.display(
|
|
|
|
description=_("To")
|
|
|
|
)
|
2015-07-23 12:41:42 +00:00
|
|
|
def display_to(self, instance):
|
2015-07-21 10:44:32 +00:00
|
|
|
return instance.to_address
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-05-07 19:00:02 +00:00
|
|
|
def get_urls(self):
|
2023-10-24 16:59:02 +00:00
|
|
|
from django.urls import re_path as url
|
2016-05-09 12:02:10 +00:00
|
|
|
urls = super().get_urls()
|
2015-05-07 19:00:02 +00:00
|
|
|
info = self.model._meta.app_label, self.model._meta.model_name
|
|
|
|
urls.insert(0,
|
2015-05-26 12:59:16 +00:00
|
|
|
url(r'^send-pending/$',
|
|
|
|
wrap_admin_view(self, self.send_pending_view),
|
|
|
|
name='%s_%s_send_pending' % info)
|
2015-05-07 19:00:02 +00:00
|
|
|
)
|
|
|
|
return urls
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-05-05 19:42:55 +00:00
|
|
|
def get_queryset(self, request):
|
2016-05-09 12:02:10 +00:00
|
|
|
qs = super().get_queryset(request)
|
|
|
|
return qs.annotate(Count('logs')).defer('content')
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-05-07 19:00:02 +00:00
|
|
|
def send_pending_view(self, request):
|
|
|
|
task(send_pending).apply_async()
|
|
|
|
self.message_user(request, _("Pending messages are being sent on the background."))
|
|
|
|
return redirect('..')
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-05-26 12:59:16 +00:00
|
|
|
def formfield_for_dbfield(self, db_field, **kwargs):
|
|
|
|
if db_field.name == 'subject':
|
|
|
|
kwargs['widget'] = forms.TextInput(attrs={'size':'100'})
|
2016-05-09 12:02:10 +00:00
|
|
|
return super().formfield_for_dbfield(db_field, **kwargs)
|
2015-05-04 19:52:53 +00:00
|
|
|
|
2015-07-21 10:44:32 +00:00
|
|
|
|
2024-01-26 13:05:02 +00:00
|
|
|
@admin.register(SMTPLog)
|
2015-05-04 19:52:53 +00:00
|
|
|
class SMTPLogAdmin(admin.ModelAdmin):
|
|
|
|
list_display = (
|
2015-05-05 19:42:55 +00:00
|
|
|
'id', 'message_link', 'colored_result', 'date_delta', 'log_message'
|
2015-05-04 19:52:53 +00:00
|
|
|
)
|
2015-05-05 19:42:55 +00:00
|
|
|
list_filter = ('result',)
|
2015-05-07 19:00:02 +00:00
|
|
|
fields = ('message_link', 'colored_result', 'date_delta', 'log_message')
|
|
|
|
readonly_fields = fields
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-05-04 19:52:53 +00:00
|
|
|
message_link = admin_link('message')
|
2015-05-05 19:42:55 +00:00
|
|
|
colored_result = admin_colored('result', colors=COLORS, bold=False)
|
|
|
|
date_delta = admin_date('date')
|
2015-05-04 19:52:53 +00:00
|
|
|
|
|
|
|
|