Module: check_mk
Branch: master
Commit: ea4dc9079c1a14fda666235c77ca1f59ded46990
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=ea4dc9079c1a14…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Mon Jul 3 08:43:01 2017 +0200
DropdownChoice: Identify choices by hash; complain invalid choices by default
Change-Id: I7a52abf84641fb865c6006ff31a701a1bf0f6c33
---
.bugs/2175 | 7 +++++--
web/htdocs/valuespec.py | 33 ++++++++++++++++++++++++++-------
2 files changed, 31 insertions(+), 9 deletions(-)
diff --git a/.bugs/2175 b/.bugs/2175
index 0e2908b..7cb4b4a 100644
--- a/.bugs/2175
+++ b/.bugs/2175
@@ -1,9 +1,9 @@
Title: DropdownChoice: Selects incorrect value if the list of options changes
Component: multisite
-State: open
+Class: bug
+State: done
Date: 2015-07-02 09:36:51
Targetversion: future
-Class: bug
1) Go to BI Rule editor
2) Create new rule, select Call-a-Rule
@@ -20,3 +20,6 @@ text that is displayed in the dropdown (i.e. the titles of the
choices).
We ship that in an URL variable. If that is present and does not match the
actual choices then we can issue a MKUserError and the user must select
again.
+
+2017-07-03 08:40:39: changed state open -> done
+Fixed
diff --git a/web/htdocs/valuespec.py b/web/htdocs/valuespec.py
index bb58055..1aae06c 100644
--- a/web/htdocs/valuespec.py
+++ b/web/htdocs/valuespec.py
@@ -1568,7 +1568,7 @@ class DropdownChoice(ValueSpec):
self._prefix_values = kwargs.get("prefix_values", False)
self._sorted = kwargs.get("sorted", False)
self._empty_text = kwargs.get("empty_text", _("There are
no elements defined for this selection yet."))
- self._invalid_choice = kwargs.get("invalid_choice",
"replace") # also possible: "complain"
+ self._invalid_choice = kwargs.get("invalid_choice",
"complain") # also possible: "replace"
self._invalid_choice_title = kwargs.get("invalid_choice_title",
_("Element does not exist anymore"))
self._invalid_choice_error = kwargs.get("invalid_choice_error",
_("The selected element is not longer available. Please select something
else."))
@@ -1623,9 +1623,11 @@ class DropdownChoice(ValueSpec):
if len(options) == 0:
html.write(self._empty_text)
elif len(options[0]) == 3:
- html.icon_dropdown(varprefix, options, deflt=defval)
+ html.icon_dropdown(varprefix, self._options_for_html(options),
+ deflt=self._option_id(defval))
else:
- html.dropdown(varprefix, options, deflt=defval, onchange=self._on_change,
sorted=self._sorted)
+ html.dropdown(varprefix, self._options_for_html(options),
+ deflt=self._option_id(defval),onchange=self._on_change,
sorted=self._sorted)
def _get_invalid_choice_title(self, value):
@@ -1647,17 +1649,34 @@ class DropdownChoice(ValueSpec):
def from_html_vars(self, varprefix):
- sel = html.var(varprefix)
choices = self.choices()
+
for n, entry in enumerate(choices):
- val = entry[0]
- if sel == str(n):
+ val, title = entry[:2]
+ if self._is_selected_option_from_html(varprefix, val):
return val
if self._invalid_choice == "replace":
return self.default_value() # garbled URL or len(choices) == 0
else:
- return sel
+ raise MKUserError(varprefix, self._invalid_choice_error)
+
+
+ def _is_selected_option_from_html(self, varprefix, val):
+ selected_value = html.var(varprefix)
+ return selected_value == self._option_id(val)
+
+
+ def _options_for_html(self, orig_options):
+ options = []
+ for val, title in orig_options:
+ options.append((self._option_id(val), title))
+ return options
+
+
+ def _option_id(self, val):
+ return "%s" % hash(val)
+
def validate_value(self, value, varprefix):
if self._no_preselect and value == self._no_preselect_value: