load manifold api metadata ...
[unfold.git] / engine / pluginset.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 from engine.prelude import Prelude
7 from engine.manifoldapi import ManifoldAPI
8
9 # decorator to deflect calls on this PluginSet to its prelude
10 def to_prelude (method):
11     def actual (self, *args, **kwds):
12         prelude_method=Prelude.__dict__[method.__name__]
13         return prelude_method(self.prelude,*args, **kwds)
14     return actual
15
16 class PluginSet:
17
18     def __init__ (self, request):
19         self._plugins = {}
20         # queue of queries
21         self._queue=[]
22         self.prelude=Prelude(css_files='css/plugin.css')
23         # no queries yet, needed ?
24         # load metadata
25         self._metadata={}
26         self._metadata_javascript='' 
27         self.load_metadata(request)
28
29     # record known plugins hashed on their domid
30     def record_plugin (self, plugin):
31         self._plugins[plugin.domid]=plugin
32
33     def get_plugin (self, domid):
34         return self._plugins.get(domid,None)
35     
36     def reset_queue (self):
37         self._queue = []
38
39     # the js async methods (see manifold_async_success)
40     # offer the option to deliver the result to a specific DOM elt
41     # otherwise it goes through the pubsub using query's uuid
42     def enqueue_query (self, query, domid=None):
43         self._queue.append ( (query,domid,) )
44
45     # return the javascript that triggers all the queries
46     def exec_queue_asynchroneously (self):
47         js = ""
48         js += "var manifold_query_array = new Array();\n"
49         for (query,domid) in self._queue:
50             qjson=query.to_json()
51             id="'%s'"%domid if domid else 'undefined'
52             js += "manifold_query_array.push({'query':%(qjson)s, 'id':%(id)s});\n"%locals()
53         js += "onFunctionAvailable('manifold_async_exec', function() {manifold_async_exec(manifold_query_array);}, this, true);"
54         self.reset_queue()
55         # run only once the document is ready
56         js = "$(document).ready(function(){%(js)s})"%locals()
57         self.add_js_chunks (js)
58
59
60     def load_metadata(self, request):
61         manifold_api_session_auth = request.session['manifold']['auth']
62         manifold_api = ManifoldAPI(auth=manifold_api_session_auth)
63         
64         fields = ['table', 'column.column',
65                     'column.description','column.header', 'column.title',
66                     'column.unit', 'column.info_type',
67                     'column.resource_type', 'column.value_type',
68                     'column.allowed_values', 'column.platforms.platform',
69                     'column.platforms.platform_url']
70
71         results = manifold_api.Get('metadata:table', [], [], fields)
72
73         for res in results:
74             method = res['table']
75             self._metadata[method] = res
76
77         request.session['metadata'] = self._metadata
78         self._metadata_javascript = "all_headers=" + json.dumps(self._metadata) + ";"
79         self.add_js_chunks(self._metadata_javascript)
80
81
82     def metadata_get_fields(self, method):
83         return self._metadata[method]['column'].sort()
84         
85
86     #################### requirements/prelude management
87     # just forward to self.pluginset - see decorator above
88     @to_prelude
89     def add_js_files (self):pass
90     @to_prelude
91     def add_css_files (self):pass
92     @to_prelude
93     def add_js_chunks (self):pass
94     @to_prelude
95     def add_css_chunks (self):pass
96     @to_prelude
97     def template_env (self):pass