changeset 41:1a0f24401866

send adhesion form results via email to the admins and the new member
author souliane <souliane@mailoo.org>
date Tue, 03 Feb 2015 15:15:59 +0100
parents dfe7139dae0a
children 02afaab69f92
files sat_website/forms.py sat_website/local_settings.py sat_website/utils.py templates/sat_website/thank_you.html
diffstat 4 files changed, 96 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/sat_website/forms.py	Tue Feb 03 15:14:42 2015 +0100
+++ b/sat_website/forms.py	Tue Feb 03 15:15:59 2015 +0100
@@ -21,8 +21,9 @@
 along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 """
 
-from django.utils.translation import ugettext_lazy as _, string_concat
+from django.utils.translation import ugettext_lazy as _, string_concat, get_language
 from django.utils.html import format_html
+from django.core.mail import send_mail
 from django import forms
 from django.conf import settings
 from collections import OrderedDict
@@ -64,7 +65,18 @@
         super(ChoiceField, self).__init__(*args, **kwargs)
         self.widget.attrs.update({'class': "form-control"})
 
+    def choice_label(self, value):
+        """Return the label corresponding to the given value.
+    
+        @param value (unicode): a choice value
+        @return: unicode
+        """
+        for current_value, label in self.choices:
+            if unicode(current_value) == unicode(value):
+                return unicode(label)
+        return u''
 
+        
 ## Forms ##
 
 
@@ -81,10 +93,10 @@
     jid = EmailField(required=False, label=_(u'Jabber ID (for example your SàT login)'))
 
     section_3 = Section(label=_(u'Subscription'))
-    subscription_amount = ChoiceField(choices=[(amount, "%s €" % amount) for amount in utils.get_asso_subscr_amounts()])
+    subscription_amount = ChoiceField(choices=[(amount, u"%s €" % amount) for amount in utils.get_asso_subscr_amounts()])
 
     section_3b = Section(label="")
-    payment_method = ChoiceField(choices=[("card", _(u"Credit or debit card")), ("transfer", _(u"Bank transfer"))],
+    payment_method = ChoiceField(choices=[(u"card", _(u"Credit or debit card")), (u"transfer", _(u"Bank transfer"))],
                                help_text=_(u'Choose "Credit or debit card" to pay via CB, Visa or Mastercard using a secure banking service. Choose "Bank transfer" to proceed manually with the association\'s IBAN/BIC numbers. For both methods, we will first send you an email containing all the details.'), widget=forms.RadioSelect)
 
     section_5 = Section(label=_(u'Reference'))
@@ -105,6 +117,10 @@
     agreement_confirmation = BooleanField(label=string_concat(*agreement_label))
 
     def sections(self):
+        """Get the fields grouped in sections.
+        
+        @return: OrderedDict binding section name to a list of fields
+        """ 
         sections = OrderedDict()
         current_section = None
         for field in self:
@@ -113,3 +129,50 @@
             else:
                 current_section.append(field)
         return sections
+
+
+    def results(self, user_readable=True):
+        """Get the results submitted by the user as a list of couple. Keep the
+        pertinent fields and filter out the inappropriate ones.
+    
+        @param user_readable: set to True to prefer the field labels to their names
+        @return: list of couple (name, value) or (label, value)
+        """
+        if not self.is_valid():
+            return ''
+        results = []
+        for field in self:
+            if isinstance(field.field, Section):
+                continue
+            if field.name in ('email_confirmation', 'agreement_confirmation') or not field.value():
+                continue
+            if field.name == "payment_method" and self['subscription_amount'].value() == "0":
+                continue
+            value = field.value()
+            if user_readable:
+                if isinstance(field.field, ChoiceField):
+                    value = field.field.choice_label(value)
+                results.append((field.label, value))
+            else:
+                results.append((field.name, value))
+        if user_readable:
+            results.append((_(u'Language'), utils.get_language_name_local(get_language())))
+        else:
+            results.append(('lang', get_language()))
+        return results
+
+    def result_as_string(self, user_readable=True):
+        """Get the result as a string to be sent for example via email.
+
+        @param user_readable: set to True to prefer the field labels to their names
+        @return: list of couple (name, value) or (label, value)
+        """
+        return '\n'.join([name + ': ' + value for name, value in self.results(user_readable)])
+        
+    def process_submitted_data(self):
+        if not self.is_valid():
+            return
+        # send email to user
+        send_mail(_(u'Inscription à Salut à Toi'), self.result_as_string(True), settings.EMAIL_BACKEND, [self['email'].value()], fail_silently=False)
+        # send email to admins
+        send_mail(_(u'Inscription à Salut à Toi'), self.result_as_string(False), settings.EMAIL_BACKEND, [email for name, email in settings.ADMINS], fail_silently=False)
--- a/sat_website/local_settings.py	Tue Feb 03 15:14:42 2015 +0100
+++ b/sat_website/local_settings.py	Tue Feb 03 15:15:59 2015 +0100
@@ -27,6 +27,16 @@
 TEMPLATE_DEBUG = DEBUG
 
 
+# Email settings
+
+# print email to stdout instead of actually sending it
+EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
+
+DEFAULT_FROM_EMAIL = u'contact@salut-a-toi.org'
+
+ADMINS = ((u'Salut à Toi', 'contact@salut-a-toi.org'),)
+
+
 # Application definition
 
 INSTALLED_APPS = (
--- a/sat_website/utils.py	Tue Feb 03 15:14:42 2015 +0100
+++ b/sat_website/utils.py	Tue Feb 03 15:15:59 2015 +0100
@@ -22,6 +22,7 @@
 
 from os.path import basename, realpath, join
 from django.conf import settings
+from django.utils import translation
 from collections import OrderedDict
 
 
@@ -78,3 +79,10 @@
 def get_asso_subscr_amounts():
     """Returns the subscription amounts as defined in the Rules of the association."""
     return settings.ASSO_SUBSCR_AMOUNTS
+
+def get_language_name_local(language):
+    """Returns the localised language name.
+    
+    @param language (str): language code (e.g. 'fr' or 'en')
+    """
+    return translation.get_language_info(language)['name_local']
--- a/templates/sat_website/thank_you.html	Tue Feb 03 15:14:42 2015 +0100
+++ b/templates/sat_website/thank_you.html	Tue Feb 03 15:15:59 2015 +0100
@@ -28,29 +28,20 @@
 {% block main_container %}
 
 <p>{% trans "Your subscription has been registered." %}</p>
-    {% for section, fields in form.sections.items %}
-        <div class="form-group">
-            {% for field in fields %}
-                {% if field.name == "payment_method" %}
-                    {% if form.subscription_amount.value != "0" %}
-                        <div>{{ field.label }}: {{ field|selected_label }}</div>
-                    {% endif %}
-                {% elif field.name == "subscription_amount" %}
-                    <div>{{ field.label }}: {{ field.value }} €</div>
-                {% elif field.name != "email_confirmation" and field.value %}
-                    <div>{{ field.label }}: {{ field.value }}</div>
-                {% endif %}
-            {% endfor %}
-        </div>
+<div class="form-group">
+    {% for label, value in form.results %}
+        <div>{{ label }}: {{ value }}</div>
     {% endfor %}
-    </fieldset>
-</form>
-
-{% if form.subscription_amount.value != "0" %}
-<div class="alert alert-info" role="alert">{% blocktrans with amount=form.subscription_amount.value method=form.payment_method|selected_label %}
-You chose to pay your annual subscription of {{ amount }} € via {{ method }}. You will receive during the next hours/days an email containing all the details (it is not automatised yet).
-{% endblocktrans %}
 </div>
 
+<div class="alert alert-info" role="alert">
+{% if form.subscription_amount.value != "0" %}
+	{% blocktrans with amount=form.subscription_amount.value method=form.payment_method|selected_label|lower %}
+	You chose to pay your annual subscription of {{ amount }} € via {{ method }}.<br/>You should receive in a couple of minutes an email from us containing all the details to proceed with the payment.
+	{% endblocktrans %}
+{% else %}
+    {% blocktrans %}You should receive in a couple of minutes a confirmation email from us.{% endblocktrans %}
 {% endif %}
+</div>
+
 {% endblock %}