def survey(course_key): try: course = ndb.Key(urlsafe=course_key).get() except db.BadKeyError: course = None if course is None: return abort(404) if request.method == 'POST': survey = StudentSurvey(parent=course.key, participant=current_user.key) survey.put() answers = [] questions = ndb.get_multi( [ndb.Key(urlsafe=question) for question in request.form.keys()]) for question, answer in zip(questions, request.form.values()): if question is None: continue if question.question_type == 'closed': answers.append( Answer(question=question.key, int_value=int(answer), parent=survey.key)) else: answers.append( Answer(question=question.key, string_value=answer, parent=survey.key)) ndb.put_multi(answers) deferred.defer(_send_to_keen, survey.key, course.key, [answer.key for answer in answers]) return redirect(url_for('home')) questions = Question.get_active().fetch() numbering = 0 dimension_check = '' for question in questions: if dimension_check != question.dimension: numbering += 1 dimension_check = question.dimension question.dimension = str(numbering) + question.dimension return render_template('survey.haml', questions=questions, course=course)
def responses(class_key, question_key): answers = [] try: class_ = ndb.Key(urlsafe=class_key).get() question = ndb.Key(urlsafe=question_key).get() except db.BadKeyError: class_ = None if class_ is None: return abort(404) surveys = StudentSurvey.query(ancestor=class_.key).fetch() for survey in surveys: answerss = Answer.query(Answer.question == question.key, ancestor=survey.key).fetch() for answer in answerss: answers.append(str(answer.string_value)) return render_template('responses.haml', answers=answers, question=question) return render_template('signup.haml')
def import_question_fixture(fixture): for question in fixture['data']: question_model = Question(question_title=question['question_title']) db.session.add(question_model) for answer in question['answers']: answer_model = Answer(answer_title=answer['answer_title'], correct=answer['correct'], question=question_model) db.session.add(answer_model) db.session.commit()
def create_question(): form = QuestionForm() if form.validate_on_submit(): question = Question(question_title=form.question_title.data) db.session.add(question) for entry in form.answers.entries: answer = Answer(answer_title=entry.data['answer_title'], correct=entry.data['correct'], question=question) db.session.add(answer) db.session.commit() flash('Question successfully added.', 'success') return redirect(url_for('exam_bp.question_list')) return render_template('create_question.html', form=form)
def create_question(self, data): if (data.get('id', None) is None) or (data.get('id') == 0): question = Question() question.text_en = data.get('text_en', '') question.text_ru = data.get('text_ru', '') question.approved = data.get('approved', False) question.image_name = data.get('image_name', '') theme = data.get('theme', None) if isinstance(theme, Theme): question.theme = theme else: print 'theme for question {} not setted'.format( question.__repr__()) for aDict in data.get('answers'): a = Answer() a.text_en = aDict.get('text_en', '') a.text_ru = aDict.get('text_ru', '') a.is_correct = aDict.get('is_correct') question.answers.append(a) # chech if answer.question_id will fill after session.commit() else: question = Question.query.get(data['id']) return question
def create_answers(cls): answers = [] faker = Faker() users = LaskUser.objects.all() questions = Question.objects.all() for _ in range(ANSWERS_COUNT): answers.append( Answer( text=faker.text(), question=random.choice(questions), answer_author=random.choice(users), rating=random.randint(1, 100), )) Answer.objects.bulk_create(answers, ANSWERS_COUNT) stdout_writer.stdout.write("Created test data: ANSWERS")
def answerPage(questionId): question = Question.query.get(questionId) if current_user.is_anonymous: flash('You need to login to answer questions', 'info') return redirect(url_for('auth.loginPage')) if request.form: answerContent = request.form.get('answer') answer = Answer(answerContent=answerContent, questionId=questionId, answerer=current_user) db.session.add(answer) db.session.commit() return redirect(url_for('main.home')) return render_template('answer.html', question=question)
def test_post_survey(self): question_key = Question(question='What do?', number=1, question_type='closed', is_active=True).put() response = self.app.post('/survey', data={question_key.urlsafe(): 4}) self.assertEqual(302, response.status_code) self.assertEqual(1, Answer.query().count())