Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/formmail/web/formmail.py

Issue 5717434384252928: Issue 2267 - Unify form handling by reusing form_handler() and encode_email_address() (Closed)
Patch Set: Addressed comment Created April 28, 2015, 8:42 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | sitescripts/reports/web/updateReport.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH 4 # Copyright (C) 2006-2015 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details. 13 # GNU General Public License for more details.
14 # 14 #
15 # You should have received a copy of the GNU General Public License 15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17 17
18 import re
19 import datetime 18 import datetime
20 from urlparse import parse_qsl 19
21 from sitescripts.utils import get_config, sendMail, setupStderr 20 from sitescripts.utils import get_config, sendMail, encode_email_address
22 from sitescripts.web import url_handler 21 from sitescripts.web import url_handler, form_handler, send_simple_response
23 22
24 @url_handler('/formmail') 23 @url_handler('/formmail')
25 def handleRequest(environ, start_response): 24 @form_handler
26 setupStderr(environ['wsgi.errors']) 25 def handleRequest(environ, start_response, data):
26 params = {name: data.get(name, '').strip() for name in ('name', 'email', 'subj ect', 'message')}
27 missing = [k for k, v in params.iteritems() if not v]
27 28
28 start_response('200 OK', [('Content-Type', 'text/plain; charset=utf-8')]) 29 if missing:
29 if environ['REQUEST_METHOD'].upper() != 'POST' or not environ.get('CONTENT_TYP E', '').startswith('application/x-www-form-urlencoded'): 30 text = 'Missing fields: ' + ', '.join(missing)
30 return 'Unsupported request method' 31 return send_simple_response(start_response, 400, text)
31 32
32 try: 33 try:
33 request_body_length = int(environ['CONTENT_LENGTH']) 34 params['email'] = encode_email_address(params['email'])
34 except: 35 except ValueError:
35 return 'Invalid or missing Content-Length header' 36 return send_simple_response(start_response, 400, 'Invalid email address')
36
37 request_body = environ['wsgi.input'].read(request_body_length)
38 params = {}
39 for key, value in parse_qsl(request_body):
40 params[key] = value.decode('utf-8').strip()
41
42 if not 'name' in params or params['name'] == '':
43 return 'No name entered'
44 if not 'email' in params or params['email'] == '':
45 return 'No email address entered'
46 if not 'subject' in params or params['subject'] == '':
47 return 'No subject entered'
48 if not 'message' in params or params['message'] == '':
49 return 'No message entered'
50
51 if not re.match(r'^\w[\w.+!-]+@\w[\w.-]+\.[a-zA-Z]{2,6}$', params['email']):
52 return 'Invalid email address'
53 37
54 params['time'] = datetime.datetime.now() 38 params['time'] = datetime.datetime.now()
55 sendMail(get_config().get('formmail', 'template'), params) 39 sendMail(get_config().get('formmail', 'template'), params)
56 return 'Message sent' 40
41 return send_simple_response(start_response, 200, 'Message sent')
OLDNEW
« no previous file with comments | « no previous file | sitescripts/reports/web/updateReport.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld