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 # not sure this is useful at all
46 # def reset_queue (self):
47 # self._queries = set()
50 # this method adds a query to the page
51 # the query will be exposed to js when calling expose_queries
52 # additionally if run_it is set to True, this query will be asynchroneously triggered on page load
53 # in this case (exec=True) the js async callback (see manifold.asynchroneous_success)
54 # offers the option to deliver the result to a specific DOM elt (in this case, set domid)
55 # otherwise (i.e. if domid not provided), it goes through the pubsub system (so all plugins can receive it)
56 def enqueue_query (self, query, run_it=True, domid=None):
57 # _queries is the set of all known queries
58 self._queries = self._queries.union(set( [ query, ] ))
59 # _queue is the list of queries that need to be triggered, with an optional domid
60 # we only do this if run_it is set
61 if run_it: self._queue.append ( (query.query_uuid,domid) )
63 # return the javascript code for exposing queries
64 # all queries are inserted in the global manifold object
65 # in addition, the ones enqueued with 'run_it=True' are triggered
66 def expose_queries (self):
67 # compute variables to expose to the template
69 # expose the json definition of all queries
70 env['queries_json'] = [ query.to_json() for query in self._queries ]
71 def query_publish_dom_tuple (a,b):
72 result={'query_uuid':a}
73 if b: result['domid']=b
75 env['query_publish_dom_tuples'] = [ query_publish_dom_tuple (a,b) for (a,b) in self._queue ]
76 javascript = render_to_string ("page-queries.js",env)
77 self.add_js_chunks (javascript)
79 # unconditionnally expose MANIFOLD_URL, this is small and manifold.js uses that for various messages
80 self.expose_js_manifold_config()
83 # needs to be called explicitly and only when metadata is actually required
84 # in particular user needs to be logged
85 def get_metadata (self):
86 # look in session's cache - we don't want to retrieve this for every request
87 session=self.request.session
88 if 'manifold' not in session:
89 print "Page.expose_js_metadata: no 'manifold' in session... - cannot retrieve metadata - skipping"
91 manifold=session['manifold']
93 if 'metadata' in manifold and isinstance(manifold['metadata'],MetaData):
94 if debug: print "Page.get_metadata: return cached value"
95 return manifold['metadata']
96 # otherwise retrieve it
97 manifold_api_session_auth = session['manifold']['auth']
98 metadata=MetaData (manifold_api_session_auth)
100 # store it for next time
101 manifold['metadata']=metadata
102 if debug: print "Page.get_metadata: return new value"
105 def expose_js_metadata (self):
106 # export in this js global...
107 self.add_js_chunks("var MANIFOLD_METADATA =" + self.get_metadata().to_json() + ";")
109 def expose_js_manifold_config (self):
110 self.add_js_chunks(Config.manifold_js_export())
112 #################### requirements/prelude management
113 # just forward to self.prelude - see decorator above
115 def add_js_files (self):pass
117 def add_css_files (self):pass
119 def add_js_chunks (self):pass
121 def add_css_chunks (self):pass
123 def prelude_env (self):pass