diff --git a/modoboa/extensions/amavis/__init__.py b/modoboa/extensions/amavis/__init__.py index 5cd208f13..89a44c34b 100644 --- a/modoboa/extensions/amavis/__init__.py +++ b/modoboa/extensions/amavis/__init__.py @@ -190,7 +190,8 @@ def extra_domain_form(user, domain): return [] return [ dict( - id="amavis", title=_("Content filter"), cls=DomainPolicyForm + id="amavis", title=_("Content filter"), cls=DomainPolicyForm, + formtpl="amavis/domain_content_filter.html" ) ] diff --git a/modoboa/extensions/amavis/forms.py b/modoboa/extensions/amavis/forms.py index 4d525abd4..231fe2cb5 100644 --- a/modoboa/extensions/amavis/forms.py +++ b/modoboa/extensions/amavis/forms.py @@ -2,19 +2,46 @@ from django import forms from models import Policy, Users -from modoboa.lib.formutils import InlineRadioSelect +from django.utils.translation import ugettext_lazy + +def is_yes(v): return v == 'Y' + +class CustomInputField(forms.CharField): + """Custom field + + Most of the columns defined by amavis use the *char* type even if + they only support 2 values (Y/N). As we want to display them using + checkboxes, we need to make some modifications in order that + values are correctly interpreted. + """ + def validate(self, value): + value = "N" if not value else "Y" + super(CustomInputField, self).validate(value) + + def clean(self, value): + value = "N" if not value else "Y" + return super(CustomInputField, self).clean(value) class DomainPolicyForm(forms.ModelForm): + bypass_spam_checks = CustomInputField(label=ugettext_lazy("Spam filter"), + widget=forms.CheckboxInput(check_test=is_yes), + help_text=ugettext_lazy("enabled")) + bypass_virus_checks = CustomInputField(label=ugettext_lazy("Virus filter"), + widget=forms.CheckboxInput(check_test=is_yes), + help_text=ugettext_lazy("enabled")) + bypass_banned_checks = CustomInputField(label=ugettext_lazy("Banned filter"), + widget=forms.CheckboxInput(check_test=is_yes), + help_text=ugettext_lazy("enabled")) + spam_modifies_subj = CustomInputField(label=ugettext_lazy("Spam marker"), + widget=forms.CheckboxInput(check_test=is_yes), + help_text=ugettext_lazy("tag subject")) + class Meta: model = Policy fields = ('bypass_virus_checks', 'bypass_spam_checks', 'spam_tag2_level', 'spam_modifies_subj', 'spam_kill_level', 'bypass_banned_checks') widgets = { - 'bypass_spam_checks' : InlineRadioSelect(), - 'bypass_virus_checks' : InlineRadioSelect(), - 'bypass_banned_checks' : InlineRadioSelect(), - 'spam_modifies_subj' : InlineRadioSelect(attrs={'class' : 'span1'}), 'spam_tag2_level' : forms.TextInput(attrs={'class' : 'span1'}), 'spam_kill_level' : forms.TextInput(attrs={'class' : 'span1'}), } diff --git a/modoboa/extensions/amavis/models.py b/modoboa/extensions/amavis/models.py index 8b322fdd5..359a0f449 100644 --- a/modoboa/extensions/amavis/models.py +++ b/modoboa/extensions/amavis/models.py @@ -75,23 +75,11 @@ class Policy(models.Model): spam_lover = models.CharField(max_length=3, blank=True) banned_files_lover = models.CharField(max_length=3, blank=True) bad_header_lover = models.CharField(max_length=3, blank=True) - bypass_virus_checks = models.CharField( - ugettext_lazy("Enable virus filter"), max_length=3, - choices=(('Y', 'yes'), ('N', 'no')), blank=False, default='Y' - ) - bypass_spam_checks = models.CharField( - ugettext_lazy("Enable spam filter"), max_length=3, - choices=(('Y', 'yes'), ('N', 'no')), blank=False, default='Y' - ) - bypass_banned_checks = models.CharField( - ugettext_lazy("Enable banned filter"), max_length=3, - choices=(('Y', 'yes'), ('N', 'no')), blank=False, default='Y' - ) + bypass_virus_checks = models.CharField(max_length=3, blank=True) + bypass_spam_checks = models.CharField(max_length=3, blank=True) + bypass_banned_checks = models.CharField(max_length=3, blank=True) bypass_header_checks = models.CharField(max_length=3, blank=True) - spam_modifies_subj = models.CharField( - ugettext_lazy("Indicate spam in subject"), - choices=(('Y', 'yes'), ('N', 'no')), max_length=3, blank=False, default='Y' - ) + spam_modifies_subj = models.CharField(max_length=3, blank=True) virus_quarantine_to = models.CharField(max_length=192, blank=True) spam_quarantine_to = models.CharField(max_length=192, blank=True) banned_quarantine_to = models.CharField(max_length=192, blank=True) diff --git a/modoboa/lib/formutils.py b/modoboa/lib/formutils.py index 6406f36be..f2d44dced 100644 --- a/modoboa/lib/formutils.py +++ b/modoboa/lib/formutils.py @@ -169,23 +169,3 @@ class DomainNameField(forms.CharField): def clean(self, value): value = self.to_python(value).strip() return super(DomainNameField, self).clean(value) - -class BootstrapRadioInput(forms.widgets.RadioInput): - def __unicode__(self): - if 'id' in self.attrs: - label_for = ' for="%s_%s"' % (self.attrs['id'], self.index) - else: - label_for = '' - choice_label = conditional_escape(force_unicode(self.choice_label)) - return mark_safe(u'%s %s' % (label_for, self.tag(), choice_label)) - -class InlineRadioFieldRenderer(forms.widgets.RadioFieldRenderer): - def __iter__(self): - for i, choice in enumerate(self.choices): - yield BootstrapRadioInput(self.name, self.value, self.attrs.copy(), choice, i) - - def render(self): - return mark_safe(u'\n%s\n' % u'\n'.join([u'%s' % force_unicode(w) for w in self])) - -class InlineRadioSelect(forms.widgets.RadioSelect): - renderer = InlineRadioFieldRenderer diff --git a/modoboa/templates/common/generic_field.html b/modoboa/templates/common/generic_field.html index 20cd257da..b8e9caae3 100644 --- a/modoboa/templates/common/generic_field.html +++ b/modoboa/templates/common/generic_field.html @@ -1,7 +1,8 @@ {% load i18n %}