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
12 from unfold.sessioncache import SessionCache
14 from myslice.configengine import ConfigEngine
15 from myslice.settings import logger
17 # decorator to deflect calls on this Page to its prelude
18 def to_prelude (method):
19 def actual (self, *args, **kwds):
20 prelude_method=Prelude.__dict__[method.__name__]
21 return prelude_method(self.prelude,*args, **kwds)
29 def __init__ (self, request):
31 # all plugins mentioned in this page
33 # the set of all queries
35 # queue of queries with maybe a domid, see enqueue_query
37 # global prelude object
38 # global requirements should go in base.html
39 self.prelude=Prelude()
41 # record known plugins hashed on their domid
42 def record_plugin (self, plugin):
43 self._plugins[plugin.domid]=plugin
45 def get_plugin (self, domid):
46 return self._plugins.get(domid,None)
48 # not sure this is useful at all
49 # def reset_queue (self):
50 # self._queries = set()
53 # this method adds a query to the page
54 # the query will be exposed to js when calling __expose_queries, which is done by prelude_env()
55 # additionally if run_it is set to True, this query will be asynchroneously triggered on page load
56 # in this case (exec=True) the js async callback (see manifold.asynchroneous_success)
57 # offers the option to deliver the result to a specific DOM elt (in this case, set domid)
58 # otherwise (i.e. if domid not provided), it goes through the pubsub system (so all plugins can receive it)
61 # analyzed_query is required because it contains query_uuid that the
62 # plugins initialized in the python part will listen to. When a result is
63 # received in javascript, subresults should be publish to the appropriate
66 def enqueue_query (self, query, run_it=True, domid=None, analyzed_query=None):
67 # _queries is the set of all known queries
68 # XXX complex XXX self._queries = self._queries.union(set( [ query, ] ))
69 self._queries.add((query, analyzed_query))
70 # _queue is the list of queries that need to be triggered, with an optional domid
71 # we only do this if run_it is set
72 if run_it: self._queue.append ( (query.query_uuid,domid) )
74 def generate_records(self, query, generators, number=10):
75 self.add_js_files('js/record_generator.js');
76 js_chunk = '$(document).ready(function() { new RecordGenerator(%s,%s,%s).run(); });'%(query.to_json(),json.dumps(generators),number);
77 self.add_js_chunks(js_chunk)
79 # return the javascript code for exposing queries
80 # all queries are inserted in the global manifold object
81 # in addition, the ones enqueued with 'run_it=True' are triggered
82 def __expose_queries (self):
83 # compute variables to expose to the template
85 # expose the json definition of all queries
86 env['queries_json'] = [ query.to_json(analyzed_query=aq) for (query, aq) in self._queries ]
87 def query_publish_dom_tuple (a,b):
88 result={'query_uuid':a}
89 if b: result['domid']=b
91 env['query_exec_tuples'] = [ query_publish_dom_tuple (a,b) for (a,b) in self._queue ]
92 javascript = render_to_string ("page-queries.js",env)
93 self.add_js_chunks (javascript)
95 # unconditionnally expose MANIFOLD_URL, this is small and manifold.js uses that for various messages
96 self.expose_js_manifold_config()
99 # needs to be called explicitly and only when metadata is actually required
100 # in particular user needs to be logged
101 def get_metadata (self):
102 cached_metadata = SessionCache().get_metadata(self.request)
103 if cached_metadata and isinstance(cached_metadata, MetaData):
104 logger.debug("Page.get_metadata: return cached value")
105 return cached_metadata
107 metadata_auth = {'AuthMethod':'anonymous'}
109 metadata = MetaData (metadata_auth)
110 metadata.fetch(self.request)
111 SessionCache().store_metadata(self.request, metadata)
112 logger.debug("Page.get_metadata: return new value")
115 def expose_js_metadata (self):
116 # expose global MANIFOLD_METADATA as a js variable
117 # xxx this is fetched synchroneously..
118 self.add_js_init_chunks("var MANIFOLD_METADATA =" + self.get_metadata().to_json() + ";\n")
120 def expose_js_var(self, name, value):
121 # expose variable as a js value
122 self.add_js_init_chunks("var " + name + "=" + value + ";\n")
124 def expose_js_manifold_config (self):
125 self.add_js_init_chunks(ConfigEngine().manifold_js_export())
127 #################### requirements/prelude management
128 # just forward to self.prelude - see decorator above
130 def add_js_files (self):pass
132 def add_css_files (self):pass
134 def add_js_init_chunks (self):pass
136 def add_js_chunks (self):pass
138 def add_css_chunks (self):pass
140 # prelude_env also does expose_queries
141 def prelude_env (self):
142 self.__expose_queries()
143 from_prelude=self.prelude.prelude_env()