Module: check_mk
Branch: master
Commit: 9c484cd2cab8f66a721459b9dcf8af93efec51ee
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=9c484cd2cab8f6…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Mon Jan 16 15:34:02 2012 +0100
New valuespec for http urls
---
web/htdocs/valuespec.py | 40 +++++++++++++++++++++++++++++++++++++++-
1 files changed, 39 insertions(+), 1 deletions(-)
diff --git a/web/htdocs/valuespec.py b/web/htdocs/valuespec.py
index 02bd5b5..e498133 100644
--- a/web/htdocs/valuespec.py
+++ b/web/htdocs/valuespec.py
@@ -24,7 +24,7 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
-import math, os, time, re
+import math, os, time, re, urlparse
from lib import *
# Abstract base class of all value declaration classes.
@@ -300,6 +300,44 @@ class EmailAddress(TextAscii):
def value_to_text(self, value):
return '<a href="mailto:%s">%s</a>' % (value,
value)
+# Valuespec for a HTTP Url (not HTTPS), that
+# automatically adds http:// to the value
+class HTTPUrl(TextAscii):
+ def __init__(self, **kwargs):
+ TextAscii.__init__(self, **kwargs)
+ self._target= kwargs.get("target")
+
+ def validate_value(self, value, varprefix):
+ TextAscii.validate_value(self, value, varprefix)
+ if value:
+ if not value.startswith("http://"):
+ raise MKUserError(varprefix, _("The URL must begin with
http://"))
+
+ def from_html_vars(self, varprefix):
+ value = TextAscii.from_html_vars(self, varprefix)
+ if value:
+ if not "://" in value:
+ value = "http://" + value
+ return value
+
+ def value_to_text(self, url):
+ if not url.startswith("http://"):
+ url = "http://" + url
+ try:
+ parts = urlparse.urlparse(url)
+ if parts.path in [ '', '/' ]:
+ text = parts.netloc
+ else:
+ text = url[7:]
+ except:
+ text = url[7:]
+
+ # Remove trailing / if the url does not contain
+ # any path component
+ return '<a %shref="%s">%s</a>' % (
+ (self._target and 'target="%s" ' % self._target or
""),
+ url, text)
+
class TextUnicode(TextAscii):
def __init__(self, **kwargs):