expose each query ONCE to js, plugin gets initialized with query_uuid only
[myslice.git] / unfold / page.py
1 # the supervisor for Plugins
2 # keeps a handle on all present plugins for managing their queries in a consistent way
3 # it is expected to exist one such object for a given page
4
5 import json
6
7 from django.template.loader import render_to_string
8
9 from manifold.manifoldapi import ManifoldAPI
10
11 from unfold.prelude import Prelude
12
13 from myslice.config import Config
14
15 # decorator to deflect calls on this Page to its prelude
16 def to_prelude (method):
17     def actual (self, *args, **kwds):
18         prelude_method=Prelude.__dict__[method.__name__]
19         return prelude_method(self.prelude,*args, **kwds)
20     return actual
21
22 class Page:
23
24     def __init__ (self, request):
25         self.request=request
26         # all plugins mentioned in this page
27         self._plugins = {}
28         # the set of all queries 
29         self._queries=set()
30         # queue of queries with maybe a domid, see enqueue_query
31         self._queue=[]
32         # global prelude object
33         self.prelude=Prelude(css_files='css/plugin.css')
34         # load metadata
35         self._metadata={}
36         # do not call this uncondionnally as we might not even have logged in
37         # self.expose_js_metadata()
38
39     # record known plugins hashed on their domid
40     def record_plugin (self, plugin):
41         self._plugins[plugin.domid]=plugin
42
43     def get_plugin (self, domid):
44         return self._plugins.get(domid,None)
45     
46     def reset_queue (self):
47         self._queries = set()
48         self._queue = []
49
50     # the js async methods (see manifold_async_success)
51     # offer the option to deliver the result to a specific DOM elt
52     # otherwise (i.e. if domid not provided) 
53     # it goes through the pubsub using query's uuid
54     def enqueue_query (self, query, domid=None):
55         self._queries = self._queries.union(set( [ query, ] ))
56         self._queue.append ( (query.query_uuid,domid,) )
57
58     # return the javascript that triggers all the queries
59     # xxx could fruitfully be renamed expose_queries_to_javascript or something
60     def exec_queue_asynchroneously (self):
61         # compute variables to expose to the template
62         env = {}
63         # expose the json definition of all queries
64         env['queries_jsons'] = [ query.to_json() for query in self._queries ]
65         env['query_uuid_domids'] = [ {'query_uuid' : a, 'domid': '"%s"'%b if b else 'null'} for (a,b) in self._queue ]
66         javascript = render_to_string ("page-queries.js",env)
67         self.reset_queue()
68         self.add_js_chunks (javascript)
69
70
71
72     def expose_js_metadata(self):
73         request=self.request
74         # xxx this code should probably not be called unconditionnally at page creation time
75         # because we're not sure a user is logged in so we might have no session...
76         if 'manifold' not in request.session:
77             print "Page.expose_js_metadata: no 'manifold' in session... - skipping"
78             return
79         # use cached version if present
80         if 'metadata' in request.session.keys(): 
81             self._metadata = request.session['metadata']
82         else:
83             manifold_api_session_auth = request.session['manifold']['auth']
84             manifold_api = ManifoldAPI(auth=manifold_api_session_auth)
85         
86             fields = ['table', 'column.column',
87                   'column.description','column.header', 'column.title',
88                   'column.unit', 'column.info_type',
89                   'column.resource_type', 'column.value_type',
90                   'column.allowed_values', 'column.platforms.platform',
91                   'column.platforms.platform_url']
92
93             results = manifold_api.Get('metadata:table', [], [], fields)
94
95             for res in results:
96                  method = res['table']
97                  self._metadata[method] = res
98
99             request.session['metadata'] = self._metadata
100
101 #        javascript = "all_headers=" + json.dumps(self._metadata) + ";"
102 #        self.add_js_chunks(javascript)
103
104     def metadata_get_fields(self, method):
105         return self._metadata[method]['column'].sort()
106         
107     def expose_js_manifold_config (self):
108         self.add_js_chunks(Config.manifold_js_export()+"\n")
109
110     #################### requirements/prelude management
111     # just forward to self.prelude - see decorator above
112     @to_prelude
113     def add_js_files (self):pass
114     @to_prelude
115     def add_css_files (self):pass
116     @to_prelude
117     def add_js_chunks (self):pass
118     @to_prelude
119     def add_css_chunks (self):pass
120     @to_prelude
121     def prelude_env (self):pass