forked from mozilla/bedrock
/
email_contribute.py
177 lines (149 loc) · 4.84 KB
/
email_contribute.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from collections import namedtuple
from django.core.mail import EmailMessage
import basket
import jingo
from jinja2.exceptions import TemplateNotFound
from l10n_utils.dotlang import _lazy as _
fa = namedtuple('FunctionalArea', ['id', 'name', 'subject', 'contacts'])
LANG_FILES = 'mozorg/contribute'
FUNCTIONAL_AREAS = (
fa('support',
_('Helping Users'),
'Support',
['jay@jaygarcia.com', 'rardila@mozilla.com', 'madasan@gmail.com'],
),
fa('qa',
_('Testing and QA'),
'QA',
['qa-contribute@mozilla.org'],
),
fa('coding',
_('Coding'),
'Coding',
['josh@joshmatthews.net'],
),
fa('marketing',
_('Marketing'),
'Marketing',
['marketing-get-involved@mozilla.com'],
),
fa('localization',
_('Localization and Translation'),
'Localization',
['rardila@mozilla.com', 'jbeatty@mozilla.com', 'arky@mozilla.com'],
),
fa('webdev',
_('Web Development'),
'Webdev',
['lcrouch@mozilla.com'],
),
fa('addons',
_('Add-ons'),
'Add-ons',
['atsay@mozilla.com'],
),
fa('design',
_('Visual Design'),
'Design',
['mnovak@mozilla.com'],
),
fa('documentation',
_('Documentation and Writing'),
'Documentation',
['jswisher@mozilla.com'],
),
fa('education',
_('Education'),
'Education',
['michelle@mozillafoundation.org'],
),
fa('other',
_('Other'),
'',
['dboswell@mozilla.com'],
),
fa('suggestions',
_('I have a suggestion for Firefox'),
'Firefox Suggestions',
['jay@jaygarcia.com'],
),
fa('issues',
_('I need help with a Firefox issue'),
'Firefox issue',
['jay@jaygarcia.com'],
),
)
INTEREST_CHOICES = (('', _('Area of interest?')),) + tuple(
(area.id, area.name) for area in FUNCTIONAL_AREAS)
FUNCTIONAL_AREAS_DICT = dict((area.id, area) for area in FUNCTIONAL_AREAS)
LOCALE_CONTACTS = {
'bn-BD': ['mahayalamkhan@gmail.com'],
'fr' : ['contact@mozfr.org'],
'el' : ['core@mozilla-greece.org'],
'es-ES': ['participa@mozilla-hispano.org'],
'hr' : ['contribute@mozilla-hr.org'],
'nl' : ['contribute@mozilla-nl.org'],
'pt-BR': ['mlcaraldi@gmail.com'],
'zh-TW': ['contribute@mail.moztw.org'],
}
def handle_form(request, form):
if form.is_valid():
data = form.cleaned_data
send(request, data)
autorespond(request, data)
if data['newsletter']:
try:
basket.subscribe(data['email'], 'about-mozilla')
except basket.BasketException:
pass
return True
return False
def send(request, data):
"""Forward contributor's email to our contacts.
All emails are sent to contribute@mozilla.org
For locales with points of contact, it is also sent to them.
For locales without, it is also sent to functional area contacts.
"""
functional_area = FUNCTIONAL_AREAS_DICT[data['interest']]
from_ = 'contribute-form@mozilla.org'
subject = 'Inquiry about Mozilla %s' % functional_area.subject
msg = jingo.render_to_string(request, 'mozorg/emails/infos.txt', data)
headers = {'Reply-To': data['email']}
to = ['contribute@mozilla.org']
cc = None
if request.locale in LOCALE_CONTACTS:
cc = LOCALE_CONTACTS[request.locale]
else:
cc = functional_area.contacts
email = EmailMessage(subject, msg, from_, to, cc=cc, headers=headers)
email.send()
def autorespond(request, data):
"""Send an auto-respond email based on chosen field of interest and locale.
You can add localized responses by creating email messages in
mozorg/emails/<category.txt>
"""
functional_area = FUNCTIONAL_AREAS_DICT[data['interest']]
subject = 'Inquiry about Mozilla %s' % functional_area.subject
to = [data['email']]
from_ = 'contribute-form@mozilla.org'
headers = {}
msg = ''
template = 'mozorg/emails/%s.txt' % functional_area.id
if request.locale != 'en-US' and request.locale in LOCALE_CONTACTS:
template = '%s/templates/%s' % (request.locale, template)
reply_to = LOCALE_CONTACTS[request.locale]
else:
reply_to = functional_area.contacts
try:
msg = jingo.render_to_string(request, template, data)
except TemplateNotFound:
# No template found means no auto-response
return False
# FIXME Why ?
msg = msg.replace('\n', '\r\n')
headers = {'Reply-To': ','.join(reply_to)}
email = EmailMessage(subject, msg, from_, to, headers=headers)
email.send()