X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=unfold%2Fpage.py;h=3627e16e3ada419e9950cda2536bf525fc2b08b3;hb=4920497313e03cc832c29aaae13e45ca060ccc94;hp=157e0046239bdb47df03ce4857b2a9859680e786;hpb=45ea2e7ea4d6240f723cf743b9ad493ad70ceed2;p=myslice.git diff --git a/unfold/page.py b/unfold/page.py index 157e0046..3627e16e 100644 --- a/unfold/page.py +++ b/unfold/page.py @@ -6,7 +6,7 @@ import json from django.template.loader import render_to_string -from manifold.manifoldapi import ManifoldAPI +from manifold.metadata import MetaData from unfold.prelude import Prelude @@ -19,6 +19,9 @@ def to_prelude (method): return prelude_method(self.prelude,*args, **kwds) return actual +debug=False +debug=True + class Page: def __init__ (self, request): @@ -31,10 +34,6 @@ class Page: self._queue=[] # global prelude object self.prelude=Prelude(css_files='css/plugin.css') - # load metadata - self._metadata={} - # do not call this uncondionnally as we might not even have logged in - # self.expose_js_metadata() # record known plugins hashed on their domid def record_plugin (self, plugin): @@ -42,70 +41,107 @@ class Page: def get_plugin (self, domid): return self._plugins.get(domid,None) - - def reset_queue (self): - self._queries = set() - self._queue = [] - - # the js async methods (see manifold.asynchroneous_success) - # offer the option to deliver the result to a specific DOM elt - # otherwise (i.e. if domid not provided) - # it goes through the pubsub using query's uuid - def enqueue_query (self, query, domid=None): - self._queries = self._queries.union(set( [ query, ] )) - self._queue.append ( (query.query_uuid,domid,) ) - - # return the javascript that triggers all the queries - # xxx could fruitfully be renamed expose_queries_to_javascript or something - def exec_queue_asynchroneously (self): + + # not sure this is useful at all +# def reset_queue (self): +# self._queries = set() +# self._queue = [] + + # this method adds a query to the page + # the query will be exposed to js when calling expose_queries + # additionally if run_it is set to True, this query will be asynchroneously triggered on page load + # in this case (exec=True) the js async callback (see manifold.asynchroneous_success) + # offers the option to deliver the result to a specific DOM elt (in this case, set domid) + # otherwise (i.e. if domid not provided), it goes through the pubsub system (so all plugins can receive it) + # + # NOTE: + # analyzed_query is required because it contains query_uuid that the + # plugins initialized in the python part will listen to. When a result is + # received in javascript, subresults should be publish to the appropriate + # query_uuid. + # + def enqueue_query (self, query, run_it=True, domid=None, analyzed_query=None): + # _queries is the set of all known queries + # XXX complex XXX self._queries = self._queries.union(set( [ query, ] )) + self._queries.add((query, analyzed_query)) + # _queue is the list of queries that need to be triggered, with an optional domid + # we only do this if run_it is set + if run_it: self._queue.append ( (query.query_uuid,domid) ) + + # return the javascript code for exposing queries + # all queries are inserted in the global manifold object + # in addition, the ones enqueued with 'run_it=True' are triggered + def expose_queries (self): # compute variables to expose to the template env = {} # expose the json definition of all queries - env['queries_jsons'] = [ query.to_json() for query in self._queries ] - env['query_uuid_domids'] = [ {'query_uuid' : a, 'domid': '"%s"'%b if b else 'null'} for (a,b) in self._queue ] + env['queries_json'] = [ query.to_json(analyzed_query=aq) for (query, aq) in self._queries ] + def query_publish_dom_tuple (a,b): + result={'query_uuid':a} + if b: result['domid']=b + return result + env['query_publish_dom_tuples'] = [ query_publish_dom_tuple (a,b) for (a,b) in self._queue ] javascript = render_to_string ("page-queries.js",env) - self.reset_queue() self.add_js_chunks (javascript) +# self.reset_queue() + # unconditionnally expose MANIFOLD_URL, this is small and manifold.js uses that for various messages + self.expose_js_manifold_config() + + +# DEPRECATED # # needs to be called explicitly and only when metadata is actually required +# DEPRECATED # # in particular user needs to be logged +# DEPRECATED # def get_metadata (self): +# DEPRECATED # # look in session's cache - we don't want to retrieve this for every request +# DEPRECATED # session=self.request.session +# DEPRECATED # if 'manifold' not in session: +# DEPRECATED # print "Page.expose_js_metadata: no 'manifold' in session... - cannot retrieve metadata - skipping" +# DEPRECATED # return +# DEPRECATED # manifold=session['manifold'] +# DEPRECATED # # if cached, use it +# DEPRECATED # if 'metadata' in manifold and isinstance(manifold['metadata'],MetaData): +# DEPRECATED # if debug: print "Page.get_metadata: return cached value" +# DEPRECATED # return manifold['metadata'] +# DEPRECATED # # otherwise retrieve it +# DEPRECATED # manifold_api_session_auth = session['manifold']['auth'] +# DEPRECATED # print "get_metadata(), manifold_api_session_auth =", session['manifold']['auth'] +# DEPRECATED # metadata=MetaData (manifold_api_session_auth) +# DEPRECATED # metadata.fetch() +# DEPRECATED # # store it for next time +# DEPRECATED # manifold['metadata']=metadata +# DEPRECATED # if debug: print "Page.get_metadata: return new value" +# DEPRECATED # return metadata + + # needs to be called explicitly and only when metadata is actually required + # in particular user needs to be logged + def get_metadata (self): + # look in session's cache - we don't want to retrieve this for every request + session=self.request.session + + if 'manifold' not in session: + session['manifold'] = {} + manifold = session['manifold'] + + # if cached, use it + if 'metadata' in manifold and isinstance(manifold['metadata'],MetaData): + if debug: print "Page.get_metadata: return cached value" + return manifold['metadata'] + + metadata_auth = {'AuthMethod':'anonymous'} + + metadata=MetaData (metadata_auth) + metadata.fetch() + # store it for next time + manifold['metadata']=metadata + if debug: print "Page.get_metadata: return new value" + return metadata + + def expose_js_metadata (self): + # export in this js global... + self.add_js_chunks("var MANIFOLD_METADATA =" + self.get_metadata().to_json() + ";") - - - def expose_js_metadata(self): - request=self.request - # xxx this code should probably not be called unconditionnally at page creation time - # because we're not sure a user is logged in so we might have no session... - if 'manifold' not in request.session: - print "Page.expose_js_metadata: no 'manifold' in session... - skipping" - return - # use cached version if present - if 'metadata' in request.session.keys(): - self._metadata = request.session['metadata'] - else: - manifold_api_session_auth = request.session['manifold']['auth'] - manifold_api = ManifoldAPI(auth=manifold_api_session_auth) - - fields = ['table', 'column.column', - 'column.description','column.header', 'column.title', - 'column.unit', 'column.info_type', - 'column.resource_type', 'column.value_type', - 'column.allowed_values', 'column.platforms.platform', - 'column.platforms.platform_url'] - - results = manifold_api.Get('metadata:table', [], [], fields) - - for res in results: - method = res['table'] - self._metadata[method] = res - - request.session['metadata'] = self._metadata - -# javascript = "all_headers=" + json.dumps(self._metadata) + ";" -# self.add_js_chunks(javascript) - - def metadata_get_fields(self, method): - return self._metadata[method]['column'].sort() - def expose_js_manifold_config (self): - self.add_js_chunks(Config.manifold_js_export()+"\n") + config=Config() + self.add_js_chunks(config.manifold_js_export()) #################### requirements/prelude management # just forward to self.prelude - see decorator above