def update_votes(item, request_handler, user_id): """ save the vote for an item :param item: {Item} :param request_handler: {BaseHandler} for the request :param user_id: {int} """ try: old_votes = Vote.query(Vote.voter == user_id, Vote.item == item.key) for v in old_votes: v.key.delete() vote = Vote() vote.item = item.key vote.voter = user_id vote.comment = unicode(request_handler.request.get('myComment')) vote.meal_kind = int(request_handler.request.get('kind')) vote.place_style= int(request_handler.request.get('style')) vote.cuisine = Category.get_by_id(request_handler.request.get('cuisine')).key vote_stars = int(request_handler.request.get("voteScore")) vote.stars = vote_stars if vote_stars == 0: vote_untried= bool(request_handler.request.get("voteUntried")) else: vote_untried = False vote.untried = vote_untried vote.put() ndb_models.mark_vote_as_updated(str(vote.key.id()), user_id) logging.info ('update_votes for %s "%s"=%d'% (item.place_name,vote.comment,vote.stars)) except Exception, ex: logging_ext.error("newOrUpdateItem votes exception", exc_info=True) raise
def load_votes(self, user, vote_score, vote_comment): vote = Vote() vote.item = self.item vote.vote = vote_score vote.comment = vote_comment vote.voter = user.key.id() vote.put()
def at_least_one_vote_per_item(self): # make sure each item has 1 vote items = Item.query() for it in items: vote = it.votes.filter("voter =", it.owner).get() if not vote: vote = Vote() vote.item = it vote.vote = VoteValue.VOTE_LIKED vote.voter = it.owner vote.comment = "blah" it.upVotes = 1 vote.put() it.save() if it.votesUp == it.votesDown == 0: if vote.vote == VoteValue.VOTE_LIKED: it.votesUp = 1 elif vote.vote == VoteValue.VOTE_DISLIKED: it.votesDown = -1 it.save()
def post(self): it_id = int(self.request.get('item_id')) it = Item.get_by_id(it_id) voteScore = int(self.request.get("vote")) voteUntried = bool(self.request.get("untried")) my_votes = Vote.query(Vote.voter == self.user_id, Vote.item == it.key) if my_votes.count() == 0: # a new vote new_vote = Vote() new_vote.item = it new_vote.voter = self.user_id else: # roll back the old vote new_vote = my_votes.get() oldVote, oldUntried = new_vote.stars, new_vote.untried new_vote.stars = voteScore new_vote.untried = voteUntried new_vote.comment = self.request.get("comment") new_vote.when = datetime.datetime.now() new_vote.put() it.save() # refresh cache self.response.out.write('OK')