Module: check_mk
Branch: master
Commit: fb90a21079f7f48e77562e2bae6646f5a2f842e1
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=fb90a21079f7f4…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Jul 27 12:46:26 2017 +0200
5045 FIX Custom icons and actions: Add macros for getting URL encoded values
The macros $HOSTNAME_URL_ENCODED$, $SERVICEDESC_URL_ENCODED$ and
$HOSTADDRESS_URL_ENCODED$
have been added to get the values of the existing macros in URL encoded format.
Change-Id: I3981f9f41e2b85003c073fe7235d3037aa83d8fc
---
.werks/5045 | 12 ++++++++++++
web/htdocs/htmllib.py | 18 ++++++++++++++++++
web/plugins/views/painters.py | 14 ++++++++++++--
web/plugins/wato/check_mk_configuration.py | 9 +++++++--
4 files changed, 49 insertions(+), 4 deletions(-)
diff --git a/.werks/5045 b/.werks/5045
new file mode 100644
index 0000000..f41e3b7
--- /dev/null
+++ b/.werks/5045
@@ -0,0 +1,12 @@
+Title: Custom icons and actions: Add macros for getting URL encoded values
+Level: 1
+Component: multisite
+Class: fix
+Compatible: compat
+Edition: cre
+State: unknown
+Version: 1.5.0i1
+Date: 1501150437
+
+The macros $HOSTNAME_URL_ENCODED$, $SERVICEDESC_URL_ENCODED$ and
$HOSTADDRESS_URL_ENCODED$
+have been added to get the values of the existing macros in URL encoded format.
diff --git a/web/htdocs/htmllib.py b/web/htdocs/htmllib.py
index 482610e..34483cb 100644
--- a/web/htdocs/htmllib.py
+++ b/web/htdocs/htmllib.py
@@ -174,6 +174,11 @@ class Encoder(object):
# Changing from the self coded urlencode to urllib.quote
# is saving more then 90% of the total HTML generating time
# on more complex pages!
+ #
+ # TODO: Cleanup self.urlencode_vars, self.urlencode and self.urlencode_plus.
+ # urlencode_vars() should directly use urlencode or urlencode_vars and
+ # not fallback to self.urlencode on it's own. self.urlencode() should
+ # work for a single value exacly as urlencode_vars() does for multiple
def urlencode_vars(self, vars):
output = []
for varname, value in sorted(vars):
@@ -206,6 +211,16 @@ class Encoder(object):
return ret
+ # Like urllib.quote() but also replaces spaces and /
+ def urlencode_plus(self, value):
+ if type(value) == unicode:
+ value = value.encode("utf-8")
+ elif value == None:
+ return ""
+
+ return urllib.quote_plus(value)
+
+
# Escape a variable name so that it only uses allowed charachters for URL variables
def varencode(self, varname):
if varname == None:
@@ -1454,6 +1469,9 @@ class html(HTMLGenerator, RequestHandler):
def urlencode(self, value):
return self.encoder.urlencode(value)
+ def urlencode_plus(self, value):
+ return self.encoder.urlencode_plus(value)
+
# Escape a variable name so that it only uses allowed charachters for URL variables
def varencode(self, varname):
return self.encoder.varencode(varname)
diff --git a/web/plugins/views/painters.py b/web/plugins/views/painters.py
index 1801e42..3bbdd64 100644
--- a/web/plugins/views/painters.py
+++ b/web/plugins/views/painters.py
@@ -387,9 +387,19 @@ def get_icons(what, row, toplevel):
def replace_action_url_macros(url, what, row):
- url = url.replace('$HOSTNAME$',
row['host_name']).replace('$HOSTADDRESS$', row['host_address'])
+ macros = {
+ "HOSTNAME" : row['host_name'],
+ "HOSTADDRESS" : row['host_address'],
+ }
if what == 'service':
- url = url.replace('$SERVICEDESC$', row['service_description'])
+ macros.update({
+ "SERVICEDESC": row['service_description'],
+ })
+
+ for key, val in macros.items():
+ url = url.replace("$%s$" % key, val)
+ url = url.replace("$%s_URL_ENCODED$" % key, html.urlencode_plus(val))
+
return url
diff --git a/web/plugins/wato/check_mk_configuration.py
b/web/plugins/wato/check_mk_configuration.py
index 5341bc7..9838a09 100644
--- a/web/plugins/wato/check_mk_configuration.py
+++ b/web/plugins/wato/check_mk_configuration.py
@@ -643,10 +643,15 @@ register_configvar(group,
help = _('This URL is opened when
clicking on the action / icon. You '
'can use some macros within the
URL which are dynamically '
'replaced for each object. These
are:<br>'
- '<ul><li>$HOSTNAME$:
Contains the name of the host</li>'
+ '<ul>'
+ '<li>$HOSTNAME$: Contains
the name of the host</li>'
+
'<li>$HOSTNAME_URL_ENCODED$: Same as above but URL encoded</li>'
'<li>$SERVICEDESC$:
Contains the service description '
'(in case this is a
service)</li>'
- '<li>$HOSTADDRESS$:
Contains the network address of the host</li></ul>'),
+
'<li>$SERVICEDESC_URL_ENCODED$: Same as above but URL encoded</li>'
+ '<li>$HOSTADDRESS$:
Contains the network address of the host</li>'
+
'<li>$HOSTADDRESS_URL_ENCODED$: Same as above but URL encoded</li>'
+ '</ul>'),
size = 80,
),
DropdownChoice(