Mercurial > sat_legacy_website
comparison sat_website/forms.py @ 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 | d721c8ffa22a |
comparison
equal
deleted
inserted
replaced
40:dfe7139dae0a | 41:1a0f24401866 |
---|---|
19 | 19 |
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 _, string_concat | 24 from django.utils.translation import ugettext_lazy as _, string_concat, get_language |
25 from django.utils.html import format_html | 25 from django.utils.html import format_html |
26 from django.core.mail import send_mail | |
26 from django import forms | 27 from django import forms |
27 from django.conf import settings | 28 from django.conf import settings |
28 from collections import OrderedDict | 29 from collections import OrderedDict |
29 from email import email | 30 from email import email |
30 import utils | 31 import utils |
62 class ChoiceField(forms.ChoiceField): | 63 class ChoiceField(forms.ChoiceField): |
63 def __init__(self, *args, **kwargs): | 64 def __init__(self, *args, **kwargs): |
64 super(ChoiceField, self).__init__(*args, **kwargs) | 65 super(ChoiceField, self).__init__(*args, **kwargs) |
65 self.widget.attrs.update({'class': "form-control"}) | 66 self.widget.attrs.update({'class': "form-control"}) |
66 | 67 |
68 def choice_label(self, value): | |
69 """Return the label corresponding to the given value. | |
70 | |
71 @param value (unicode): a choice value | |
72 @return: unicode | |
73 """ | |
74 for current_value, label in self.choices: | |
75 if unicode(current_value) == unicode(value): | |
76 return unicode(label) | |
77 return u'' | |
67 | 78 |
79 | |
68 ## Forms ## | 80 ## Forms ## |
69 | 81 |
70 | 82 |
71 class RegistrationForm(forms.Form): | 83 class RegistrationForm(forms.Form): |
72 | 84 |
79 email = EmailField(label=_(u'Email address')) | 91 email = EmailField(label=_(u'Email address')) |
80 email_confirmation = EmailField(label=_(u'Email address confirmation')) | 92 email_confirmation = EmailField(label=_(u'Email address confirmation')) |
81 jid = EmailField(required=False, label=_(u'Jabber ID (for example your SàT login)')) | 93 jid = EmailField(required=False, label=_(u'Jabber ID (for example your SàT login)')) |
82 | 94 |
83 section_3 = Section(label=_(u'Subscription')) | 95 section_3 = Section(label=_(u'Subscription')) |
84 subscription_amount = ChoiceField(choices=[(amount, "%s €" % amount) for amount in utils.get_asso_subscr_amounts()]) | 96 subscription_amount = ChoiceField(choices=[(amount, u"%s €" % amount) for amount in utils.get_asso_subscr_amounts()]) |
85 | 97 |
86 section_3b = Section(label="") | 98 section_3b = Section(label="") |
87 payment_method = ChoiceField(choices=[("card", _(u"Credit or debit card")), ("transfer", _(u"Bank transfer"))], | 99 payment_method = ChoiceField(choices=[(u"card", _(u"Credit or debit card")), (u"transfer", _(u"Bank transfer"))], |
88 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) | 100 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) |
89 | 101 |
90 section_5 = Section(label=_(u'Reference')) | 102 section_5 = Section(label=_(u'Reference')) |
91 ref = CharField(required=False, label=_(u"Reference"), placeholder=_(u"Adherent number in case of a renewal")) | 103 ref = CharField(required=False, label=_(u"Reference"), placeholder=_(u"Adherent number in case of a renewal")) |
92 | 104 |
103 _(u" of the association, and agree to both of them."), | 115 _(u" of the association, and agree to both of them."), |
104 ] | 116 ] |
105 agreement_confirmation = BooleanField(label=string_concat(*agreement_label)) | 117 agreement_confirmation = BooleanField(label=string_concat(*agreement_label)) |
106 | 118 |
107 def sections(self): | 119 def sections(self): |
120 """Get the fields grouped in sections. | |
121 | |
122 @return: OrderedDict binding section name to a list of fields | |
123 """ | |
108 sections = OrderedDict() | 124 sections = OrderedDict() |
109 current_section = None | 125 current_section = None |
110 for field in self: | 126 for field in self: |
111 if isinstance(field.field, Section): | 127 if isinstance(field.field, Section): |
112 current_section = sections.setdefault(field.label, []) | 128 current_section = sections.setdefault(field.label, []) |
113 else: | 129 else: |
114 current_section.append(field) | 130 current_section.append(field) |
115 return sections | 131 return sections |
132 | |
133 | |
134 def results(self, user_readable=True): | |
135 """Get the results submitted by the user as a list of couple. Keep the | |
136 pertinent fields and filter out the inappropriate ones. | |
137 | |
138 @param user_readable: set to True to prefer the field labels to their names | |
139 @return: list of couple (name, value) or (label, value) | |
140 """ | |
141 if not self.is_valid(): | |
142 return '' | |
143 results = [] | |
144 for field in self: | |
145 if isinstance(field.field, Section): | |
146 continue | |
147 if field.name in ('email_confirmation', 'agreement_confirmation') or not field.value(): | |
148 continue | |
149 if field.name == "payment_method" and self['subscription_amount'].value() == "0": | |
150 continue | |
151 value = field.value() | |
152 if user_readable: | |
153 if isinstance(field.field, ChoiceField): | |
154 value = field.field.choice_label(value) | |
155 results.append((field.label, value)) | |
156 else: | |
157 results.append((field.name, value)) | |
158 if user_readable: | |
159 results.append((_(u'Language'), utils.get_language_name_local(get_language()))) | |
160 else: | |
161 results.append(('lang', get_language())) | |
162 return results | |
163 | |
164 def result_as_string(self, user_readable=True): | |
165 """Get the result as a string to be sent for example via email. | |
166 | |
167 @param user_readable: set to True to prefer the field labels to their names | |
168 @return: list of couple (name, value) or (label, value) | |
169 """ | |
170 return '\n'.join([name + ': ' + value for name, value in self.results(user_readable)]) | |
171 | |
172 def process_submitted_data(self): | |
173 if not self.is_valid(): | |
174 return | |
175 # send email to user | |
176 send_mail(_(u'Inscription à Salut à Toi'), self.result_as_string(True), settings.EMAIL_BACKEND, [self['email'].value()], fail_silently=False) | |
177 # send email to admins | |
178 send_mail(_(u'Inscription à Salut à Toi'), self.result_as_string(False), settings.EMAIL_BACKEND, [email for name, email in settings.ADMINS], fail_silently=False) |