Merge branch 'master' of ssh://git.onelab.eu/git/myslice
[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         print "Loading... CSS OneLab"
38         self.prelude=Prelude(css_files='css/onelab_marko.css')
39 #        self.prelude=Prelude(css_files=['css/plugin.css','css/onelab_marko.css',])
40
41     # record known plugins hashed on their domid
42     def record_plugin (self, plugin):
43         self._plugins[plugin.domid]=plugin
44
45     def get_plugin (self, domid):
46         return self._plugins.get(domid,None)
47
48     # not sure this is useful at all
49 #    def reset_queue (self):
50 #        self._queries = set()
51 #        self._queue = []
52
53     # this method adds a query to the page
54     # the query will be exposed to js when calling expose_queries
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)
59     # 
60     # NOTE:
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
64     # query_uuid.
65     # 
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) )
73
74     # return the javascript code for exposing queries
75     # all queries are inserted in the global manifold object
76     # in addition, the ones enqueued with 'run_it=True' are triggered 
77     def expose_queries (self):
78         # compute variables to expose to the template
79         env = {}
80         # expose the json definition of all queries
81         env['queries_json'] = [ query.to_json(analyzed_query=aq) for (query, aq) in self._queries ]
82         def query_publish_dom_tuple (a,b):
83             result={'query_uuid':a}
84             if b: result['domid']=b
85             return result
86         env['query_publish_dom_tuples'] = [ query_publish_dom_tuple (a,b) for (a,b) in self._queue ]
87         javascript = render_to_string ("page-queries.js",env)
88         self.add_js_chunks (javascript)
89 #        self.reset_queue()
90         # unconditionnally expose MANIFOLD_URL, this is small and manifold.js uses that for various messages
91         self.expose_js_manifold_config()
92
93
94 # DEPRECATED #    # needs to be called explicitly and only when metadata is actually required
95 # DEPRECATED #    # in particular user needs to be logged
96 # DEPRECATED #    def get_metadata (self):
97 # DEPRECATED #        # look in session's cache - we don't want to retrieve this for every request
98 # DEPRECATED #        session=self.request.session
99 # DEPRECATED #        if 'manifold' not in session:
100 # DEPRECATED #            print "Page.expose_js_metadata: no 'manifold' in session... - cannot retrieve metadata - skipping"
101 # DEPRECATED #            return
102 # DEPRECATED #        manifold=session['manifold']
103 # DEPRECATED #        # if cached, use it
104 # DEPRECATED #        if 'metadata' in manifold and isinstance(manifold['metadata'],MetaData):
105 # DEPRECATED #            if debug: print "Page.get_metadata: return cached value"
106 # DEPRECATED #            return manifold['metadata']
107 # DEPRECATED #        # otherwise retrieve it
108 # DEPRECATED #        manifold_api_session_auth = session['manifold']['auth']
109 # DEPRECATED #        print "get_metadata(), manifold_api_session_auth =", session['manifold']['auth']
110 # DEPRECATED #        metadata=MetaData (manifold_api_session_auth)
111 # DEPRECATED #        metadata.fetch()
112 # DEPRECATED #        # store it for next time
113 # DEPRECATED #        manifold['metadata']=metadata
114 # DEPRECATED #        if debug: print "Page.get_metadata: return new value"
115 # DEPRECATED #        return metadata
116
117     # needs to be called explicitly and only when metadata is actually required
118     # in particular user needs to be logged
119     def get_metadata (self):
120         # look in session's cache - we don't want to retrieve this for every request
121         session=self.request.session
122
123         if 'manifold' not in session:
124             session['manifold'] = {}
125         manifold = session['manifold']
126
127         # if cached, use it
128         if 'metadata' in manifold and isinstance(manifold['metadata'],MetaData):
129             if debug: print "Page.get_metadata: return cached value"
130             return manifold['metadata']
131
132         metadata_auth = {'AuthMethod':'anonymous'}
133
134         metadata=MetaData (metadata_auth)
135         metadata.fetch()
136         # store it for next time
137         manifold['metadata']=metadata
138         if debug: print "Page.get_metadata: return new value"
139         return metadata
140             
141     def expose_js_metadata (self):
142         # export in this js global...
143         self.add_js_chunks("var MANIFOLD_METADATA =" + self.get_metadata().to_json() + ";")
144
145     def expose_js_manifold_config (self):
146         config=Config()
147         self.add_js_chunks(config.manifold_js_export())
148
149     #################### requirements/prelude management
150     # just forward to self.prelude - see decorator above
151     @to_prelude
152     def add_js_files (self):pass
153     @to_prelude
154     def add_css_files (self):pass
155     @to_prelude
156     def add_js_chunks (self):pass
157     @to_prelude
158     def add_css_chunks (self):pass
159     @to_prelude
160     def prelude_env (self):pass