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 manifoldapi.metadata import MetaData
11 from unfold.prelude import Prelude
13 from myslice.configengine import ConfigEngine
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 # global requirements should go in base.html
37 self.prelude=Prelude()
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 # not sure this is useful at all
47 # def reset_queue (self):
48 # self._queries = set()
51 # this method adds a query to the page
52 # the query will be exposed to js when calling __expose_queries, which is done by prelude_env()
53 # additionally if run_it is set to True, this query will be asynchroneously triggered on page load
54 # in this case (exec=True) the js async callback (see manifold.asynchroneous_success)
55 # offers the option to deliver the result to a specific DOM elt (in this case, set domid)
56 # otherwise (i.e. if domid not provided), it goes through the pubsub system (so all plugins can receive it)
59 # analyzed_query is required because it contains query_uuid that the
60 # plugins initialized in the python part will listen to. When a result is
61 # received in javascript, subresults should be publish to the appropriate
64 def enqueue_query (self, query, run_it=True, domid=None, analyzed_query=None):
65 # _queries is the set of all known queries
66 # XXX complex XXX self._queries = self._queries.union(set( [ query, ] ))
67 self._queries.add((query, analyzed_query))
68 # _queue is the list of queries that need to be triggered, with an optional domid
69 # we only do this if run_it is set
70 if run_it: self._queue.append ( (query.query_uuid,domid) )
72 def generate_records(self, query, generators, number=10):
73 self.add_js_files('js/record_generator.js');
74 js_chunk = '$(document).ready(function() { new RecordGenerator(%s,%s,%s).run(); });'%(query.to_json(),json.dumps(generators),number);
75 self.add_js_chunks(js_chunk)
77 # return the javascript code for exposing queries
78 # all queries are inserted in the global manifold object
79 # in addition, the ones enqueued with 'run_it=True' are triggered
80 def __expose_queries (self):
81 # compute variables to expose to the template
83 # expose the json definition of all queries
84 env['queries_json'] = [ query.to_json(analyzed_query=aq) for (query, aq) in self._queries ]
85 def query_publish_dom_tuple (a,b):
86 result={'query_uuid':a}
87 if b: result['domid']=b
89 env['query_exec_tuples'] = [ query_publish_dom_tuple (a,b) for (a,b) in self._queue ]
90 javascript = render_to_string ("page-queries.js",env)
91 self.add_js_chunks (javascript)
93 # unconditionnally expose MANIFOLD_URL, this is small and manifold.js uses that for various messages
94 self.expose_js_manifold_config()
97 # needs to be called explicitly and only when metadata is actually required
98 # in particular user needs to be logged
99 def get_metadata (self):
100 # look in session's cache - we don't want to retrieve this for every request
101 session=self.request.session
103 if 'manifold' not in session:
104 session['manifold'] = {}
105 manifold = session['manifold']
108 if 'metadata' in manifold and isinstance(manifold['metadata'],MetaData):
109 if debug: print "Page.get_metadata: return cached value"
110 return manifold['metadata']
112 metadata_auth = {'AuthMethod':'anonymous'}
114 metadata=MetaData (metadata_auth)
115 metadata.fetch(self.request)
116 # store it for next time
117 manifold['metadata']=metadata
118 if debug: print "Page.get_metadata: return new value"
121 def expose_js_metadata (self):
122 # expose global MANIFOLD_METADATA as a js variable
123 # xxx this is fetched synchroneously..
124 self.add_js_init_chunks("var MANIFOLD_METADATA =" + self.get_metadata().to_json() + ";\n")
126 def expose_js_manifold_config (self):
127 self.add_js_init_chunks(ConfigEngine().manifold_js_export())
129 #################### requirements/prelude management
130 # just forward to self.prelude - see decorator above
132 def add_js_files (self):pass
134 def add_css_files (self):pass
136 def add_js_init_chunks (self):pass
138 def add_js_chunks (self):pass
140 def add_css_chunks (self):pass
142 # prelude_env also does expose_queries
143 def prelude_env (self):
144 self.__expose_queries()
145 from_prelude=self.prelude.prelude_env()