From 51ac729ce1e7a97302a0ccfc26c651f97642da64 Mon Sep 17 00:00:00 2001 From: Marc Aymerich Date: Wed, 4 May 2016 13:00:03 +0000 Subject: [PATCH] Show forwards on mailboxes change view --- TODO.md | 2 -- orchestra/contrib/mailboxes/admin.py | 21 +++++++++++++++------ orchestra/contrib/mailboxes/forms.py | 11 ++++++----- orchestra/contrib/mailboxes/models.py | 8 ++++++++ 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/TODO.md b/TODO.md index da754e1e..9a2bd186 100644 --- a/TODO.md +++ b/TODO.md @@ -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) diff --git a/orchestra/contrib/mailboxes/admin.py b/orchestra/contrib/mailboxes/admin.py index eff1557a..471949b8 100644 --- a/orchestra/contrib/mailboxes/admin.py +++ b/orchestra/contrib/mailboxes/admin.py @@ -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('%s' % (url, addr.email)) + return '
'.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 diff --git a/orchestra/contrib/mailboxes/forms.py b/orchestra/contrib/mailboxes/forms.py index 63c5cde3..b4a5372d 100644 --- a/orchestra/contrib/mailboxes/forms.py +++ b/orchestra/contrib/mailboxes/forms.py @@ -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 diff --git a/orchestra/contrib/mailboxes/models.py b/orchestra/contrib/mailboxes/models.py index ce6e5edc..7caf240a 100644 --- a/orchestra/contrib/mailboxes/models.py +++ b/orchestra/contrib/mailboxes/models.py @@ -81,6 +81,14 @@ class Mailbox(models.Model): if not settings.MAILBOXES_LOCAL_DOMAIN: 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):