Module: check_mk
Branch: master
Commit: 0bc7a8f1a5359f8ca093ce21b9f8dfb6103ad6b5
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=0bc7a8f1a5359f…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Aug 20 14:05:00 2014 +0200
Dashboards: URL dashlets can now reference to a python function which generates the url
dynamically
---
web/htdocs/dashboard.py | 26 +++++++++++++++++++++++++-
web/plugins/dashboard/dashlets.py | 5 +++++
2 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/web/htdocs/dashboard.py b/web/htdocs/dashboard.py
index d742fb3..6051110 100644
--- a/web/htdocs/dashboard.py
+++ b/web/htdocs/dashboard.py
@@ -116,6 +116,11 @@ def transform_builtin_dashboards():
dashlet['type'] = dashlet['url'][8:-3]
del dashlet['url']
+ elif dashlet.get('urlfunc') and type(dashlet['urlfunc']) !=
str:
+ raise MKGeneralException(_('Unable to transform dashlet %d of
dashboard %s: '
+ 'the dashlet is using "urlfunc"
which can not be '
+ 'converted automatically.') % (nr,
name))
+
elif dashlet.get('url', '') != '' or
dashlet.get('urlfunc') or dashlet.get('iframe'):
# Normal URL based dashlet
dashlet['type'] = 'url'
@@ -292,7 +297,26 @@ def render_dashboard(name):
# dashlets using the 'urlfunc' method will dynamically compute
# an url (using HTML context variables at their wish).
if "urlfunc" in dashlet:
- dashlet["url"] = dashlet["urlfunc"]()
+ urlfunc = dashlet['urlfunc']
+ # We need to support function pointers to be compatible to old dashboard
plugin
+ # based definitions. The new dashboards use strings to reference functions
within
+ # the global context or functions of a module. An example would be:
+ #
+ # urlfunc: "my_custom_url_rendering_function"
+ #
+ # or within a module:
+ #
+ # urlfunc: "my_module.render_my_url"
+ if type(urlfunc) == type(lambda x: x):
+ dashlet["url"] = urlfunc()
+ else:
+ if '.' in urlfunc:
+ module_name, func_name = urlfunc.split('.', 1)
+ module = __import__(module_name)
+ fp = module.__dict__[func_name]
+ else:
+ fp = globals()[urlfunc]
+ dashlet["url"] = fp()
dashlet_type = dashlet_types[dashlet['type']]
diff --git a/web/plugins/dashboard/dashlets.py b/web/plugins/dashboard/dashlets.py
index a818c68..8e19a94 100644
--- a/web/plugins/dashboard/dashlets.py
+++ b/web/plugins/dashboard/dashlets.py
@@ -581,5 +581,10 @@ dashlet_types["url"] = {
title = _('URL'),
size = 50,
)),
+ ("urlfunc", TextAscii(
+ title = _('Dynamic URL rendering function'),
+ size = 50,
+ )),
],
+ "opt_params": ['url', 'urlfunc'],
}