Slice Resources View: fixed view, default fields are setup in the view, fields of...
[myslice.git] / plugins / querytable / __init__.py
1 from unfold.plugin import Plugin
2
3 class QueryTable (Plugin):
4
5     """A plugin for displaying a query as a list
6
7 More accurately, we consider a subject entity (say, a slice) 
8 that can be linked to any number of related entities (say, resources, or users)
9 The 'query' argument will correspond to the subject, while
10 'query_all' will fetch the complete list of 
11 possible candidates for the relationship.
12
13 Current implementation makes the following assumptions
14 * query will only retrieve for the related items a list of fields
15   that corresponds to the initial set of fields displayed in the table
16 * query_all on the contrary is expected to return the complete set of 
17   available attributes that may be of interest, so that using a QueryEditor
18   one can easily extend this table without having to query the backend
19 * checkboxes is a boolean flag, set to true if a rightmost column
20   with checkboxes is desired
21 * optionally pass columns as the initial set of columns
22   if None then this is taken from the query's fields
23 * init_key is the name of a column that should appear in both queries
24   and used internally in the plugin for checkboxes initialization. 
25   If not specified, metadata will be used to find out a primary key.
26   However in the case of nodes & slice for example, the default key
27   as returned by the metadata would be 'urn', but 'urn' could only 
28   be used for this purpose if it gets displayed initially, which is
29   not necessarily a good idea.
30   This is why a slice view would use 'hrn' here instead.
31 * datatables_options are passed to dataTables as-is; 
32   however please refrain from passing an 'aoColumns' 
33   as we use 'aoColumnDefs' instead.
34 """
35
36
37     def __init__ (self, query=None, query_all=None, 
38                   checkboxes=False, columns=None, 
39                   init_key=None,
40                   datatables_options={}, **settings):
41         Plugin.__init__ (self, **settings)
42         self.query          = query
43         # Until we have a proper way to access queries in Python
44         self.query_all      = query_all
45         self.query_all_uuid = query_all.query_uuid if query_all else None
46         self.checkboxes     = checkboxes
47
48         # XXX We need to have some hidden columns until we properly handle dynamic queries
49         if columns is not None:
50             _columns = columns
51             _hidden_columns = []
52         elif self.query:
53             print "self.query.fields = ", self.query_all.fields
54             # Columns displayed by default
55             if self.default_fields is not None:
56                 _columns = [field for field in self.default_fields if not field == 'urn']
57             else:
58                 _columns = [field for field in self.query.fields if not field == 'urn']
59             if query_all:
60                 # We need a list because sets are not JSON-serializable
61                 if self.default_fields is not None:
62                     print self.query_all.fields
63                     _hidden_columns = list(self.query_all.fields - set(self.default_fields))
64                 else:
65                     _hidden_columns = list(self.query_all.fields - self.query.fields)
66                 _hidden_columns.append('urn')
67             else:
68                 _hidden_columns = []
69         else:
70             _columns = []
71             _hidden_columns = []
72
73         print "_columns=", _columns
74         self.columns = { self.mapping.get(c, c) : c for c in _columns }
75         self.hidden_columns = { self.mapping.get(c, c) : c for c in _hidden_columns }
76         print "self.columns", self.columns
77         print "self.hidden_columns", self.hidden_columns
78
79         self.init_key=init_key
80         self.datatables_options=datatables_options
81         # if checkboxes were required, we tell datatables about this column's type
82         # so that sorting can take place on a selected-first basis (or -last of course)
83         # this relies on the template exposing the checkboxes 'th' with class 'checkbox'
84         if self.checkboxes:
85             # we use aoColumnDefs rather than aoColumns -- ignore user-provided aoColumns
86             if 'aoColumns' in self.datatables_options:
87                 print 'WARNING: querytable uses aoColumnDefs, your aoColumns spec. is discarded'
88                 del self.datatables_options['aoColumns']
89             # set aoColumnDefs in datatables_options - might already have stuff in there
90             aoColumnDefs = self.datatables_options.setdefault ('aoColumnDefs',[])
91             # here 'checkbox' is the class that we give to the <th> dom elem
92             # dom-checkbox is a sorting type that we define in querytable.js
93             #aoColumnDefs.insert (0, {'aTargets': ['checkbox'], 'sSortDataType': 'dom-checkbox' } )
94
95     def template_file (self):
96         return "querytable.html"
97
98     def template_env (self, request):
99         env={}
100         env.update(self.__dict__)
101         env['columns']=self.columns
102         return env
103
104     def requirements (self):
105         reqs = {
106             'js_files' : [ "js/spin-presets.js", "js/spin.min.js", "js/jquery.spin.js", 
107                            "js/dataTables.js", "js/dataTables.bootstrap.js", "js/with-datatables.js",
108                            "js/manifold.js", "js/manifold-query.js", 
109                            "js/unfold-helper.js",
110                           # querytable.js needs to be loaded after dataTables.js as it extends 
111                           # dataTableExt.afnSortData
112                            "js/querytable.js", 
113                            ] ,
114             'css_files': [ #"css/dataTables.bootstrap.css",
115                            # hopefully temporary, when/if datatables supports sPaginationType=bootstrap3
116                            # for now we use full_numbers, with our own ad hoc css 
117                            #"css/dataTables.full_numbers.css",
118                            "css/querytable.css" , 
119                            ],
120             }
121         return reqs
122
123     # the list of things passed to the js plugin
124     def json_settings_list (self):
125         return ['plugin_uuid', 'domid', 
126                 'query_uuid', 'query_all_uuid', 
127                 'checkboxes', 'datatables_options', 
128                 'hidden_columns', 'init_key',]