Example #1
0
def json_comment_submit():
    u = get_user()
    if u:
        data = request.get_json(force=True)
        c = Comment()
        c.body = data["comment"]
        c.post_id = data["post_id"]
        c.parent_id = data["parent_id"]
        c.user_id = u.id
        c.user_display_name = u.display_name
        c.timestamp = datetime.datetime.utcnow()
        data = []
        if c.validate():
            db.session.add(c)
            db.session.commit()
            data.append({'commentid': c.id})
            # add vote
            v = Vote()
            v.user_id = u.id
            v.post_id = c.post_id
            v.comment_id = c.id
            v.vote = 1
            v.timestamp = datetime.datetime.utcnow()
            db.session.add(v)
            db.session.commit()
        else:
            data.append({'error': 'no data.'})
    else:
        data = [{'error': 'user not authenticated.'}]

    return jsonify(result=data)
Example #2
0
def ajax_comment_vote():
    u = get_user()
    if u:
        post_id = request.args.get('post_id')
        comment_id = request.args.get('comment_id')
        direction = request.args.get('direction')
        if id and direction:
            # check for existing vote
            old_vote = Vote.query.filter_by(user_id=u.id, comment_id=comment_id).first()
            if old_vote:
                if old_vote.vote != direction:
                    old_vote.vote = direction
                    old_vote.timestamp = datetime.datetime.utcnow()
                    db.session.add(old_vote)
                    db.session.commit()
                return str(old_vote.id)
            v = Vote()
            v.user_id = u.id
            v.post_id = post_id
            v.comment_id = comment_id
            v.vote = direction
            v.timestamp = datetime.datetime.utcnow()
            db.session.add(v)
            db.session.commit()
            return str(v.id)
        else:
            return "error"
    else:
        return 'error : user not authenticated.'