Index: sitescripts/formmail/web/formmail2.py |
=================================================================== |
--- a/sitescripts/formmail/web/formmail2.py |
+++ b/sitescripts/formmail/web/formmail2.py |
@@ -7,20 +7,21 @@ |
# |
# 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 os |
import datetime |
import collections |
+from csv import DictWriter |
from sitescripts.utils import get_config, sendMail, encode_email_address |
from sitescripts.web import registerUrlHandler, form_handler |
def get_config_items(): |
config = get_config() |
default_keys = set(config.defaults()) |
for name, value in config.items('formmail2'): |
@@ -47,57 +48,81 @@ |
def make_error(spec, check_type, default_message): |
if check_type in spec: |
return spec[check_type].value |
return default_message |
+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.
|
+ if os.path.isfile(path): |
+ with open(path, 'a') as formlog: |
+ writer = DictWriter(formlog, fieldnames=params.keys()) |
+ writer.writerow(params) |
+ return |
+ with open(path, 'w') as new_formlog: |
+ writer = DictWriter(new_formlog, fieldnames=params.keys()) |
+ writer.writeheader() |
+ writer.writerow(params) |
+ return |
+ |
+ |
+def validate_fields(fields, params): |
+ errors = [] |
+ for field, spec in fields.items(): |
+ if 'mandatory' in spec.value: |
+ if field not in params.keys(): |
+ errors.append(make_error(spec, 'mandatory', |
+ 'No {} entered'.format(field))) |
+ if 'email' in spec.value and field in params.keys(): |
+ try: |
+ params[field] = encode_email_address(params[field]) |
+ except ValueError: |
+ errors.append(make_error(spec, 'email', 'Invalid email')) |
+ return errors |
+ |
+ |
def make_handler(name, config): |
try: |
+ log_path = config['csv_log'].value |
+ except KeyError: |
+ 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.
|
+ 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) |
+ except KeyError: |
+ 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.
|
try: |
fields = config['fields'] |
for field, spec in fields.items(): |
spec.value = {s.strip() for s in spec.value.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) |
@form_handler |
def handler(environ, start_response, params): |
response_headers = [('Content-Type', 'text/plain; charset=utf-8')] |
- errors = [] |
- for field, spec in fields.items(): |
- if 'mandatory' in spec.value: |
- if field not in params.keys(): |
- errors.append(make_error(spec, 'mandatory', |
- 'No {} entered'.format(field))) |
- if 'email' in spec.value and field in params.keys(): |
- try: |
- params[field] = encode_email_address(params[field]) |
- except ValueError: |
- errors.append(make_error(spec, 'email', 'Invalid email')) |
+ errors = validate_fields(fields, params) |
if errors: |
start_response('400 Bad Request', response_headers) |
return '\n'.join(errors) |
- |
- template_args = { |
- 'time': datetime.datetime.now(), |
- 'fields': {field: params.get(field, '') for field in fields} |
- } |
- sendMail(template, template_args) |
+ params['time'] = datetime.datetime.now() |
+ if template != '': |
+ template_args = { |
+ 'time': params['time'], |
+ 'fields': {field: params.get(field, '') for field in fields} |
+ } |
+ sendMail(template, template_args) |
+ collect_formdata(name, params, log_path) |
start_response('200 OK', response_headers) |
return '' |
return url, handler |
conf_dict = conf_parse(get_config_items()) |
for name, config in conf_dict.items(): |