summaryrefslogtreecommitdiff
path: root/sublab_project/sublab_monitor/templatetags/host_status.py
diff options
context:
space:
mode:
Diffstat (limited to 'sublab_project/sublab_monitor/templatetags/host_status.py')
-rw-r--r--sublab_project/sublab_monitor/templatetags/host_status.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/sublab_project/sublab_monitor/templatetags/host_status.py b/sublab_project/sublab_monitor/templatetags/host_status.py
new file mode 100644
index 0000000..e119f60
--- /dev/null
+++ b/sublab_project/sublab_monitor/templatetags/host_status.py
@@ -0,0 +1,46 @@
+from django import template
+import sublab_monitor
+
+register = template.Library()
+
+class HostStatusNode(template.Node):
+ def __init__(self, host):
+ self.host = host
+
+ def host_status(self, context):
+ if self not in context.render_context:
+ try:
+ context.render_context[self] = sublab_monitor.Storage('network_status')
+ except sublab_monitor.StorageError:
+ return None
+
+ storage = context.render_context[self]
+ try:
+ return storage.get(self.host)
+ except sublab_monitor.StorageError:
+ return None
+
+ def host_context(self, context):
+ context_data = {
+ 'hostname': self.host,
+ 'status': self.host_status(context)
+ }
+
+ return template.context.Context(context_data, autoescape=context.autoescape)
+
+ def render(self, context):
+ t = template.loader.get_template('sublab_monitor/host_status.html')
+ c = self.host_context(context)
+ return t.render(c)
+
+@register.tag
+def host_status(parser, token):
+ try:
+ tag_name, host = token.split_contents()
+ except ValueError:
+ raise template.TemplateSyntaxError(
+ "%r tag requires a single argument" % token.contents.split()[0])
+ if not host[0] == host[-1] and host[0] in ('"', "'"):
+ raise template.TemplateSyntaxError(
+ "%r tag's argument should be in quotes" % token.contents.split()[0])
+ return HostStatusNode(host[1:-1])