From e388346f9a378419dee9dc2c2caa819c5e540896 Mon Sep 17 00:00:00 2001 From: Marc Aymerich Date: Fri, 19 Feb 2016 11:51:26 +0000 Subject: [PATCH] Imporved mailbox custom filtering management --- orchestra/contrib/mailboxes/admin.py | 9 +++++++++ orchestra/contrib/mailboxes/forms.py | 9 +++++---- orchestra/contrib/mailboxes/models.py | 5 ++--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/orchestra/contrib/mailboxes/admin.py b/orchestra/contrib/mailboxes/admin.py index ad405e80..f22308c7 100644 --- a/orchestra/contrib/mailboxes/admin.py +++ b/orchestra/contrib/mailboxes/admin.py @@ -174,6 +174,15 @@ class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedMo def save_model(self, request, obj, form, change): """ save hacky mailbox.addresses and local domain clashing """ + if obj.filtering != obj.CUSTOM: + msg = _("You have provided a custom filtering but filtering " + "selected option is %s") % obj.get_filtering_display() + if change: + old = Mailbox.objects.get(pk=obj.pk) + if old.custom_filtering != obj.custom_filtering: + messages.warning(request, msg) + elif obj.custom_filtering: + messages.warning(request, msg) super(MailboxAdmin, self).save_model(request, obj, form, change) obj.addresses = form.cleaned_data['addresses'] diff --git a/orchestra/contrib/mailboxes/forms.py b/orchestra/contrib/mailboxes/forms.py index 1675af03..63c5cde3 100644 --- a/orchestra/contrib/mailboxes/forms.py +++ b/orchestra/contrib/mailboxes/forms.py @@ -15,8 +15,8 @@ class MailboxForm(forms.ModelForm): """ hacky form for adding reverse M2M form field for Mailbox.addresses """ # TODO keep track of this ticket for future reimplementation # https://code.djangoproject.com/ticket/897 - addresses = forms.ModelMultipleChoiceField(queryset=Address.objects.select_related('domain'), - required=False, + addresses = forms.ModelMultipleChoiceField(required=False, + queryset=Address.objects.select_related('domain'), widget=widgets.FilteredSelectMultiple(verbose_name=_('addresses'), is_stacked=False)) def __init__(self, *args, **kwargs): @@ -30,16 +30,17 @@ class MailboxForm(forms.ModelForm): self.fields['addresses'].widget = widgets.RelatedFieldWidgetWrapper(widget, field, 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' % self.modeladmin.account.pk + args = 'account=%i' % account.pk output = output.replace('/add/?', '/add/?%s&' % args) return mark_safe(output) self.fields['addresses'].widget.render = render queryset = self.fields['addresses'].queryset - realted_addresses = queryset.filter(account_id=self.modeladmin.account.pk).order_by('name') + realted_addresses = queryset.filter(account_id=account.pk).order_by('name') self.fields['addresses'].queryset = realted_addresses if self.instance and self.instance.pk: diff --git a/orchestra/contrib/mailboxes/models.py b/orchestra/contrib/mailboxes/models.py index aae48ef4..cb0630de 100644 --- a/orchestra/contrib/mailboxes/models.py +++ b/orchestra/contrib/mailboxes/models.py @@ -60,9 +60,7 @@ class Mailbox(models.Model): return os.path.normpath(settings.MAILBOXES_HOME % context) def clean(self): - if self.custom_filtering and self.filtering != self.CUSTOM: - self.custom_filtering = '' - elif self.filtering == self.CUSTOM and not self.custom_filtering: + if self.filtering == self.CUSTOM and not self.custom_filtering: raise ValidationError({ 'custom_filtering': _("Custom filtering is selected but not provided.") }) @@ -70,6 +68,7 @@ class Mailbox(models.Model): def get_filtering(self): name, content = settings.MAILBOXES_MAILBOX_FILTERINGS[self.filtering] if callable(content): + # Custom filtering content = content(self) return (name, content)