Package GChartWrapper :: Package charts :: Package templatetags :: Module charts
[hide private]
[frames] | no frames]

Source Code for Module GChartWrapper.charts.templatetags.charts

  1  """
 
  2  Django templatetags for chart and note types
 
  3  Now takes an as argument
 
  4  If the as argument is 'img', it will return a XHTML <img/>
 
  5  If the as argument is 'url', it will simply return the url of the chart
 
  6  If the as argument is anything else, the chart will be loaded into the context
 
  7  and named what the as argument is
 
  8  
 
  9  {% chart ... [as url|img|varname] %}
 
 10  ...
 
 11  {% endchart %}
 
 12  
 
 13  Example:
 
 14  
 
 15      {% chart Pie3D 1 2 3 4 5 as pie %}
 
 16          {% label A B C D %}
 
 17          {% color green %}
 
 18      {% endchart %}
 
 19  
 
 20      {% pie %} # The chart obj itself
 
 21      {% pie.image %} # The PIL instance
 
 22      {% pie.checksum %} # An SHA1 checksum
 
 23  
 
 24  The FancyNode powers the tag for Note,Pin,Text and Bubble charts
 
 25  The <type> argument is one of the chart types in lower case
 
 26  
 
 27      {% <type> ... [as url|img|varname]%}
 
 28      
 
 29      Example:
 
 30          {% bubble icon_text_big snack bb $2.99 ffbb00 black as img %}
 
 31      """ 
 32  
 
 33  from django.template import Library,Node 
 34  from django.template import resolve_variable 
 35  import GChartWrapper 
 36  
 
 37  register = Library() 
 38  
 
39 -class GenericNode(Node):
40 - def __init__(self, args):
41 self.args = map(unicode,args)
42 - def render(self,context):
43 for n,arg in enumerate(self.args): 44 if arg in context: 45 self.args[n] = resolve_variable(arg, context) 46 elif arg[0] == '"' and arg[-1] == '"': 47 self.args[n] = arg[1:-1] 48 elif arg[0] == "'" and arg[-1] == "'": 49 self.args[n] = arg[1:-1] 50 return self.post_render(context)
51 - def post_render(self, context): return self.args
52
53 -def attribute(parser, token):
54 return GenericNode(token.split_contents())
55 56 for tag in GChartWrapper.constants.TTAGSATTRS: 57 register.tag(tag, attribute) 58
59 -class ChartNode(Node):
60 - def __init__(self, tokens, nodelist):
61 self.type = None 62 self.tokens = [] 63 self.mode = None 64 if tokens and len(tokens)>1: 65 self.type = tokens[1] 66 if tokens[-2] == 'as': 67 self.mode = tokens[-1] 68 self.tokens = tokens[2:-2] 69 else: 70 self.tokens = tokens[2:] 71 self.nodelist = nodelist
72 - def render(self, context):
73 args = [] 74 kwargs = {} 75 for t in self.tokens: 76 try: 77 args.append(resolve_variable(t,context)) 78 except: 79 try: 80 args.append(float(t)) 81 except: 82 arg = str(t) 83 if arg.find('=')>-1: 84 k,v = arg.split('=')[:2] 85 kwargs[k] = v 86 else: 87 args.append(arg) 88 if len(args) == 1 and type(args[0]) in map(type,[[],()]): 89 args = args[0] 90 if self.type in dir(GChartWrapper): 91 chart = getattr(GChartWrapper,self.type)(args,**kwargs) 92 elif self.type in GChartWrapper.constants.TYPES: 93 chart = GChartWrapper.GChart(self.type,args,**kwargs) 94 else: 95 raise TypeError('Chart type %s not recognized'%self.type) 96 imgkwargs = {} 97 for n in self.nodelist: 98 rend = n.render(context) 99 if type(rend) == type([]): 100 if rend[0] == 'img': 101 for k,v in map(lambda x: x.split('='), rend[1:]): 102 imgkwargs[k] = v 103 continue 104 if rend[0] == 'axes': 105 getattr(getattr(chart, rend[0]), rend[1])(*rend[2:]) 106 else: 107 getattr(chart, rend[0])(*rend[1:]) 108 if self.mode: 109 if self.mode == 'img': 110 return chart.img(**imgkwargs) 111 elif self.mode == 'url': 112 return str(chart) 113 else: 114 context[self.mode] = chart 115 else: 116 return chart.img(**imgkwargs)
117
118 -def make_chart(parser, token):
119 nodelist = parser.parse(('endchart',)) 120 parser.delete_first_token() 121 tokens = token.contents.split() 122 return ChartNode(tokens,nodelist)
123 124 register.tag('chart', make_chart) 125
126 -class FancyNode(GenericNode):
127 cls = None
128 - def post_render(self,context):
129 mode = None 130 self.args = self.args[1:] 131 if self.args[-2] == 'as': 132 mode = self.args[-1] 133 self.args = self.args[:-2] 134 for n,arg in enumerate(self.args): 135 self.args[n] = arg.replace('\\n','\n').replace('\\r','\r') 136 G = self.cls(*self.args) 137 if mode: 138 if mode == 'img': 139 return G.img() 140 if mode == 'url': 141 return str(G) 142 else: 143 context[mode] = G 144 else: 145 return G.img()
146
147 -class NoteNode(FancyNode):
148 cls = GChartWrapper.Note
149 -def note(parser, token):
150 return NoteNode(token.split_contents())
151 register.tag(note) 152
153 -class PinNode(FancyNode):
154 cls = GChartWrapper.Pin
155 -def pin(parser, token):
156 return PinNode(token.split_contents())
157 register.tag(pin) 158
159 -class TextNode(FancyNode):
160 cls = GChartWrapper.Text
161 -def text(parser, token):
162 return TextNode(token.split_contents())
163 register.tag(text) 164
165 -class BubbleNode(FancyNode):
166 cls = GChartWrapper.Bubble
167 -def bubble(parser, token):
168 return BubbleNode(token.split_contents())
169 register.tag(bubble) 170