1
0
Fork 1
mirror of https://github.com/coddingtonbear/django-mailbox.git synced 2026-07-09 22:38:19 +02:00
django-mailbox/django_mailbox/admin.py

117 lines
2.8 KiB
Python
Raw Permalink Normal View History

2014-08-29 17:24:38 -03:00
#!/usr/bin/env python
"""
Model configuration in application ``django_mailbox`` for administration
console.
"""
import logging
2012-06-27 20:45:01 -07:00
from django.conf import settings
from django.contrib import admin
2024-04-10 07:42:52 +02:00
from django.db.models import Count
from django.utils.translation import gettext_lazy as _
2012-06-27 20:45:01 -07:00
2013-01-15 12:21:49 +00:00
from django_mailbox.models import MessageAttachment, Message, Mailbox
from django_mailbox.signals import message_received
from django_mailbox.utils import convert_header_to_unicode
2014-08-29 17:24:38 -03:00
logger = logging.getLogger(__name__)
2012-06-27 20:45:01 -07:00
2013-06-22 14:35:47 -07:00
2012-06-27 20:45:01 -07:00
def get_new_mail(mailbox_admin, request, queryset):
2020-07-26 14:21:21 +08:00
queryset.get_new_mail()
2017-05-09 23:59:42 +02:00
get_new_mail.short_description = _('Get new mail')
2012-06-27 20:45:01 -07:00
2013-06-22 14:35:47 -07:00
def resend_message_received_signal(message_admin, request, queryset):
for message in queryset.all():
logger.debug('Resending \'message_received\' signal for %s' % message)
message_received.send(sender=message_admin, message=message)
2014-08-29 17:24:38 -03:00
2013-06-22 14:35:47 -07:00
resend_message_received_signal.short_description = (
_('Re-send message received signal')
2013-06-22 14:35:47 -07:00
)
2012-06-27 20:45:01 -07:00
class MailboxAdmin(admin.ModelAdmin):
list_display = (
2013-06-22 14:35:47 -07:00
'name',
'uri',
'from_email',
'active',
2016-08-16 01:01:25 +02:00
'last_polling',
2013-06-22 14:35:47 -07:00
)
2016-08-16 01:01:25 +02:00
readonly_fields = ['last_polling', ]
2012-06-27 20:45:01 -07:00
actions = [get_new_mail]
2013-01-15 12:21:49 +00:00
class MessageAttachmentAdmin(admin.ModelAdmin):
raw_id_fields = ('message', )
list_display = ('message', 'document',)
class MessageAttachmentInline(admin.TabularInline):
model = MessageAttachment
extra = 0
2013-01-15 12:21:49 +00:00
2012-06-27 20:45:01 -07:00
class MessageAdmin(admin.ModelAdmin):
2024-04-10 07:42:52 +02:00
def get_queryset(self, *args, **kwargs):
return super().get_queryset(*args, **kwargs).annotate(num_attachments=Count('attachments'))
2024-04-10 07:42:52 +02:00
def attachment_count(self, msg):
2024-04-10 07:42:52 +02:00
return msg.num_attachments
attachment_count.short_description = _('Attachment count')
2013-07-26 20:15:39 -07:00
def subject(self, msg):
return convert_header_to_unicode(msg.subject)
2013-07-25 17:49:27 +00:00
def envelope_headers(self, msg):
email = msg.get_email_object()
return '\n'.join(
[('{}: {}'.format(h, v)) for h, v in email.items()]
)
inlines = [
MessageAttachmentInline,
]
2012-06-27 20:45:01 -07:00
list_display = (
2013-07-26 20:15:39 -07:00
'subject',
2013-06-22 14:35:47 -07:00
'processed',
'read',
'mailbox',
'outgoing',
'attachment_count',
)
ordering = ['-processed']
list_filter = (
2013-06-22 14:35:47 -07:00
'mailbox',
'outgoing',
'processed',
'read',
)
exclude = (
'body',
)
raw_id_fields = (
2013-06-22 14:35:47 -07:00
'in_reply_to',
)
readonly_fields = (
'envelope_headers',
'text',
2014-08-29 17:24:38 -03:00
'html',
)
actions = [resend_message_received_signal]
2012-06-27 20:45:01 -07:00
2017-05-09 23:59:42 +02:00
2012-06-27 20:45:01 -07:00
if getattr(settings, 'DJANGO_MAILBOX_ADMIN_ENABLED', True):
admin.site.register(Message, MessageAdmin)
admin.site.register(MessageAttachment, MessageAttachmentAdmin)
2012-06-27 20:45:01 -07:00
admin.site.register(Mailbox, MailboxAdmin)