def test_get_survey(self): Question(question='What do?', number=1, question_type='closed', is_active=True).put() Question(question='Shouldn\'t show', number=2, question_type='closed', is_active=False).put() response = self.app.get('/survey') self.assertEqual(200, response.status_code) self.assertTrue('What do?' in response.data) self.assertFalse('Shouldn\'t show' in response.data)
def add_question(): if current_user.user_type != 'admin': return abort(403) form = AddQuestionForm() if form.validate_on_submit(): try: survey = ndb.Key(urlsafe=form.survey.data).get() except db.BadKeyError: survey = None if survey is None: return abort(400) question = Question( question=form.question.data, question_type=form.question_type.data, parent=survey.key, dimension=form.dimension.data, is_active=form.is_active.data, number=(Question.query( parent=survey.key).order(-Question.number).get().number) + 1) question.put() return redirect(url_for('home')) surveys = Survey.query() return render_template('add_question.haml', form=form, surveys=surveys)
def create_question(): """ create new question using POSTed json """ # parse POSTed json: question_created = request.get_json() error = True try: question = Question(**question_created) # insert: db.session.add(question) db.session.commit() error = False except: # rollback: db.session.rollback() error = True finally: db.session.close() if error: abort(500, description="Failed to create new Question") # format: response = jsonify({"success": True}) return response, 201
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 __prepare_question_vote(self, username): with transaction.atomic(): lecture = Lecture.objects.filter(hash='3j7t950').first() participant = Participant(username=username, public_nickname=username) participant.save() lecture.participants.add(participant) question = Question(text='How are you?', tags='hello', creator=participant, event=lecture) question.save() vote = QuestionVote(value=1, voter=participant, question=question) vote.save() lecture.save() return participant, lecture, question
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 faq_callback(): form = QuestionForm() form_success = False items_list = Question.query.filter(Question.anwser.isnot(None)).all() if form.validate_on_submit(): question = Question() form.populate_obj(question) db.session.add(question) db.session.commit() form_success = True return render_template('faq/root.html', question_form=form, items_list=items_list, form_success=form_success)
def create_questions(cls): users = LaskUser.objects.all() tags = Tag.objects.all() faker = Faker() for _ in range(QUESTIONS_COUNT): question = Question( title=faker.sentence(), text=faker.text(), question_author=random.choice(users), rating=random.randint(1, 100), ) question.save() for _ in range(random.randint(1, 4)): question.tags.add(random.choice(tags)) stdout_writer.stdout.write("Created test data: QUESTIONS")
def askQuestion(): if request.form: if current_user.is_anonymous: flash('You have to login to post questions', 'info') return redirect(url_for('main.home')) else: userQuestion = request.form.get('question') questionTag = request.form.get('tags') question = Question(content=userQuestion, tags=questionTag, askerId=current_user.id) db.session.add(question) db.session.commit() return redirect(url_for('main.home'))
def create_exam(): if request.method == 'POST': exam_payload = {} answer_ids = request.form.getlist('answer') question_ids = request.form.getlist('question') questions = Question.query.filter(Question.id.in_(question_ids)).all() for question in questions: exam_payload.update({ question.id: { 'question': question.question_title, 'answers': {} } }) for answer in question.answers: exam_payload[question.id]['answers'].update({ answer.id: { 'answer': answer.answer_title, 'correct': answer.correct, 'marked': str(answer.id) in answer_ids } }) exam_payload = pickle.dumps(exam_payload) # Save exam to database exam = Exam(user_id=current_user.id, result_payload=exam_payload) db.session.add(exam) db.session.commit() return redirect(url_for('exam_bp.view_exam', id=exam.id)) questions_dict = {} questions_model = Question() # Check if database has less then 20 questions if questions_model.count_records() < 20: flash('Exam fail to start, there is less then 20 questions.', 'danger') return redirect(url_for('exam_bp.exam_list')) for i in range(20): questions = questions_model.random_questions() # This needs to be optimized it can create a lot of query while questions_dict.get(questions.id) is not None: questions = questions_model.random_questions() questions_dict.update({questions.id: questions}) return render_template('create_exam.html', questions=questions_dict)
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 populate(): import datetime admin = User.create('admin', 'password', 'admin') admin.put() principal_user = User.create('principal', 'password', 'lecturer') principal = Lecturer(name='Principal', title='Dr', user=principal_user.key) principal.put() school = School(name='University of The West Indies - Mona', principal=principal.key) school.put() hof_user1 = User.create('hof1', 'password', 'lecturer') hof_user2 = User.create('hof2', 'password', 'lecturer') hof1 = Lecturer(name='Head Of Pure and Applied', title='Dr', user=hof_user1.key) hof2 = Lecturer(name='Head Of Medical Sciences', title='Dr', user=hof_user2.key) hof1.put() hof2.put() faculty1 = Faculty(name='Pure and Applied Science', school=school.key, head_of_faculty=hof1.key) faculty2 = Faculty(name='Medical Sciences', school=school.key, head_of_faculty=hof2.key) faculty1.put() faculty2.put() hod_user1 = User.create('hod1', 'password', 'lecturer') hod_user2 = User.create('hod2', 'password', 'lecturer') hod_user3 = User.create('hod3', 'password', 'lecturer') hod_user4 = User.create('hod4', 'password', 'lecturer') hod1 = Lecturer(name='Head Of Computing', title='Dr', user=hod_user1.key) hod2 = Lecturer(name='Head Of Mathematics', title='Dr', user=hod_user2.key) hod3 = Lecturer(name='Head Of Medicine', title='Dr', user=hod_user3.key) hod4 = Lecturer(name='Head Of Microbiology', title='Dr', user=hod_user4.key) hod1.put() hod2.put() hod3.put() hod4.put() department1 = Department(name='Computing', faculty=faculty1.key, head_of_department=hod1.key) department2 = Department(name='Mathematics', faculty=faculty1.key, head_of_department=hod2.key) department3 = Department(name='Medicine', faculty=faculty2.key, head_of_department=hod3.key) department4 = Department(name='Microbiology', faculty=faculty2.key, head_of_department=hod4.key) department1.put() department2.put() department3.put() department4.put() principal.department = department4.key hof1.department = department2.key hof2.department = department3.key hod1.department = department1.key hod2.department = department2.key hod3.department = department3.key hod4.department = department4.key principal.put() hof1.put() hof2.put() hod1.put() hod2.put() hod3.put() hod4.put() student_user = User.create('student', 'password', 'student') student = Student(name='Kevin Leyow', email_address='*****@*****.**', user=student_user.key, dob=datetime.date(year=1992, month=4, day=12), year=3, status='FT', gender='M') lecturer_user = User.create('lecturer', 'password', 'lecturer') lecturer = Lecturer(name='Jimmy', title='Dr', user=lecturer_user.key, department=department1.key) course = Course(name='Database Management Systems', code='COMP3161', total_students=90, department=department1.key, faculty=faculty1.key) course2 = Course(name='Theory Of Computation', code='COMP3702', total_students=20, department=department1.key, faculty=faculty1.key) ndb.put_multi([lecturer, course]) ndb.put_multi([lecturer, course2]) class_ = Class(course=course.key, lecturer=lecturer.key) class2_ = Class(course=course2.key, lecturer=lecturer.key) class_.put() class2_.put() student.courses = [class_.key, class2_.key] lecturer.courses = [class_.key, class2_.key] ndb.put_multi([student, lecturer]) survey = Survey(title='General survey', description='A general survey') survey_key = survey.put() with open('application/questions.txt') as f: questions = [] for number, line in enumerate(f.readlines()): question_type, dimension, question = line.split('|') questions.append( Question(question_type=question_type, dimension=dimension, question=question, is_active=True, number=number + 1, parent=survey_key)) ndb.put_multi(questions) return 'Done.'
def test_question_can_vote_2(self): participant = Participant.objects.create(username='******') lecture = Lecture.objects.get(hash='3j7t950') question = Question(text='How are you?', tags='hello', creator=participant, event=lecture) question.save() self.assertTrue(question.can_vote(participant))
import csv from application import db from application.models import Question, Test import pandas as pd csv_path = './RandDtest.csv' data = pd.read_csv(csv_path) data.columns = ['question', 'answer'] questions = [] for index, row in data.iterrows(): question = Question(question=row[0], answer=row[1]) questions.append(question) test = Test(questions=questions) db.session.add(test) db.session.commit() #for index, row in data.iterrows(): #answer =Answers(answer=row[1]) #db.session.add(answer) # db.session.commit()
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())