Module: check_mk
Branch: master
Commit: cdde79713cf680061c5976185bba4379aee6b89b
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=cdde79713cf680…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Wed Feb 1 13:24:33 2012 +0100
new ValueSpec AbsoluteDate()
---
web/htdocs/check_mk.css | 15 ++++++++++++
web/htdocs/valuespec.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 74 insertions(+), 0 deletions(-)
diff --git a/web/htdocs/check_mk.css b/web/htdocs/check_mk.css
index 078cef3..52f5bef 100644
--- a/web/htdocs/check_mk.css
+++ b/web/htdocs/check_mk.css
@@ -292,3 +292,18 @@ td.content table.valuespec_tuple td.left {
vertical-align: top;
padding-bottom: 10px;
}
+
+table.vs_date {
+ border-spacing: 0px;
+}
+
+table.vs_date td {
+ padding-top: 0px;
+ padding-right: 3px;
+}
+table.vs_date th {
+ text-align: center;
+ font-weight: normal;
+ font-size: 7pt;
+ padding: 0px;
+}
diff --git a/web/htdocs/valuespec.py b/web/htdocs/valuespec.py
index fbb5577..f5e3ead 100644
--- a/web/htdocs/valuespec.py
+++ b/web/htdocs/valuespec.py
@@ -821,6 +821,65 @@ class RelativeDate(OptionalDropdownChoice):
def validate_value(self, value, varprefix):
pass
+# A ValueSpec for editing a date. The date is
+# represented as a UNIX timestamp x where x % seconds_per_day
+# is zero (or will be ignored if non-zero).
+class AbsoluteDate(ValueSpec):
+ def __init__(self, **kwargs):
+ ValueSpec.__init__(self, **kwargs)
+ self._default_value = today()
+
+ def canonical_value(self):
+ return self._default_value
+
+ def split_date(self, value):
+ lt = time.localtime(value)
+ return lt.tm_year, lt.tm_mon, lt.tm_mday
+
+ def render_input(self, varprefix, value):
+ html.write('<table class=vs_date>')
+
html.write('<tr><th>%s</th><th>%s</th><th>%s</th></tr>'
% (
+ _("Year"), _("Month"), _("Day")))
+ html.write('<tr><td>')
+ year, month, day = self.split_date(value)
+ html.number_input(varprefix + "_year", year, size=4)
+ html.write('</td><td>')
+ html.number_input(varprefix + "_month", month, size=2)
+ html.write('</td><td>')
+ html.number_input(varprefix + "_day", day, size=2)
+ html.write('</td></tr></table>')
+
+ def set_focus(self, varprefix):
+ html.set_focus(varprefix + "_year")
+
+ def value_to_text(self, value):
+ return time.strftime("%F", time.localtime(value))
+
+ def from_html_vars(self, varprefix):
+ parts = []
+ for what, mmin, mmax in [
+ ("year", 1970, 2038),
+ ("month", 1, 12),
+ ("day", 1, 31)]:
+ try:
+ varname = varprefix + "_" + what
+ part = int(html.var(varname))
+ except:
+ raise MKUserError(varname, _("Please enter a correct number"))
+ if part < mmin or part > mmax:
+ raise MKUserError(varname, _("The value for %s must be between %d
and %d" % (mmin, mmax)))
+ parts.append(part)
+ parts += [0] * 6
+ return time.mktime(tuple(parts))
+
+ def validate_datatype(self, value, varprefix):
+ if type(value) not in [ int, float ]:
+ raise MKUserError(varprefix, _("The type of the timestamp must be int or
float, but is %s") %
+ type(value))
+
+ def validate_value(self, value, varprefix):
+ if value < 0 or int(value) > (2**31-1):
+ return MKUserError(varprefix, _("%s is not a valid UNIX timestamp")
% value)