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'] resource_id = _get_or_bust(data_dict, 'resource_id') view_type = _get_or_bust(data_dict, 'view_type') view_plugin = ckan.lib.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 )} ) default = logic.schema.default_create_resource_view_schema(view_plugin) schema = context.get('schema', default) 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_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'] resource_id = _get_or_bust(data_dict, 'resource_id') view_type = _get_or_bust(data_dict, 'view_type') view_plugin = ckan.lib.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) }) default = logic.schema.default_create_resource_view_schema(view_plugin) schema = context.get('schema', default) 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: Context, data_dict: DataDict) -> ActionResult.ResourceViewUpdate: '''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") resource_view = model.ResourceView.get(id) if not resource_view: raise NotFound view_plugin = ckan.lib.datapreview.get_view_plugin(resource_view.view_type) schema = (context.get('schema') or schema_.default_update_resource_view_schema(view_plugin)) assert view_plugin 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 resource = model.Resource.get(resource_view.resource_id) if resource is None: raise NotFound('Resource was not found.') context['resource'] = resource _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 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 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") resource_view = model.ResourceView.get(id) if not resource_view: raise NotFound view_plugin = ckan.lib.datapreview.get_view_plugin(resource_view.view_type) schema = context.get("schema") or schema_.default_update_resource_view_schema(view_plugin) 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 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") resource_view = model.ResourceView.get(id) if not resource_view: raise NotFound view_plugin = ckan.lib.datapreview.get_view_plugin(resource_view.view_type) schema = (context.get('schema') or schema_.default_update_resource_view_schema(view_plugin)) 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() # add activity for resource view update try: user = context['user'] user_id = model.User.by_name(user.decode('utf8')).id except AttributeError: # do not create activity for non-users pass else: activity_dict = { 'user_id': user_id, 'object_id': context['resource'].package_id, 'activity_type': 'changed resource view', 'data': { 'id': resource_view.id }, } activity_create_context = { 'model': model, 'user': user, 'defer_commit': False, 'ignore_auth': True, 'session': context['session'], } logic.get_action('activity_create')(activity_create_context, activity_dict) return model_dictize.resource_view_dictize(resource_view, context)