Show forwards on mailboxes change view

This commit is contained in:
Marc Aymerich 2016-05-04 13:00:03 +00:00
parent 72ed0c3062
commit 51ac729ce1
4 changed files with 29 additions and 13 deletions

View File

@ -454,5 +454,3 @@ mkhomedir_helper or create ssh homes with bash.rc and such
# exclude from change list action, support for multiple exclusion
# breadcrumbs https://orchestra.pangea.org/admin/domains/domain/?account_id=930
# SHow addresses on mailboxes (+add address)

View File

@ -66,10 +66,10 @@ class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedMo
'fields': ('custom_filtering',),
}),
(_("Addresses"), {
'fields': ('addresses',)
'fields': ('addresses', 'display_forwards')
}),
)
readonly_fields = ('account_link', 'display_addresses')
readonly_fields = ('account_link', 'display_addresses', 'display_forwards')
change_readonly_fields = ('name',)
add_form = MailboxCreationForm
form = MailboxChangeForm
@ -90,6 +90,15 @@ class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedMo
display_addresses.short_description = _("Addresses")
display_addresses.allow_tags = True
def display_forwards(self, mailbox):
forwards = []
for addr in mailbox.get_forwards():
url = change_url(addr)
forwards.append('<a href="%s">%s</a>' % (url, addr.email))
return '<br>'.join(forwards)
display_forwards.short_description = _("Forwards")
display_forwards.allow_tags = True
def display_filtering(self, mailbox):
""" becacuse of allow_tags = True """
return mailbox.get_filtering_display()
@ -133,10 +142,10 @@ class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedMo
return super(MailboxAdmin, self).render_change_form(
request, context, add, change, form_url, obj)
def log_addition(self, request, object):
def log_addition(self, request, object, *args, **kwargs):
self.check_unrelated_address(request, object)
self.check_matching_address(request, object)
return super(MailboxAdmin, self).log_addition(request, object)
return super(MailboxAdmin, self).log_addition(request, object, *args, **kwargs)
def check_matching_address(self, request, obj):
local_domain = settings.MAILBOXES_LOCAL_DOMAIN
@ -267,9 +276,9 @@ class AddressAdmin(SelectAccountAdminMixin, ExtendedModelAdmin):
return super(AddressAdmin, self).render_change_form(
request, context, add, change, form_url, obj)
def log_addition(self, request, object):
def log_addition(self, request, object, *args, **kwargs):
self.check_matching_mailbox(request, object)
return super(AddressAdmin, self).log_addition(request, object)
return super(AddressAdmin, self).log_addition(request, object, *args, **kwargs)
def check_matching_mailbox(self, request, obj):
# Check if new addresse matches with a mbox because of having a local domain

View File

@ -22,20 +22,21 @@ class MailboxForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MailboxForm, self).__init__(*args, **kwargs)
# Hack the widget in order to display add button
field = AttrDict(**{
'to': Address,
remote_field_mock = AttrDict(**{
'model': Address,
'get_related_field': lambda: AttrDict(name='id'),
})
widget = self.fields['addresses'].widget
self.fields['addresses'].widget = widgets.RelatedFieldWidgetWrapper(widget, field,
self.modeladmin.admin_site, can_add_related=True)
self.fields['addresses'].widget = widgets.RelatedFieldWidgetWrapper(
widget, remote_field_mock, self.modeladmin.admin_site, can_add_related=True)
account = self.modeladmin.account
# Filter related addresses by account
old_render = self.fields['addresses'].widget.render
def render(*args, **kwargs):
output = old_render(*args, **kwargs)
args = 'account=%i' % account.pk
args = 'account=%i&mailboxes=%s' % (account.pk, self.instance.pk)
output = output.replace('/add/?', '/add/?%s&' % args)
return mark_safe(output)
self.fields['addresses'].widget.render = render

View File

@ -82,6 +82,14 @@ class Mailbox(models.Model):
raise AttributeError("Mailboxes do not have a defined local address domain.")
return '@'.join((self.name, settings.MAILBOXES_LOCAL_DOMAIN))
def get_forwards(self):
return Address.objects.filter(forward__regex=r'(^|.*\s)%s(\s.*|$)' % self.name)
def get_addresses(self):
mboxes = self.addresses.all()
forwards = self.get_forwards()
return set(mboxes).union(set(forwards))
class Address(models.Model):
name = models.CharField(_("name"), max_length=64, blank=True,