Created
October 21, 2015 12:53
-
-
Save UndergroundLabs/674eb7e8dde675789936 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/vote/<int:selfie_id>', methods=['GET']) | |
def vote(selfie_id): | |
selfie = models.Selfie.query.filter_by(id=selfie_id).first() | |
if not selfie: | |
return jsonify(error="Something went wrong.") | |
# Check if the user has already voted on this item | |
this_vote = models.Vote.query.filter_by(user_id=current_user.id, selfie_id=selfie_id).first() | |
if this_vote: | |
return jsonify(error='You have already voted on this selfie. Try again in 24 hours.') | |
last_vote = models.Vote.query \ | |
.filter_by(user_id=current_user.id) \ | |
.order_by(models.Vote.created_at.desc()) \ | |
.first() | |
# Has the user cast a vote? | |
if last_vote: | |
created_at = last_vote.created_at | |
future = created_at + datetime.timedelta(seconds=30) | |
# Has the user cast a vote within the last 30 seconds? | |
if future > datetime.datetime.utcnow(): | |
return jsonify(error='Slow down, you are going too fast.', created_at=created_at) | |
vote = models.Vote(selfie_id=selfie_id, user_id=current_user.id) | |
db.session.add(vote) | |
db.session.commit() | |
return jsonify(success='Your vote has been counted.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment