def resource_view_create(context, data_dict): '''Creates a new resource view. :param resource_id: id of the resource :type resource_id: string :param title: the title of the view :type title: string :param description: a description of the view (optional) :type description: string :param view_type: type of view :type view_type: string :param config: options necessary to recreate a view state (optional) :type config: JSON string :returns: the newly created resource view :rtype: dictionary ''' model = context['model'] schema = (context.get('schema') or ckan.logic.schema.default_create_resource_view_schema()) resource_id = _get_or_bust(data_dict, 'resource_id') view_type = _get_or_bust(data_dict, 'view_type') view_plugin = datapreview.get_view_plugin(view_type) if not view_plugin: raise ValidationError( {"view_type": "No plugin found for view_type {view_type}".format( view_type=view_type )} ) plugin_schema = view_plugin.info().get('schema', {}) schema.update(plugin_schema) data, errors = _validate(data_dict, schema, context) if errors: model.Session.rollback() raise ValidationError(errors) _check_access('resource_view_create', context, data_dict) if context.get('preview'): return data max_order = model.Session.query( func.max(model.ResourceView.order) ).filter_by(resource_id=resource_id).first() order = 0 if max_order[0] is not None: order = max_order[0] + 1 data['order'] = order resource_view = model_save.resource_view_dict_save(data, context) if not context.get('defer_commit'): model.repo.commit() return model_dictize.resource_view_dictize(resource_view, context)
def resource_view_update(context, data_dict): '''Update a resource view. To update a resource_view you must be authorized to update the resource that the resource_view belongs to. For further parameters see ``resource_view_create()``. :param id: the id of the resource_view to update :type id: string :returns: the updated resource_view :rtype: string ''' model = context['model'] id = _get_or_bust(data_dict, "id") schema = (context.get('schema') or ckan.logic.schema.default_update_resource_view_schema()) resource_view = model.ResourceView.get(id) if not resource_view: raise NotFound view_plugin = datapreview.get_view_plugin(resource_view.view_type) plugin_schema = view_plugin.info().get('schema', {}) schema.update(plugin_schema) data, errors = _validate(data_dict, schema, context) if errors: model.Session.rollback() raise ValidationError(errors) context["resource_view"] = resource_view context['resource'] = model.Resource.get(resource_view.resource_id) _check_access('resource_view_update', context, data_dict) if context.get('preview'): return data resource_view = model_save.resource_view_dict_save(data, context) if not context.get('defer_commit'): model.repo.commit() return model_dictize.resource_view_dictize(resource_view, context)
def get(self, package_type, id, resource_id, view_id=None, post_extra=None): context, extra_vars = self._prepare(id, resource_id) to_preview = extra_vars[u'to_preview'] if post_extra: extra_vars.update(post_extra) package_type = _get_package_type(id) data = extra_vars[u'data'] if u'data' in extra_vars else None if data and u'view_type' in data: view_type = data.get(u'view_type') else: view_type = request.args.get(u'view_type') # view_id exists only when updating if view_id: if not data or not view_type: try: view_data = get_action(u'resource_view_show')( context, { u'id': view_id }) view_type = view_data[u'view_type'] if data: data.update(view_data) else: data = view_data except (NotFound, NotAuthorized): return base.abort(404, _(u'View not found')) # might as well preview when loading good existing view if not extra_vars[u'errors']: to_preview = True data[u'view_type'] = view_type view_plugin = lib_datapreview.get_view_plugin(view_type) if not view_plugin: return base.abort(404, _(u'View Type Not found')) _setup_template_variables(context, {u'id': id}, package_type=package_type) data_dict = { u'package': extra_vars[u'pkg_dict'], u'resource': extra_vars[u'resource'], u'resource_view': data } view_template = view_plugin.view_template(context, data_dict) form_template = view_plugin.form_template(context, data_dict) extra_vars.update({ u'form_template': form_template, u'view_template': view_template, u'data': data, u'to_preview': to_preview, u'datastore_available': plugins.plugin_loaded(u'datastore') }) extra_vars.update( view_plugin.setup_template_variables(context, data_dict) or {}) extra_vars.update(data_dict) if view_id: return base.render(u'package/edit_view.html', extra_vars) return base.render(u'package/new_view.html', extra_vars)
def get( self, package_type, id, resource_id, view_id=None, post_extra=None ): context, extra_vars = self._prepare(id, resource_id) to_preview = extra_vars[u'to_preview'] if post_extra: extra_vars.update(post_extra) package_type = _get_package_type(id) view_type = None # view_id exists only when updating if view_id: try: old_data = get_action(u'resource_view_show')( context, { u'id': view_id } ) data = extra_vars[u'data'] or old_data view_type = old_data.get(u'view_type') # might as well preview when loading good existing view if not extra_vars[u'errors']: to_preview = True except (NotFound, NotAuthorized): return base.abort(404, _(u'View not found')) view_type = view_type or request.args.get(u'view_type') data[u'view_type'] = view_type view_plugin = lib_datapreview.get_view_plugin(view_type) if not view_plugin: return base.abort(404, _(u'View Type Not found')) _setup_template_variables( context, {u'id': id}, package_type=package_type ) data_dict = { u'package': extra_vars[u'pkg_dict'], u'resource': extra_vars[u'resource'], u'resource_view': data } view_template = view_plugin.view_template(context, data_dict) form_template = view_plugin.form_template(context, data_dict) extra_vars.update({ u'form_template': form_template, u'view_template': view_template, u'data': data, u'to_preview': to_preview, u'datastore_available': plugins.plugin_loaded(u'datastore') }) extra_vars.update( view_plugin.setup_template_variables(context, data_dict) or {} ) extra_vars.update(data_dict) if view_id: return base.render(u'package/edit_view.html', extra_vars) return base.render(u'package/new_view.html', extra_vars)
try: old_data = get_action('resource_view_show')(context, {'id': view_id}) data = data or old_data view_type = old_data.get('view_type') ## might as well preview when loading good existing view if not errors: to_preview = True except NotFound: abort(404, _('View not found')) except NotAuthorized: abort(401, _('Unauthorized to view View %s') % view_id) view_type = view_type or request.GET.get('view_type') data['view_type'] = view_type view_plugin = datapreview.get_view_plugin(view_type) if not view_plugin: abort(404, _('View Type Not found')) self._setup_template_variables(context, {'id': id}, package_type=package_type) data_dict = {'package': c.pkg_dict, 'resource': c.resource, 'resource_view': data} view_template = view_plugin.view_template(context, data_dict) form_template = view_plugin.form_template(context, data_dict) vars = {'form_template': form_template, 'view_template': view_template, 'data': data,