Mercurial > sat_legacy_website
diff sat_website/forms.py @ 57:bfa8009f0769
improve the subscription form
author | souliane <souliane@mailoo.org> |
---|---|
date | Thu, 14 May 2015 22:46:57 +0200 |
parents | a1e457ac6871 |
children | 0d20fb28c32e |
line wrap: on
line diff
--- a/sat_website/forms.py Sun May 10 15:48:04 2015 +0200 +++ b/sat_website/forms.py Thu May 14 22:46:57 2015 +0200 @@ -21,7 +21,7 @@ along with Foobar. If not, see <http://www.gnu.org/licenses/>. """ -from django.utils.translation import ugettext_lazy as _, string_concat, get_language +from django.utils.translation import ugettext_lazy as _, ugettext, string_concat, get_language from django.utils.html import format_html from django.core.mail import send_mail from django import forms @@ -29,6 +29,7 @@ from collections import OrderedDict from email import email import utils +import re ## Fields ## @@ -83,9 +84,9 @@ class RegistrationForm(forms.Form): section_1 = Section(label=_(u'Identity')) - name = CharField(label=_(u'Name')) - surname = CharField(label=_(u'Surname')) - address = CharField(label=_(u'Address, postal code, municipality')) + name = CharField(label=_(u'Given name')) + surname = CharField(label=_(u'Family name')) + address = CharField(label=_(u"Address, postal code, municipality"), widget=forms.Textarea(attrs={'rows': 3})) section_2 = Section(label=_(u'Contacts')) email = EmailField(label=_(u'Email address')) @@ -100,11 +101,13 @@ help_text=_(u'Choose "Bank transfer" to proceed manually with the association\'s IBAN/BIC numbers. Choose "Credit or debit card" to pay via CB, Visa or Mastercard using a secure banking service. For both methods, we will first send you an email containing all the details.'), widget=forms.RadioSelect) section_5 = Section(label=_(u'Reference')) - ref = CharField(required=False, label=_(u"Reference"), placeholder=_(u"Adherent number in case of a renewal")) + reference = CharField(required=False, label=_(u"Reference"), placeholder=_(u"Adherent number in case of a renewal")) section_6 = Section(label=_(u'Comment')) comment = CharField(required=False, label=_(u"Comment"), placeholder="", widget=forms.Textarea(attrs={'rows': 3})) + newsletter = BooleanField(required=False, label=_(u'I would like to receive the newsletter.')) + def html_link(url, label): return string_concat('<a target="#" href="', url, '">', label, '</a>') @@ -141,14 +144,14 @@ 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 + """Get the results submitted by the user as a list. + + Keep the pertinent fields and filter out the inappropriate ones. + @param user_readable: (bool) 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 '' + return None results = [] for field in self: if isinstance(field.field, Section): @@ -157,31 +160,106 @@ continue if field.name == "payment_method" and self['subscription_amount'].value() == "0": continue - value = field.value() + key = field.name + if isinstance(field.field, BooleanField) and user_readable: + value = ugettext(u"yes") if field.value() else ugettext(u"no") + else: + value = re.sub(r"[\n\r]+", ", ", unicode(field.value())) if user_readable: + if isinstance(field.field, BooleanField): + key = key.capitalize() # to get Newsletter instead of "Please add me..." + else: + key = field.label if isinstance(field.field, ChoiceField): value = field.field.choice_label(value) - results.append((field.label, value)) - else: - results.append((field.name, value)) + results.append((key, 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_dict(self, user_readable=True): + """Get the results submitted by the user as an OrderedDict. + + @param user_readable (bool): set to True to prefer the field labels to their names + @return: dict {name: value} or {label: value} + """ + return {key: value for key, value in self.results(user_readable)} + 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) + @param user_readable (bool): set to True to prefer the field labels to their names + @return: unicode """ return '\n'.join([name + ': ' + value for name, value in self.results(user_readable)]) + def prepareResultForUser(self): + """Get the email body to send to the subscriber. + + @return: unicode + """ + ref = self['reference'].value() + data = {'name': self['name'].value(), + 'surname': self['surname'].value(), + 'amount': self['subscription_amount'].value(), + 'ref_info': '(ref. {ref})'.format(ref=ref) if ref else '', + 'iban': settings.ASSO_IBAN, + 'bic': settings.ASSO_BIC, + 'result': self.result_as_string(True) + } + + HEADER = ugettext(u"""Tank you, {name}! + +We received your submission and we are happy to count you in the members of the association. + +""") + PAYMENT = ugettext(u"""You chose to support Salut à Toi with a subscription of {amount} euros. Please complete your adhesion with a bank transfer to: + +Payee: Salut à Toi +IBAN: {iban} +BIC: {bic} +Reason: subscription of {name} {surname} {ref_info} + +Or, if you prefer, you can proceed with a credit or debit card: + +https://www.apayer.fr/salut-a-toi + +""") + FOOTER = ugettext(u"""Below a copy of the information we received: + +{result} + +If you have any question, feel free to contact us. + +Association Salut à Toi +http://salut-a-toi.org""") + + return (HEADER + (PAYMENT if int(data['amount']) > 0 else "") + FOOTER).format(**data) + + def prepareResultForAdmin(self): + """Get the email body to send to the admins. + + @return: unicode + """ + data = {'name': self['name'].value(), + 'result': self.result_as_string(False) + } + + MSG = ugettext(u"""New subscription received! + +{result} + +An email has been automatically sent to {name}, no additional action is required from your side.""") + + return MSG.format(**data) + def process_submitted_data(self): + """Send emails to the subscriber and the admins.""" if not self.is_valid(): return # send email to user - send_mail(_(u'Subscription to Salut à Toi'), self.result_as_string(True), settings.EMAIL_BACKEND, [self['email'].value()], fail_silently=False) + send_mail(_(u'Subscription to Salut à Toi'), self.prepareResultForUser(), settings.EMAIL_BACKEND, [self['email'].value()], fail_silently=False) # send email to admins - send_mail(_(u'Subscription to Salut à Toi'), self.result_as_string(False), settings.EMAIL_BACKEND, [email for name, email in settings.ADMINS], fail_silently=False) + send_mail(_(u'Subscription to Salut à Toi'), self.prepareResultForAdmin(), settings.EMAIL_BACKEND, [email for name, email in settings.ADMINS], fail_silently=False)