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.metadata import MetaData
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)
27 def __init__ (self, request):
29 # all plugins mentioned in this page
31 # the set of all queries
33 # queue of queries with maybe a domid, see enqueue_query
35 # global prelude object
36 self.prelude=Prelude(css_files='css/plugin.css')
38 # record known plugins hashed on their domid
39 def record_plugin (self, plugin):
40 self._plugins[plugin.domid]=plugin
42 def get_plugin (self, domid):
43 return self._plugins.get(domid,None)
45 def reset_queue (self):
49 # the js async methods (see manifold.asynchroneous_success)
50 # offer the option to deliver the result to a specific DOM elt
51 # otherwise (i.e. if domid not provided)
52 # it goes through the pubsub using query's uuid
53 def enqueue_query (self, query, domid=None):
54 self._queries = self._queries.union(set( [ query, ] ))
55 self._queue.append ( (query.query_uuid,domid,) )
57 # return the javascript that triggers all the queries
58 # xxx could fruitfully be renamed expose_queries_to_javascript or something
59 def exec_queue_asynchroneously (self):
60 # compute variables to expose to the template
62 # expose the json definition of all queries
63 env['queries_json'] = [ query.to_json() for query in self._queries ]
64 env['query_uuid_domids'] = [ {'query_uuid' : a, 'domid': '"%s"'%b if b else 'null'} for (a,b) in self._queue ]
65 javascript = render_to_string ("page-queries.js",env)
67 self.add_js_chunks (javascript)
70 # needs to be called explicitly and only when metadata is actually required
71 # in particular user needs to be logged
72 def get_metadata (self):
73 # look in session's cache - we don't want to retrieve this for every request
74 session=self.request.session
75 if 'manifold' not in session:
76 print "Page.expose_js_metadata: no 'manifold' in session... - cannot retrieve metadata - skipping"
78 manifold=session['manifold']
80 if 'metadata' in manifold and isinstance(manifold['metadata'],MetaData):
81 if debug: print "Page.get_metadata: return cached value"
82 return manifold['metadata']
83 # otherwise retrieve it
84 manifold_api_session_auth = session['manifold']['auth']
85 metadata=MetaData (manifold_api_session_auth)
87 # store it for next time
88 manifold['metadata']=metadata
89 if debug: print "Page.get_metadata: return new value"
92 def expose_js_metadata (self):
93 # export in this js global...
94 self.add_js_chunks("var MANIFOLD_METADATA =" + self.get_metadata().to_json() + ";")
96 def expose_js_manifold_config (self):
97 self.add_js_chunks(Config.manifold_js_export())
99 #################### requirements/prelude management
100 # just forward to self.prelude - see decorator above
102 def add_js_files (self):pass
104 def add_css_files (self):pass
106 def add_js_chunks (self):pass
108 def add_css_chunks (self):pass
110 def prelude_env (self):pass