2014-10-06 14:57:02 +00:00
|
|
|
from django import forms
|
2014-10-21 15:29:36 +00:00
|
|
|
from django.contrib.admin import widgets
|
|
|
|
from django.utils.safestring import mark_safe
|
2014-10-09 17:04:12 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2014-10-06 14:57:02 +00:00
|
|
|
|
2014-10-09 17:04:12 +00:00
|
|
|
from orchestra.forms import UserCreationForm, UserChangeForm
|
2014-10-21 15:29:36 +00:00
|
|
|
from orchestra.utils.python import AttrDict
|
2014-10-06 14:57:02 +00:00
|
|
|
|
2015-04-04 18:10:39 +00:00
|
|
|
from .models import Address
|
2014-10-06 14:57:02 +00:00
|
|
|
|
2014-10-21 15:29:36 +00:00
|
|
|
|
|
|
|
class MailboxForm(forms.ModelForm):
|
|
|
|
""" hacky form for adding reverse M2M form field for Mailbox.addresses """
|
2014-10-21 16:13:18 +00:00
|
|
|
# TODO keep track of this ticket for future reimplementation
|
|
|
|
# https://code.djangoproject.com/ticket/897
|
2014-11-02 14:33:55 +00:00
|
|
|
addresses = forms.ModelMultipleChoiceField(queryset=Address.objects.select_related('domain'), required=False,
|
2014-10-27 13:29:02 +00:00
|
|
|
widget=widgets.FilteredSelectMultiple(verbose_name=_('addresses'), is_stacked=False))
|
2014-10-21 15:29:36 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(MailboxForm, self).__init__(*args, **kwargs)
|
2014-10-21 16:13:18 +00:00
|
|
|
# Hack the widget in order to display add button
|
2014-10-21 15:29:36 +00:00
|
|
|
field = AttrDict(**{
|
|
|
|
'to': 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)
|
2014-10-21 16:13:18 +00:00
|
|
|
|
|
|
|
# Filter related addresses by account
|
2014-10-21 15:29:36 +00:00
|
|
|
old_render = self.fields['addresses'].widget.render
|
|
|
|
def render(*args, **kwargs):
|
|
|
|
output = old_render(*args, **kwargs)
|
|
|
|
args = 'account=%i' % self.modeladmin.account.pk
|
|
|
|
output = output.replace('/add/?', '/add/?%s&' % args)
|
|
|
|
return mark_safe(output)
|
|
|
|
self.fields['addresses'].widget.render = render
|
|
|
|
queryset = self.fields['addresses'].queryset
|
|
|
|
self.fields['addresses'].queryset = queryset.filter(account=self.modeladmin.account.pk)
|
|
|
|
|
|
|
|
if self.instance and self.instance.pk:
|
|
|
|
self.fields['addresses'].initial = self.instance.addresses.all()
|
|
|
|
|
2014-10-09 17:04:12 +00:00
|
|
|
def clean_custom_filtering(self):
|
2014-11-09 10:16:07 +00:00
|
|
|
# TODO move to model.clean?
|
2014-10-09 17:04:12 +00:00
|
|
|
filtering = self.cleaned_data['filtering']
|
|
|
|
custom_filtering = self.cleaned_data['custom_filtering']
|
|
|
|
if filtering == self._meta.model.CUSTOM and not custom_filtering:
|
2014-11-05 20:22:01 +00:00
|
|
|
raise forms.ValidationError({
|
|
|
|
'custom_filtering': _("You didn't provide any custom filtering.")
|
|
|
|
})
|
2014-10-09 17:04:12 +00:00
|
|
|
return custom_filtering
|
|
|
|
|
|
|
|
|
2014-10-21 15:29:36 +00:00
|
|
|
class MailboxChangeForm(UserChangeForm, MailboxForm):
|
2014-10-09 17:04:12 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-10-21 15:29:36 +00:00
|
|
|
class MailboxCreationForm(UserCreationForm, MailboxForm):
|
2014-10-06 14:57:02 +00:00
|
|
|
def clean_name(self):
|
|
|
|
# Since model.clean() will check this, this is redundant,
|
|
|
|
# but it sets a nicer error message than the ORM and avoids conflicts with contrib.auth
|
|
|
|
name = self.cleaned_data["name"]
|
|
|
|
try:
|
|
|
|
self._meta.model._default_manager.get(name=name)
|
|
|
|
except self._meta.model.DoesNotExist:
|
|
|
|
return name
|
2014-10-21 15:29:36 +00:00
|
|
|
raise forms.ValidationError(self.error_messages['duplicate_username'])
|
2014-10-06 14:57:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AddressForm(forms.ModelForm):
|
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super(AddressForm, self).clean()
|
2014-10-21 15:29:36 +00:00
|
|
|
if not cleaned_data.get('mailboxes', True) and not cleaned_data['forward']:
|
2014-11-05 20:22:01 +00:00
|
|
|
raise forms.ValidationError(_("Mailboxes or forward address should be provided."))
|