def update_score_trx(): delta = 0 # update score entity score = query.get() if not score: if not value: return old_value = 0 score = GSoCScore(parent=data.url_proposal, author=data.ndb_profile.key.to_old_key(), value=value) score.put() delta = 1 else: old_value = score.value if not value: delta = -1 score.delete() else: score.value = value score.put() # update total score for the proposal proposal = db.get(data.url_proposal.key()) proposal.score += value - old_value proposal.nr_scores += delta proposal.put()
def createOrUpdateScore(self, value): """Creates a new score or updates a score if there is already one posted by the current user. If the value passed in is 0 then the Score of the user will be removed and None will be returned. Args: value: The value of the score the user gave as an integer. Returns: The score entity that was created/updated or None if value is 0. """ assert isSet(self.data.proposal) query = db.Query(GSoCScore) query.filter('author = ', self.data.profile) query.ancestor(self.data.proposal) score = query.get() if not score: score = GSoCScore(parent=self.data.proposal, author=self.data.profile, value=value) else: score.value = value proposal_key = self.data.proposal.key() def update_score_trx(score): if score and not score.value: score.delete() else: score.put() # update total score for the proposal proposal = db.get(proposal_key) proposal.score += (value - (score.value if score else 0)) proposal.put() db.run_in_transaction(update_score_trx, score)