Advertisement
OlexBy

vote/unvote api calls

Jun 9th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. @issues_bp.route('/<int:issue_id>/vote/', methods=['POST'])
  2. @login_required
  3. def voting(issue_id):
  4. issue = Issue()
  5. vote_filter = db.session.query(Vote). \
  6. filter(and_(Vote.target_id == issue_id, Vote.author_id == g.user.id)
  7. ).all()
  8. if vote_filter:
  9. return jsonify({'msg': 'Already voted'})
  10. else:
  11. vote = Vote(author=g.user, vote=True, target=issue)
  12. vote.target_id = issue_id
  13. db.session.add(vote)
  14. db.session.commit()
  15. return jsonify({}), 201
  16.  
  17.  
  18. @issues_bp.route('/<int:issue_id>/unvote/', methods=['DELETE'])
  19. @login_required
  20. def remove_vote(issue_id):
  21. vote_filter = db.session.query(Vote). \
  22. filter(and_(Vote.target_id == issue_id, Vote.author_id == g.user.id))
  23. result = vote_filter.delete(synchronize_session='fetch')
  24. if result:
  25. db.session.commit()
  26. return jsonify({'status': 0})
  27. else:
  28. db.session.rollback()
  29. return jsonify({'msg': 'vote not found', 'status': 1})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement