-
Notifications
You must be signed in to change notification settings - Fork 2k
/
plugin.py
102 lines (79 loc) · 3.11 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from logging import getLogger
from ckan.common import json
import ckan.plugins as p
import ckan.plugins.toolkit as toolkit
log = getLogger(__name__)
ignore_empty = p.toolkit.get_validator('ignore_empty')
natural_number_validator = p.toolkit.get_validator('natural_number_validator')
class ReclineView(p.SingletonPlugin):
'''
This base class for the Recline view extensions.
'''
p.implements(p.IConfigurer, inherit=True)
p.implements(p.IResourceView, inherit=True)
# schema fields that apply to all Recline views
schema = {'offset': [ignore_empty, natural_number_validator],
'limit': [ignore_empty, natural_number_validator]}
def update_config(self, config):
'''
Set up the resource library, public directory and
template directory for the preview
'''
toolkit.add_public_directory(config, 'theme/public')
toolkit.add_template_directory(config, 'theme/templates')
toolkit.add_resource('theme/public', 'ckanext-reclinepreview')
def can_view(self, data_dict):
if data_dict['resource'].get('datastore_active'):
return True
return False
def setup_template_variables(self, context, data_dict):
return {'resource_json': json.dumps(data_dict['resource']),
'resource_view_json': json.dumps(data_dict['data'])}
def view_template(self, context, data_dict):
return 'recline_view.html'
class ReclineGrid(ReclineView):
'''
This extension views resources using a Recline grid.
'''
def info(self):
return {'name': 'recline_grid',
'title': 'Grid',
'schema': self.schema}
def form_template(self, context, data_dict):
return 'recline_grid_form.html'
class ReclineGraph(ReclineView):
'''
This extension views resources using a Recline graph.
'''
graph_types = [{'value': 'lines-and-points',
'text': 'Lines and points'},
{'value': 'lines', 'text': 'Lines'},
{'value': 'points', 'text': 'Points'},
{'value': 'bars', 'text': 'Bars'},
{'value': 'columns', 'text': 'Columns'}]
def info(self):
# TODO: add validators
self.schema.update({
'graph_type': [ignore_empty],
'group_column': [ignore_empty],
'series_a': [ignore_empty]
})
return {'name': 'recline_graph',
'title': 'Graph',
'schema': self.schema}
def setup_template_variables(self, context, data_dict):
vars = ReclineView.setup_template_variables(self, context, data_dict)
vars.update({'graph_types': self.graph_types})
return vars
def form_template(self, context, data_dict):
return 'recline_graph_form.html'
class ReclineMap(ReclineView):
'''
This extension views resources using a Recline map.
'''
def info(self):
return {'name': 'recline_map',
'title': 'Map',
'schema': self.schema}
def form_template(self, context, data_dict):
return 'recline_map_form.html'