def test_blank_name(self): question = Question() question.text = 'Blank test question' question.answers = [] try: question.save() except KeyError, e: print 'Adding blank question failed: %s' % e
def test_creation(self): q = {'name': 'q1', 'text': 'Random question one, choose a.', 'answers': [{'value': 'a', 'text': 'a'}, {'value': 'b', 'text': 'b'}, {'value': 'c', 'text': 'c'},{'value': 'd', 'text': 'd'}] } question = Question(q) assert question.name == q['name'] and question.text == q['text'] and question.answers == q['answers'] question.save()
def test_duplicate(self): q = {'name': 'q2', 'text': 'Random question one, choose a.', 'answers': [{'value': 'a', 'text': 'a'}, {'value': 'b', 'text': 'b'}, {'value': 'c', 'text': 'c'},{'value': 'd', 'text': 'd'}] } question = Question(q) assert question.name == q['name'] and question.text == q['text'] and question.answers == q['answers'] question.save() try: question.save() except KeyError, e: print 'Trying to save duplicate: %s' % e
def import_data(questions, categories, answers): categories_dict = {} num_lines = sum(1 for line in open(categories)) with open(categories, newline="") as csv_file: reader = csv.reader(csv_file) for row in tqdm(reader, desc="Reading categories", total=num_lines): id_ = int(row[0]) category = row[2] categories_dict[id_] = category if questions != "skip": num_lines = sum(1 for line in open(questions)) with open(questions, newline="") as csv_file: reader = csv.reader(csv_file) it = tqdm(reader, desc="Reading questions", total=num_lines) for i, row in enumerate(it): try: id_ = int(row[0]) category_id = int(row[3]) question = Question(meta={"id": id_}) question.date = row[1] question.category = categories_dict[category_id] question.title = row[4] question.body = "\n".join(row[5:]) question.save() except (IndexError, ValueError): continue if answers != "skip": with open(answers, newline="") as csv_file: reader = csv.reader(csv_file) it = tqdm(reader, desc="Reading answers") for i, row in enumerate(it): try: question_id = int(row[3]) question = Question.get(id=question_id) if question.answers is None: question.answers = row[4] else: question.answers += "\n\n" + row[4] question.save() except (IndexError, ValueError, NotFoundError): continue
def questions(): form = NewQuestionForm(request.form) if request.method == 'POST' and form.validate(): question = Question(form.category.data, form.question.data, form.option_a.data, form.option_b.data, form.option_c.data, form.option_d.data, form.answer.data) question.save() flash('Question successfully created!', 'positive') return render_template('question_created.html', question=question, amount=Question.size()) elif request.method == 'POST' and not form.validate(): flash('Oops, your submitted question appears to be invalid.', 'negative') return render_template('question_new.html', form=form, amount=Question.size()) else: return render_template('invalid_request.html')
def extractClue(self, clue, game, categories, i): clueDiv = clue.find("div") clueText = clue.find("td", {"class": "clue_text"}).get_text() if i == 3: category = categories order = 61 row = 0 else: coords = clue.find("td", { "class": "clue_text" }).attrs["id"].split('_') category = categories[int(coords[2]) - 1] row = coords[3] order = clueDiv.find("td", { "class": "clue_order_number" }).get_text() question = clueDiv.attrs["onmouseout"] answer = clueDiv.attrs["onmouseover"] answer = answer[7:-1].split('\', \'') #Get rid of the quotes surrounding the question answerText = answer[2][:-1].replace("\\", "") answerObj = BeautifulSoup(str(answerText), "lxml") #Weird problem: Can't get an answer for these... if answerObj.find("em", {"class": "correct_response"}) is None: print("NO ANSWER FOUND!") return answerText = answerObj.find("em", { "class": "correct_response" }).get_text() notes = None amount = None if i == 3: #Final Jeopardy amount = 0 else: #Find dollar values if clue.find("td", {"class": "clue_value_daily_double"}) != None: #DAILY DUBBBBLE! notes = "DD" amount = self.formatDollars( clue.find("td", { "class": "clue_value_daily_double" }).get_text()) else: amount = self.formatDollars( clue.find("td", { "class": "clue_value" }).get_text()) question = Question(None, game, i, category, row, order, clueText, answerText, amount, notes) question = question.save(self.db) #Get the answers if answerObj.find("td", {"class": "right"}) is not None: #Someone answered it right! name = answerObj.find("td", {"class": "right"}).get_text() rightPlayer = self.byShortname(game.players, name) answer = Answer(question, rightPlayer, "true") answer.save(self.db) wrongs = answerObj.findAll("td", {"class": "wrong"}) for wrong in wrongs: if wrong.get_text().lower() != "triple stumper": wrongPlayer = self.byShortname(game.players, wrong.get_text()) answer = Answer(question, wrongPlayer, "false") answer.save(self.db)