def analyzeQuestion(self,question): answers=[] try: answers=dbhelper.fetchAnswersOf(question) except dbhelper.InvalidIdError: raise #TODO must check answers size correctAnswereeThetas={} totalAnswereeThetas={} #initialize all possible estimation thetas for j in range(0,10,1): correctAnswereeThetas[float(j)]=0.0 totalAnswereeThetas[float(j)]=0.0 for answer in answers: #find number of people who gave this answer, make it distinct for user, meaning only one answer by a given user will be tabulated query=AnsweredQuestion.query(AnsweredQuestion.answer==ndb.Key('Answer',answer.key.id()),projection=[AnsweredQuestion.user],distinct=True) givenAnswers=query.fetch() #who are the people who answered this question? for givenAnswer in givenAnswers: who=EstimationCredentials.query(EstimationCredentials.user==givenAnswer.user) #TODO check if there is only one credential(not implemented just now) #find theta of this person theta=who.get().estimatedTheta totalAnswereeThetas[theta]=totalAnswereeThetas[theta]+1.0 if answer.correct: #increment the specific estimatedTheta counter by 1 correctAnswereeThetas[theta]=correctAnswereeThetas[theta]+1.0 #normalize correctAnswerrThetas for j in range(0,10,1): if totalAnswereeThetas[float(j)]!=0: #ensures we don't divide by zero correctAnswereeThetas[float(j)]=correctAnswereeThetas[float(j)]/totalAnswereeThetas[float(j)] return (answers, correctAnswereeThetas, totalAnswereeThetas)
def get(self,q_id): #get the current user user=users.get_current_user() if not user: #if user object is None i.e. No user can be determined #redirect to the user authentication page self.redirect(users.create_login_url(self.request.uri)) question=None answers=[] try: question=dbhelper.fetchQuestion(int(q_id)) answers=dbhelper.fetchAnswersOf(question) except dbhelper.InvalidIdError: question=Question(question="Could not find this question") #should ideally redirect to 404 page #Values to pass to the View component vals={'question':question,'answers':answers,'current_user':user} #render to the template template=jinjaEnv.get_template('answerQuestion.html') #write it to the handler's output stream self.response.out.write(template.render(vals))