Module: check_mk
Branch: master
Commit: f0bdd61ebcd66a32780ee7722c3086f0eadf4d45
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=f0bdd61ebcd66a…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Sat Jun 10 15:57:44 2017 +0200
Cleaned up custom attribute handling
---
web/htdocs/userdb.py | 39 +++++++--------------------------------
web/htdocs/wato.py | 33 ++++++++++++++++++++++++++++-----
2 files changed, 35 insertions(+), 37 deletions(-)
diff --git a/web/htdocs/userdb.py b/web/htdocs/userdb.py
index 9bad29c..5cdb5c8 100644
--- a/web/htdocs/userdb.py
+++ b/web/htdocs/userdb.py
@@ -55,9 +55,6 @@ def load_plugins(force):
# during runtime, means they need to be loaded during each page request.
# But delete the old definitions before to also apply removals of attributes
if user_attributes:
- for attr_name in user_attributes.keys():
- if attr_name not in builtin_user_attribute_names:
- del user_attributes[attr_name]
declare_custom_user_attrs()
connection_dict.clear()
@@ -1021,36 +1018,14 @@ class GroupChoice(DualListChoice):
# | Mange custom attributes of users (in future hosts etc.) |
# '----------------------------------------------------------------------'
-# TODO: userdb is a bad place for this when it manages user and host attributes!
-# Maybe move to own module?
-def load_custom_attrs():
- try:
- filename = multisite_dir + "custom_attrs.mk"
- if not os.path.exists(filename):
- return {}
-
- vars = {
- 'wato_user_attrs': [],
- 'wato_host_attrs': [],
- }
- execfile(filename, vars, vars)
-
- attrs = {}
- for what in [ "user", "host" ]:
- attrs[what] = vars.get("wato_%s_attrs" % what, [])
- return attrs
-
- except Exception, e:
- if config.debug:
- raise
- raise MKGeneralException(_("Cannot read configuration file %s: %s") %
- (filename, e))
-
-
def declare_custom_user_attrs():
- all_attrs = load_custom_attrs()
- attrs = all_attrs.setdefault('user', [])
- for attr in attrs:
+ # First remove all previously registered custom host attributes
+ for attr_name in user_attributes.keys():
+ if attr_name not in builtin_user_attribute_names:
+ del user_attributes[attr_name]
+
+ # now declare the custom attributes
+ for attr in config.wato_user_attrs:
vs = globals()[attr['type']](title = attr['title'], help =
attr['help'])
declare_user_attribute(attr['name'], vs,
user_editable = attr['user_editable'],
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index 08a0682..a7b7333 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -14721,6 +14721,31 @@ custom_attr_types = [
('TextAscii', _('Simple Text')),
]
+def load_custom_attrs():
+ try:
+ filename = multisite_dir + "custom_attrs.mk"
+ if not os.path.exists(filename):
+ return {}
+
+ vars = {
+ 'wato_user_attrs': [],
+ 'wato_host_attrs': [],
+ }
+ execfile(filename, vars, vars)
+
+ attrs = {}
+ for what in [ "user", "host" ]:
+ attrs[what] = vars.get("wato_%s_attrs" % what, [])
+ return attrs
+
+ except Exception, e:
+ if config.debug:
+ raise
+ raise MKGeneralException(_("Cannot read configuration file %s: %s") %
+ (filename, e))
+
+
+
def save_custom_attrs(attrs):
output = wato_fileheader()
for what in [ "user", "host" ]:
@@ -14752,7 +14777,7 @@ def mode_edit_custom_attr(phase, what):
html.context_button(_("Back"),
folder_preserving_link([("mode", "%s_attrs" % what)]),
"back")
return
- all_attrs = userdb.load_custom_attrs()
+ all_attrs = load_custom_attrs()
attrs = all_attrs.setdefault(what, [])
if not new:
@@ -14914,7 +14939,7 @@ def mode_custom_attrs(phase, what):
html.context_button(_("New Attribute"),
folder_preserving_link([("mode", "edit_%s_attr" % what)]),
"new")
return
- all_attrs = userdb.load_custom_attrs()
+ all_attrs = load_custom_attrs()
attrs = all_attrs.get(what, {})
if phase == "action":
@@ -14985,9 +15010,7 @@ def declare_custom_host_attrs():
undeclare_host_attribute(attr_name)
# now declare the custom attributes
- all_attrs = userdb.load_custom_attrs()
- attrs = all_attrs.setdefault('host', [])
- for attr in attrs:
+ for attr in config.wato_host_attrs:
vs = globals()[attr['type']](title = attr['title'], help =
attr['help'])
if attr['add_custom_macro']: