Module: check_mk
Branch: master
Commit: 6454cc5211615533df9a6ad46384e30abbe63904
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=6454cc52116155…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Mon Apr 8 12:36:59 2013 +0200
Improved handling of valuespec validations in WATO rule editor
Conflicts:
ChangeLog
---
ChangeLog | 4 ++++
web/htdocs/valuespec.py | 44 +++++++++++++++++++++++++++-----------------
web/htdocs/wato.py | 8 +++++++-
3 files changed, 38 insertions(+), 18 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index ebf450e..645dc65 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,4 @@
+<<<<<<< HEAD
1.2.3i1:
Core:
* Agents can send data for other hosts "piggyback". This is being
@@ -111,6 +112,9 @@
* FIX: do not remove directories of non-exisant users anymore. This lead to
a deletion of users' settings in case of an external authentication
(like mod_ldap).
+ WATO:
+ * Improved handling of valuespec validations in WATO rule editor. Displaying a
+ warning message when going to throw away the current settings.
1.2.2:
Core:
diff --git a/web/htdocs/valuespec.py b/web/htdocs/valuespec.py
index 2564c00..daf92f5 100644
--- a/web/htdocs/valuespec.py
+++ b/web/htdocs/valuespec.py
@@ -27,6 +27,12 @@
import math, os, time, re, sre_constants, urlparse, forms, htmllib
from lib import *
+def type_name(v):
+ try:
+ return type(v).__name__
+ except:
+ return htmllib.attrencode(type(v))
+
# Abstract base class of all value declaration classes.
class ValueSpec:
def __init__(self, **kwargs):
@@ -190,7 +196,8 @@ class Age(ValueSpec):
def validate_datatype(self, value, varprefix):
if type(value) != int:
- raise MKUserError(varprefix, _("The value %r has type %s, but must be of
type int") % (value, type(value)))
+ raise MKUserError(varprefix, _("The value %r has type %s, but must be of
type int") %
+ (value, type_name(value)))
# Editor for a single integer
@@ -254,7 +261,7 @@ class Integer(ValueSpec):
def validate_datatype(self, value, varprefix):
if type(value) != int:
raise MKUserError(varprefix, _("The value %r has the wrong type %s, but
must be of type int")
- % (value, type(value)))
+ % (value, type_name(value)))
def validate_value(self, value, varprefix):
if self._minvalue != None and value < self._minvalue:
@@ -346,7 +353,8 @@ class TextAscii(ValueSpec):
return
if type(value) != str:
- raise MKUserError(varprefix, _("The value must be of type str, but it
has type %s") % type(value))
+ raise MKUserError(varprefix, _("The value must be of type str, but it
has type %s") %
+ type_name(value))
def validate_value(self, value, varprefix):
if self._none_is_empty and value == "":
@@ -370,7 +378,8 @@ class TextUnicode(TextAscii):
def validate_datatype(self, value, varprefix):
if type(value) not in [ str, unicode ]:
- raise MKUserError(varprefix, _("The value must be of type str or
unicode, but it has type %s") % type(value))
+ raise MKUserError(varprefix, _("The value must be of type str or
unicode, but it has type %s") %
+
type_name(value))
# Internal ID as used in many places (for contact names, group name,
# an so on)
@@ -646,7 +655,7 @@ class ListOfStrings(ValueSpec):
def validate_datatype(self, value, vp):
if type(value) != list:
raise MKUserError(varprefix, _("Expected data type is "
- "list, but your type is %s." % type(value)))
+ "list, but your type is %s." % type_name(value)))
for nr, s in enumerate(value):
self._valuespec.validate_datatype(s, vp + "_%d" % nr)
@@ -785,7 +794,7 @@ class ListOf(ValueSpec):
def validate_datatype(self, value, varprefix):
if type(value) != list:
- raise MKUserError(varprefix, _("The type must be list, but is %r")
% type(value))
+ raise MKUserError(varprefix, _("The type must be list, but is %s")
% type_name(value))
for n, v in enumerate(value):
self._valuespec.validate_datatype(v, varprefix + "_%d" % (n+1))
@@ -823,7 +832,7 @@ class Float(Integer):
if type(value) != float and not \
(type(value) == int and self._accept_int):
raise MKUserError(varprefix, _("The value %r has type %s, but must be of
type float%s") %
- (value, type(value), self._accept_int and _(" or int") or
""))
+ (value, type_name(value), self._accept_int and _(" or int") or
""))
class Percentage(Float):
@@ -844,7 +853,7 @@ class Percentage(Float):
if self._allow_int:
if type(value) not in [ int, float ]:
raise MKUserError(varprefix, _("The value %r has type %s, but must
be either float or int")
- % (value, type(value)))
+ % (value, type_name(value)))
else:
Float.validate_datatype(self, value, varprefix)
@@ -874,7 +883,8 @@ class Checkbox(ValueSpec):
def validate_datatype(self, value, varprefix):
if type(value) != bool:
- raise MKUserError(varprefix, _("The value %r has type %s, but must be of
type bool") % (value, type(value)))
+ raise MKUserError(varprefix, _("The value %r has type %s, but must be of
type bool") %
+ (value, type_name(value)))
# A type-save dropdown choice. Parameters:
# help_separator: if you set this to a character, e.g. "-", then
@@ -1212,7 +1222,7 @@ class ListChoice(ValueSpec):
def validate_datatype(self, value, varprefix):
self.load_elements()
if type(value) != list:
- raise MKUserError(varprefix, _("The datatype must be list, but is
%s") % type(value))
+ raise MKUserError(varprefix, _("The datatype must be list, but is
%s") % type_name(value))
d = dict(self._elements)
for v in value:
if v not in d:
@@ -1463,7 +1473,7 @@ class AbsoluteDate(ValueSpec):
def validate_datatype(self, value, varprefix):
if type(value) not in [ int, float ]:
raise MKUserError(varprefix, _("The type of the timestamp must be int or
float, but is %s") %
- type(value))
+ type_name(value))
def validate_value(self, value, varprefix):
if value < 0 or int(value) > (2**31-1):
@@ -1520,14 +1530,14 @@ class Timeofday(ValueSpec):
return
if type(value) != tuple:
- raise MKUserError(varprefix, _("The datatype must be tuple, but ist
%s" % type(value)))
+ raise MKUserError(varprefix, _("The datatype must be tuple, but ist
%s" % type_name(value)))
if len(value) != 2:
raise MKUserError(varprefix, _("The tuple must contain two elements, but
you have %d" % len(value)))
for x in value:
if type(x) != int:
- raise MKUserError(varprefix, _("All elements of the tuple must be of
type int, you have %s" % type(x)))
+ raise MKUserError(varprefix, _("All elements of the tuple must be of
type int, you have %s" % type_name(x)))
def validate_value(self, value, varprefix):
if not self._allow_empty and value == None:
@@ -1585,7 +1595,7 @@ class TimeofdayRange(ValueSpec):
return
if type(value) != tuple:
- raise MKUserError(varprefix, _("The datatype must be tuple, but ist
%s" % type(value)))
+ raise MKUserError(varprefix, _("The datatype must be tuple, but ist
%s" % type_name(value)))
if len(value) != 2:
raise MKUserError(varprefix, _("The tuple must contain two elements, but
you have %d" % len(value)))
@@ -1862,7 +1872,7 @@ class Tuple(ValueSpec):
def validate_datatype(self, value, varprefix):
if type(value) != tuple:
raise MKUserError(varprefix,
- _("The datatype must be a tuple, but is %r") % type(value))
+ _("The datatype must be a tuple, but is %s") % type_name(value))
if len(value) != len(self._elements):
raise MKUserError(varprefix,
_("The number of elements in the tuple must be exactly %d.") %
len(self._elements))
@@ -2074,7 +2084,7 @@ class Dictionary(ValueSpec):
value = self.migrate(value)
if type(value) != dict:
- raise MKUserError(varprefix, _("The type must be a dictionary, but it is
a %s") % type(value))
+ raise MKUserError(varprefix, _("The type must be a dictionary, but it is
a %s") % type_name(value))
for param, vs in self._get_elements():
if param in value:
@@ -2157,7 +2167,7 @@ class ElementSelection(ValueSpec):
def validate_datatype(self, value, varprefix):
if type(value) != str:
- raise MKUserError(varprefix, _("The datatype must be str (string), but
is %s") % type(value))
+ raise MKUserError(varprefix, _("The datatype must be str (string), but
is %s") % type_name(value))
class AutoTimestamp(FixedValue):
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 3828f0b..06bcc3c 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -10739,9 +10739,15 @@ def mode_edit_rule(phase, new = False):
try:
valuespec.validate_datatype(value, "ve")
valuespec.render_input("ve", value)
- except:
+ except Exception, e:
if config.debug:
raise
+ else:
+ html.show_warning(_('Unable to read current options of this rule.
Falling back to '
+ 'default values. When saving this rule now, your
previous settings '
+ 'will be overwritten. Problem was: %s.') %
e)
+
+ # In case of validation problems render the input with default values
valuespec.render_input("ve", valuespec.default_value())
valuespec.set_focus("ve")