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

Unified Diff: sitescripts/formmail/web/formmail.py

Issue 5717434384252928: Issue 2267 - Unify form handling by reusing form_handler() and encode_email_address() (Closed)
Patch Set: Created April 2, 2015, 2:24 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sitescripts/reports/web/updateReport.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/formmail/web/formmail.py
===================================================================
--- a/sitescripts/formmail/web/formmail.py
+++ b/sitescripts/formmail/web/formmail.py
@@ -16,39 +16,29 @@
# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
import re
-from urlparse import parse_qsl
from sitescripts.utils import get_config, sendMail, setupStderr
-from sitescripts.web import url_handler
+from sitescripts.web import url_handler, form_handler
@url_handler('/formmail')
-def handleRequest(environ, start_response):
+@form_handler
+def handleRequest(environ, start_response, data):
setupStderr(environ['wsgi.errors'])
- start_response('200 OK', [('Content-Type', 'text/plain; charset=utf-8')])
- if environ['REQUEST_METHOD'].upper() != 'POST' or not environ.get('CONTENT_TYPE', '').startswith('application/x-www-form-urlencoded'):
- return 'Unsupported request method'
+ params = {}
+ for field in ('name', 'email', 'subject', 'message'):
+ value = data.get(field, '').strip()
- try:
- request_body_length = int(environ['CONTENT_LENGTH'])
- except:
- return 'Invalid or missing Content-Length header'
+ if not value:
+ start_response('400 Bad request')
+ return ['No "%s" given' % field]
- request_body = environ['wsgi.input'].read(request_body_length)
- params = {}
- for key, value in parse_qsl(request_body):
- params[key] = value.decode('utf-8').strip()
-
- if not 'name' in params or params['name'] == '':
- return 'No name entered'
- if not 'email' in params or params['email'] == '':
- return 'No email address entered'
- if not 'subject' in params or params['subject'] == '':
- return 'No subject entered'
- if not 'message' in params or params['message'] == '':
- return 'No message entered'
+ params[field] = value
if not re.match(r'^\w[\w.+!-]+@\w[\w.-]+\.[a-zA-Z]{2,6}$', params['email']):
- return 'Invalid email address'
+ start_response('400 Bad request')
+ return ['Invalid email address']
sendMail(get_config().get('formmail', 'template'), params)
- return 'Message sent'
+
+ start_response('200 OK')
+ return ['Message sent']
« 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