Ejemplo n.º 1
0
def vote(request):
    s = request.session
    p = request.session['safe_post']
    dbsession = DBSession()
    if 'logged_in' in s:
        way = request.matchdict['way']
        if way == 'up':
            points = 1
        elif way == 'down':
            points = -1

        comment_id = None
        if 'target_type' in p and p['target_type'] == 'comment':
            # the post comes in with comment id in sub_id spot
            # here, we make sub_id the real sub_id
            sub_id = submission.get_comment_parent_story(p['sub_id'])[0]
            comment_id = p['sub_id']
            vote_list = dbsession.query(Vote).filter(
                Vote.comment_id == comment_id).filter(
                    Vote.user_id == s['users.id']).all()
        else:
            sub_id = p['sub_id']
            vote_list = dbsession.query(Vote).filter(
                Vote.submission_id == p['sub_id']).filter(
                    Vote.comment_id == None).filter(
                        Vote.user_id == s['users.id']).all()

        # find out if the user has already voted on this submission
        if len(vote_list) > 0:
            if vote_list[0].direction == points:
                return {
                    'message': 'You have already voted on this submission.',
                    'code': 'EOLDVOTE',
                    'success': False
                }
            else:
                dbsession.delete(vote_list[0])

        v = Vote(sub_id, s['users.id'], points, p['target_type'], comment_id)
        v.direction = points
        dbsession = DBSession()
        dbsession.add(v)
        return HTTPFound(p['jump_to'])
    else:
        return {
            'message': 'Sorry, you are not logged in.',
            'code': 'ENOLOGIN',
            'success': False
        }
Ejemplo n.º 2
0
def vote(request):
    s = request.session
    p = request.session["safe_post"]
    dbsession = DBSession()
    if "logged_in" in s:
        way = request.matchdict["way"]
        if way == "up":
            points = 1
        elif way == "down":
            points = -1

        comment_id = None
        if "target_type" in p and p["target_type"] == "comment":
            # the post comes in with comment id in sub_id spot
            # here, we make sub_id the real sub_id
            sub_id = queries.get_comment_parent_story(p["sub_id"])[0]
            comment_id = p["sub_id"]
            vote_list = (
                dbsession.query(Vote).filter(Vote.comment_id == comment_id).filter(Vote.user_id == s["users.id"]).all()
            )
        else:
            sub_id = p["sub_id"]
            vote_list = (
                dbsession.query(Vote)
                .filter(Vote.submission_id == p["sub_id"])
                .filter(Vote.comment_id == None)
                .filter(Vote.user_id == s["users.id"])
                .all()
            )

        # find out if the user has already voted on this submission
        if len(vote_list) > 0:
            if vote_list[0].direction == points:
                return {"message": "You have already voted on this submission.", "code": "EOLDVOTE", "success": False}
            else:
                dbsession.delete(vote_list[0])

        v = Vote(sub_id, s["users.id"], points, p["target_type"], comment_id)
        v.direction = points
        dbsession = DBSession()
        dbsession.add(v)
        return HTTPFound(p["jump_to"])
    else:
        return {"message": "Sorry, you are not logged in.", "code": "ENOLOGIN", "success": False}
Ejemplo n.º 3
0
def vote(request):
    s = request.session
    p = request.session['safe_post']
    dbsession = DBSession()
    if 'logged_in' in s:
        way = request.matchdict['way']
        if way == 'up':
            points = 1
        elif way == 'down':
            points = -1

        comment_id = None
        if 'target_type' in p and p['target_type'] == 'comment':
            # the post comes in with comment id in sub_id spot
            # here, we make sub_id the real sub_id
            sub_id = submission.get_comment_parent_story(p['sub_id'])[0]
            comment_id = p['sub_id']
            vote_list = dbsession.query(Vote).filter(Vote.comment_id == comment_id).filter(Vote.user_id == s['users.id']).all()
        else:
            sub_id = p['sub_id']
            vote_list = dbsession.query(Vote).filter(Vote.submission_id == p['sub_id']).filter(Vote.comment_id == None).filter(Vote.user_id == s['users.id']).all()

        # find out if the user has already voted on this submission
        if len(vote_list) > 0:
            if vote_list[0].direction == points:
                return {'message': 'You have already voted on this submission.', 'code': 'EOLDVOTE', 'success': False}
            else:
                dbsession.delete(vote_list[0])

        v = Vote(sub_id, s['users.id'], points, p['target_type'], comment_id)
        v.direction = points
        dbsession = DBSession()
        dbsession.add(v)
        return HTTPFound(p['jump_to'])
    else:
        return {'message': 'Sorry, you are not logged in.', 'code': 'ENOLOGIN', 'success': False}