From 0eb04cae51c67e5c7d866e99982f338f67bc1c1d Mon Sep 17 00:00:00 2001 From: Ferry Boender Date: Thu, 18 Jun 2015 08:21:59 +0200 Subject: [PATCH] Don't raise KeyError if a non-required file wasn't uploaded. --- examples/megacorp_acc/job_import_employees.sh | 5 +++++ src/formdefinition.py | 5 ++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/examples/megacorp_acc/job_import_employees.sh b/examples/megacorp_acc/job_import_employees.sh index 85263ad..7f2a976 100755 --- a/examples/megacorp_acc/job_import_employees.sh +++ b/examples/megacorp_acc/job_import_employees.sh @@ -1,5 +1,10 @@ #!/bin/sh +if [ -z "$csv_file" ]; then + echo "No file specified" >&2 + exit 1 +fi + { echo ".separator ," echo ".import $csv_file employee" diff --git a/src/formdefinition.py b/src/formdefinition.py index fe7273d..d16bf2e 100644 --- a/src/formdefinition.py +++ b/src/formdefinition.py @@ -225,7 +225,10 @@ class FormDefinition(object): """ Validate a form field of type 'file'. """ - value = form_values[field_def['name']] + try: + value = form_values[field_def['name']] + except KeyError: + return None field_name = field_def['name'] upload_fname = form_values[u'{0}__name'.format(field_name)] upload_fname_ext = os.path.splitext(upload_fname)[-1].lstrip('.')