django-orchestra-test/orchestra/contrib/mailboxes/admin.py

172 lines
6.7 KiB
Python
Raw Normal View History

2014-09-29 14:45:51 +00:00
import copy
2015-04-03 10:14:45 +00:00
from urllib.parse import parse_qs
2014-09-29 14:45:51 +00:00
2014-08-22 15:31:44 +00:00
from django import forms
from django.contrib import admin
2015-04-24 11:39:20 +00:00
from django.db.models import F, Value as V
from django.db.models.functions import Concat
2014-08-22 15:31:44 +00:00
from django.utils.translation import ugettext_lazy as _
2014-10-06 14:57:02 +00:00
from orchestra.admin import ExtendedModelAdmin, ChangePasswordAdminMixin
2014-09-26 15:05:20 +00:00
from orchestra.admin.utils import admin_link, change_url
2015-04-05 10:46:24 +00:00
from orchestra.contrib.accounts.admin import SelectAccountAdminMixin
from orchestra.contrib.accounts.filters import IsActiveListFilter
2014-08-22 15:31:44 +00:00
2014-11-27 19:17:26 +00:00
from . import settings
from .actions import SendMailboxEmail
2014-08-22 15:31:44 +00:00
from .filters import HasMailboxListFilter, HasForwardListFilter, HasAddressListFilter
2014-10-09 17:04:12 +00:00
from .forms import MailboxCreationForm, MailboxChangeForm, AddressForm
2014-08-22 15:31:44 +00:00
from .models import Mailbox, Address, Autoresponse
class AutoresponseInline(admin.StackedInline):
model = Autoresponse
verbose_name_plural = _("autoresponse")
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'subject':
kwargs['widget'] = forms.TextInput(attrs={'size':'118'})
return super(AutoresponseInline, self).formfield_for_dbfield(db_field, **kwargs)
2014-10-21 15:29:36 +00:00
class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedModelAdmin):
2014-08-22 15:31:44 +00:00
list_display = (
'name', 'account_link', 'filtering', 'display_addresses', 'display_active',
2014-08-22 15:31:44 +00:00
)
list_filter = (IsActiveListFilter, HasAddressListFilter, 'filtering')
2014-11-17 14:17:33 +00:00
search_fields = ('account__username', 'account__short_name', 'account__full_name', 'name')
2014-08-22 15:31:44 +00:00
add_fieldsets = (
(None, {
2014-10-21 15:29:36 +00:00
'fields': ('account_link', 'name', 'password1', 'password2', 'filtering'),
2014-08-22 15:31:44 +00:00
}),
2014-10-09 17:04:12 +00:00
(_("Custom filtering"), {
2014-09-29 14:45:51 +00:00
'classes': ('collapse',),
'fields': ('custom_filtering',),
2014-08-22 15:31:44 +00:00
}),
2014-10-21 15:29:36 +00:00
(_("Addresses"), {
'fields': ('addresses',)
}),
2014-08-22 15:31:44 +00:00
)
fieldsets = (
(None, {
2014-10-09 17:04:12 +00:00
'fields': ('name', 'password', 'is_active', 'account_link', 'filtering'),
2014-08-22 15:31:44 +00:00
}),
2014-10-09 17:04:12 +00:00
(_("Custom filtering"), {
2014-09-29 14:45:51 +00:00
'classes': ('collapse',),
'fields': ('custom_filtering',),
2014-08-22 15:31:44 +00:00
}),
(_("Addresses"), {
2014-10-21 15:29:36 +00:00
'fields': ('addresses',)
2014-08-22 15:31:44 +00:00
}),
)
2014-10-21 15:29:36 +00:00
readonly_fields = ('account_link', 'display_addresses')
2014-10-06 14:57:02 +00:00
change_readonly_fields = ('name',)
add_form = MailboxCreationForm
2014-10-09 17:04:12 +00:00
form = MailboxChangeForm
list_prefetch_related = ('addresses__domain',)
2014-08-22 15:31:44 +00:00
def display_addresses(self, mailbox):
addresses = []
for addr in mailbox.addresses.all():
2014-09-18 15:07:39 +00:00
url = change_url(addr)
2014-08-22 15:31:44 +00:00
addresses.append('<a href="%s">%s</a>' % (url, addr.email))
return '<br>'.join(addresses)
display_addresses.short_description = _("Addresses")
display_addresses.allow_tags = True
2014-11-27 19:17:26 +00:00
def get_actions(self, request):
if settings.MAILBOXES_LOCAL_ADDRESS_DOMAIN:
self.actions = (SendMailboxEmail(),)
else:
self.actions = ()
return super(MailboxAdmin, self).get_actions(request)
2014-09-29 14:45:51 +00:00
def get_fieldsets(self, request, obj=None):
fieldsets = super(MailboxAdmin, self).get_fieldsets(request, obj)
2014-10-09 17:04:12 +00:00
if obj and obj.filtering == obj.CUSTOM:
2014-10-23 15:38:46 +00:00
# not collapsed filtering when exists
2014-09-29 14:45:51 +00:00
fieldsets = copy.deepcopy(fieldsets)
fieldsets[1][1]['classes'] = fieldsets[0][1]['fields'] + ('open',)
2014-10-23 15:38:46 +00:00
elif '_to_field' in parse_qs(request.META['QUERY_STRING']):
# remove address from popup
fieldsets = list(copy.deepcopy(fieldsets))
fieldsets.pop(-1)
2014-09-29 14:45:51 +00:00
return fieldsets
2014-10-21 15:29:36 +00:00
def get_form(self, *args, **kwargs):
form = super(MailboxAdmin, self).get_form(*args, **kwargs)
form.modeladmin = self
return form
def save_model(self, request, obj, form, change):
""" save hacky mailbox.addresses """
super(MailboxAdmin, self).save_model(request, obj, form, change)
obj.addresses = form.cleaned_data['addresses']
2014-08-22 15:31:44 +00:00
class AddressAdmin(SelectAccountAdminMixin, ExtendedModelAdmin):
list_display = (
2015-04-24 11:39:20 +00:00
'display_email', 'account_link', 'domain_link', 'display_mailboxes', 'display_forward',
2014-08-22 15:31:44 +00:00
)
list_filter = (HasMailboxListFilter, HasForwardListFilter)
2015-04-07 15:14:49 +00:00
fields = ('account_link', 'email_link', 'mailboxes', 'forward')
add_fields = ('account_link', ('name', 'domain'), 'mailboxes', 'forward')
2014-08-22 15:31:44 +00:00
inlines = [AutoresponseInline]
2015-04-24 14:03:42 +00:00
search_fields = ('forward', 'mailboxes__name', 'account__username', 'computed_email')
2014-08-22 15:31:44 +00:00
readonly_fields = ('account_link', 'domain_link', 'email_link')
filter_by_account_fields = ('domain', 'mailboxes')
filter_horizontal = ['mailboxes']
2014-10-06 14:57:02 +00:00
form = AddressForm
list_prefetch_related = ('mailboxes', 'domain')
2014-08-22 15:31:44 +00:00
domain_link = admin_link('domain', order='domain__name')
2015-04-24 11:39:20 +00:00
def display_email(self, address):
return address.computed_email
display_email.short_description = _("Email")
display_email.admin_order_field = 'computed_email'
2014-08-22 15:31:44 +00:00
def email_link(self, address):
link = self.domain_link(address)
return "%s@%s" % (address.name, link)
email_link.short_description = _("Email")
email_link.allow_tags = True
def display_mailboxes(self, address):
boxes = []
for mailbox in address.mailboxes.all():
2014-09-18 15:07:39 +00:00
url = change_url(mailbox)
2014-08-22 15:31:44 +00:00
boxes.append('<a href="%s">%s</a>' % (url, mailbox.name))
return '<br>'.join(boxes)
display_mailboxes.short_description = _("Mailboxes")
display_mailboxes.allow_tags = True
def display_forward(self, address):
values = [ dest for dest in address.forward.split() ]
return '<br>'.join(values)
display_forward.short_description = _("Forward")
display_forward.allow_tags = True
2015-04-24 14:03:42 +00:00
display_forward.admin_order_field = 'forward'
2014-08-22 15:31:44 +00:00
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'forward':
kwargs['widget'] = forms.TextInput(attrs={'size':'118'})
return super(AddressAdmin, self).formfield_for_dbfield(db_field, **kwargs)
2014-10-21 15:29:36 +00:00
def get_fields(self, request, obj=None):
""" Remove mailboxes field when creating address from a popup i.e. from mailbox add form """
fields = super(AddressAdmin, self).get_fields(request, obj)
2014-10-21 15:29:36 +00:00
if '_to_field' in parse_qs(request.META['QUERY_STRING']):
# Add address popup
fields = list(fields)
fields.remove('mailboxes')
return fields
2015-04-24 11:39:20 +00:00
def get_queryset(self, request):
qs = super(AddressAdmin, self).get_queryset(request)
return qs.annotate(computed_email=Concat(F('name'), V('@'), F('domain__name')))
2014-08-22 15:31:44 +00:00
admin.site.register(Mailbox, MailboxAdmin)
admin.site.register(Address, AddressAdmin)