f5a334e083ad9176783bc310ba70e58a9d12fffe
[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         print "METADATA FIELDS", md_fields
34
35         # XXX use django templating system here
36         for md_field in md_fields['column']:
37
38             if md_field['type'] == 'string':
39                 if 'allowed_values' in md_field:
40                     allowed_values = md_field['allowed_values'].split(',')
41
42                     options = []
43                     for v in allowed_values:
44                         v_desc = v.split('-')
45                         options.append(v_desc[0])
46
47                     env = {'options': options}
48                     filter_input = render_to_string('filter_input_string_values.html', env)
49                 else:
50                     env = {'filter_id': "%s-filter-%s" % (self.domid, md_field['name'])}
51                     filter_input = render_to_string('filter_input_string.html', env)
52                     
53             elif md_field['type'] == 'int':
54                 allowed_values = md_field.get('allowed_values', '0,0').split(',')
55                 env = {'min': allowed_values[0], 'max': allowed_values[1]}
56                 filter_input = render_to_string('filter_input_integer.html', env)
57             else:
58                 env = {'filter_id': "%s-filter-%s" % (self.domid, md_field['name'])}
59                 filter_input = render_to_string('filter_input_others.html', env)
60
61             fields.append({
62                 'name':          md_field['name'],
63                 'type':          md_field['type'],
64                 'resource_type': 'N/A',
65                 'filter_input':  filter_input,
66                 'header':        None,
67             })
68         return { 'fields': fields }