def get_answer(id): sql = "Select * from answers where id=%s" cursor = connection.cursor() cursor.execute(sql, [id]) record = cursor.fetchone() answer = Answers(id=record[0], description=record[1], is_correct=record[3]) return answer
def get_all_answers_for_question(question_id): sql = "Select * from answers where question_id=%s" cursor = connection.cursor() cursor.execute(sql, [question_id]) records = cursor.fetchall() answers = [] for record in records: answer = Answers(id=record[0], description=record[1], is_correct=record[3]) answers.append(answer) return answers
def question_details(self): inputs = self.getInput() try: #import pdb;pdb.set_trace() question = Questions.get(Questions.id == inputs['id']) self.privData['QUESTION_DETAIL'] =question albumList = Albums.select().where(Albums.question ==inputs['id']) self.privData['ALUBUM'] =albumList answersList=Answers.select().where(Answers.question ==inputs['id']) self.privData['ANSWERS_LIST'] =answersList return self.display('question-details') except Exception, e: return self.error(msg='获取问题详情失败!')
def questions(self): try: #import pdb;pdb.set_trace() questionList = Questions.select().order_by(Questions.id.desc()) Listtemp=[] for item in questionList: length = Answers.select().where(item.id ==Answers.question).count() Listtemp.append([item,length]) self.privData['QUESTIONS_LIST'] = Listtemp return self.display('questions-list') except Exception,e: print e return self.error(msg='获取问题列表信息失败!')
def answer_question(self): inputs = self.getInput() try: #import pdb;pdb.set_trace() content = inputs['content'] user = Users.get(Users.name == self.isLogin()) answer = Answers.create( owner = user, content = content, question = inputs['id'], ) answer.save() return web.seeother(self.makeUrl('/wap/question_details', {'id':inputs['id']})) except Exception,e: print e return self.error(msg='回复问题失败!')
import unittest from models.answers import Answers from daos.answers_dao import AnswersDAO from daos.daos_impl.answers_dao_impl import AnswersDaoImpl from models.questions import Questions from daos.questions_dao import QuestionsDAO from daos.daos_impl.questions_dao_impl import QuestionDaoImpl answer_dao = AnswersDaoImpl() question_dao = QuestionDaoImpl() test_answer = Answers(0, 'Test answer', False) test_question = Questions(1, 1, 'What is the speed of light?') class AnswerTest(unittest.TestCase): def test_get_all_answers_for_question(self): retrieved_answer = answer_dao.get_all_answers_for_question( test_question.id) assert all(retrieved_answer) def test_create_answer(self): new_answer = answer_dao.create_answer(test_answer, test_question.id, False) assert new_answer == True