2014-08-29 17:24:38 -03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
|
|
"""
|
2021-02-08 23:39:19 -05:00
|
|
|
Model configuration in application ``django_mailbox2`` for administration
|
2014-08-29 17:24:38 -03:00
|
|
|
console.
|
|
|
|
|
"""
|
|
|
|
|
|
2013-01-27 04:58:09 -08:00
|
|
|
import logging
|
|
|
|
|
|
2012-06-27 20:45:01 -07:00
|
|
|
from django.conf import settings
|
|
|
|
|
from django.contrib import admin
|
2019-10-15 05:31:13 +02:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2012-06-27 20:45:01 -07:00
|
|
|
|
2021-02-08 23:39:19 -05:00
|
|
|
from django_mailbox2.models import MessageAttachment, Message, Mailbox
|
|
|
|
|
from django_mailbox2.signals import message_received
|
|
|
|
|
from django_mailbox2.utils import convert_header_to_unicode
|
2013-01-27 04:58:09 -08:00
|
|
|
|
2014-08-29 17:24:38 -03:00
|
|
|
|
2013-01-27 04:58:09 -08: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
|
|
|
|
|
|
|
|
|
2021-02-03 23:05:48 -05: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
|
|
|
|
2013-01-27 04:58:09 -08:00
|
|
|
def resend_message_received_signal(message_admin, request, queryset):
|
|
|
|
|
for message in queryset.all():
|
2021-02-03 23:05:48 -05:00
|
|
|
logger.debug("Resending 'message_received' signal for %s" % message)
|
2013-01-27 04:58:09 -08:00
|
|
|
message_received.send(sender=message_admin, message=message)
|
2014-08-29 17:24:38 -03:00
|
|
|
|
|
|
|
|
|
2021-02-03 23:05:48 -05:00
|
|
|
resend_message_received_signal.short_description = _("Re-send message received signal")
|
2013-06-22 14:35:47 -07:00
|
|
|
|
2013-01-27 04:58:09 -08:00
|
|
|
|
2012-06-27 20:45:01 -07:00
|
|
|
class MailboxAdmin(admin.ModelAdmin):
|
|
|
|
|
list_display = (
|
2021-02-03 23:05:48 -05:00
|
|
|
"name",
|
|
|
|
|
"uri",
|
|
|
|
|
"from_email",
|
|
|
|
|
"active",
|
|
|
|
|
"last_polling",
|
2013-06-22 14:35:47 -07:00
|
|
|
)
|
2021-02-03 23:05:48 -05:00
|
|
|
readonly_fields = [
|
|
|
|
|
"last_polling",
|
|
|
|
|
]
|
2012-06-27 20:45:01 -07:00
|
|
|
actions = [get_new_mail]
|
|
|
|
|
|
2013-06-11 21:59:54 -07:00
|
|
|
|
2013-01-15 12:21:49 +00:00
|
|
|
class MessageAttachmentAdmin(admin.ModelAdmin):
|
2021-02-03 23:05:48 -05:00
|
|
|
raw_id_fields = ("message",)
|
|
|
|
|
list_display = (
|
|
|
|
|
"message",
|
|
|
|
|
"document",
|
|
|
|
|
)
|
2013-06-06 01:21:04 -07:00
|
|
|
|
2013-06-11 21:59:54 -07:00
|
|
|
|
2013-06-06 01:21:04 -07:00
|
|
|
class MessageAttachmentInline(admin.TabularInline):
|
|
|
|
|
model = MessageAttachment
|
|
|
|
|
extra = 0
|
2013-01-15 12:21:49 +00:00
|
|
|
|
2013-06-11 21:59:54 -07:00
|
|
|
|
2012-06-27 20:45:01 -07:00
|
|
|
class MessageAdmin(admin.ModelAdmin):
|
2013-06-06 01:21:04 -07:00
|
|
|
def attachment_count(self, msg):
|
|
|
|
|
return msg.attachments.count()
|
|
|
|
|
|
2021-02-03 23:05:48 -05:00
|
|
|
attachment_count.short_description = _("Attachment count")
|
2017-07-09 13:33:56 +03:00
|
|
|
|
2013-07-26 20:15:39 -07:00
|
|
|
def subject(self, msg):
|
2014-04-22 15:49:49 -07:00
|
|
|
return convert_header_to_unicode(msg.subject)
|
2013-07-25 17:49:27 +00:00
|
|
|
|
2014-12-31 09:40:36 -06:00
|
|
|
def envelope_headers(self, msg):
|
|
|
|
|
email = msg.get_email_object()
|
2021-02-03 23:05:48 -05:00
|
|
|
return "\n".join([("{}: {}".format(h, v)) for h, v in email.items()])
|
2014-12-31 09:40:36 -06:00
|
|
|
|
2013-06-06 01:21:04 -07:00
|
|
|
inlines = [
|
|
|
|
|
MessageAttachmentInline,
|
|
|
|
|
]
|
2012-06-27 20:45:01 -07:00
|
|
|
list_display = (
|
2021-02-03 23:05:48 -05:00
|
|
|
"subject",
|
|
|
|
|
"processed",
|
|
|
|
|
"read",
|
|
|
|
|
"mailbox",
|
|
|
|
|
"outgoing",
|
|
|
|
|
"attachment_count",
|
2013-06-22 14:35:47 -07:00
|
|
|
)
|
2021-02-03 23:05:48 -05:00
|
|
|
ordering = ["-processed"]
|
2012-10-09 05:52:04 +00:00
|
|
|
list_filter = (
|
2021-02-03 23:05:48 -05:00
|
|
|
"mailbox",
|
|
|
|
|
"outgoing",
|
|
|
|
|
"processed",
|
|
|
|
|
"read",
|
2013-06-22 14:35:47 -07:00
|
|
|
)
|
2021-02-03 23:05:48 -05:00
|
|
|
exclude = ("body",)
|
|
|
|
|
raw_id_fields = ("in_reply_to",)
|
2013-06-11 22:32:13 -07:00
|
|
|
readonly_fields = (
|
2021-02-03 23:05:48 -05:00
|
|
|
"envelope_headers",
|
|
|
|
|
"text",
|
|
|
|
|
"html",
|
2013-06-11 22:32:13 -07:00
|
|
|
)
|
2013-01-27 04:58:09 -08:00
|
|
|
actions = [resend_message_received_signal]
|
2012-06-27 20:45:01 -07:00
|
|
|
|
2017-05-09 23:59:42 +02:00
|
|
|
|
2021-02-03 23:05:48 -05:00
|
|
|
if getattr(settings, "DJANGO_MAILBOX_ADMIN_ENABLED", True):
|
2012-06-27 20:45:01 -07:00
|
|
|
admin.site.register(Message, MessageAdmin)
|
2013-01-15 12:23:16 +00:00
|
|
|
admin.site.register(MessageAttachment, MessageAttachmentAdmin)
|
2012-06-27 20:45:01 -07:00
|
|
|
admin.site.register(Mailbox, MailboxAdmin)
|