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 _getOrCreateVote(self, pointRootKey): vote = UserVote.query(UserVote.pointRootKey == pointRootKey, ancestor=self.key).get() if not vote: # No vote yet, create a new one vote = UserVote(pointRootKey=pointRootKey, value=0, ribbon=False, parent=self.key) return vote
def getVoteValues(self, pointRootKey): vote = UserVote.query( UserVote.pointRootKey==pointRootKey, ancestor=self.key).get() if vote: return vote.value, vote.ribbon else: return 0, False
def userVotes(self): if not hasattr(self, "_userVotes"): votes = UserVote.query(ancestor=self.key) # votes = qry.run() self._userVotes = {} if votes: for vote in votes: self._userVotes[vote.pointRootKey] = vote return self._userVotes
def _getOrCreateVote(self, pointRootKey): vote = UserVote.query( UserVote.pointRootKey==pointRootKey, ancestor=self.key).get() if not vote: # No vote yet, create a new one vote = UserVote( pointRootKey=pointRootKey, value=0, ribbon=False, parent=self.key ) return vote
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
def getVoteValue_async(self, pointRootKey): vote = yield UserVote.query( UserVote.pointRootKey==pointRootKey, ancestor=self.key).get_async() raise ndb.Return(vote.value if vote else 0)
def getVoteValue(self, pointRootKey): vote = UserVote.query( UserVote.pointRootKey==pointRootKey, ancestor=self.key).get() return vote.value if vote else 0
def getVoteFuture(self, pointRootKey): return UserVote.query( UserVote.pointRootKey==pointRootKey, ancestor=self.key).get_async()