def setRibbon(self, point, ribbonValue, updatePoint=True): pointRootKey = point.key.parent() previousRibbon = False newVote = None if pointRootKey in self.userVotes: # Vote already exists. Update previousRibbon = self.userVotes[pointRootKey].ribbon newVote = self.userVotes[pointRootKey] newVote.ribbon = ribbonValue if not newVote: # Create a brand new one newVote = UserVote( pointRootKey=pointRootKey, value=0, ribbon=ribbonValue, parent=self.key ) newVote.put() if updatePoint: if not previousRibbon and ribbonValue: point.ribbonTotal = point.ribbonTotal + 1 point.put() elif previousRibbon and not ribbonValue: point.ribbonTotal = point.ribbonTotal - 1 point.put() if ribbonValue: point.addNotificationTask(pointRootKey, self.key, "awarded a ribbon to") return newVote
def addVote(self, point, voteValue, updatePoint=True): pointRootKey = point.key.parent() previousVoteValue = 0 newVote = None if pointRootKey in self.userVotes: # Vote already exists. Update previousVoteValue = self.userVotes[pointRootKey].value newVote = self.userVotes[pointRootKey] newVote.value = voteValue if not newVote: # Create a brand new one newVote = UserVote( pointRootKey=pointRootKey, value=voteValue, parent=self.key ) newVote.put() if voteValue == 1: point.addNotificationTask(pointRootKey, self.key, "agreed with") if updatePoint: if previousVoteValue == 0 and voteValue == 1: # UPVOTE point.upVotes = point.upVotes + 1 if previousVoteValue == 0 and voteValue == -1: # DOWNVOTE point.downVotes = point.downVotes + 1 if previousVoteValue == 1 and voteValue == 0: # CANCEL UPVOTE point.upVotes = point.upVotes - 1 if previousVoteValue == -1 and voteValue == 0: # CANCEL DOWNVOTE point.downVotes = point.downVotes - 1 if previousVoteValue == -1 and voteValue == 1: # DOWN TO UP point.downVotes = point.downVotes - 1 point.upVotes = point.upVotes + 1 if previousVoteValue == 1 and voteValue == -1: # UP TO DOWN point.downVotes = point.downVotes + 1 point.upVotes = point.upVotes - 1 point.voteTotal = point.upVotes - point.downVotes point.put() return newVote