示例#1
0
def downvote():
    """Decrease the vote of the article."""

    article_id = request.args.get('article_id')

    if not article_id:
        return redirect(url_for('mod_main.all_feeds'))

    article = FeedArticle.query.get(article_id)
    if not article:
        abort(404)

    vote = FeedVote.query.get((g.current_user.id, article_id))

    # No vote from the user yet, add one
    if not vote:
        vote = FeedVote(
            user_id=g.current_user.id,
            article_id=article_id,
            is_upvote=False)
        article.downvote += 1
        article.update_wilson_score()
        db.session.add(vote)
    else:
        if vote.is_upvote:
            vote.is_upvote = False
            article.upvote -= 1
            article.downvote += 1
            article.update_wilson_score()

    db.session.commit()
    return jsonify({
        'article_id': article_id,
        'downvote': article.downvote})
示例#2
0
def upvote():
    """Increase the vote of the article"""

    article_id = request.args.get('article_id')

    if not article_id:
        abort(404)

    article = FeedArticle.query.get(article_id)
    if not article:
        abort(404)

    vote = FeedVote.query.get((g.current_user.id, article_id))

    # No vote from the user yet, add one
    if not vote:
        vote = FeedVote(
            user_id=g.current_user.id,
            article_id=article_id,
            is_upvote=True)

        article.upvote += 1
        article.update_wilson_score()
        db.session.add(vote)
    else:
        if not vote.is_upvote:
            vote.is_upvote = True
            article.upvote += 1
            article.downvote -= 1
            article.update_wilson_score()
    db.session.commit()

    return jsonify({
        'article_id': article_id,
        'upvote': article.upvote})
def upvote():
    """Increase the vote of the article."""

    article_id = request.args.get('article_id')

    if not article_id:
        return redirect(url_for('mod_main.all_feeds'))

    article = FeedArticle.query.get(article_id)
    if not article:
        abort(404)

    vote = FeedVote.query.get((current_user.id, article_id))

    # No vote from the user yet, add one
    if not vote:
        vote = FeedVote(
            user_id=current_user.id,
            article_id=article_id,
            is_upvote=True)

        article.upvote += 1
        article.update_wilson_score()
        db.session.add(vote)
    else:
        if not vote.is_upvote:
            vote.is_upvote = True
            article.upvote += 1
            article.downvote -= 1
            article.update_wilson_score()

    return redirect(url_for('mod_feed.index'))