Module: check_mk
Branch: master
Commit: b0c23b6e52c61c825001724ea1058aa94b1f7ec5
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=b0c23b6e52c61c…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Dec 13 13:47:57 2018 +0100
Host attributes: Split host tag attributes into list / checkbox
* Split different tag group inputs into two dedicated classes
* Whether or not an attribute is a tag attribute is now checked
by using an explicit property instead on relying on the class
hierarchy
CMK-1421
Change-Id: I8364f2ca36f4796617c2dbab8ff407d404ac3493
---
cmk/gui/plugins/wato/utils/__init__.py | 6 +-
cmk/gui/watolib.py | 153 ++++++++++++++++++++++-----------
2 files changed, 104 insertions(+), 55 deletions(-)
diff --git a/cmk/gui/plugins/wato/utils/__init__.py
b/cmk/gui/plugins/wato/utils/__init__.py
index 27b9592..977e9ce 100644
--- a/cmk/gui/plugins/wato/utils/__init__.py
+++ b/cmk/gui/plugins/wato/utils/__init__.py
@@ -1671,7 +1671,7 @@ def configure_attributes(new,
inherited_value = container.attributes()[attrname]
has_inherited = True
- if isinstance(attr, watolib.HostTagAttribute):
+ if attr.is_tag_attribute:
inherited_tags["attr_%s" % attrname] =
'|'.join(
attr.get_tag_list(inherited_value))
break
@@ -1682,7 +1682,7 @@ def configure_attributes(new,
inherited_from = _("Default value")
inherited_value = attr.default_value()
# Also add the default values to the inherited values dict
- if isinstance(attr, watolib.HostTagAttribute):
+ if attr.is_tag_attribute:
inherited_tags["attr_%s" % attrname] = '|'.join(
attr.get_tag_list(inherited_value))
@@ -1757,7 +1757,7 @@ def configure_attributes(new,
if len(values) == 1:
defvalue = values[0]
- elif attr.is_checkbox_tag():
+ elif attr.is_checkbox_tag:
defvalue = True
else:
defvalue = attr.default_value()
diff --git a/cmk/gui/watolib.py b/cmk/gui/watolib.py
index 54ab44b..e87f81f 100644
--- a/cmk/gui/watolib.py
+++ b/cmk/gui/watolib.py
@@ -1710,7 +1710,7 @@ class CREFolder(BaseFolder):
# we drop this one day?
attributes = {}
for attr, _topic in all_host_attributes():
- if isinstance(attr, HostTagAttribute):
+ if attr.is_tag_attribute:
tagvalue = attr.get_tag_value(host_tags)
attributes[attr.name()] = tagvalue
return attributes
@@ -3342,9 +3342,14 @@ class Attribute(object):
def get_tag_list(self, value):
return []
+ @property
def is_checkbox_tag(self):
return False
+ @property
+ def is_tag_attribute(self):
+ return False
+
# A simple text attribute. It is stored in
# a Python unicode string
@@ -3470,7 +3475,17 @@ class EnumAttribute(Attribute):
class HostTagAttribute(Attribute):
- """A selection dropdown for a host tag"""
+ __metaclass__ = abc.ABCMeta
+
+ @abc.abstractproperty
+ def is_checkbox_tag(self):
+ # type: () -> bool
+ raise NotImplementedError()
+
+ @abc.abstractmethod
+ def _default_tag_value(self):
+ # type: () -> typing.Optional[str]
+ raise NotImplementedError()
def __init__(self, tag_id, title, tag_list):
self._taglist = tag_list
@@ -3481,34 +3496,11 @@ class HostTagAttribute(Attribute):
default_value=self._default_tag_value(),
)
- def _default_tag_value(self):
- if not self.is_checkbox_tag():
- return self._taglist[0][0]
- return None
-
- def is_checkbox_tag(self):
- return len(self._taglist) == 1
-
- def paint(self, value, hostname):
- # Localize the titles. To make the strings available in the scanned localization
- # files the _() function must also be placed in the configuration files
- # But don't localize empty strings - This empty string is connected to the
header
- # of the .mo file
- if self.is_checkbox_tag():
- title = self._taglist[0][1]
- if title:
- title = _u(title)
- if value:
- return "", title
- return "", "%s %s" % (_("Not"), title)
- for entry in self._taglist:
- if value == entry[0]:
- return "", entry[1] and _u(entry[1]) or ''
- return "", "" # Should never happen, at least one entry
should match
- # But case could occur if tags definitions have been changed.
+ @property
+ def is_tag_attribute(self):
+ return True
- def render_input(self, varprefix, value):
- varname = varprefix + "attr_" + self.name()
+ def _get_tag_value_and_choices(self, varname, value):
if value is None:
value = html.var(varname, "") # "" is important for tag
groups with an empty tag entry
@@ -3527,28 +3519,7 @@ class HostTagAttribute(Attribute):
if value != "" and value == tagvalue and secondary_tags:
value = value + "|" + "|".join(secondary_tags)
- if self.is_checkbox_tag():
- html.checkbox(
- varname,
- value != "",
- label=choices[0][1],
- onclick='wato_fix_visibility();',
- tags=choices[0][0])
- else:
- html.dropdown(varname, choices, value,
onchange="wato_fix_visibility();")
-
- def from_html_vars(self, varprefix):
- varname = varprefix + "attr_" + self.name()
- if self.is_checkbox_tag():
- if html.get_checkbox(varname):
- return self._taglist[0][0]
- return None
-
- # strip of secondary tags
- value = html.var(varname).split("|")[0]
- if not value:
- value = None
- return value
+ return value, choices
def get_tag_value(self, tags):
"""Special function for computing the setting of a specific
@@ -3572,6 +3543,79 @@ class HostTagAttribute(Attribute):
return [] # No matching tag
+class HostTagListAttribute(HostTagAttribute):
+ """A selection dropdown for a host tag"""
+
+ def _default_tag_value(self):
+ return self._taglist[0][0]
+
+ @property
+ def is_checkbox_tag(self):
+ return False
+
+ @property
+ def is_tag_attribute(self):
+ return True
+
+ def paint(self, value, hostname):
+ for entry in self._taglist:
+ if value == entry[0]:
+ return "", entry[1] and _u(entry[1]) or ''
+ return "", ""
+
+ def render_input(self, varprefix, value):
+ varname = varprefix + "attr_" + self.name()
+ value, choices = self._get_tag_value_and_choices(varname, value)
+ html.dropdown(varname, choices, value,
onchange="wato_fix_visibility();")
+
+ def from_html_vars(self, varprefix):
+ varname = varprefix + "attr_" + self.name()
+
+ # strip of secondary tags
+ value = html.var(varname).split("|")[0]
+ if not value:
+ value = None
+ return value
+
+
+class HostTagCheckboxAttribute(HostTagAttribute):
+ """A checkbox for a host tag group"""
+
+ def _default_tag_value(self):
+ return None
+
+ @property
+ def is_checkbox_tag(self):
+ return True
+
+ def paint(self, value, hostname):
+ title = self._taglist[0][1]
+ if title:
+ title = _u(title)
+ if value:
+ return "", title
+ return "", "%s %s" % (_("Not"), title)
+
+ def render_input(self, varprefix, value):
+ varname = varprefix + "attr_" + self.name()
+
+ value, choices = self._get_tag_value_and_choices(varname, value)
+
+ html.checkbox(
+ varname,
+ value != "",
+ label=choices[0][1],
+ onclick='wato_fix_visibility();',
+ tags=choices[0][0])
+
+ def from_html_vars(self, varprefix):
+ varname = varprefix + "attr_" + self.name()
+ if self.is_checkbox_tag:
+ if html.get_checkbox(varname):
+ return self._taglist[0][0]
+ return None
+
+
# An attribute using the generic ValueSpec mechanism
class ValueSpecAttribute(Attribute):
def __init__(self, name, vs):
@@ -3853,8 +3897,13 @@ def declare_host_tag_attributes():
if not topic:
topic = _('Host tags')
+ if len(entry[2]) == 1:
+ vs = HostTagCheckboxAttribute(*entry[:3])
+ else:
+ vs = HostTagListAttribute(*entry[:3])
+
declare_host_attribute(
- HostTagAttribute(*entry[:3]),
+ vs,
show_in_table=False,
show_in_folder=True,
editable=attr_editable,