From 5b302babd099aff7216e8d6853679ed8ea7446fb Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Fri, 22 Nov 2013 15:13:17 +0100 Subject: [PATCH] Replacing %(name)s macro in default autoreply content. see #432 --- modoboa/core/app_settings.py | 2 +- .../extensions/postfix_autoreply/app_settings.py | 13 +++++++++++++ .../postfix_autoreply/general_callbacks.py | 3 ++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/modoboa/core/app_settings.py b/modoboa/core/app_settings.py index 684138a36..0194ba035 100644 --- a/modoboa/core/app_settings.py +++ b/modoboa/core/app_settings.py @@ -202,7 +202,7 @@ class GeneralParametersForm(parameters.AdminParametersForm): tpl = self.cleaned_data["ldap_user_dn_template"] try: test = tpl % {"user": "toto"} - except ValueError: + except (KeyError, ValueError): raise forms.ValidationError(_("Invalid syntax")) return tpl diff --git a/modoboa/extensions/postfix_autoreply/app_settings.py b/modoboa/extensions/postfix_autoreply/app_settings.py index aba9535e3..464c226df 100644 --- a/modoboa/extensions/postfix_autoreply/app_settings.py +++ b/modoboa/extensions/postfix_autoreply/app_settings.py @@ -39,3 +39,16 @@ Best regards, ), widget=forms.widgets.Textarea ) + + def clean_default_content(self): + """Check if the provided value is valid. + + Must be a valid format string which will be used with the % + operator. + """ + tpl = self.cleaned_data["default_content"] + try: + test = tpl % {"name": "Antoine Nguyen"} + except (KeyError, ValueError): + raise forms.ValidationError(ugettext_lazy("Invalid syntax")) + return tpl diff --git a/modoboa/extensions/postfix_autoreply/general_callbacks.py b/modoboa/extensions/postfix_autoreply/general_callbacks.py index 86b89f9c1..2cf11a2b9 100644 --- a/modoboa/extensions/postfix_autoreply/general_callbacks.py +++ b/modoboa/extensions/postfix_autoreply/general_callbacks.py @@ -138,7 +138,8 @@ def save_extra_mailform_fields(form_name, mailbox, values): else: arm = ARmessage(mbox=mailbox) arm.subject = parameters.get_admin("DEFAULT_SUBJECT") - arm.content = parameters.get_admin("DEFAULT_CONTENT") + arm.content = parameters.get_admin("DEFAULT_CONTENT") \ + % {'name': mailbox.user.fullname} arm.fromdate = timezone.now() arm.enabled = True if values['autoreply'] == 'yes' else False arm.save()