示例#1
0
def review_vote_put_handler(review_id, user):
    """Set your vote for a specified review.

    **OAuth scope:** vote

    :json boolean vote: ``true`` if upvote, ``false`` if downvote

    :statuscode 200: success
    :statuscode 400: invalid request (see source)
    :statuscode 403: daily vote limit exceeded
    :statuscode 404: review not found

    :resheader Content-Type: *application/json*
    """
    def fetch_params():
        vote = Parser.bool('json', 'vote')
        return vote

    review = Review.query.get_or_404(str(review_id))
    if review.is_hidden:
        raise NotFound("Review has been hidden.")
    vote = fetch_params()
    if review.user_id == user.id:
        raise InvalidRequest(desc='You cannot rate your own review.')
    if user.is_vote_limit_exceeded is True and user.has_voted(review) is False:
        raise LimitExceeded('You have exceeded your limit of votes per day.')
    Vote.create(user, review, vote)  # overwrites an existing vote, if needed
    return jsonify(message='Request processed successfully')
示例#2
0
def review_vote_put_handler(review_id, user):
    """Set your vote for a specified review.

    **OAuth scope:** vote

    **Request Example:**

    .. code-block:: bash

        $ curl "https://critiquebrainz.org/ws/1/review/9cb11424-d070-4ac1-8771-a8703ae5cccd/vote" \\
               -X PUT \\
               -H "Content-type: application/json" \\
               -H "Authorization: Bearer <access token>" \\
               -d '{"vote":true}'

    **Response Example:**

    .. code-block:: json

        {
          "message": "Request processed successfully"
        }

    :json boolean vote: ``true`` if upvote, ``false`` if downvote

    **NOTE:** Voting on reviews without text is not allowed.

    :statuscode 200: success
    :statuscode 400: invalid request (see source)
    :statuscode 403: daily vote limit exceeded
    :statuscode 404: review not found

    :resheader Content-Type: *application/json*
    """
    def fetch_params():
        vote = Parser.bool('json', 'vote')
        return vote

    review = get_review_or_404(review_id)
    if review["is_hidden"]:
        raise NotFound("Review has been hidden.")
    vote = fetch_params()
    if str(review["user_id"]) == user.id:
        raise InvalidRequest(desc='You cannot rate your own review.')
    if review["text"] is None:
        raise InvalidRequest(
            desc='Voting on reviews without text is not allowed.')
    if user.is_vote_limit_exceeded and not db_users.has_voted(
            user.id, review_id):
        raise LimitExceeded('You have exceeded your limit of votes per day.')

    db_vote.submit(
        user_id=user.id,
        revision_id=review["last_revision"]["id"],
        vote=vote,  # overwrites an existing vote, if needed
    )

    return jsonify(message='Request processed successfully')
示例#3
0
def review_post_handler(user):
    """Publish a review.

    **OAuth scope:** review

    :reqheader Content-Type: *application/json*

    :json uuid entity_id: UUID of the entity that is being reviewed
    :json string entity_type: One of the supported reviewable entities. 'release_group' or 'event' etc.
    :json string text: Text part of review, min length is 25, max is 5000 **(optional)**
    :json integer rating: Rating part of review, min is 1, max is 5 **(optional)**
    :json string license_choice: license ID
    :json string lang: language code (ISO 639-1), default is ``en`` **(optional)**
    :json boolean is_draft: whether the review should be saved as a draft or not, default is ``False`` **(optional)**

    **NOTE:** You must provide some text or rating for the review.

    :resheader Content-Type: *application/json*
    """

    def fetch_params():
        is_draft = Parser.bool('json', 'is_draft', optional=True) or False
        if is_draft:
            REVIEW_TEXT_MIN_LENGTH = None
        entity_id = Parser.uuid('json', 'entity_id')
        entity_type = Parser.string('json', 'entity_type', valid_values=ENTITY_TYPES)
        text = Parser.string('json', 'text', min=REVIEW_TEXT_MIN_LENGTH, max=REVIEW_TEXT_MAX_LENGTH, optional=True)
        rating = Parser.int('json', 'rating', min=REVIEW_RATING_MIN, max=REVIEW_RATING_MAX, optional=True)
        license_choice = Parser.string('json', 'license_choice')
        language = Parser.string('json', 'language', min=2, max=3, optional=True) or 'en'
        if text is None and rating is None:
            raise InvalidRequest(desc='Review must have either text or rating')
        if language and language not in supported_languages:
            raise InvalidRequest(desc='Unsupported language')
        if db_review.list_reviews(user_id=user.id, entity_id=entity_id)[1]:
            raise InvalidRequest(desc='You have already published a review for this album')
        return entity_id, entity_type, text, rating, license_choice, language, is_draft

    if user.is_review_limit_exceeded:
        raise LimitExceeded('You have exceeded your limit of reviews per day.')
    entity_id, entity_type, text, rating, license_choice, language, is_draft = fetch_params()
    review = db_review.create(
        user_id=user.id,
        entity_id=entity_id,
        entity_type=entity_type,
        text=text,
        rating=rating,
        license_id=license_choice,
        language=language,
        is_draft=is_draft,
    )
    return jsonify(message='Request processed successfully', id=review["id"])
示例#4
0
def review_post_handler(user):
    """Publish a review.

    **OAuth scope:** review

    :reqheader Content-Type: *application/json*

    :json uuid entity_id: UUID of the release group that is being reviewed
    :json string entity_type: One of the supported reviewable entities. 'release_group' or 'event' etc.
    :json string text: review contents, min length is 25, max is 5000
    :json string license_choice: license ID
    :json string lang: language code (ISO 639-1), default is ``en`` **(optional)**
    :json boolean is_draft: whether the review should be saved as a draft or not, default is ``False`` **(optional)**

    :resheader Content-Type: *application/json*
    """
    def fetch_params():
        is_draft = Parser.bool('json', 'is_draft', optional=True) or False
        if is_draft:
            REVIEW_MIN_LENGTH = None
        entity_id = Parser.uuid('json', 'entity_id')
        entity_type = Parser.string('json',
                                    'entity_type',
                                    valid_values=ENTITY_TYPES)
        text = Parser.string('json',
                             'text',
                             min=REVIEW_MIN_LENGTH,
                             max=REVIEW_MAX_LENGTH)
        license_choice = Parser.string('json', 'license_choice')
        language = Parser.string(
            'json', 'language', min=2, max=3, optional=True) or 'en'
        if language and language not in supported_languages:
            raise InvalidRequest(desc='Unsupported language')
        if Review.query.filter_by(user=user, entity_id=entity_id).count():
            raise InvalidRequest(
                desc='You have already published a review for this album')
        return entity_id, entity_type, text, license_choice, language, is_draft

    if user.is_review_limit_exceeded:
        raise LimitExceeded('You have exceeded your limit of reviews per day.')
    entity_id, entity_type, text, license_choice, language, is_draft = fetch_params(
    )
    review = Review.create(user=user,
                           entity_id=entity_id,
                           entity_type=entity_type,
                           text=text,
                           license_id=license_choice,
                           language=language,
                           is_draft=is_draft)
    return jsonify(message='Request processed successfully', id=review.id)