Module: check_mk
Branch: master
Commit: aaaa35414b3e83c40f2f501acf9e6438100da929
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=aaaa35414b3e83…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Sat Feb 25 20:13:50 2012 +0100
New valuespec MultiSelect
---
web/htdocs/htmllib.py | 11 +++++++++++
web/htdocs/index.py | 10 +++++++++-
web/htdocs/valuespec.py | 27 +++++++++++++++++++++++++++
3 files changed, 47 insertions(+), 1 deletions(-)
diff --git a/web/htdocs/htmllib.py b/web/htdocs/htmllib.py
index e1789c0..c3e8c33 100644
--- a/web/htdocs/htmllib.py
+++ b/web/htdocs/htmllib.py
@@ -865,6 +865,17 @@ class html:
else:
return val.decode("utf-8")
+ # Return all values of a variable that possible occurs more
+ # than once in the URL. note: req.listvars does contain those
+ # variable only, if the really occur more than once.
+ def list_var(self, varname):
+ if varname in self.req.listvars:
+ return self.req.listvars[varname]
+ elif varname in self.req.vars:
+ return [self.req.vars[varname]]
+ else:
+ return []
+
def set_var(self, varname, value):
if value == None:
self.del_var(varname)
diff --git a/web/htdocs/index.py b/web/htdocs/index.py
index 8aadb0b..7079202 100644
--- a/web/htdocs/index.py
+++ b/web/htdocs/index.py
@@ -58,11 +58,19 @@ if defaults.omd_root:
def read_get_vars(req):
req.vars = {}
+ req.listvars = {} # for variables with more than one occurrance
fields = util.FieldStorage(req, keep_blank_values = 1)
for field in fields.list:
varname = field.name
value = field.value
- req.vars[varname] = value
+ # Multiple occurrance of a variable? Store in extra list dict
+ if varname in req.vars:
+ if varname in req.listvars:
+ req.listvars[varname].append(value)
+ else:
+ req.listvars[varname] = [ req.vars[varname], value ]
+ else:
+ req.vars[varname] = value
def read_cookies(req):
req.cookies = Cookie.get_cookies(req)
diff --git a/web/htdocs/valuespec.py b/web/htdocs/valuespec.py
index 23608c5..b6c54cb 100644
--- a/web/htdocs/valuespec.py
+++ b/web/htdocs/valuespec.py
@@ -885,6 +885,33 @@ class ListChoice(ValueSpec):
raise MKUserError(varprefix, _("%s is not an allowed value") %
v)
+# A alternative way of editing list choices
+class MultiSelect(ListChoice):
+ def __init__(self, **kwargs):
+ ListChoice.__init__(self, **kwargs)
+
+ def render_input(self, varprefix, value):
+ self.load_elements()
+ html.write("<select multiple name='%s'>" % varprefix)
+ for nr, (key, title) in enumerate(self._elements):
+ if key in value:
+ sel = " selected"
+ else:
+ sel = ""
+ html.write('<option value="%s"%s>%s</option>\n'
% (key, sel, title))
+ html.write("</select>")
+
+ def from_html_vars(self, varprefix):
+ self.load_elements()
+ value = []
+ hv = html.list_var(varprefix)
+ for key, title in self._elements:
+ if key in hv:
+ value.append(key)
+ return value
+
+
+
# A type-save dropdown choice with one extra field that
# opens a further value spec for entering an alternative