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

Source Code for Module GChartWrapper.charts.templatetags.charts

  1  """
 
  2  Contains the 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          {% chart Pie3D 1 2 3 4 5 as pie %}
 
 15              {% label A B C D %}
 
 16              {% color green %}
 
 17          {% endchart %}
 
 18      
 
 19          {% pie %} # The chart obj itself
 
 20          {% pie.image %} # The PIL instance
 
 21          {% pie.checksum %} # An SHA1 checksum
 
 22  
 
 23  The FancyNode powers the tag for Note,Pin,Text and Bubble charts
 
 24  The <type> argument is one of the chart types in lower case
 
 25  
 
 26      {% <type> ... [as url|img|varname]%}
 
 27      
 
 28      Example:
 
 29          {% bubble icon_text_big snack bb $2.99 ffbb00 black as img %}
 
 30      """ 
 31  
 
 32  from django.template import Library,Node 
 33  from django.template import resolve_variable 
 34  import GChartWrapper 
 35  
 
 36  register = Library() 
 37  
 
38 -class GenericNode(Node):
39 - def __init__(self, args):
40 self.args = map(unicode,args)
41 - def render(self,context):
42 for n,arg in enumerate(self.args): 43 if arg in context: 44 self.args[n] = resolve_variable(arg, context) 45 elif arg[0] == '"' and arg[-1] == '"': 46 self.args[n] = arg[1:-1] 47 elif arg[0] == "'" and arg[-1] == "'": 48 self.args[n] = arg[1:-1] 49 return self.post_render(context)
50 - def post_render(self, context): return self.args
51 52
53 -def attribute(parser, token):
54 return GenericNode(token.split_contents())
55 for tag in GChartWrapper.constants.TTAGSATTRS: 56 register.tag(tag, attribute) 57 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 register.tag('chart', make_chart) 124 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('\\r\\n','\r\n')\ 136 .replace('\\n','\n').replace('\\r','\r') 137 G = self.cls(*self.args) 138 if mode: 139 if mode == 'img': 140 return G.img() 141 if mode == 'url': 142 return str(G) 143 else: 144 context[mode] = G 145 else: 146 return G.img()
147
148 -class NoteNode(FancyNode):
149 cls = GChartWrapper.Note
150 -def note(parser, token):
151 return NoteNode(token.split_contents())
152 register.tag(note) 153
154 -class PinNode(FancyNode):
155 cls = GChartWrapper.Pin
156 -def pin(parser, token):
157 return PinNode(token.split_contents())
158 register.tag(pin) 159
160 -class TextNode(FancyNode):
161 cls = GChartWrapper.Text
162 -def text(parser, token):
163 return TextNode(token.split_contents())
164 register.tag(text) 165
166 -class BubbleNode(FancyNode):
167 cls = GChartWrapper.Bubble
168 -def bubble(parser, token):
169 return BubbleNode(token.split_contents())
170 register.tag(bubble) 171