Created
May 25, 2011 02:03
-
-
Save mzupan/990177 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.template import Library, Node, TemplateSyntaxError, resolve_variable | |
from operations.models import Graph | |
register = Library() | |
class RenderGraphList(Node): | |
def __init__(self, parser, token, t="instance"): | |
self.tokens = token.contents.split() | |
if len(self.tokens) != 5: | |
raise TemplateSyntaxError, "%s tag needs to be used like (%s for instanceObject as returnedList)" % (self.tokens[0], self.tokens[0]) | |
if self.tokens[1] != "for": | |
raise TemplateSyntaxError, "Second argument needs to be 'for'" | |
if self.tokens[3] != "as": | |
raise TemplateSyntaxError, "Fourth argument needs to be 'as'" | |
self.obj = self.tokens[2] | |
self.returnList = self.tokens[4] | |
self.t = t | |
def render(self, context): | |
obj = resolve_variable(self.obj, context) | |
# | |
# grabbing all graphs for the host | |
# | |
list = [] | |
for g in obj.group.graphs.all(): | |
graph_obj = { | |
'img': '/graph/%i/%s/%i/hourly/small/' % (g.id, self.t, obj.id), | |
'link': '/operations/%s/%i/graph/list/%i/' % (self.t, obj.id, g.id) | |
} | |
list.append(graph_obj) | |
try: | |
context[self.returnList] = list | |
except: | |
pass | |
return "" | |
def get_instance_graphs_index(parser, token): | |
""" | |
""" | |
return RenderGraphList(parser, token) | |
def get_group_graphs_index(parser, token): | |
""" | |
""" | |
return RenderGraphList(parser, token, "group") | |
get_list = register.tag(get_instance_graphs_index) | |
get_list = register.tag(get_group_graphs_index) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment