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

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

Issue 29352643: Issue 4377 - Add Configurable Form to Email Service (Closed)
Patch Set: adds test and fixed logic in handler Created Sept. 12, 2016, 1:58 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 | « sitescripts/formmail/test/test_formmail2.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/formmail/web/formmail2.py
===================================================================
new file mode 100644
--- /dev/null
+++ b/sitescripts/formmail/web/formmail2.py
@@ -0,0 +1,140 @@
+# This file is part of the Adblock Plus web scripts,
+# Copyright (C) 2006-2016 Eyeo GmbH
+#
+# Adblock Plus is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3 as
+# published by the Free Software Foundation.
+#
+# Adblock Plus is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+
+import re
+import datetime
+import collections
+from urlparse import parse_qsl
+from sitescripts.utils import get_config, sendMail, setupStderr
+from sitescripts.web import registerUrlHandler
+
+
+class BadRequestError(Exception):
+ pass
+
+
+class ConfDict(collections.OrderedDict):
+ __slots__ = ('value',)
+
+
+def get_config_items():
+ config = get_config()
+ default_keys = set(config.defaults())
+ for name, value in config.items('formmail2'):
+ if name not in default_keys:
+ yield name, value
+
+
+def store_value(conf_dict, path, value):
+ head, tail = path[0], path[1:]
+ if head not in conf_dict:
+ conf_dict[head] = ConfDict()
+ if tail:
+ store_value(conf_dict[head], tail, value)
+ else:
+ conf_dict[head].value = value
+
+
+def conf_parse(conf_items):
+ conf_dict = ConfDict()
+ for key, value in conf_items:
+ path = key.split('.')
+ store_value(conf_dict, path, value)
+ return conf_dict
+
+
+def post_handler(handler):
+ def wrapped_handler(environ, start_response):
+ setupStderr(environ['wsgi.errors'])
+ response_headers = [('Content-Type', 'text/plain; charset=utf-8')]
+
+ try:
+ request_method = environ['REQUEST_METHOD'].upper()
+ url_encoded = 'application/x-www-form-urlencoded'
+ is_url_encoded = environ.get(
+ 'CONTENT_TYPE', '').startswith(url_encoded)
+ if request_method != 'POST' or not is_url_encoded:
+ raise BadRequestError('Unsupported request method')
+ try:
+ request_body_length = int(environ['CONTENT_LENGTH'])
+ except:
+ raise BadRequestError(
+ 'Invalid or missing Content-Length header')
+ 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()
+
+ response = handler(params)
+ except BadRequestError as error:
+ start_response('400 Bad Request', response_headers)
+ return str(error)
+ start_response('200 OK', response_headers)
+ return response
+ return wrapped_handler
+
+
+def make_error(field_name, spec, check_type, error_message):
+ if check_type in spec:
+ return spec[field_name].value
+ return error_message.format(field_name)
+
+
+def make_handler(name, config):
+ try:
+ url = config['url'].value
+ except (KeyError, AttributeError):
+ raise Exception('No URL configured for form handler:' + name)
+ try:
+ template = config['template'].value
+ except (KeyError, AttributeError):
+ raise Exception('No template configured for form handler:' + name)
+ try:
+ fields = config['fields']
+ for field, spec in fields.items():
+ spec.value = set(spec.value.replace(' ', '').split(','))
+ except KeyError:
+ raise Exception('No fields configured for form handler:' + name)
+ if len(fields) == 0:
+ raise Exception('No fields configured for form handler:' + name)
+
+ @post_handler
+ def handler(params):
+ email_regex = r'^\w[\w.+!-]+@\w[\w.-]+\.[a-zA-Z]{2,6}$'
+ errors = []
+ for field, spec in fields.items():
+ if 'mandatory' in spec.value:
+ if field not in params.keys():
+ errors.append((make_error(field, spec, 'mandatory',
+ 'No {} entered'.format(field))))
+ if 'email' in spec.value and field in params.keys():
+ if not re.search(email_regex, params[field]):
+ errors.append(make_error(field, spec, 'email',
+ 'Invalid email'))
+ if errors:
+ raise BadRequestError('\n'.join(errors))
+ template_args = {
+ 'time': datetime.datetime.now(),
+ 'fields': {field: params.get(field, '') for field in fields}
+ }
+ sendMail(template, template_args)
+ return ''
+ return url, handler
+
+
+conf_dict = conf_parse(get_config_items())
+for name, config in conf_dict.items():
+ url, handler = make_handler(name, config)
+ registerUrlHandler(url, handler)
« no previous file with comments | « sitescripts/formmail/test/test_formmail2.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld