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 # exclude from change list action, support for multiple exclusion
# breadcrumbs https://orchestra.pangea.org/admin/domains/domain/?account_id=930 # 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',), 'fields': ('custom_filtering',),
}), }),
(_("Addresses"), { (_("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',) change_readonly_fields = ('name',)
add_form = MailboxCreationForm add_form = MailboxCreationForm
form = MailboxChangeForm form = MailboxChangeForm
@ -90,6 +90,15 @@ class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedMo
display_addresses.short_description = _("Addresses") display_addresses.short_description = _("Addresses")
display_addresses.allow_tags = True 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): def display_filtering(self, mailbox):
""" becacuse of allow_tags = True """ """ becacuse of allow_tags = True """
return mailbox.get_filtering_display() return mailbox.get_filtering_display()
@ -133,10 +142,10 @@ class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedMo
return super(MailboxAdmin, self).render_change_form( return super(MailboxAdmin, self).render_change_form(
request, context, add, change, form_url, obj) 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_unrelated_address(request, object)
self.check_matching_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): def check_matching_address(self, request, obj):
local_domain = settings.MAILBOXES_LOCAL_DOMAIN local_domain = settings.MAILBOXES_LOCAL_DOMAIN
@ -267,9 +276,9 @@ class AddressAdmin(SelectAccountAdminMixin, ExtendedModelAdmin):
return super(AddressAdmin, self).render_change_form( return super(AddressAdmin, self).render_change_form(
request, context, add, change, form_url, obj) 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) 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): def check_matching_mailbox(self, request, obj):
# Check if new addresse matches with a mbox because of having a local domain # 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): def __init__(self, *args, **kwargs):
super(MailboxForm, self).__init__(*args, **kwargs) super(MailboxForm, self).__init__(*args, **kwargs)
# Hack the widget in order to display add button # Hack the widget in order to display add button
field = AttrDict(**{ remote_field_mock = AttrDict(**{
'to': Address, 'model': Address,
'get_related_field': lambda: AttrDict(name='id'), 'get_related_field': lambda: AttrDict(name='id'),
}) })
widget = self.fields['addresses'].widget widget = self.fields['addresses'].widget
self.fields['addresses'].widget = widgets.RelatedFieldWidgetWrapper(widget, field, self.fields['addresses'].widget = widgets.RelatedFieldWidgetWrapper(
self.modeladmin.admin_site, can_add_related=True) widget, remote_field_mock, self.modeladmin.admin_site, can_add_related=True)
account = self.modeladmin.account account = self.modeladmin.account
# Filter related addresses by account # Filter related addresses by account
old_render = self.fields['addresses'].widget.render old_render = self.fields['addresses'].widget.render
def render(*args, **kwargs): def render(*args, **kwargs):
output = old_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) output = output.replace('/add/?', '/add/?%s&' % args)
return mark_safe(output) return mark_safe(output)
self.fields['addresses'].widget.render = render 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.") raise AttributeError("Mailboxes do not have a defined local address domain.")
return '@'.join((self.name, settings.MAILBOXES_LOCAL_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): class Address(models.Model):
name = models.CharField(_("name"), max_length=64, blank=True, name = models.CharField(_("name"), max_length=64, blank=True,