def set_upvote(username, postuid): vote, created = Vote.objects.get_or_create(username=username, postuid=postuid) if created: Vote.objects(username=username, postuid=postuid).update_one(set__upvoted=True, set__downvoted=False)
def upvote(): if 'username' not in session: return 'not logged in' chop = request.form['chop'] chopObject = Post.objects.get(filename=chop) print chopObject.rank vote, created = Vote.objects.get_or_create(username=session['username'], postuid=chopObject.uid) if created: Post.objects(filename=chop).update_one(inc__upvotes=1) chopObject.reload() rank = ranking.hot(chopObject.upvotes, chopObject.downvotes, chopObject.date) Post.objects(filename=chop).update_one(set__rank=rank) Vote.objects(username=session['username'], postuid=chopObject.uid).update_one(set__upvoted=True, set__downvoted=False) return 'upvoted' elif vote.upvoted == False and vote.downvoted == True: Post.objects(filename=chop).update_one(inc__upvotes=1, dec__downvotes=1) #reload post object for updated rank since downvote chopObject.reload() # set rank using algorithm rank = ranking.hot(chopObject.upvotes, chopObject.downvotes, chopObject.date) # Update rank in database Post.objects(filename=chop).update_one(set__rank=rank) Vote.objects(username=session['username'], postuid=chopObject.uid).update_one(set__upvoted=True, set__downvoted=False) return 'upvoted2' else: Vote.objects(username=session['username'], postuid=chopObject.uid).update_one(set__upvoted=True, set__downvoted=False) return 'changed to upvote' print 'Upvoted: ', vote.upvoted print 'Downvoted: ', vote.downvoted