summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAssaf Gordon <assafgordon@gmail.com>2016-06-19 04:52:42 (GMT)
committerAssaf Gordon <assafgordon@gmail.com>2016-06-21 07:03:24 (GMT)
commit002f8f87a239c51daf0cbd991cf3e7f5a68bf80c (patch)
treebb676363de7540885607b08caddc70274067a4e9
parentfe9e70d5cf7fb091d31298b17b10503cbffdaddc (diff)
downloadcgi-tools-002f8f87a239c51daf0cbd991cf3e7f5a68bf80c.zip
cgi-tools-002f8f87a239c51daf0cbd991cf3e7f5a68bf80c.tar.gz
cgi-tools-002f8f87a239c51daf0cbd991cf3e7f5a68bf80c.tar.bz2
params.py: new method 'get_options_param()'
-rw-r--r--cgi_tools/__init__.py4
-rw-r--r--cgi_tools/params.py10
2 files changed, 13 insertions, 1 deletions
diff --git a/cgi_tools/__init__.py b/cgi_tools/__init__.py
index adcf9a5..cba2a08 100644
--- a/cgi_tools/__init__.py
+++ b/cgi_tools/__init__.py
@@ -12,7 +12,9 @@ from .http_responses import http_bad_request_error, http_server_error, \
from .validators import valid_regex, valid_int, valid_float, valid_in_list
-from .params import save_cgi_file_param, get_cgi_first_non_empty_param
+from .params import save_cgi_file_param, get_cgi_first_non_empty_param, \
+ get_options_param
+
# Try to load the version string.
# the 'version.py' file is auto-generated by 'setup.py'.
diff --git a/cgi_tools/params.py b/cgi_tools/params.py
index 91f532e..194905f 100644
--- a/cgi_tools/params.py
+++ b/cgi_tools/params.py
@@ -6,6 +6,7 @@ License: BSD (See LICENSE file)
from tempfile import NamedTemporaryFile
from .http_responses import http_bad_request_error, http_server_error, log
from .types import to_str_list
+from .validators import valid_in_list
def save_cgi_file_param(form,var_name,suffix=None):
if not var_name in form:
@@ -80,3 +81,12 @@ def get_cgi_first_non_empty_param(form,param_names,allow_empty=False):
l = ','.join(param_names)
http_bad_request_error("at least one CGI parameters must be non-empty: "+l)
+
+def get_options_param(form, param_name, allowed_options):
+ text = form.getfirst(param_name,"")
+ if not valid_in_list(text,allowed_options):
+ l = ','.join(allowed_options)
+ http_bad_request_error("CGI param '%s' must be one of: %s" \
+ % (param_name,l))
+
+ return text