コード例 #1
0
ファイル: create.py プロジェクト: ephekt/ckan
def follow_dataset(context, data_dict):
    '''Start following a dataset.

    You must provide your API key in the Authorization header.

    :param id: the id or name of the dataset to follow, e.g. ``'warandpeace'``
    :type id: string

    :returns: a representation of the 'follower' relationship between yourself
        and the dataset
    :rtype: dictionary

    '''

    if not context.has_key('user'):
        raise logic.NotAuthorized(
                _("You must be logged in to follow a dataset."))

    model = context['model']
    session = context['session']

    userobj = model.User.get(context['user'])
    if not userobj:
        raise logic.NotAuthorized(
                _("You must be logged in to follow a dataset."))

    schema = (context.get('schema')
            or ckan.logic.schema.default_follow_dataset_schema())

    validated_data_dict, errors = _validate(data_dict, schema, context)

    if errors:
        model.Session.rollback()
        raise ValidationError(errors)

    # Don't let a user follow a dataset she is already following.
    if model.UserFollowingDataset.is_following(userobj.id,
            validated_data_dict['id']):
        # FIXME really package model should have this logic and provide
        # 'display_name' like users and groups
        pkgobj = model.Package.get(validated_data_dict['id'])
        name = pkgobj.title or pkgobj.name or pkgobj.id
        message = _(
                'You are already following {0}').format(name)
        raise ValidationError({'message': message}, error_summary=message)

    follower = model_save.follower_dict_save(validated_data_dict, context,
            model.UserFollowingDataset)

    if not context.get('defer_commit'):
        model.repo.commit()

    log.debug(u'User {follower} started following dataset {object}'.format(
        follower=follower.follower_id, object=follower.object_id))

    return model_dictize.user_following_dataset_dictize(follower, context)
コード例 #2
0
def follow_dataset(context, data_dict):
    '''Start following a dataset.

    You must provide your API key in the Authorization header.

    :param id: the id or name of the dataset to follow, e.g. ``'warandpeace'``
    :type id: string

    :returns: a representation of the 'follower' relationship between yourself
        and the dataset
    :rtype: dictionary

    '''

    if not 'user' in context:
        raise logic.NotAuthorized(
            _("You must be logged in to follow a dataset."))

    model = context['model']
    session = context['session']

    userobj = model.User.get(context['user'])
    if not userobj:
        raise logic.NotAuthorized(
            _("You must be logged in to follow a dataset."))

    schema = (context.get('schema')
              or ckan.logic.schema.default_follow_dataset_schema())

    validated_data_dict, errors = _validate(data_dict, schema, context)

    if errors:
        model.Session.rollback()
        raise ValidationError(errors)

    # Don't let a user follow a dataset she is already following.
    if model.UserFollowingDataset.is_following(userobj.id,
                                               validated_data_dict['id']):
        # FIXME really package model should have this logic and provide
        # 'display_name' like users and groups
        pkgobj = model.Package.get(validated_data_dict['id'])
        name = pkgobj.title or pkgobj.name or pkgobj.id
        message = _(
            'You are already following {0}').format(name)
        raise ValidationError({'message': message}, error_summary=message)

    follower = model_save.follower_dict_save(validated_data_dict, context,
                                             model.UserFollowingDataset)

    if not context.get('defer_commit'):
        model.repo.commit()

    log.debug(u'User {follower} started following dataset {object}'.format(
        follower=follower.follower_id, object=follower.object_id))

    return model_dictize.user_following_dataset_dictize(follower, context)
コード例 #3
0
ファイル: create.py プロジェクト: netcon-source/ckan
def follow_dataset(context, data_dict):
    """Start following a dataset.

    You must provide your API key in the Authorization header.

    :param id: the id or name of the dataset to follow, e.g. ``'warandpeace'``
    :type id: string

    :returns: a representation of the 'follower' relationship between yourself
        and the dataset
    :rtype: dictionary

    """

    if not context.has_key("user"):
        raise logic.NotAuthorized(_("You must be logged in to follow a dataset."))

    model = context["model"]
    session = context["session"]

    userobj = model.User.get(context["user"])
    if not userobj:
        raise logic.NotAuthorized(_("You must be logged in to follow a dataset."))

    schema = context.get("schema") or ckan.logic.schema.default_follow_dataset_schema()

    validated_data_dict, errors = _validate(data_dict, schema, context)

    if errors:
        model.Session.rollback()
        raise ValidationError(errors)

    # Don't let a user follow a dataset she is already following.
    if model.UserFollowingDataset.is_following(userobj.id, validated_data_dict["id"]):
        message = _("You are already following {0}").format(data_dict["id"])
        raise ValidationError({"message": message}, error_summary=message)

    follower = model_save.user_following_dataset_dict_save(validated_data_dict, context)

    activity_dict = {"user_id": userobj.id, "object_id": validated_data_dict["id"], "activity_type": "follow dataset"}
    activity_dict["data"] = {
        "dataset": ckan.lib.dictization.table_dictize(model.Package.get(validated_data_dict["id"]), context)
    }
    activity_create_context = {"model": model, "user": userobj, "defer_commit": True, "session": session}
    logic.get_action("activity_create")(activity_create_context, activity_dict, ignore_auth=True)

    if not context.get("defer_commit"):
        model.repo.commit()

    log.debug(
        u"User {follower} started following dataset {object}".format(
            follower=follower.follower_id, object=follower.object_id
        )
    )

    return model_dictize.user_following_dataset_dictize(follower, context)
