def _upvote_post(user, post, state, callback=None): votes = filter(lambda x: x.post_id == post.id, user.votes) if len(votes) > 1: raise Exception("There can only be one or 0 in this case :)!!") if len(votes) == 0: vote = Vote(state=0, post_id=post.id) user.votes.append(vote) else: vote = votes[0] if vote.state == -1: vote.state = 0 post.update(dec__downvotes=1) post.user.update(inc__reputation=2) elif vote.state == 0: vote.state = 1 post.update(inc__upvotes=1) post.user.update(inc__reputation=2) elif vote.state == 1: raise Exception("User has already upvoted this post") post.save() post.reload() post.rank = util.ranking.hot(post.upvotes, post.downvotes, post.date_created) if post.rank == 0: post.rank = 1 post.save() user.save() if callback != None: return callback(vote) return vote
def _downvote_reply(user, post, reply, state, callback=None): votes = filter(lambda x: x.post_id == reply.id, user.votes) if len(votes) > 1: raise Exception("There can only be one!!") if len(votes) == 0: vote = Vote(state=0, post_id=reply.id) user.votes.append(vote) else: vote = votes[0] if vote.state == -1: raise Exception("User has already downvoted this post") elif vote.state == 0: vote.state = -1 reply.downvotes += 1 reply.user.update(dec__reputation=2) elif vote.state == 1: vote.state = 0 reply.upvotes -= 1 reply.user.update(dec__reputation=2) reply.rank = util.ranking.confidence(reply.upvotes, reply.downvotes) post.save() user.save() if callback != None: return callback(vote) return vote
def _downvote_post(user,post,state,callback=None): votes = filter(lambda x: x.post_id == post.id, user.votes) if len(votes) > 1: raise Exception("There can only be one!!") if len(votes) == 0: vote = Vote(state=0,post_id=post.id) user.votes.append(vote) else: vote = votes[0] if vote.state == -1: raise Exception("User has already downvoted this post") elif vote.state == 0: vote.state = -1 post.update(inc__downvotes=1) post.user.update(dec__reputation=2) elif vote.state == 1: vote.state = 0 post.update(dec__upvotes=1) post.user.update(dec__reputation=2) post.save() post.reload() post.rank = util.ranking.hot(post.upvotes,post.downvotes,post.date_created) if post.rank == 0: post.rank = 1 post.save() user.save() if callback != None: return callback(vote) return vote
def _downvote_reply(user,post,reply,state,callback=None): votes = filter(lambda x: x.post_id == reply.id, user.votes) if len(votes) > 1: raise Exception("There can only be one!!") if len(votes) == 0: vote = Vote(state=0,post_id=reply.id) user.votes.append(vote) else: vote = votes[0] if vote.state == -1: raise Exception("User has already downvoted this post") elif vote.state == 0: vote.state = -1 reply.downvotes += 1 reply.user.update(dec__reputation=2) elif vote.state == 1: vote.state = 0 reply.upvotes -=1 reply.user.update(dec__reputation=2) reply.rank = util.ranking.confidence(reply.upvotes,reply.downvotes) post.save() user.save() if callback != None: return callback(vote) return vote
def _vote(user, post_id, state, save=True): vote = next((x for x in user.votes if x.id == post_id), None) if vote == None: vote = Vote(post_id=post_id, state=state) user.update(push__votes=vote) vote.state = state if save: user.save()