manifold: fix in plugin.js
[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 # XXX We need naming helpers in the python Plugin class also, used in template
6
7 class QueryEditor(Plugin):
8
9     def template_file(self):
10         return "query_editor.html"
11
12     def requirements (self):
13         reqs = {
14             'js_files' : [
15                 # XXX datatables
16                 'js/query_editor.js',
17             ] ,
18             'css_files': [
19                 'css/query_editor.css',
20                 'css/demo_page.css',
21                 'css/demo_table.css',
22             ]
23         }
24         return reqs
25
26     def json_settings_list (self):
27         return ['plugin_uuid', 'domid', 'query_uuid']
28
29     def export_json_settings (self):
30         return True
31
32     def template_env(self, request):
33         fields = []
34         metadata = self.page.get_metadata()
35         md_fields = metadata.details_by_object('resource')
36
37         # XXX use django templating system here
38         for md_field in md_fields['column']:
39
40             if md_field['type'] == 'string':
41                 if 'allowed_values' in md_field:
42                     allowed_values = md_field['allowed_values'].split(',')
43
44                     options = []
45                     for v in allowed_values:
46                         v_desc = v.split('-')
47                         options.append(v_desc[0])
48
49                     env = {
50                         'domid': self.domid,
51                         'options': options
52                     }
53                     filter_input = render_to_string('filter_input_string_values.html', env)
54                 else:
55                     env = {
56                         'domid': self.domid,
57                         'field': md_field['name']
58                     }
59                     filter_input = render_to_string('filter_input_string.html', env)
60                     
61             elif md_field['type'] == 'int':
62                 allowed_values = md_field.get('allowed_values', '0,0').split(',')
63                 env = {
64                     'domid': self.domid,
65                     'field': md_field['name'],
66                     'min'  : allowed_values[0],
67                     'max'  : allowed_values[1]
68                 }
69                 filter_input = render_to_string('filter_input_integer.html', env)
70             else:
71                 env = {
72                     'domid': self.domid,
73                     'field': md_field['name']
74                 }
75                 filter_input = render_to_string('filter_input_others.html', env)
76
77             fields.append({
78                 'name':          md_field['name'],
79                 'type':          md_field['type'],
80                 'resource_type': 'N/A',
81                 'filter_input':  filter_input,
82                 'header':        None,
83                 'checked':       md_field['name'] in self.query.get_select()
84             })
85         return { 'fields': fields }