Mercurial > sat_legacy_website
annotate sat_website/forms.py @ 160:ff48d544bfdd default tip
gallery: replaced <video> tag by <img> for videos thumbnails
| author | Goffi <goffi@goffi.org> |
|---|---|
| date | Sun, 23 Sep 2018 16:41:30 +0200 |
| parents | b1c16cd53b62 |
| children |
| rev | line source |
|---|---|
|
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
3 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
4 """ |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
5 SàT website: Salut à Toi's presentation website |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
6 Copyright (C) 2012 Jérôme Poisson (goffi@goffi.org) |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
7 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
8 This file is part of SàT website. |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
9 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
10 SàT website is free software: you can redistribute it and/or modify |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
11 it under the terms of the GNU Affero General Public License as published by |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
12 the Free Software Foundation, either version 3 of the License, or |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
13 (at your option) any later version. |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
14 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
15 Foobar is distributed in the hope that it will be useful, |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
16 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
18 GNU Affero General Public License for more details. |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
19 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
20 You should have received a copy of the GNU Affero General Public License |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
21 along with Foobar. If not, see <http://www.gnu.org/licenses/>. |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
22 """ |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
23 |
|
57
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
24 from django.utils.translation import ugettext_lazy as _, ugettext, string_concat, get_language |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
25 from django.core.mail import send_mail |
|
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
26 from django import forms |
|
40
dfe7139dae0a
add links to the statutes and rules of the association in the adhesion form
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
27 from django.conf import settings |
|
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
28 from collections import OrderedDict |
|
96
c868187b8d00
add the current date in the subscription form's results
souliane <souliane@mailoo.org>
parents:
95
diff
changeset
|
29 from time import strftime |
| 60 | 30 import unicodecsv |
|
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
31 import utils |
|
57
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
32 import re |
|
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
33 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
34 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
35 ## Fields ## |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
36 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
37 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
38 class Section(forms.Field): |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
39 def __init__(self, *args, **kwargs): |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
40 kwargs['required'] = False |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
41 super(Section, self).__init__(*args, **kwargs) |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
42 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
43 class BooleanField(forms.BooleanField): |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
44 def __init__(self, *args, **kwargs): |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
45 super(BooleanField, self).__init__(*args, **kwargs) |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
46 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
47 class CharField(forms.CharField): |
|
95
dc8a30f6c369
fixes the display of form results which was ignoring some labels
souliane <souliane@mailoo.org>
parents:
94
diff
changeset
|
48 def __init__(self, *args, **kwargs): |
|
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
49 try: |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
50 placeholder = kwargs.pop('placeholder') |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
51 except KeyError: |
|
95
dc8a30f6c369
fixes the display of form results which was ignoring some labels
souliane <souliane@mailoo.org>
parents:
94
diff
changeset
|
52 placeholder = kwargs['label'] |
|
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
53 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
54 super(CharField, self).__init__(*args, **kwargs) |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
55 self.widget.attrs.update({'class': "form-control", |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
56 'placeholder': placeholder}) |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
57 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
58 class EmailField(forms.EmailField): |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
59 def __init__(self, *args, **kwargs): |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
60 super(EmailField, self).__init__(*args, **kwargs) |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
61 self.widget.attrs.update({'class': "form-control", |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
62 'placeholder': self.label}) |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
63 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
64 class ChoiceField(forms.ChoiceField): |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
65 def __init__(self, *args, **kwargs): |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
66 super(ChoiceField, self).__init__(*args, **kwargs) |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
67 self.widget.attrs.update({'class': "form-control"}) |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
68 |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
69 def choice_label(self, value): |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
70 """Return the label corresponding to the given value. |
| 65 | 71 |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
72 @param value (unicode): a choice value |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
73 @return: unicode |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
74 """ |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
75 for current_value, label in self.choices: |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
76 if unicode(current_value) == unicode(value): |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
77 return unicode(label) |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
78 return u'' |
|
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
79 |
| 65 | 80 |
|
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
81 ## Forms ## |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
82 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
83 |
|
149
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
84 def html_link(url, label): |
|
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
85 return string_concat('<a target="#" href="', url, '">', label, '</a>') |
|
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
86 |
|
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
87 |
|
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
88 class RegistrationForm(forms.Form): |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
89 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
90 section_1 = Section(label=_(u'Identity')) |
|
57
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
91 name = CharField(label=_(u'Given name')) |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
92 surname = CharField(label=_(u'Family name')) |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
93 address = CharField(label=_(u"Address, postal code, municipality"), widget=forms.Textarea(attrs={'rows': 3})) |
|
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
94 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
95 section_2 = Section(label=_(u'Contacts')) |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
96 email = EmailField(label=_(u'Email address')) |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
97 email_confirmation = EmailField(label=_(u'Email address confirmation')) |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
98 jid = EmailField(required=False, label=_(u'Jabber ID (for example your SàT login)')) |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
99 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
100 section_3 = Section(label=_(u'Subscription')) |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
101 subscription_amount = ChoiceField(choices=[(amount, u"%s €" % amount) for amount in utils.get_asso_subscr_amounts()]) |
|
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
102 |
|
147
d0cd185f9b61
replace apayer with liberapay
souliane <souliane@mailoo.org>
parents:
96
diff
changeset
|
103 section_4 = Section(label=_(u'Reference')) |
|
57
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
104 reference = CharField(required=False, label=_(u"Reference"), placeholder=_(u"Adherent number in case of a renewal")) |
|
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
105 |
|
147
d0cd185f9b61
replace apayer with liberapay
souliane <souliane@mailoo.org>
parents:
96
diff
changeset
|
106 section_5 = Section(label=_(u'Comment')) |
|
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
107 comment = CharField(required=False, label=_(u"Comment"), placeholder="", widget=forms.Textarea(attrs={'rows': 3})) |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
108 |
|
74
651c4049f32b
rename "Newsletter" to "Mailing list" in the adhesion form
souliane <souliane@mailoo.org>
parents:
66
diff
changeset
|
109 mailing = BooleanField(required=False, label=_(u"I would like to subscribe to the adherents' mailing list.")) |
|
57
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
110 |
|
40
dfe7139dae0a
add links to the statutes and rules of the association in the adhesion form
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
111 agreement_label = [_(u"I read the "), |
|
dfe7139dae0a
add links to the statutes and rules of the association in the adhesion form
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
112 html_link(settings.ASSO_URL_STATUTES, _(u"Statutes")), |
|
dfe7139dae0a
add links to the statutes and rules of the association in the adhesion form
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
113 _(u" and "), |
|
dfe7139dae0a
add links to the statutes and rules of the association in the adhesion form
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
114 html_link(settings.ASSO_URL_RULES, _(u"Rules")), |
|
dfe7139dae0a
add links to the statutes and rules of the association in the adhesion form
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
115 _(u" of the association, and agree to both of them."), |
|
dfe7139dae0a
add links to the statutes and rules of the association in the adhesion form
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
116 ] |
|
dfe7139dae0a
add links to the statutes and rules of the association in the adhesion form
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
117 agreement_confirmation = BooleanField(label=string_concat(*agreement_label)) |
|
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
118 |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
119 def sections(self): |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
120 """Get the fields grouped in sections. |
| 65 | 121 |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
122 @return: OrderedDict binding section name to a list of fields |
| 65 | 123 """ |
|
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
124 sections = OrderedDict() |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
125 current_section = None |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
126 for field in self: |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
127 if isinstance(field.field, Section): |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
128 current_section = sections.setdefault(field.label, []) |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
129 else: |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
130 current_section.append(field) |
|
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
131 return sections |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
132 |
| 44 | 133 def clean(self): |
| 134 cleaned_data = super(RegistrationForm, self).clean() | |
| 135 email = cleaned_data.get("email") | |
| 136 email_confirmation = cleaned_data.get("email_confirmation") | |
| 137 | |
| 138 if email and email_confirmation and email != email_confirmation: | |
| 139 msg = _(u"Passwords don't match.") | |
| 140 self.add_error('email_confirmation', msg) | |
| 141 | |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
142 |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
143 def results(self, user_readable=True): |
|
57
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
144 """Get the results submitted by the user as a list. |
| 65 | 145 |
| 60 | 146 @param user_readable: (bool) set to True to prefer the fields' labels |
| 147 to their names, also filter out the empty fields. | |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
148 @return: list of couple (name, value) or (label, value) |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
149 """ |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
150 if not self.is_valid(): |
|
57
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
151 return None |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
152 results = [] |
|
96
c868187b8d00
add the current date in the subscription form's results
souliane <souliane@mailoo.org>
parents:
95
diff
changeset
|
153 |
|
c868187b8d00
add the current date in the subscription form's results
souliane <souliane@mailoo.org>
parents:
95
diff
changeset
|
154 date = strftime("%Y-%m-%d") |
|
c868187b8d00
add the current date in the subscription form's results
souliane <souliane@mailoo.org>
parents:
95
diff
changeset
|
155 if user_readable: |
|
c868187b8d00
add the current date in the subscription form's results
souliane <souliane@mailoo.org>
parents:
95
diff
changeset
|
156 results.append((_(u"Date"), date)) |
|
c868187b8d00
add the current date in the subscription form's results
souliane <souliane@mailoo.org>
parents:
95
diff
changeset
|
157 else: |
|
c868187b8d00
add the current date in the subscription form's results
souliane <souliane@mailoo.org>
parents:
95
diff
changeset
|
158 results.append(('date', date)) |
|
c868187b8d00
add the current date in the subscription form's results
souliane <souliane@mailoo.org>
parents:
95
diff
changeset
|
159 |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
160 for field in self: |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
161 if isinstance(field.field, Section): |
| 60 | 162 continue # filter out section fields |
| 163 if field.name in ('email_confirmation', 'agreement_confirmation'): | |
| 164 continue # filter out confirmation fields | |
| 165 if user_readable and not field.value(): | |
| 166 continue # filter out empty value | |
|
57
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
167 key = field.name |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
168 if isinstance(field.field, BooleanField) and user_readable: |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
169 value = ugettext(u"yes") if field.value() else ugettext(u"no") |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
170 else: |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
171 value = re.sub(r"[\n\r]+", ", ", unicode(field.value())) |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
172 if user_readable: |
|
57
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
173 if isinstance(field.field, BooleanField): |
|
74
651c4049f32b
rename "Newsletter" to "Mailing list" in the adhesion form
souliane <souliane@mailoo.org>
parents:
66
diff
changeset
|
174 key = key.capitalize() # to get Mailing instead of "I would like..." |
|
57
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
175 else: |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
176 key = field.label |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
177 if isinstance(field.field, ChoiceField): |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
178 value = field.field.choice_label(value) |
|
57
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
179 results.append((key, value)) |
|
96
c868187b8d00
add the current date in the subscription form's results
souliane <souliane@mailoo.org>
parents:
95
diff
changeset
|
180 |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
181 if user_readable: |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
182 results.append((_(u'Language'), utils.get_language_name_local(get_language()))) |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
183 else: |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
184 results.append(('lang', get_language())) |
|
96
c868187b8d00
add the current date in the subscription form's results
souliane <souliane@mailoo.org>
parents:
95
diff
changeset
|
185 |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
186 return results |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
187 |
|
57
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
188 def result_as_dict(self, user_readable=True): |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
189 """Get the results submitted by the user as an OrderedDict. |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
190 |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
191 @param user_readable (bool): set to True to prefer the field labels to their names |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
192 @return: dict {name: value} or {label: value} |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
193 """ |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
194 return {key: value for key, value in self.results(user_readable)} |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
195 |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
196 def result_as_string(self, user_readable=True): |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
197 """Get the result as a string to be sent for example via email. |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
198 |
|
57
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
199 @param user_readable (bool): set to True to prefer the field labels to their names |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
200 @return: unicode |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
201 """ |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
202 return '\n'.join([name + ': ' + value for name, value in self.results(user_readable)]) |
| 65 | 203 |
|
57
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
204 def prepareResultForUser(self): |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
205 """Get the email body to send to the subscriber. |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
206 |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
207 @return: unicode |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
208 """ |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
209 ref = self['reference'].value() |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
210 data = {'name': self['name'].value(), |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
211 'surname': self['surname'].value(), |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
212 'amount': self['subscription_amount'].value(), |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
213 'ref_info': '(ref. {ref})'.format(ref=ref) if ref else '', |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
214 'iban': settings.ASSO_IBAN, |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
215 'bic': settings.ASSO_BIC, |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
216 'result': self.result_as_string(True) |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
217 } |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
218 |
| 66 | 219 HEADER = ugettext(u"""Thank you, {name}! |
| 65 | 220 |
|
57
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
221 We received your submission and we are happy to count you in the members of the association. |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
222 |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
223 """) |
|
77
13ebb548f234
use membership instead of adhesion in English (also rename the files)
souliane <souliane@mailoo.org>
parents:
74
diff
changeset
|
224 PAYMENT = ugettext(u"""You chose to support Salut à Toi with a subscription of {amount} euros. Please complete your membership with a bank transfer to: |
|
57
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
225 |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
226 Payee: Salut à Toi |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
227 IBAN: {iban} |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
228 BIC: {bic} |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
229 Reason: subscription of {name} {surname} {ref_info} |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
230 |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
231 """) |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
232 FOOTER = ugettext(u"""Below a copy of the information we received: |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
233 |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
234 {result} |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
235 |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
236 If you have any question, feel free to contact us. |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
237 |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
238 Association Salut à Toi |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
239 http://salut-a-toi.org""") |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
240 |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
241 return (HEADER + (PAYMENT if int(data['amount']) > 0 else "") + FOOTER).format(**data) |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
242 |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
243 def prepareResultForAdmin(self): |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
244 """Get the email body to send to the admins. |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
245 |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
246 @return: unicode |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
247 """ |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
248 data = {'name': self['name'].value(), |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
249 'result': self.result_as_string(False) |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
250 } |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
251 |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
252 MSG = ugettext(u"""New subscription received! |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
253 |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
254 {result} |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
255 |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
256 An email has been automatically sent to {name}, no additional action is required from your side.""") |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
257 |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
258 return MSG.format(**data) |
|
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
259 |
| 60 | 260 def writeResultToCSV(self): |
|
149
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
261 result = [unicode(value) for dummy, value in self.results(False)] |
| 60 | 262 with open(settings.ASSO_SUBSCR_CSV, 'a+') as csvfile: |
|
94
c1b77f07f644
replace shipped file unicodecsv.py with the module of the same name
souliane <souliane@mailoo.org>
parents:
77
diff
changeset
|
263 writer = unicodecsv.writer(csvfile, delimiter=';') |
| 60 | 264 writer.writerow(result) |
| 265 | |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
266 def process_submitted_data(self): |
|
57
bfa8009f0769
improve the subscription form
souliane <souliane@mailoo.org>
parents:
55
diff
changeset
|
267 """Send emails to the subscriber and the admins.""" |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
268 if not self.is_valid(): |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
269 return |
|
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
270 # send email to user |
|
149
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
271 send_mail( |
|
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
272 _(u'Subscription to Salut à Toi'), |
|
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
273 self.prepareResultForUser(), |
|
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
274 settings.DEFAULT_FROM_EMAIL, |
|
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
275 [self['email'].value()], |
|
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
276 fail_silently=False |
|
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
277 ) |
|
41
1a0f24401866
send adhesion form results via email to the admins and the new member
souliane <souliane@mailoo.org>
parents:
40
diff
changeset
|
278 # send email to admins |
|
149
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
279 send_mail( |
|
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
280 _(u'Subscription to Salut à Toi'), |
|
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
281 self.prepareResultForAdmin(), |
|
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
282 settings.DEFAULT_FROM_EMAIL, |
|
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
283 [email for dummy, email in settings.ADMINS], |
|
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
284 fail_silently=False |
|
b1c16cd53b62
update django to version 1.11, refactor project structure, better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
148
diff
changeset
|
285 ) |
| 60 | 286 # save to a CSV file |
| 287 self.writeResultToCSV() |
