django-orchestra/orchestra/contrib/mailer/admin.py

88 lines
3 KiB
Python
Raw Normal View History

2015-05-04 19:52:53 +00:00
from django.contrib import admin
2015-05-05 19:42:55 +00:00
from django.core.urlresolvers import reverse
2015-05-07 19:00:02 +00:00
from django.db.models import Count, Prefetch
from django.shortcuts import redirect
2015-05-05 19:42:55 +00:00
from django.utils.translation import ugettext_lazy as _
2015-05-04 19:52:53 +00:00
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
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-05-05 19:42:55 +00:00
COLORS = {
Message.QUEUED: 'purple',
Message.SENT: 'green',
Message.DEFERRED: 'darkorange',
Message.FAILED: 'red',
SMTPLog.SUCCESS: 'green',
SMTPLog.FAILURE: 'red',
}
2015-05-04 19:52:53 +00:00
class MessageAdmin(admin.ModelAdmin):
list_display = (
2015-05-07 19:00:02 +00:00
'display_subject', 'colored_state', 'priority', 'to_address', 'from_address', 'created_at_delta',
2015-05-05 19:42:55 +00:00
'retries', 'last_retry_delta', 'num_logs',
2015-05-04 19:52:53 +00:00
)
2015-05-05 19:42:55 +00:00
list_filter = ('state', 'priority', 'retries')
2015-05-07 19:00:02 +00:00
list_prefetch_related = ('logs__id')
2015-05-05 19:42:55 +00:00
colored_state = admin_colored('state', colors=COLORS)
created_at_delta = admin_date('created_at')
last_retry_delta = admin_date('last_retry')
2015-05-07 19:00:02 +00:00
def display_subject(self, instance):
return instance.subject[:32]
display_subject.short_description = _("Subject")
display_subject.admin_order_field = 'subject'
2015-05-05 19:42:55 +00:00
def num_logs(self, instance):
num = instance.logs__count
2015-05-07 19:00:02 +00:00
if num == 1:
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
return '<a href="%s" onclick="return showAddAnotherPopup(this);">%d</a>' % (url, num)
2015-05-05 19:42:55 +00:00
num_logs.short_description = _("Logs")
num_logs.admin_order_field = 'logs__count'
num_logs.allow_tags = True
2015-05-07 19:00:02 +00:00
def get_urls(self):
from django.conf.urls import url
urls = super(MessageAdmin, self).get_urls()
info = self.model._meta.app_label, self.model._meta.model_name
urls.insert(0,
url(r'^send-pending/$', wrap_admin_view(self, self.send_pending_view), name='%s_%s_send_pending' % info)
)
return urls
2015-05-05 19:42:55 +00:00
def get_queryset(self, request):
qs = super(MessageAdmin, self).get_queryset(request)
2015-05-07 19:00:02 +00:00
return qs.annotate(Count('logs')).prefetch_related('logs')
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('..')
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
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
admin.site.register(Message, MessageAdmin)
admin.site.register(SMTPLog, SMTPLogAdmin)