| Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| 1 # This file is part of the Adblock Plus web scripts, | 1 # This file is part of the Adblock Plus web scripts, | 
| 2 # Copyright (C) 2006-2016 Eyeo GmbH | 2 # Copyright (C) 2006-2016 Eyeo GmbH | 
| 3 # | 3 # | 
| 4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify | 
| 5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as | 
| 6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. | 
| 7 # | 7 # | 
| 8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, | 
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
| 11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. | 
| 12 # | 12 # | 
| 13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License | 
| 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 
| 15 | 15 import os | 
| 16 import datetime | 16 import datetime | 
| 17 import collections | 17 import collections | 
| 18 from csv import DictWriter, DictReader | |
| 18 | 19 | 
| 19 from sitescripts.utils import get_config, sendMail, encode_email_address | 20 from sitescripts.utils import get_config, sendMail, encode_email_address | 
| 20 from sitescripts.web import registerUrlHandler, form_handler | 21 from sitescripts.web import registerUrlHandler, form_handler | 
| 21 | 22 | 
| 22 | 23 | 
| 23 def get_config_items(): | 24 def get_config_items(): | 
| 24 config = get_config() | 25 config = get_config() | 
| 25 default_keys = set(config.defaults()) | 26 default_keys = set(config.defaults()) | 
| 26 for name, value in config.items('formmail2'): | 27 for name, value in config.items('formmail2'): | 
| 27 if name not in default_keys: | 28 if name not in default_keys: | 
| (...skipping 17 matching lines...) Expand all Loading... | |
| 45 store_value(conf_dict, path, value) | 46 store_value(conf_dict, path, value) | 
| 46 return conf_dict | 47 return conf_dict | 
| 47 | 48 | 
| 48 | 49 | 
| 49 def make_error(spec, check_type, default_message): | 50 def make_error(spec, check_type, default_message): | 
| 50 if check_type in spec: | 51 if check_type in spec: | 
| 51 return spec[check_type].value | 52 return spec[check_type].value | 
| 52 return default_message | 53 return default_message | 
| 53 | 54 | 
| 54 | 55 | 
| 56 def formfield_error(parameters, log_path): | |
| 57 err_file = os.path.basename(log_path) + '_error' | |
| 58 err_path = os.path.join(os.path.dirname(log_path), err_file) | |
| 59 if os.path.isfile(err_path): | |
| 60 with open(err_path, 'a') as error_log: | |
| 61 writer = DictWriter(error_log, fieldnames=parameters.keys()) | |
| 62 writer.writerow(parameters) | |
| 63 raise Exception('Field names have changed, error log ' | |
| 64 'appended to ' + err_path) | |
| 65 with open(err_path, 'w') as error_log: | |
| 66 writer = DictWriter(error_log, fieldnames=parameters.keys()) | |
| 67 writer.writeheader() | |
| 68 writer.writerow(parameters) | |
| 69 raise Exception('Field names have changed, error log ' | |
| 70 'written to ' + err_path) | |
| 71 | |
| 72 | |
| 73 def log_formdata(params, path): | |
| 74 if os.path.isfile(path): | |
| 75 with open(path, 'ab+') as formlog: | |
| 76 formlog.seek(0) | |
| 77 reader = DictReader(formlog) | |
| 78 if reader.fieldnames != params.keys(): | |
| 79 formfield_error(params, path) | |
| 80 formlog.seek(os.SEEK_END) | |
| 81 writer = DictWriter(formlog, fieldnames=params.keys()) | |
| 82 writer.writerow(params) | |
| 83 return | |
| 84 with open(path, 'w') as new_formlog: | |
| 85 writer = DictWriter(new_formlog, fieldnames=params.keys()) | |
| 86 writer.writeheader() | |
| 87 writer.writerow(params) | |
| 88 return | |
| 89 | |
| 90 | |
| 91 def validate_fields(fields, params): | |
| 92 errors = [] | |
| 93 for field, spec in fields.items(): | |
| 94 if 'mandatory' in spec.value: | |
| 95 if field not in params.keys(): | |
| 96 errors.append(make_error(spec, 'mandatory', | |
| 97 'No {} entered'.format(field))) | |
| 98 if 'email' in spec.value and field in params.keys(): | |
| 99 try: | |
| 100 params[field] = encode_email_address(params[field]) | |
| 101 except ValueError: | |
| 102 errors.append(make_error(spec, 'email', 'Invalid email')) | |
| 103 return errors | |
| 104 | |
| 105 | |
| 55 def make_handler(name, config): | 106 def make_handler(name, config): | 
| 56 try: | 107 try: | 
| 57 url = config['url'].value | 108 url = config['url'].value | 
| 58 except (KeyError, AttributeError): | 109 except (KeyError, AttributeError): | 
| 59 raise Exception('No URL configured for form handler:' + name) | 110 raise Exception('No URL configured for form handler:' + name) | 
| 60 try: | 111 try: | 
| 61 template = config['template'].value | 112 template = config['template'].value | 
| 62 except (KeyError, AttributeError): | 113 except KeyError: | 
| 63 raise Exception('No template configured for form handler:' + name) | 114 template = None | 
| 64 try: | 115 try: | 
| 65 fields = config['fields'] | 116 fields = config['fields'] | 
| 66 for field, spec in fields.items(): | 117 for field, spec in fields.items(): | 
| 67 spec.value = {s.strip() for s in spec.value.split(',')} | 118 spec.value = {s.strip() for s in spec.value.split(',')} | 
| 68 except KeyError: | 119 except KeyError: | 
| 69 raise Exception('No fields configured for form handler:' + name) | 120 raise Exception('No fields configured for form handler:' + name) | 
| 70 if len(fields) == 0: | 121 if len(fields) == 0: | 
| 71 raise Exception('No fields configured for form handler:' + name) | 122 raise Exception('No fields configured for form handler:' + name) | 
| 72 | 123 | 
| 73 @form_handler | 124 @form_handler | 
| 74 def handler(environ, start_response, params): | 125 def handler(environ, start_response, params): | 
| 75 response_headers = [('Content-Type', 'text/plain; charset=utf-8')] | 126 response_headers = [('Content-Type', 'text/plain; charset=utf-8')] | 
| 76 errors = [] | 127 errors = validate_fields(fields, params) | 
| 77 for field, spec in fields.items(): | |
| 78 if 'mandatory' in spec.value: | |
| 79 if field not in params.keys(): | |
| 80 errors.append(make_error(spec, 'mandatory', | |
| 81 'No {} entered'.format(field))) | |
| 82 if 'email' in spec.value and field in params.keys(): | |
| 83 try: | |
| 84 params[field] = encode_email_address(params[field]) | |
| 85 except ValueError: | |
| 86 errors.append(make_error(spec, 'email', 'Invalid email')) | |
| 87 if errors: | 128 if errors: | 
| 88 start_response('400 Bad Request', response_headers) | 129 start_response('400 Bad Request', response_headers) | 
| 89 return '\n'.join(errors) | 130 return '\n'.join(errors) | 
| 131 params = {field: params.get(field, '').encode('utf8') | |
| 132 for field in fields} | |
| 133 time = datetime.datetime.now() | |
| 90 | 134 | 
| 91 template_args = { | 135 if template is not None: | 
| 
 
Vasily Kuznetsov
2017/03/09 19:58:09
I like how you did it with 'csv_log' in config bel
 
Jon Sonesen
2017/03/10 09:28:51
I agree
 
 | |
| 92 'time': datetime.datetime.now(), | 136 template_args = {'time': time, 'fields': {field: | 
| 
 
Vasily Kuznetsov
2017/03/09 19:58:09
Yeah, like this it's hard to read indeed. What I w
 
Jon Sonesen
2017/03/10 09:28:51
Done.
 
 | |
| 93 'fields': {field: params.get(field, '') for field in fields} | 137 params.get(field, '') for field in fields}} | 
| 94 } | 138 sendMail(template, template_args) | 
| 95 sendMail(template, template_args) | 139 | 
| 140 params['time'] = time | |
| 
 
Vasily Kuznetsov
2017/03/09 19:58:10
This could also be inside of the if on the followi
 
Jon Sonesen
2017/03/10 09:28:51
Actually I thought that we always wanted a timesta
 
Vasily Kuznetsov
2017/03/10 09:54:28
Yeah, indeed, we can just put the time into the pa
 
Jon Sonesen
2017/03/14 19:41:26
Done.
 
 | |
| 141 if 'csv_log' in config: | |
| 142 log_formdata(params, config['csv_log'].value) | |
| 96 start_response('200 OK', response_headers) | 143 start_response('200 OK', response_headers) | 
| 97 return '' | 144 return '' | 
| 98 | 145 | 
| 99 return url, handler | 146 return url, handler | 
| 100 | 147 | 
| 101 | 148 | 
| 102 conf_dict = conf_parse(get_config_items()) | 149 conf_dict = conf_parse(get_config_items()) | 
| 103 for name, config in conf_dict.items(): | 150 for name, config in conf_dict.items(): | 
| 104 url, handler = make_handler(name, config) | 151 url, handler = make_handler(name, config) | 
| 105 registerUrlHandler(url, handler) | 152 registerUrlHandler(url, handler) | 
| OLD | NEW |