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
7 from django.template.loader import render_to_string
9 from manifold.manifoldapi import ManifoldAPI
11 from unfold.prelude import Prelude
13 from myslice.config import Config
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)
24 def __init__ (self, request):
26 # all plugins mentioned in this page
28 # the set of all queries
30 # queue of queries with maybe a domid, see enqueue_query
32 # global prelude object
33 self.prelude=Prelude(css_files='css/plugin.css')
36 # do not call this uncondionnally as we might not even have logged in
37 # self.expose_js_metadata()
39 # record known plugins hashed on their domid
40 def record_plugin (self, plugin):
41 self._plugins[plugin.domid]=plugin
43 def get_plugin (self, domid):
44 return self._plugins.get(domid,None)
46 def reset_queue (self):
50 # the js async methods (see manifold.asynchroneous_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,) )
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
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)
68 self.add_js_chunks (javascript)
72 def expose_js_metadata(self):
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"
79 # use cached version if present
80 if 'metadata' in request.session.keys():
81 self._metadata = request.session['metadata']
83 manifold_api_session_auth = request.session['manifold']['auth']
84 manifold_api = ManifoldAPI(auth=manifold_api_session_auth)
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']
93 results = manifold_api.Get('metadata:table', [], [], fields)
97 self._metadata[method] = res
99 request.session['metadata'] = self._metadata
101 javascript = "var MANIFOLD_METADATA =" + json.dumps(self._metadata) + ";"
102 self.add_js_chunks(javascript)
104 def metadata_get_fields(self, method):
105 return self._metadata[method]['column'].sort()
107 def expose_js_manifold_config (self):
108 self.add_js_chunks(Config.manifold_js_export())
110 #################### requirements/prelude management
111 # just forward to self.prelude - see decorator above
113 def add_js_files (self):pass
115 def add_css_files (self):pass
117 def add_js_chunks (self):pass
119 def add_css_chunks (self):pass
121 def prelude_env (self):pass