def check_for_user_vote(comment,vote): uservote = UserVote.all().ancestor(comment).filter('author =',users.get_current_user()).get() if not uservote: new_uservote = UserVote(parent=comment.key(), author=users.get_current_user(),vote=vote) new_uservote.put() return True old_uservote = uservote.vote if old_uservote == vote: return False else: uservote.vote = old_uservote + vote uservote.put() return True
def show_category(key): category = Category.get(key) if category.expiration and datetime.now() > category.expiration: return results(key, "This category has expired. Voting is no longer possible.") if not request.form.has_key("item") or request.form.has_key("skip"): random_items = get_random_items(category) return render_template( "category.html", key=key, title=category.title, owner=category.owner.email(), items=random_items ) winner = request.form.get("item") loser = request.form.get("2") if winner == request.form.get("1") else request.form.get("1") winner_item = get_item(category, winner) loser_item = get_item(category, loser) winner_item.wins = winner_item.wins + 1 loser_item.losses = loser_item.losses + 1 uservote_winner = get_uservote(winner_item) uservote_loser = get_uservote(loser_item) if not uservote_winner: uservote_winner = UserVote(parent=winner_item, voter=users.get_current_user(), wins=1) else: uservote_winner.wins = uservote_winner.wins + 1 if not uservote_loser: uservote_loser = UserVote(parent=loser_item, voter=users.get_current_user(), losses=1) else: uservote_loser.losses = uservote_loser.losses + 1 uservote_winner.put() uservote_loser.put() winner_item.put() loser_item.put() random_items = get_random_items(category) return render_template( "category.html", key=key, title=category.title, owner=category.owner.email(), items=random_items, winner=winner, loser=loser, )
def update(self, username, question_key): response = {} allowed_selections = ['aye', 'no', 'dont-care', 'dont-understand'] if self.request.get('selection') not in allowed_selections: logging.debug("hello") response['status'] = 'error' response['error'] = 'You did not send a selection [aye, no, dont-care, dont-understand]' self.returnJSON(406, response) # 406 Not Acceptable return None try: user = User.all().filter('username ='******'error'] = 'Cannot find user or question' self.returnJSON(404, response) return None # Get existing or new question existing = UserVote.all().filter('user_username ='******'question =', question_key) if existing.count() > 0: vote = existing[0] else: logging.debug(question) vote = UserVote(parent=question) vote.question = question_key vote.user_username = user.username vote.constituency = user.constituency vote.selection = self.request.get('selection') vote.put() response['vote'] = utils.vote_to_dict(vote) response['user'] = utils.user_to_dict(user) return response
def show_category(key): category = Category.get(key) if category.expiration and datetime.now() > category.expiration: return results(key,"This category has expired. Voting is no longer possible.") if not request.form.has_key('item') or request.form.has_key('skip'): random_items = get_random_items(category) return render_template('category.html', key=key, title=category.title, owner=category.owner.email(), items=random_items) winner = request.form.get('item') loser = request.form.get('2') if winner == request.form.get('1') else request.form.get('1') winner_item = get_item(category,winner) loser_item = get_item(category,loser) winner_item.wins = winner_item.wins + 1 loser_item.losses = loser_item.losses + 1 uservote_winner = get_uservote(winner_item) uservote_loser = get_uservote(loser_item) if not uservote_winner: uservote_winner = UserVote(parent=winner_item,voter=users.get_current_user(),wins=1) else: uservote_winner.wins = uservote_winner.wins + 1 if not uservote_loser: uservote_loser = UserVote(parent=loser_item,voter=users.get_current_user(),losses=1) else: uservote_loser.losses = uservote_loser.losses + 1 uservote_winner.put() uservote_loser.put() winner_item.put() loser_item.put() random_items = get_random_items(category) return render_template('category.html', key=key, title=category.title, owner=category.owner.email(), items=random_items, winner=winner, loser=loser)