c20847a119a9568bd931fce6a3bf34ac110d42ca
[myslice.git] / plugins / active_filters / __init__.py
1 from unfold.plugin import Plugin
2 from manifold.util.predicate import eq, ne, lt, le, gt, ge, and_, or_, contains, included 
3
4 # NOTE: Python should pass templates to javascript for creating new filters
5 # - option variable
6 # - or, better, hidden div in the page
7 # In the meantime, templates are duplicated in the javascript code
8 # RECIPES FOR PLUGINS
9
10 # NOTE: having classes would help
11
12 class ActiveFilters(Plugin):
13
14     def __init__ (self, query=None, **settings):
15         Plugin.__init__ (self, **settings)
16
17         self.query = query
18
19     def template_file (self):
20         return "active_filters.html"
21
22     def template_env (self, request):
23
24         def get_op_str(predicate):
25             map = {
26                 eq      : 'eq',
27                 ne      : 'ne',
28                 lt      : 'lt',
29                 le      : 'le',
30                 gt      : 'gt',
31                 ge      : 'ge',
32                 and_    : 'and',
33                 or_     : 'or',
34                 contains: 'contains',
35                 included: 'included'
36             }
37             return map[predicate.get_op()]
38
39         filters = [[f.get_key(), get_op_str(f), f.get_value()] for p in self.query.get_where()]
40         env={}
41         env.update(self.__dict__)
42         env['filters'] = filters
43         return env
44
45     def requirements (self):
46         reqs = {
47             'js_files' : [
48                 'js/active_filters.js',
49              ],
50             'css_files': [ 
51                 'css/demo_table.css',
52                 'css/active_filters.css'
53             ]
54         }
55         return reqs
56
57     def json_settings_list (self):
58         return ['plugin_uuid', 'domid', 'query_uuid']
59
60     def export_json_settings (self):
61         return True