Imporved mailbox custom filtering management
This commit is contained in:
parent
ebd5ff03ce
commit
e388346f9a
|
@ -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']
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue