handle metadata in a separate class and related js in manifold/
[myslice.git] / unfold / page.py
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
4
5 import json
6
7 from django.template.loader import render_to_string
8
9 from manifold.metadata import MetaData
10
11 from unfold.prelude import Prelude
12
13 from myslice.config import Config
14
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)
20     return actual
21
22 debug=False
23 debug=True
24
25 class Page:
26
27     def __init__ (self, request):
28         self.request=request
29         # all plugins mentioned in this page
30         self._plugins = {}
31         # the set of all queries 
32         self._queries=set()
33         # queue of queries with maybe a domid, see enqueue_query
34         self._queue=[]
35         # global prelude object
36         self.prelude=Prelude(css_files='css/plugin.css')
37
38     # record known plugins hashed on their domid
39     def record_plugin (self, plugin):
40         self._plugins[plugin.domid]=plugin
41
42     def get_plugin (self, domid):
43         return self._plugins.get(domid,None)
44     
45     def reset_queue (self):
46         self._queries = set()
47         self._queue = []
48
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,) )
56
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
61         env = {}
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)
66         self.reset_queue()
67         self.add_js_chunks (javascript)
68
69
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"
77             return
78         manifold=session['manifold']
79         # if cached, use it
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)
86         metadata.fetch()
87             # store it for next time
88         manifold['metadata']=metadata
89         if debug: print "Page.get_metadata: return new value"
90         return metadata
91             
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() + ";")
95
96     def expose_js_manifold_config (self):
97         self.add_js_chunks(Config.manifold_js_export())
98
99     #################### requirements/prelude management
100     # just forward to self.prelude - see decorator above
101     @to_prelude
102     def add_js_files (self):pass
103     @to_prelude
104     def add_css_files (self):pass
105     @to_prelude
106     def add_js_chunks (self):pass
107     @to_prelude
108     def add_css_chunks (self):pass
109     @to_prelude
110     def prelude_env (self):pass