Пример #1
0
def follow_user(context, data_dict):
    """Start following another user.

    You must provide your API key in the Authorization header.

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

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

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

    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 users"))

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

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

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

    # Don't let a user follow herself.
    if userobj.id == validated_data_dict["id"]:
        message = _("You cannot follow yourself")
        raise ValidationError({"message": message}, error_summary=message)

    # Don't let a user follow someone she is already following.
    if model.UserFollowingUser.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_user_dict_save(validated_data_dict, context)

    activity_dict = {"user_id": userobj.id, "object_id": validated_data_dict["id"], "activity_type": "follow user"}
    activity_dict["data"] = {
        "user": ckan.lib.dictization.table_dictize(model.User.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 user {object}".format(
            follower=follower.follower_id, object=follower.object_id
        )
    )

    return model_dictize.user_following_user_dictize(follower, context)
Пример #2
0
def follow_user(context, data_dict):
    '''Start following another user.

    You must provide your API key in the Authorization header.

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

    :returns: a representation of the 'follower' relationship between yourself
        and the other user
    :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_user_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 herself.
    if userobj.id == data_dict['id']:
        message = _('You cannot follow yourself')
        raise ValidationError({'message': message})

    # Don't let a user follow someone she is already following.
    if model.UserFollowingUser.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_user_dict_save(data_dict, context)

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

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

    return model_dictize.user_following_user_dictize(follower, context)