Mercurial > sat_legacy_website
comparison sat_website/forms.py @ 65:26353615cc2e
fixed email sending in form
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 16 May 2015 17:28:19 +0200 |
parents | 0d20fb28c32e |
children | 64977f537e9b |
comparison
equal
deleted
inserted
replaced
64:1fb1e233d63f | 65:26353615cc2e |
---|---|
20 You should have received a copy of the GNU Affero General Public License | 20 You should have received a copy of the GNU Affero General Public License |
21 along with Foobar. If not, see <http://www.gnu.org/licenses/>. | 21 along with Foobar. If not, see <http://www.gnu.org/licenses/>. |
22 """ | 22 """ |
23 | 23 |
24 from django.utils.translation import ugettext_lazy as _, ugettext, string_concat, get_language | 24 from django.utils.translation import ugettext_lazy as _, ugettext, string_concat, get_language |
25 from django.utils.html import format_html | |
26 from django.core.mail import send_mail | 25 from django.core.mail import send_mail |
27 from django import forms | 26 from django import forms |
28 from django.conf import settings | 27 from django.conf import settings |
29 from collections import OrderedDict | 28 from collections import OrderedDict |
30 from email import email | |
31 import unicodecsv | 29 import unicodecsv |
32 import utils | 30 import utils |
33 import re | 31 import re |
34 | 32 |
35 | 33 |
67 super(ChoiceField, self).__init__(*args, **kwargs) | 65 super(ChoiceField, self).__init__(*args, **kwargs) |
68 self.widget.attrs.update({'class': "form-control"}) | 66 self.widget.attrs.update({'class': "form-control"}) |
69 | 67 |
70 def choice_label(self, value): | 68 def choice_label(self, value): |
71 """Return the label corresponding to the given value. | 69 """Return the label corresponding to the given value. |
72 | 70 |
73 @param value (unicode): a choice value | 71 @param value (unicode): a choice value |
74 @return: unicode | 72 @return: unicode |
75 """ | 73 """ |
76 for current_value, label in self.choices: | 74 for current_value, label in self.choices: |
77 if unicode(current_value) == unicode(value): | 75 if unicode(current_value) == unicode(value): |
78 return unicode(label) | 76 return unicode(label) |
79 return u'' | 77 return u'' |
80 | 78 |
81 | 79 |
82 ## Forms ## | 80 ## Forms ## |
83 | 81 |
84 | 82 |
85 class RegistrationForm(forms.Form): | 83 class RegistrationForm(forms.Form): |
86 | 84 |
120 ] | 118 ] |
121 agreement_confirmation = BooleanField(label=string_concat(*agreement_label)) | 119 agreement_confirmation = BooleanField(label=string_concat(*agreement_label)) |
122 | 120 |
123 def sections(self): | 121 def sections(self): |
124 """Get the fields grouped in sections. | 122 """Get the fields grouped in sections. |
125 | 123 |
126 @return: OrderedDict binding section name to a list of fields | 124 @return: OrderedDict binding section name to a list of fields |
127 """ | 125 """ |
128 sections = OrderedDict() | 126 sections = OrderedDict() |
129 current_section = None | 127 current_section = None |
130 for field in self: | 128 for field in self: |
131 if isinstance(field.field, Section): | 129 if isinstance(field.field, Section): |
132 current_section = sections.setdefault(field.label, []) | 130 current_section = sections.setdefault(field.label, []) |
144 self.add_error('email_confirmation', msg) | 142 self.add_error('email_confirmation', msg) |
145 | 143 |
146 | 144 |
147 def results(self, user_readable=True): | 145 def results(self, user_readable=True): |
148 """Get the results submitted by the user as a list. | 146 """Get the results submitted by the user as a list. |
149 | 147 |
150 @param user_readable: (bool) set to True to prefer the fields' labels | 148 @param user_readable: (bool) set to True to prefer the fields' labels |
151 to their names, also filter out the empty fields. | 149 to their names, also filter out the empty fields. |
152 @return: list of couple (name, value) or (label, value) | 150 @return: list of couple (name, value) or (label, value) |
153 """ | 151 """ |
154 if not self.is_valid(): | 152 if not self.is_valid(): |
195 | 193 |
196 @param user_readable (bool): set to True to prefer the field labels to their names | 194 @param user_readable (bool): set to True to prefer the field labels to their names |
197 @return: unicode | 195 @return: unicode |
198 """ | 196 """ |
199 return '\n'.join([name + ': ' + value for name, value in self.results(user_readable)]) | 197 return '\n'.join([name + ': ' + value for name, value in self.results(user_readable)]) |
200 | 198 |
201 def prepareResultForUser(self): | 199 def prepareResultForUser(self): |
202 """Get the email body to send to the subscriber. | 200 """Get the email body to send to the subscriber. |
203 | 201 |
204 @return: unicode | 202 @return: unicode |
205 """ | 203 """ |
212 'bic': settings.ASSO_BIC, | 210 'bic': settings.ASSO_BIC, |
213 'result': self.result_as_string(True) | 211 'result': self.result_as_string(True) |
214 } | 212 } |
215 | 213 |
216 HEADER = ugettext(u"""Tank you, {name}! | 214 HEADER = ugettext(u"""Tank you, {name}! |
217 | 215 |
218 We received your submission and we are happy to count you in the members of the association. | 216 We received your submission and we are happy to count you in the members of the association. |
219 | 217 |
220 """) | 218 """) |
221 PAYMENT = ugettext(u"""You chose to support Salut à Toi with a subscription of {amount} euros. Please complete your adhesion with a bank transfer to: | 219 PAYMENT = ugettext(u"""You chose to support Salut à Toi with a subscription of {amount} euros. Please complete your adhesion with a bank transfer to: |
222 | 220 |
267 def process_submitted_data(self): | 265 def process_submitted_data(self): |
268 """Send emails to the subscriber and the admins.""" | 266 """Send emails to the subscriber and the admins.""" |
269 if not self.is_valid(): | 267 if not self.is_valid(): |
270 return | 268 return |
271 # send email to user | 269 # send email to user |
272 send_mail(_(u'Subscription to Salut à Toi'), self.prepareResultForUser(), settings.EMAIL_BACKEND, [self['email'].value()], fail_silently=False) | 270 send_mail(_(u'Subscription to Salut à Toi'), self.prepareResultForUser(), settings.FORM_FROM_EMAIL, [self['email'].value()], fail_silently=False) |
273 # send email to admins | 271 # send email to admins |
274 send_mail(_(u'Subscription to Salut à Toi'), self.prepareResultForAdmin(), settings.EMAIL_BACKEND, [email for name, email in settings.ADMINS], fail_silently=False) | 272 send_mail(_(u'Subscription to Salut à Toi'), self.prepareResultForAdmin(), settings.FORM_FROM_EMAIL, settings.FORM_TO_EMAILS, fail_silently=False) |
275 # save to a CSV file | 273 # save to a CSV file |
276 self.writeResultToCSV() | 274 self.writeResultToCSV() |