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