07879e7b846fb9344865747d8f8773016d785d7e
[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     # not sure this is useful at all
46 #    def reset_queue (self):
47 #        self._queries = set()
48 #        self._queue = []
49
50     # this method adds a query to the page
51     # the query will be exposed to js when calling expose_queries
52     # additionally if run_it is set to True, this query will be asynchroneously triggered on page load
53     # in this case (exec=True) the js async callback (see manifold.asynchroneous_success)
54     # offers the option to deliver the result to a specific DOM elt (in this case, set domid)
55     # otherwise (i.e. if domid not provided), it goes through the pubsub system (so all plugins can receive it)
56     # 
57     # NOTE:
58     # analyzed_query is required because it contains query_uuid that the
59     # plugins initialized in the python part will listen to. When a result is
60     # received in javascript, subresults should be publish to the appropriate
61     # query_uuid.
62     # 
63     def enqueue_query (self, query, run_it=True, domid=None, analyzed_query=None):
64         # _queries is the set of all known queries
65         # XXX complex XXX self._queries = self._queries.union(set( [ query, ] ))
66         self._queries.add((query, analyzed_query))
67         # _queue is the list of queries that need to be triggered, with an optional domid
68         # we only do this if run_it is set
69         if run_it: self._queue.append ( (query.query_uuid,domid) )
70
71     # return the javascript code for exposing queries
72     # all queries are inserted in the global manifold object
73     # in addition, the ones enqueued with 'run_it=True' are triggered 
74     def expose_queries (self):
75         # compute variables to expose to the template
76         env = {}
77         # expose the json definition of all queries
78         env['queries_json'] = [ query.to_json(analyzed_query=aq) for (query, aq) in self._queries ]
79         def query_publish_dom_tuple (a,b):
80             result={'query_uuid':a}
81             if b: result['domid']=b
82             return result
83         env['query_publish_dom_tuples'] = [ query_publish_dom_tuple (a,b) for (a,b) in self._queue ]
84         javascript = render_to_string ("page-queries.js",env)
85         self.add_js_chunks (javascript)
86 #        self.reset_queue()
87         # unconditionnally expose MANIFOLD_URL, this is small and manifold.js uses that for various messages
88         self.expose_js_manifold_config()
89
90
91 # DEPRECATED #    # needs to be called explicitly and only when metadata is actually required
92 # DEPRECATED #    # in particular user needs to be logged
93 # DEPRECATED #    def get_metadata (self):
94 # DEPRECATED #        # look in session's cache - we don't want to retrieve this for every request
95 # DEPRECATED #        session=self.request.session
96 # DEPRECATED #        if 'manifold' not in session:
97 # DEPRECATED #            print "Page.expose_js_metadata: no 'manifold' in session... - cannot retrieve metadata - skipping"
98 # DEPRECATED #            return
99 # DEPRECATED #        manifold=session['manifold']
100 # DEPRECATED #        # if cached, use it
101 # DEPRECATED #        if 'metadata' in manifold and isinstance(manifold['metadata'],MetaData):
102 # DEPRECATED #            if debug: print "Page.get_metadata: return cached value"
103 # DEPRECATED #            return manifold['metadata']
104 # DEPRECATED #        # otherwise retrieve it
105 # DEPRECATED #        manifold_api_session_auth = session['manifold']['auth']
106 # DEPRECATED #        print "get_metadata(), manifold_api_session_auth =", session['manifold']['auth']
107 # DEPRECATED #        metadata=MetaData (manifold_api_session_auth)
108 # DEPRECATED #        metadata.fetch()
109 # DEPRECATED #        # store it for next time
110 # DEPRECATED #        manifold['metadata']=metadata
111 # DEPRECATED #        if debug: print "Page.get_metadata: return new value"
112 # DEPRECATED #        return metadata
113
114     # needs to be called explicitly and only when metadata is actually required
115     # in particular user needs to be logged
116     def get_metadata (self):
117         # look in session's cache - we don't want to retrieve this for every request
118         session=self.request.session
119
120         if 'manifold' not in session:
121             session['manifold'] = {}
122         manifold = session['manifold']
123
124         # if cached, use it
125         if 'metadata' in manifold and isinstance(manifold['metadata'],MetaData):
126             if debug: print "Page.get_metadata: return cached value"
127             return manifold['metadata']
128
129         metadata_auth = {'AuthMethod':'anonymous'}
130
131         metadata=MetaData (metadata_auth)
132         metadata.fetch()
133         # store it for next time
134         manifold['metadata']=metadata
135         if debug: print "Page.get_metadata: return new value"
136         return metadata
137             
138     def expose_js_metadata (self):
139         # export in this js global...
140         self.add_js_chunks("var MANIFOLD_METADATA =" + self.get_metadata().to_json() + ";")
141
142     def expose_js_manifold_config (self):
143         self.add_js_chunks(Config.manifold_js_export())
144
145     #################### requirements/prelude management
146     # just forward to self.prelude - see decorator above
147     @to_prelude
148     def add_js_files (self):pass
149     @to_prelude
150     def add_css_files (self):pass
151     @to_prelude
152     def add_js_chunks (self):pass
153     @to_prelude
154     def add_css_chunks (self):pass
155     @to_prelude
156     def prelude_env (self):pass