def vote_submit(review_id): review_id = str(review_id) if 'yes' in request.form: vote = True elif 'no' in request.form: vote = False else: vote = None review = get_review_or_404(review_id) if review["is_hidden"] and not current_user.is_admin(): raise NotFound(gettext("Review has been hidden.")) if review["user"] == current_user: flash.error(gettext("You cannot rate your own review.")) return redirect(url_for('.entity', id=review_id)) if current_user.is_vote_limit_exceeded and not db_users.has_voted( current_user.id, review_id): flash.error(gettext("You have exceeded your limit of votes per day.")) return redirect(url_for('.entity', id=review_id)) if current_user.is_blocked: flash.error( gettext("You are not allowed to rate this review because " "your account has been blocked by a moderator.")) return redirect(url_for('.entity', id=review_id)) db_vote.submit( user_id=current_user.id, revision_id=review["last_revision"]["id"], vote=vote, # overwrites an existing vote, if needed ) flash.success(gettext("You have rated this review!")) return redirect(url_for('.entity', id=review_id))
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')
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')
def test_vote(self): voted = db_users.has_voted(self.user1.id, self.review["id"]) self.assertEqual(voted, True) voted = db_users.has_voted(self.user2.id, self.review["id"]) self.assertEqual(voted, False)