コード例 #4
0
ファイル: create.py プロジェクト: arkka/ckan
def follow_dataset(context, data_dict):
    '''Start following a dataset.

    You must provide your API key in the Authorization header.

    :param id: the id or name of the dataset to follow, e.g. ``'warandpeace'``
    :type id: string

    :returns: a representation of the 'follower' relationship between yourself
        and the dataset
    :rtype: dictionary

    '''

    if not context.has_key('user'):
        raise logic.NotAuthorized

    model = context['model']

    userobj = model.User.get(context['user'])
    if not userobj:
        raise logic.NotAuthorized

    schema = (context.get('schema')
            or ckan.logic.schema.default_follow_dataset_schema())

    data_dict, errors = _validate(data_dict, schema, context)

    if errors:
        model.Session.rollback()
        raise ValidationError(errors, _error_summary(errors))

    # Don't let a user follow a dataset she is already following.
    if model.UserFollowingDataset.get(userobj.id, data_dict['id']) is not None:
        message = _(
                'You are already following {id}').format(id=data_dict['id'])
        raise ValidationError({'message': message})


    follower = model_save.user_following_dataset_dict_save(data_dict, context)

    if not context.get('defer_commit'):
        model.repo.commit()

    log.debug('User {follower} started following dataset {object}'.format(
        follower=follower.follower_id, object=follower.object_id))

    return model_dictize.user_following_dataset_dictize(follower, context)
コード例 #5
0
ファイル: create.py プロジェクト: Big-Data/ckan
def follow_dataset(context, data_dict):
    '''Start following a dataset.

    You must provide your API key in the Authorization header.

    :param id: the id or name of the dataset to follow, e.g. ``'warandpeace'``
    :type id: string

    :returns: a representation of the 'follower' relationship between yourself
        and the dataset
    :rtype: dictionary

    '''

    if not context.has_key('user'):
        raise logic.NotAuthorized(
                _("You must be logged in to follow a dataset."))

    model = context['model']
    session = context['session']

    userobj = model.User.get(context['user'])
    if not userobj:
        raise logic.NotAuthorized(
                _("You must be logged in to follow a dataset."))

    schema = (context.get('schema')
            or ckan.logic.schema.default_follow_dataset_schema())

    validated_data_dict, errors = _validate(data_dict, schema, context)

    if errors:
        model.Session.rollback()
        raise ValidationError(errors)

    # Don't let a user follow a dataset she is already following.
    if model.UserFollowingDataset.is_following(userobj.id,
            validated_data_dict['id']):
        message = _(
                'You are already following {0}').format(data_dict['id'])
        raise ValidationError({'message': message}, error_summary=message)

    follower = model_save.follower_dict_save(validated_data_dict, context,
            model.UserFollowingDataset)

    activity_dict = {
            'user_id': userobj.id,
            'object_id': validated_data_dict['id'],
            'activity_type': 'follow dataset',
            }
    activity_dict['data'] = {
            'dataset': ckan.lib.dictization.table_dictize(
                model.Package.get(validated_data_dict['id']), context),
            }
    activity_create_context = {
        'model': model,
        'user': userobj,
        'defer_commit':True,
        'session': session
    }
    logic.get_action('activity_create')(activity_create_context,
            activity_dict, ignore_auth=True)

    if not context.get('defer_commit'):
        model.repo.commit()

    log.debug(u'User {follower} started following dataset {object}'.format(
        follower=follower.follower_id, object=follower.object_id))

    return model_dictize.user_following_dataset_dictize(follower, context)
コード例 #6
0
def follow_dataset(context, data_dict):
    '''Start following a dataset.

    You must provide your API key in the Authorization header.

    :param id: the id or name of the dataset to follow, e.g. ``'warandpeace'``
    :type id: string

    :returns: a representation of the 'follower' relationship between yourself
        and the dataset
    :rtype: dictionary

    '''

    if not context.has_key('user'):
        raise logic.NotAuthorized

    model = context['model']
    session = context['session']

    userobj = model.User.get(context['user'])
    if not userobj:
        raise logic.NotAuthorized

    schema = (context.get('schema')
              or ckan.logic.schema.default_follow_dataset_schema())

    data_dict, errors = _validate(data_dict, schema, context)

    if errors:
        model.Session.rollback()
        raise ValidationError(errors)

    # Don't let a user follow a dataset she is already following.
    if model.UserFollowingDataset.get(userobj.id, data_dict['id']) is not None:
        message = _('You are already following {id}').format(
            id=data_dict['id'])
        raise ValidationError({'message': message})

    follower = model_save.user_following_dataset_dict_save(data_dict, context)

    activity_dict = {
        'user_id': userobj.id,
        'object_id': data_dict['id'],
        'activity_type': 'follow dataset',
    }
    activity_dict['data'] = {
        'dataset':
        ckan.lib.dictization.table_dictize(model.Package.get(data_dict['id']),
                                           context),
    }
    activity_create_context = {
        'model': model,
        'user': userobj,
        'defer_commit': True,
        'session': session
    }
    logic.get_action('activity_create')(activity_create_context,
                                        activity_dict,
                                        ignore_auth=True)

    if not context.get('defer_commit'):
        model.repo.commit()

    log.debug(u'User {follower} started following dataset {object}'.format(
        follower=follower.follower_id, object=follower.object_id))

    return model_dictize.user_following_dataset_dictize(follower, context)