diff --git a/ckanext/reclinepreview/plugin.py b/ckanext/reclinepreview/plugin.py index da48c0c3dfc..c0d4595ef08 100644 --- a/ckanext/reclinepreview/plugin.py +++ b/ckanext/reclinepreview/plugin.py @@ -3,10 +3,27 @@ from ckan.common import json import ckan.plugins as p import ckan.plugins.toolkit as toolkit +import ckan.lib.navl.dictization_functions as df log = getLogger(__name__) ignore_empty = p.toolkit.get_validator('ignore_empty') natural_number_validator = p.toolkit.get_validator('natural_number_validator') +Invalid = df.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 class ReclineView(p.SingletonPlugin): @@ -61,6 +78,8 @@ class ReclineGraph(ReclineView): This extension views resources using a Recline graph. ''' + graph_field_types = ['numeric', 'int4', 'timestamp'] + graph_types = [{'value': 'lines-and-points', 'text': 'Lines and points'}, {'value': 'lines', 'text': 'Lines'}, @@ -68,20 +87,38 @@ class ReclineGraph(ReclineView): {'value': 'bars', 'text': 'Bars'}, {'value': 'columns', 'text': 'Columns'}] + datastore_fields = [] + def info(self): - # TODO: add validators + # in_list validator here is passed functions because this + # method does not know what the possible values of the + # datastore fields are (requires datastore search) self.schema.update({ - 'graph_type': [ignore_empty], - 'group_column': [ignore_empty], - 'series_a': [ignore_empty] + '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', 'title': 'Graph', 'schema': self.schema} + 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 _datastore_fields(self, resource): + 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 self.graph_field_types] + def setup_template_variables(self, context, data_dict): + self.datastore_fields = self._datastore_fields(data_dict['resource']) vars = ReclineView.setup_template_variables(self, context, data_dict) - vars.update({'graph_types': self.graph_types}) + vars.update({'graph_types': self.graph_types, + 'graph_fields': self.datastore_fields}) return vars def form_template(self, context, data_dict): diff --git a/ckanext/reclinepreview/theme/public/preview_recline.js b/ckanext/reclinepreview/theme/public/preview_recline.js index 6e8071c2314..58826fc9e9a 100644 --- a/ckanext/reclinepreview/theme/public/preview_recline.js +++ b/ckanext/reclinepreview/theme/public/preview_recline.js @@ -70,7 +70,9 @@ this.ckan.module('reclinepreview', function (jQuery, _) { if(reclineView.view_type === "recline_graph") { view = new recline.View.Graph({ model: dataset, - el: this.el + state: {"graphType": reclineView.graph_type, + "group": reclineView.group, + "series": [reclineView.series]} }); } else if(reclineView.view_type == "recline_map") { view = new recline.View.Map({ @@ -79,13 +81,17 @@ this.ckan.module('reclinepreview', function (jQuery, _) { }); } else { view = new recline.View.SlickGrid({ - model: dataset, - el: this.el + model: dataset }); } + this.el.replaceWith(view.el); view.visible = true; view.render(); + + if(reclineView.view_type === "recline_graph") { + view.redraw(); + } }, normalizeUrl: function (url) { diff --git a/ckanext/reclinepreview/theme/templates/recline_graph_form.html b/ckanext/reclinepreview/theme/templates/recline_graph_form.html index 6197d8d87e5..6fc89385612 100644 --- a/ckanext/reclinepreview/theme/templates/recline_graph_form.html +++ b/ckanext/reclinepreview/theme/templates/recline_graph_form.html @@ -4,5 +4,5 @@ {{ form.input('limit', id='field-limit', label=_('Number of rows'), placeholder=_('eg: 100'), value=data.limit, error=errors.limit, classes=['control-medium']) }} {{ form.select('graph_type', label=_('Graph type'), options=graph_types, selected=data.graph_type, error=errors.graph_type) }} -{{ form.select('group_column', label=_('Group column (Axis 1)'), options=columns, selected=data.group_column, error=errors.graph_column) }} -{{ form.select('series_a', label=_('Series A (Axis 2)'), options=columns, selected=data.series_a, error=errors.series_a) }} +{{ form.select('group', label=_('Group (Axis 1)'), options=graph_fields, selected=data.group, error=errors.group) }} +{{ form.select('series', label=_('Series (Axis 2)'), options=graph_fields, selected=data.series, error=errors.series) }} \ No newline at end of file