def related_update(context, data_dict): model = context['model'] user = context['user'] id = data_dict["id"] schema = context.get('schema') or ckan.logic.schema.default_related_schema() model.Session.remove() related = model.Related.get(id) context["related"] = related if not related: logging.error('Could not find related ' + id) raise NotFound(_('Related was not found.')) check_access('related_update', context, data_dict) data, errors = validate(data_dict, schema, context) if errors: model.Session.rollback() raise ValidationError(errors, error_summary(errors)) related = model_save.related_dict_save(data, context) if not context.get('defer_commit'): model.repo.commit() return model_dictize.related_dictize(related, context)
def related_show(context, data_dict=None): """ Shows a single related item context: model - The CKAN model module user - The name of the current user data_dict: id - The ID of the related item we want to show """ model = context['model'] id = data_dict['id'] related = model.Related.get(id) context['related'] = related if related is None: raise NotFound check_access('related_show',context, data_dict) schema = context.get('schema') or ckan.logic.schema.default_related_schema() related_dict = model_dictize.related_dictize(related, context) related_dict, errors = validate(related_dict, schema, context=context) return related_dict
def related_show(context, data_dict): # Don't use related_show action, because it removes "created" attribute and puts "view_count" into "__extras" dict. # related_dict = plugins.toolkit.get_action('related_show')(context, data_dict) model = context['model'] related = model.Session.query(model.Related).get(data_dict['id']) related_dict = model_dictize.related_dictize(related, context) related_dataset = model.Session.query(model.RelatedDataset).filter( model.RelatedDataset.related_id == data_dict['id'], model.RelatedDataset.status == u'active', ).first() if related_dataset is not None: related_dict['dataset_id'] = related_dataset.dataset_id return related_dict
def related_create(context, data_dict): '''Add a new related item to a dataset. You must provide your API key in the Authorization header. :param title: the title of the related item :type title: string :param type: the type of the related item, e.g. ``'Application'``, ``'Idea'`` or ``'Visualisation'`` :type type: string :param id: the id of the related item (optional) :type id: string :param description: the description of the related item (optional) :type description: string :param url: the URL to the related item (optional) :type url: string :param image_url: the URL to the image for the related item (optional) :type image_url: string :param dataset_id: the name or id of the dataset that the related item belongs to (optional) :type dataset_id: string :returns: the newly created related item :rtype: dictionary ''' model = context['model'] user = context['user'] userobj = model.User.get(user) data_dict["owner_id"] = userobj.id data, errors = _validate(data_dict, ckan.logic.schema.default_related_schema(), context) if errors: model.Session.rollback() raise ValidationError(errors, _error_summary(errors)) related = model_save.related_dict_save(data, context) if not context.get('defer_commit'): model.repo.commit_and_remove() if 'dataset_id' in data_dict: dataset = model.Package.get(data_dict['dataset_id']) dataset.related.append( related ) model.repo.commit_and_remove() context["related"] = related context["id"] = related.id log.debug('Created object %s' % str(related.title)) return model_dictize.related_dictize(related, context)
def related_delete(context, data_dict): '''Delete a related item from a dataset. You must be a sysadmin or the owner of the related item to delete it. :param id: the id of the related item :type id: string ''' model = context['model'] session = context['session'] user = context['user'] userobj = model.User.get(user) id = _get_or_bust(data_dict, 'id') entity = model.Related.get(id) if entity is None: raise NotFound _check_access('related_delete',context, data_dict) related_dict = model_dictize.related_dictize(entity, context) activity_dict = { 'user_id': userobj.id, 'object_id': entity.id, 'activity_type': 'deleted related item', } activity_dict['data'] = { 'related': related_dict } activity_create_context = { 'model': model, 'user': user, 'defer_commit': True, 'ignore_auth': True, 'session': session } # DGU checks if activity streams are enabled first, to avoid Auth Audit # issue #1421 if converters.asbool( config.get('ckan.activity_streams_enabled', 'true')): _get_action('activity_create')(activity_create_context, activity_dict) session.commit() entity.delete() model.repo.commit()
def related_delete(context, data_dict): '''Delete a related item from a dataset. You must be a sysadmin or the owner of the related item to delete it. :param id: the id of the related item :type id: string ''' model = context['model'] session = context['session'] user = context['user'] userobj = model.User.get(user) id = _get_or_bust(data_dict, 'id') entity = model.Related.get(id) if entity is None: raise NotFound _check_access('related_delete',context, data_dict) related_dict = model_dictize.related_dictize(entity, context) activity_dict = { 'user_id': userobj.id, 'object_id': entity.id, 'activity_type': 'deleted related item', } activity_dict['data'] = { 'related': related_dict } activity_create_context = { 'model': model, 'user': user, 'defer_commit': True, 'ignore_auth': True, 'session': session } _get_action('activity_create')(activity_create_context, activity_dict) session.commit() entity.delete() model.repo.commit()
def related_update(context, data_dict): '''Update a related item. You must be the owner of a related item to update it. For further parameters see ``related_create()``. :param id: the id of the related item to update :type id: string :returns: the updated related item :rtype: dictionary ''' model = context['model'] id = _get_or_bust(data_dict, "id") schema = context.get( 'schema') or ckan.logic.schema.default_related_schema() model.Session.remove() related = model.Related.get(id) context["related"] = related if not related: logging.error('Could not find related ' + id) raise NotFound(_('Related was not found.')) _check_access('related_update', context, data_dict) data, errors = _validate(data_dict, schema, context) if errors: model.Session.rollback() raise ValidationError(errors, _error_summary(errors)) related = model_save.related_dict_save(data, context) if not context.get('defer_commit'): model.repo.commit() return model_dictize.related_dictize(related, context)
def related_update(context, data_dict): """Update a related item. You must be the owner of a related item to update it. For further parameters see ``related_create()``. :param id: the id of the related item to update :type id: string :returns: the updated related item :rtype: dictionary """ model = context["model"] user = context["user"] id = _get_or_bust(data_dict, "id") schema = context.get("schema") or ckan.logic.schema.default_related_schema() model.Session.remove() related = model.Related.get(id) context["related"] = related if not related: logging.error("Could not find related " + id) raise NotFound(_("Item was not found.")) _check_access("related_update", context, data_dict) data, errors = _validate(data_dict, schema, context) if errors: model.Session.rollback() raise ValidationError(errors) related = model_save.related_dict_save(data, context) if not context.get("defer_commit"): model.repo.commit() return model_dictize.related_dictize(related, context)
def related_update(context, data_dict): '''Update a related item. You must be the owner of a related item to update it. For further parameters see ``related_create()``. :param id: the id of the related item to update :type id: string :returns: the updated related item :rtype: dictionary ''' model = context['model'] id = _get_or_bust(data_dict, "id") schema = context.get('schema') or ckan.logic.schema.default_related_schema() model.Session.remove() related = model.Related.get(id) context["related"] = related if not related: logging.error('Could not find related ' + id) raise NotFound(_('Related was not found.')) _check_access('related_update', context, data_dict) data, errors = _validate(data_dict, schema, context) if errors: model.Session.rollback() raise ValidationError(errors, _error_summary(errors)) related = model_save.related_dict_save(data, context) if not context.get('defer_commit'): model.repo.commit() return model_dictize.related_dictize(related, context)
def related_create(context, data_dict): model = context["model"] user = context["user"] userobj = model.User.get(user) data_dict["owner_id"] = userobj.id data, errors = validate(data_dict, ckan.logic.schema.default_related_schema(), context) if errors: model.Session.rollback() raise ValidationError(errors, error_summary(errors)) related = model_save.related_dict_save(data, context) if not context.get("defer_commit"): model.repo.commit_and_remove() if "dataset_id" in data_dict: dataset = model.Package.get(data_dict["dataset_id"]) dataset.related.append(related) model.repo.commit_and_remove() context["related"] = related context["id"] = related.id log.debug("Created object %s" % str(related.title)) return model_dictize.related_dictize(related, context)
def related_create(context, data_dict): '''Add a new related item to a dataset. You must provide your API key in the Authorization header. :param title: the title of the related item :type title: string :param type: the type of the related item, e.g. ``'Application'``, ``'Idea'`` or ``'Visualisation'`` :type type: string :param id: the id of the related item (optional) :type id: string :param description: the description of the related item (optional) :type description: string :param url: the URL to the related item (optional) :type url: string :param image_url: the URL to the image for the related item (optional) :type image_url: string :param dataset_id: the name or id of the dataset that the related item belongs to (optional) :type dataset_id: string :returns: the newly created related item :rtype: dictionary ''' model = context['model'] session = context['session'] user = context['user'] userobj = model.User.get(user) _check_access('related_create', context, data_dict) data_dict["owner_id"] = userobj.id data, errors = _validate(data_dict, ckan.logic.schema.default_related_schema(), context) if errors: model.Session.rollback() raise ValidationError(errors) related = model_save.related_dict_save(data, context) if not context.get('defer_commit'): model.repo.commit_and_remove() dataset_dict = None if 'dataset_id' in data_dict: dataset = model.Package.get(data_dict['dataset_id']) dataset.related.append(related) model.repo.commit_and_remove() dataset_dict = ckan.lib.dictization.table_dictize(dataset, context) session.flush() related_dict = model_dictize.related_dictize(related, context) activity_dict = { 'user_id': userobj.id, 'object_id': related.id, 'activity_type': 'new related item', } activity_dict['data'] = { 'related': related_dict, 'dataset': dataset_dict, } activity_create_context = { 'model': model, 'user': user, 'defer_commit': True, 'ignore_auth': True, 'session': session } logic.get_action('activity_create')(activity_create_context, activity_dict) session.commit() context["related"] = related context["id"] = related.id log.debug('Created object %s' % related.title) return related_dict
def related_update(context, data_dict): '''Update a related item. You must be the owner of a related item to update it. For further parameters see ``related_create()``. :param id: the id of the related item to update :type id: string :returns: the updated related item :rtype: dictionary ''' model = context['model'] id = _get_or_bust(data_dict, "id") session = context['session'] schema = context.get('schema') or schema_.default_update_related_schema() related = model.Related.get(id) context["related"] = related if not related: logging.error('Could not find related ' + id) raise NotFound(_('Item was not found.')) _check_access('related_update', context, data_dict) data, errors = _validate(data_dict, schema, context) if errors: model.Session.rollback() raise ValidationError(errors) related = model_save.related_dict_save(data, context) dataset_dict = None if 'package' in context: dataset = context['package'] dataset_dict = ckan.lib.dictization.table_dictize(dataset, context) related_dict = model_dictize.related_dictize(related, context) activity_dict = { 'user_id': context['user'], 'object_id': related.id, 'activity_type': 'changed related item', } activity_dict['data'] = { 'related': related_dict, 'dataset': dataset_dict, } activity_create_context = { 'model': model, 'user': context['user'], 'defer_commit': True, 'ignore_auth': True, 'session': session } # DGU checks if activity streams are enabled first, to avoid Auth Audit # issue #1421 if converters.asbool( config.get('ckan.activity_streams_enabled', 'true')): _get_action('activity_create')(activity_create_context, activity_dict) if not context.get('defer_commit'): model.repo.commit() return model_dictize.related_dictize(related, context)
def related_create(context, data_dict): '''Add a new related item to a dataset. You must provide your API key in the Authorization header. :param title: the title of the related item :type title: string :param type: the type of the related item, e.g. ``'Application'``, ``'Idea'`` or ``'Visualisation'`` :type type: string :param id: the id of the related item (optional) :type id: string :param description: the description of the related item (optional) :type description: string :param url: the URL to the related item (optional) :type url: string :param image_url: the URL to the image for the related item (optional) :type image_url: string :param dataset_id: the name or id of the dataset that the related item belongs to (optional) :type dataset_id: string :returns: the newly created related item :rtype: dictionary ''' model = context['model'] session = context['session'] user = context['user'] userobj = model.User.get(user) _check_access('related_create', context, data_dict) data_dict["owner_id"] = userobj.id data, errors = _validate(data_dict, ckan.logic.schema.default_related_schema(), context) if errors: model.Session.rollback() raise ValidationError(errors) related = model_save.related_dict_save(data, context) if not context.get('defer_commit'): model.repo.commit_and_remove() if 'dataset_id' in data_dict: dataset = model.Package.get(data_dict['dataset_id']) dataset.related.append( related ) model.repo.commit_and_remove() session.flush() related_dict = model_dictize.related_dictize(related, context) activity_dict = { 'user_id': userobj.id, 'object_id': related.id, 'activity_type': 'new related item', } activity_dict['data'] = { 'related': related_dict } activity_create_context = { 'model': model, 'user': user, 'defer_commit':True, 'session': session } activity_create(activity_create_context, activity_dict, ignore_auth=True) session.commit() context["related"] = related context["id"] = related.id log.debug('Created object %s' % related.title) return related_dict
def related_update(context, data_dict): '''Update a related item. You must be the owner of a related item to update it. For further parameters see ``related_create()``. :param id: the id of the related item to update :type id: string :returns: the updated related item :rtype: dictionary ''' model = context['model'] id = _get_or_bust(data_dict, "id") session = context['session'] schema = context.get('schema') or schema_.default_related_schema() related = model.Related.get(id) context["related"] = related if not related: logging.error('Could not find related ' + id) raise NotFound(_('Item was not found.')) _check_access('related_update', context, data_dict) data, errors = _validate(data_dict, schema, context) if errors: model.Session.rollback() raise ValidationError(errors) related = model_save.related_dict_save(data, context) dataset_dict = None if 'package' in context: dataset = context['package'] dataset_dict = ckan.lib.dictization.table_dictize(dataset, context) related_dict = model_dictize.related_dictize(related, context) activity_dict = { 'user_id': context['user'], 'object_id': related.id, 'activity_type': 'changed related item', } activity_dict['data'] = { 'related': related_dict, 'dataset': dataset_dict, } activity_create_context = { 'model': model, 'user': context['user'], 'defer_commit': True, 'ignore_auth': True, 'session': session } _get_action('activity_create')(activity_create_context, activity_dict) if not context.get('defer_commit'): model.repo.commit() return model_dictize.related_dictize(related, context)
def related_create(context, data_dict): """Add a new related item to a dataset. You must provide your API key in the Authorization header. :param title: the title of the related item :type title: string :param type: the type of the related item, e.g. ``'Application'``, ``'Idea'`` or ``'Visualisation'`` :type type: string :param id: the id of the related item (optional) :type id: string :param description: the description of the related item (optional) :type description: string :param url: the URL to the related item (optional) :type url: string :param image_url: the URL to the image for the related item (optional) :type image_url: string :param dataset_id: the name or id of the dataset that the related item belongs to (optional) :type dataset_id: string :returns: the newly created related item :rtype: dictionary """ model = context["model"] session = context["session"] user = context["user"] userobj = model.User.get(user) _check_access("related_create", context, data_dict) data_dict["owner_id"] = userobj.id data, errors = _validate(data_dict, ckan.logic.schema.default_related_schema(), context) if errors: model.Session.rollback() raise ValidationError(errors) related = model_save.related_dict_save(data, context) if not context.get("defer_commit"): model.repo.commit_and_remove() dataset_dict = None if "dataset_id" in data_dict: dataset = model.Package.get(data_dict["dataset_id"]) dataset.related.append(related) model.repo.commit_and_remove() dataset_dict = ckan.lib.dictization.table_dictize(dataset, context) session.flush() related_dict = model_dictize.related_dictize(related, context) activity_dict = {"user_id": userobj.id, "object_id": related.id, "activity_type": "new related item"} activity_dict["data"] = {"related": related_dict, "dataset": dataset_dict} activity_create_context = { "model": model, "user": user, "defer_commit": True, "ignore_auth": True, "session": session, } logic.get_action("activity_create")(activity_create_context, activity_dict) session.commit() context["related"] = related context["id"] = related.id log.debug("Created object %s" % related.title) return related_dict