Esempio n. 1
0
def vote(topic_id, answer_id):
    form = FlaskForm()
    if form.validate_on_submit():
        answer = PollAnswer.query.filter_by(id=answer_id,
                                            topic_id=topic_id,
                                            deleted=False).first_or_404()
        if current_user.get_vote(answer.topic):
            flash(lazy_gettext('You have already voted for this poll.'))
        else:
            answer.topic.add_vote(current_user, answer)
            flash(lazy_gettext('Your vote has been taken.'))
    return redirect(
        request.args.get('next') or url_for('main.topic', topic_id=topic_id))
Esempio n. 2
0
def vote():
    """All ajax voting routes in one!"""
    entity_cls = classes.get(request.args.get('entity'), None)
    if not entity_cls:
        return 'You messed up, bro.'
    entity_id = request.args.get('id').strip(lowercase)
    if not entity_cls:
        return jsonify({
            'success': False,
            'rollback': False,
            'status': 'not-a-thing'
        })
    if not issubclass(entity_cls, classes['VotableMixin']):
        return jsonify({
            'success': False,
            'rollback': False,
            'status': 'not-votable'
        })
    if not current_user.is_authenticated:
        flash(f"You must login to vote.")
        return jsonify({
            'success': False,
            'rollback': False,
            'status': 'login'
        })
    entity = entity_cls.query.get(entity_id)
    original_weight = entity.weight
    vote = current_user.get_vote(entity)
    if isinstance(entity, classes['Annotation']) and not entity.active:
        flash("You cannot vote on deactivated annotations.")
        return jsonify({
            'success': False,
            'rollback': False,
            'status': 'deactivated'
        })
    up = True if request.args.get('up').lower() == 'true' else False
    status = (entity.upvote(current_user)
              if up else entity.downvote(current_user))
    db.session.commit()
    new_weight = entity.weight
    change = new_weight - original_weight
    status['change'] = change
    return jsonify(status)
Esempio n. 3
0
def topic(topic_id):
    tpc = Topic.query.filter_by(id=topic_id, deleted=False).first_or_404()

    form = CommentForm(current_user) if current_user.can(
        Permission.PARTICIPATE) else None
    if form and form.validate_on_submit():
        tpc.add_comment(current_user, form.body.data)
        flash(lazy_gettext('Your comment has been pushed foward.'))
        return redirect(
            url_for('main.topic',
                    topic_id=topic_id,
                    page=-1,
                    _anchor='comment-last'))

    page = request.args.get('page', 1, type=int)
    if page == -1:
        page = max(
            (tpc.comments_count - 1) // current_app.config['COMMENTS_PER_PAGE']
            + 1, 1)

    pagination = Comment.query.with_entities(Comment, User).join(
        User, Comment.author_id == User.id).filter(
            and_(Comment.topic_id == tpc.id,
                 Comment.deleted == False)).order_by(
                     Comment.created_at.asc()).paginate(
                         page,
                         per_page=current_app.config['COMMENTS_PER_PAGE'],
                         error_out=True)

    user_vote = current_user.get_vote(tpc)
    if tpc.poll and user_vote:
        poll_data = tpc.get_poll_results()
    else:
        poll_data = [(a.id, a.body)
                     for a in tpc.poll_answers.filter_by(deleted=False).all()]

    return render_template('topic.html',
                           topic=tpc,
                           form=form,
                           user_vote=user_vote,
                           poll_data=poll_data,
                           comments=pagination.items,
                           pagination=pagination)