-
Notifications
You must be signed in to change notification settings - Fork 2k
/
plugin.py
225 lines (183 loc) · 7.97 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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')
Invalid = p.toolkit.Invalid
def in_list(list_possible_values):
'''
Validator that checks that the input value is one of the given
possible values.
:param list_possible_values: function that returns list of possible values
for validated field
:type possible_values: function
'''
def validate(key, data, errors, context):
if not data[key] in list_possible_values():
raise Invalid('"{0}" is not a valid parameter'.format(data[key]))
return validate
def datastore_fields(resource, valid_field_types):
'''
Return a list of all datastore fields for a given resource, as long as
the datastore field type is in valid_field_types.
:param resource: resource dict
:type resource: dict
:param valid_field_types: field types to include in returned list
:type valid_field_types: list of strings
'''
data = {'resource_id': resource['id'], 'limit': 0}
fields = toolkit.get_action('datastore_search')({}, data)['fields']
return [{'value': f['id'], 'text': f['id']} for f in fields
if f['type'] in valid_field_types]
class ReclineViewBase(p.SingletonPlugin):
'''
This base class for the Recline view extensions.
'''
p.implements(p.IConfigurer, inherit=True)
p.implements(p.IResourceView, inherit=True)
def update_config(self, config):
'''
Set up the resource library, public directory and
template directory for the view
'''
toolkit.add_public_directory(config, 'theme/public')
toolkit.add_template_directory(config, 'theme/templates')
toolkit.add_resource('theme/public', 'ckanext-reclineview')
def can_view(self, data_dict):
resource = data_dict['resource']
return (resource.get('datastore_active') or
resource.get('url') == '_datastore_only_resource')
def setup_template_variables(self, context, data_dict):
return {'resource_json': json.dumps(data_dict['resource']),
'resource_view_json': json.dumps(data_dict['resource_view'])}
def view_template(self, context, data_dict):
return 'recline_view.html'
class ReclineView(ReclineViewBase):
'''
This extension views resources using a Recline MultiView.
'''
def info(self):
return {'name': 'recline_view',
'title': 'Data Explorer',
'filterable': True,
'icon': 'table',
'requires_datastore': False,
'default_title': p.toolkit._('Data Explorer'),
}
def can_view(self, data_dict):
resource = data_dict['resource']
if (resource.get('datastore_active') or
resource.get('url') == '_datastore_only_resource'):
return True
resource_format = resource.get('format', None)
if resource_format:
return resource_format.lower() in ['csv', 'xls', 'xlsx', 'tsv']
else:
return False
class ReclineGridView(ReclineViewBase):
'''
This extension views resources using a Recline grid.
'''
def info(self):
return {'name': 'recline_grid_view',
'title': 'Grid',
'filterable': True,
'icon': 'table',
'requires_datastore': True,
'default_title': p.toolkit._('Table'),
}
class ReclineGraphView(ReclineViewBase):
'''
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'}]
datastore_fields = []
datastore_field_types = ['numeric', 'int4', 'timestamp']
def list_graph_types(self):
return [t['value'] for t in self.graph_types]
def list_datastore_fields(self):
return [t['value'] for t in self.datastore_fields]
def info(self):
# in_list validator here is passed functions because this
# method does not know what the possible values of the
# datastore fields are (requires a datastore search)
schema = {
'offset': [ignore_empty, natural_number_validator],
'limit': [ignore_empty, natural_number_validator],
'graph_type': [ignore_empty, in_list(self.list_graph_types)],
'group': [ignore_empty, in_list(self.list_datastore_fields)],
'series': [ignore_empty, in_list(self.list_datastore_fields)]
}
return {'name': 'recline_graph_view',
'title': 'Graph',
'filterable': True,
'icon': 'bar-chart',
'requires_datastore': True,
'schema': schema,
'default_title': p.toolkit._('Graph'),
}
def setup_template_variables(self, context, data_dict):
self.datastore_fields = datastore_fields(data_dict['resource'],
self.datastore_field_types)
vars = ReclineViewBase.setup_template_variables(self, context,
data_dict)
vars.update({'graph_types': self.graph_types,
'graph_fields': self.datastore_fields})
return vars
def form_template(self, context, data_dict):
return 'recline_graph_form.html'
class ReclineMapView(ReclineViewBase):
'''
This extension views resources using a Recline map.
'''
map_field_types = [{'value': 'lat_long',
'text': 'Latitude / Longitude fields'},
{'value': 'geojson', 'text': 'GeoJSON'}]
datastore_fields = []
datastore_field_types = ['numeric']
def list_map_field_types(self):
return [t['value'] for t in self.map_field_types]
def list_datastore_fields(self):
return [t['value'] for t in self.datastore_fields]
def info(self):
# in_list validator here is passed functions because this
# method does not know what the possible values of the
# datastore fields are (requires a datastore search)
schema = {
'offset': [ignore_empty, natural_number_validator],
'limit': [ignore_empty, natural_number_validator],
'map_field_type': [ignore_empty,
in_list(self.list_map_field_types)],
'latitude_field': [ignore_empty,
in_list(self.list_datastore_fields)],
'longitude_field': [ignore_empty,
in_list(self.list_datastore_fields)],
'geojson_field': [ignore_empty,
in_list(self.list_datastore_fields)],
'auto_zoom': [ignore_empty],
'cluster_markers': [ignore_empty]
}
return {'name': 'recline_map_view',
'title': 'Map',
'schema': schema,
'filterable': True,
'icon': 'map-marker',
'default_title': p.toolkit._('Map'),
}
def setup_template_variables(self, context, data_dict):
self.datastore_fields = datastore_fields(data_dict['resource'],
self.datastore_field_types)
vars = ReclineViewBase.setup_template_variables(self, context,
data_dict)
vars.update({'map_field_types': self.map_field_types,
'map_fields': self.datastore_fields})
return vars
def form_template(self, context, data_dict):
return 'recline_map_form.html'