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+ if isinstance (rend [1 ], list ) or isinstance (rend [1 ], tuple ):
108+ getattr (chart , rend [0 ])(* rend [1 ])
109+ else :
110+ getattr (chart , rend [0 ])(* rend [1 :])
111+ if self .mode :
112+ if self .mode == 'img' :
113+ return chart .img (** imgkwargs )
114+ elif self .mode == 'url' :
115+ return str (chart )
116+ else :
117+ context [self .mode ] = chart
118+ else :
119+ return chart .img (** imgkwargs )
120+
121+ def make_chart (parser , token ):
122+ nodelist = parser .parse (('endchart' ,))
123+ parser .delete_first_token ()
124+ tokens = token .contents .split ()
125+ return ChartNode (tokens ,nodelist )
126+
127+ register .tag ('chart' , make_chart )
128+
129+ class FancyNode (GenericNode ):
130+ cls = None
131+ def post_render (self ,context ):
132+ mode = None
133+ self .args = self .args [1 :]
134+ if self .args [- 2 ] == 'as' :
135+ mode = self .args [- 1 ]
136+ self .args = self .args [:- 2 ]
137+ for n ,arg in enumerate (self .args ):
138+ self .args [n ] = arg .replace ('\\ n' ,'\n ' ).replace ('\\ r' ,'\r ' )
139+ G = self .cls (* self .args )
140+ if mode :
141+ if mode == 'img' :
142+ return G .img ()
143+ if mode == 'url' :
144+ return str (G )
145+ else :
146+ context [mode ] = G
147+ else :
148+ return G .img ()
149+
150+ class NoteNode (FancyNode ):
151+ cls = GChartWrapper .Note
152+ def note (parser , token ):
153+ return NoteNode (token .split_contents ())
154+ register .tag (note )
155+
156+ class PinNode (FancyNode ):
157+ cls = GChartWrapper .Pin
158+ def pin (parser , token ):
159+ return PinNode (token .split_contents ())
160+ register .tag (pin )
161+
162+ class TextNode (FancyNode ):
163+ cls = GChartWrapper .Text
164+ def text (parser , token ):
165+ return TextNode (token .split_contents ())
166+ register .tag (text )
167+
168+ class BubbleNode (FancyNode ):
169+ cls = GChartWrapper .Bubble
170+ def bubble (parser , token ):
171+ return BubbleNode (token .split_contents ())
172+ register .tag (bubble )
0 commit comments