def vote_video(request, video_id, delta, user_id): delta = int(delta) user_id = int(user_id) video_id = int(video_id) video = get_object_or_404(Video, pk=video_id) if video.status is 'F': return HttpResponse('FAILURE') if delta not in {1, -1}: return HttpResponse('FAILURE') old_vote = UserVote.objects.filter(user_id=user_id, video_id=video_id) if not old_vote: vote = UserVote(user=User.objects.get(pk=user_id), video=Video.objects.get(pk=video_id), delta=delta) vote.save() video.votes += delta elif old_vote[0].delta == 1: if delta == 1: return HttpResponse('FAILURE') else: old_vote[0].delete() video.votes -= 1 else: if delta == -1: return HttpResponse('FAILURE') else: old_vote[0].delete() video.votes += 1 video.save() return HttpResponse('SUCCESS')
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 results(key, expired=None): my_votes = {} my_comments = {} user_comments = {} category = Category.get(key) items = Item.all().ancestor(category).order("-wins").order("losses") count = 0 for i in items: item = ( UserVote.all().ancestor(i).order("-wins").order("losses").filter("voter =", users.get_current_user()).get() ) if not item: my_votes[count] = [i.title, "-", "-"] else: my_votes[count] = [i.title, item.wins, item.losses] # the count helps maintain the order -wins and losses count += 1 for c in items: user_comments[c.title] = UserComment.all().ancestor(c) my_comment = UserComment.all().ancestor(c).filter("commenter =", users.get_current_user()).get() if my_comment: my_comments[c.title] = my_comment.comment return render_template( "results.html", items=items, key=key, title=category.title, owner=category.owner.email(), my_votes=my_votes, my_comments=my_comments, user_comments=user_comments, expired=expired, )
def getAnsweredQuestions(self): if self.answered_ids is None: self.user_votes = UserVote.all().filter('user_username =', self.username) self.answered_ids = [v.question for v in self.user_votes] return Question.get(self.answered_ids)
def compareMP(user): # Calculating MP match in real time. Should do this once, when votes are inserted. questions = Question.all() # Get UserVotes and capture the IDs user_votes = UserVote.all().filter('user_username ='******'question =', u_vote.question) if mp_vote.count() > 0: both_voted_on = both_voted_on + 1 if score == 100: exact_match_on = exact_match_on + 1 score = score + calculate_score(mp_vote[0].selection, u_vote.selection) if user_votes.count() > 0: score = score / user_votes.count() return { 'both_voted_on': both_voted_on, 'exact_match_on': exact_match_on, 'politmus_score': score }
def get(self, username, question_key): response = {} try: vote = UserVote.all().filter('user_username ='******'question =', question_key)[0] response['vote'] = utils.vote_to_dict(vote) except: response['error'] = 'Cannot find username' self.returnJSON(404, response) return self.returnJSON(200, 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, )
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 delete(self, username, question_key): response = {} try: vote = UserVote.all().filter('user_username ='******'question =', question_key)[0] vote.delete() response['status'] = 200 except: response['error'] = 'Cannot find username' self.returnJSON(404, response) return self.returnJSON(200, response)
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 get(self, username): response = {} try: user = User.all().filter('username ='******'user'] = utils.user_to_dict(user) except: self.returnJSON(404, response) return self.query = UserVote.all().filter('user_username ='******'question') self.filterQueryOnParam('selection') response['votes'] = [] for vote in self.query: response['votes'].append(utils.vote_to_dict(vote)) response['total'] = len(response['votes']) self.returnJSON(200, response)
def results(key,expired=None): my_votes = {} my_comments = {} user_comments = {} category = Category.get(key) items = Item.all().ancestor(category).order('-wins').order('losses') count = 0 for i in items: item = UserVote.all().ancestor(i).order('-wins').order('losses').filter('voter =', users.get_current_user()).get() if not item: my_votes[count] = [i.title, "-", "-"] else: my_votes[count] = [i.title, item.wins, item.losses] # the count helps maintain the order -wins and losses count += 1 for c in items: user_comments[c.title] = UserComment.all().ancestor(c) my_comment = UserComment.all().ancestor(c).filter('commenter =',users.get_current_user()).get() if my_comment: my_comments[c.title] = my_comment.comment return render_template('results.html',items=items, key=key, title=category.title, owner=category.owner.email(), my_votes=my_votes, my_comments=my_comments, user_comments=user_comments,expired=expired)
def get_uservote(item): return UserVote.gql("WHERE ANCESTOR IS :1 AND voter = :2",item,users.get_current_user()).get()
def getAnsweredQuestionsFor(username): return UserVote.all().filter('username =', username)
def get_uservote(item): return UserVote.gql("WHERE ANCESTOR IS :1 AND voter = :2", item, users.get_current_user()).get()