def test_options_deleted_after_questions_deleted(self): ''' When a question is deleted, its related options should be deleted along with it. ''' u = User('*****@*****.**') options = [ Option(body='This in option 1', is_right=True), Option(body='This is option 2'), Option(body='This is option 3') ] ques = Question(body='test Question', user=u, options=options) db.session.add(ques) db.session.add(u) db.session.commit() ques_query_obj = Question.query.filter_by(id=ques.id).one() self.assertTrue(ques_query_obj) db.session.delete(ques) db.session.commit() opts = Option.query.all() self.assertTrue(opts == [])
def test_for_building_database1(self): option1 = Option(option_body='World of Warcraft') option2 = Option(option_body='Team Fortress 2') option3 = Option(option_body='Steam') option4 = Option(option_body='Dota 2') question1 = Question(question_body='which one is a moba game?') option1.question = question1 option2.question = question1 option4.correct = True option4.question = question1 option3.question = question1 user1 = User(username='******', email='*****@*****.**') quiz1 = Quiz(usersesh=user1) db.session.add(quiz1) db.session.add(question1) db.session.commit() for question in db.session.query(Question).all(): for option in question.options: print("test_for_building_database1 option: " + option.option_body) print( "test_for_building_database1 quiz to user relationship (expected answer 1) user_id is: " + str(db.session.query(User).first().quiz[0].user_id))
def test_select_field(self): opt1 = Option("男", 1) opt2 = Option("女", 0) field = SelectField(True, [opt1, opt2]) form = Form([field]) self.db.session.add(form) self.db.session.commit() self.assertEquals(form.id, 1) self.assertEquals(opt1.id, 1) self.assertEquals(opt2.id, 2) self.assertEquals(field.multiple, True)
def handle(self, *args, **options): try: Option(key='is_camera_on', value=False).save() Option(key='is_send_email', value=False).save() Option(key='with_photo', value=False).save() Option(key='is_minute', value=False).save() user = User(username='******', first_name='Saye', last_name='Banayee') user.set_password('123456') user.save() except IntegrityError as e: pass
def add_question(user, qn_text, options, answer, topicID): '''Adds a question to the database Input qn_text : str options : seq of str answer : int (1 to 4) topic : int ''' # Generate item parameters from CatSim item = generate_item_bank(1)[0] # Add question question = Question(question=qn_text, discrimination=item[0], \ difficulty=item[1], guessing=item[2], upper=item[3], topicID = topicID, userID=user.id) db.session.add(question) db.session.flush() qnID = question.id # Add options and answer for opt in options: o = Option(qnID=qnID,option=opt) answer -= 1 db.session.add(o) db.session.flush() if answer == 0: optID = o.id question.answerID = optID db.session.flush() db.session.commit() return question
def add_qn(org_qns): '''Adds questions to the database, where questions are formatted to be in a dictionary {<question>:{'answer':<options>,'difficulty':<difficulty>} <questions> is str <options> is list of str <difficulty> is float (not added yet) ''' if Question.query.all(): return for q in org_qns.keys(): item = generate_item_bank(1)[0] qn = Question(question=q, discrimination=item[0], \ difficulty=item[1], guessing=item[2], upper=item[3], topicID=1) db.session.add(qn) db.session.commit() qid = qn.id b=True for o in org_qns[q]['answers']: opt=Option(qnID=qid,option=o) db.session.add(opt) if b: db.session.flush() qn.answerID = opt.id b=False db.session.commit()
def options(): form = OptionsForm() if request.method == 'POST' and form.validate(): x = Option(option=request.form.get('option'), level=request.form.get('level')) db.session.add(x) db.session.commit() return redirect('/options') options = Option.query.all() return render_template('options.html', form=form, options=options)
def insert_one(self, option): """Insert a New Option""" if self.get_one_by_key(option["key"]) is not False: return True option = Option( key=option["key"], value=option["value"], autoload=option["autoload"] if "autoload" in option else False) option.save() return False if option.pk is None else option
def add_option(): id_question = request.form['id_question'] label = request.form['label'] option = Option(id_question=id_question, label=label) db.session.add(option) db.session.commit() return Response('{"data":"Option created successfully."}', status=200, mimetype='application/json')
def post(self): form = request.form print(form) content = form.get("content") category_id = form.get("category_id") right_answer = form.get("right_answer") options = form.get("options") # 判断参数是否为空 if not content or not right_answer or not category_id: return error.error_1001() # 判断right_answer是否合法 answers_lst = right_answer.split(",") for answer in answers_lst: if answer not in ["A", "B", "C", "D", "E"]: return error.error_1007() # 判断题目是否重复 content_count = Question.Query().filter_by(content=content).count() if content_count: return error.error_1002("题目不能重重") # 将题目添加到到数据库 question = Question(form) db.session.add(question) db.session.flush() # 获取题目的临时id question_id = question.id # 解析选项 options = json.loads(options) for r in options: r["question_id"] = question_id # 判断label是否合法 if not r.get("label") or r.get("label") not in [ "A", "B", "C", "D", "E" ]: return error.error_1007() # 同一题目A,B,C,D,E出现两次 label_count = Option.Query().filter_by(question_id=question_id, label=r["label"]).count() print("label_count ", label_count) if label_count: return error.error_1002(r["label"] + "重复") option = Option(r) db.session.add(option) db.session.commit() return error.success()
def register_vote(token): user, payload = User.verify_user_token(token, task='vote') if not user: flash('Invalid token', 'danger') return redirect(url_for('main.index')) if user != current_user: flash('Login and try again', 'danger') return redirect(url_for('main.index')) if not 'match' in payload: flash('Invalid token payload', 'danger') return redirect(url_for('main.index')) match_id = payload['match'] match = Match.query.filter_by(id=match_id).first() poll = match.poll if not poll: flash('Voting has not oppened for this match.', 'warning') return redirect(url_for('main.index')) if current_user in poll.users: flash('You have already voted for this match.', 'warning') return redirect(url_for('main.index')) else: poll.users.append(current_user) players = [ Player.query.filter_by(id=c).first() for c in request.form.getlist('choices') ] for p in players: if not p: pass option = poll.options.filter_by(player_id=p.id).first() if option: option.votes += 1 else: option = Option(poll_id=poll.id, player_id=p.id, votes=1) db.session.add(option) db.session.commit() flash('Thanks for voting!', 'success') return redirect(url_for('main.index'))
def insert_qns(path): '''Inserts questions formatted as a json file {<number>: {'answer':<extra text><answer>, 'option_texts':<extra text><options>, 'question_text':<extra text><question><extra text>}} all are strings ''' qn_dict = {} for filename in glob.glob(os.path.join(path, '*.json')): print("===") print(filename) print("===") with open(filename, 'r') as f: # open in readonly mode data = json.load(f) for qn_set in data.values(): qn_txt = qn_set["question_text"] n, qn_text = qn_txt.split(")",1) options = qn_set["option_texts"] options = [[o[0], o[6:]] for o in options] answer = qn_set["answer"] a, answer = answer.split("Answer / Explanation :\n\nAnswer : ", 1) answer, explanation = answer.split(".", 1) item = generate_item_bank(1)[0] question = Question(question=qn_text, discrimination=item[0], \ difficulty=item[1], guessing=item[2], upper=item[3], topicID=1) db.session.add(question) db.session.flush() qid = question.id for opt in options: o = Option(qnID=qid, option=opt[1]) db.session.add(o) if opt[0] == answer: db.session.flush() optID = o.id question.answerID = optID db.session.commit()
for faction in factions: c = 0 for i in range(5): q = Question(text='%s q.no. %d' % (faction.name, (c + 1)), for_first_round=False, duration=15, faction_id=faction.id) db.session.add(q) c += 1 db.session.commit() questions = Question.query.all() for question in questions: c = 0 for i in range(4): o = Option(question=question, text='option no. %d' % c) db.session.add(o) c += 1 db.session.commit() ######################################################### for k, v in lists.items(): q = Question(text=k, for_first_round=True, duration=15) db.session.add(q) db.session.commit() for k, v in lists.items(): q = Question.query.filter(Question.text == k).first() for i in range(4): o = Option(question=q, text=v[i]) db.session.add(o) db.session.commit() #########################################################
for faction in factions: c = 0 for i in range(15): q = Question(text='%s q.no. %d' % (faction.name, (c + 1)), for_first_round=False, duration=20, faction_id=faction.id) db.session.add(q) c += 1 db.session.commit() questions = Question.query.all() for question in questions: c = 0 for i in range(4): o = Option(question=question, text='option no. %d' % c) db.session.add(o) c += 1 db.session.commit() options = Option.query.join(Question).filter(Question.for_first_round == True) c = 0 for option in options: if c % 4 == 0: w = Weight(option=option, f1=60, f2=20, f3=10, f4=0, f5=0) elif c % 4 == 1: w = Weight(option=option, f1=0, f2=70, f3=0, f4=50, f5=20) elif c % 4 == 2: w = Weight(option=option, f1=20, f2=10, f3=70, f4=20, f5=40) elif c % 4 == 3: w = Weight(option=option, f1=20, f2=0, f3=20, f4=30, f5=60)