| 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 | 18 |
| 19 from csv import DictWriter | |
| 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: |
| 28 yield name, value | 29 yield name, value |
| (...skipping 16 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 collect_formdata(option, params, path): | |
|
Vasily Kuznetsov
2017/02/08 18:16:17
`option` is not used?
Jon Sonesen
2017/02/10 14:04:43
Acknowledged.
| |
| 57 if os.path.isfile(path): | |
| 58 with open(path, 'a') as formlog: | |
| 59 writer = DictWriter(formlog, fieldnames=params.keys()) | |
| 60 writer.writerow(params) | |
| 61 return | |
| 62 with open(path, 'w') as new_formlog: | |
| 63 writer = DictWriter(new_formlog, fieldnames=params.keys()) | |
| 64 writer.writeheader() | |
| 65 writer.writerow(params) | |
| 66 return | |
| 67 | |
| 68 | |
| 69 def validate_fields(fields, params): | |
| 70 errors = [] | |
| 71 for field, spec in fields.items(): | |
| 72 if 'mandatory' in spec.value: | |
| 73 if field not in params.keys(): | |
| 74 errors.append(make_error(spec, 'mandatory', | |
| 75 'No {} entered'.format(field))) | |
| 76 if 'email' in spec.value and field in params.keys(): | |
| 77 try: | |
| 78 params[field] = encode_email_address(params[field]) | |
| 79 except ValueError: | |
| 80 errors.append(make_error(spec, 'email', 'Invalid email')) | |
| 81 return errors | |
| 82 | |
| 83 | |
| 55 def make_handler(name, config): | 84 def make_handler(name, config): |
| 56 try: | 85 try: |
| 86 log_path = config['csv_log'].value | |
| 87 except KeyError: | |
| 88 raise Exception('No log configured for form handler: ' + name) | |
|
Vasily Kuznetsov
2017/02/08 18:16:17
The log should be also optional and the exception
Jon Sonesen
2017/02/10 14:04:43
Done.
| |
| 89 try: | |
| 57 url = config['url'].value | 90 url = config['url'].value |
| 58 except (KeyError, AttributeError): | 91 except (KeyError, AttributeError): |
| 59 raise Exception('No URL configured for form handler:' + name) | 92 raise Exception('No URL configured for form handler:' + name) |
| 60 try: | 93 try: |
| 61 template = config['template'].value | 94 template = config['template'].value |
| 62 except (KeyError, AttributeError): | 95 except KeyError: |
| 63 raise Exception('No template configured for form handler:' + name) | 96 template = '' |
|
Vasily Kuznetsov
2017/02/08 18:16:17
It would probably be cleaner to use None here and
Jon Sonesen
2017/02/10 14:04:43
Done.
| |
| 64 try: | 97 try: |
| 65 fields = config['fields'] | 98 fields = config['fields'] |
| 66 for field, spec in fields.items(): | 99 for field, spec in fields.items(): |
| 67 spec.value = {s.strip() for s in spec.value.split(',')} | 100 spec.value = {s.strip() for s in spec.value.split(',')} |
| 68 except KeyError: | 101 except KeyError: |
| 69 raise Exception('No fields configured for form handler:' + name) | 102 raise Exception('No fields configured for form handler:' + name) |
| 70 if len(fields) == 0: | 103 if len(fields) == 0: |
| 71 raise Exception('No fields configured for form handler:' + name) | 104 raise Exception('No fields configured for form handler:' + name) |
| 72 | 105 |
| 73 @form_handler | 106 @form_handler |
| 74 def handler(environ, start_response, params): | 107 def handler(environ, start_response, params): |
| 75 response_headers = [('Content-Type', 'text/plain; charset=utf-8')] | 108 response_headers = [('Content-Type', 'text/plain; charset=utf-8')] |
| 76 errors = [] | 109 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: | 110 if errors: |
| 88 start_response('400 Bad Request', response_headers) | 111 start_response('400 Bad Request', response_headers) |
| 89 return '\n'.join(errors) | 112 return '\n'.join(errors) |
| 90 | 113 params['time'] = datetime.datetime.now() |
| 91 template_args = { | 114 if template != '': |
| 92 'time': datetime.datetime.now(), | 115 template_args = { |
| 93 'fields': {field: params.get(field, '') for field in fields} | 116 'time': params['time'], |
| 94 } | 117 'fields': {field: params.get(field, '') for field in fields} |
| 95 sendMail(template, template_args) | 118 } |
| 119 sendMail(template, template_args) | |
| 120 collect_formdata(name, params, log_path) | |
| 96 start_response('200 OK', response_headers) | 121 start_response('200 OK', response_headers) |
| 97 return '' | 122 return '' |
| 98 | 123 |
| 99 return url, handler | 124 return url, handler |
| 100 | 125 |
| 101 | 126 |
| 102 conf_dict = conf_parse(get_config_items()) | 127 conf_dict = conf_parse(get_config_items()) |
| 103 for name, config in conf_dict.items(): | 128 for name, config in conf_dict.items(): |
| 104 url, handler = make_handler(name, config) | 129 url, handler = make_handler(name, config) |
| 105 registerUrlHandler(url, handler) | 130 registerUrlHandler(url, handler) |
| OLD | NEW |