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 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 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 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 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 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 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 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 getAnsweredQuestionsFor(username): return UserVote.all().filter('username =', username)
def getAnsweredQuestionsFor(username): return UserVote.all().filter('username =', username)