plugins: supporting columns adding/removing + passed query instead of uuid as parameters
[myslice.git] / plugins / query_editor / __init__.py
1 from unfold.plugin import Plugin
2
3 from django.template.loader import render_to_string
4
5 class QueryEditor(Plugin):
6
7     def template_file(self):
8         return "query_editor.html"
9
10     def requirements (self):
11         reqs = {
12             'js_files' : [
13                 'js/query_editor.js',
14             ] ,
15             'css_files': [
16                 'css/query_editor.css',
17                 'css/demo_page.css',
18                 'css/demo_table.css',
19             ]
20         }
21         return reqs
22
23     def json_settings_list (self):
24         return ['plugin_uuid', 'domid', 'query_uuid']
25
26     def export_json_settings (self):
27         return True
28
29     def template_env(self, request):
30         fields = []
31         metadata = self.page.get_metadata()
32         md_fields = metadata.details_by_object('resource')
33
34         # XXX use django templating system here
35         for md_field in md_fields['column']:
36
37             if md_field['type'] == 'string':
38                 if 'allowed_values' in md_field:
39                     allowed_values = md_field['allowed_values'].split(',')
40
41                     options = []
42                     for v in allowed_values:
43                         v_desc = v.split('-')
44                         options.append(v_desc[0])
45
46                     env = {'options': options}
47                     filter_input = render_to_string('filter_input_string_values.html', env)
48                 else:
49                     env = {'filter_id': "%s-filter-%s" % (self.domid, md_field['name'])}
50                     filter_input = render_to_string('filter_input_string.html', env)
51                     
52             elif md_field['type'] == 'int':
53                 allowed_values = md_field.get('allowed_values', '0,0').split(',')
54                 env = {'min': allowed_values[0], 'max': allowed_values[1]}
55                 filter_input = render_to_string('filter_input_integer.html', env)
56             else:
57                 env = {'filter_id': "%s-filter-%s" % (self.domid, md_field['name'])}
58                 filter_input = render_to_string('filter_input_others.html', env)
59
60             fields.append({
61                 'name':          md_field['name'],
62                 'type':          md_field['type'],
63                 'resource_type': 'N/A',
64                 'filter_input':  filter_input,
65                 'header':        None,
66                 'checked':       md_field['name'] in self.query.get_select()
67             })
68         return { 'fields': fields }