def create(self, quiz: md.Quiz): section = md.Section( user_id=current_user.id, container=quiz, order_number=self.order_number.data, name=self.name.data ) md.db.session.add(section) for i in range(self.number_of_questions.data): question = md.Question( text=_('Statement %(num)s', num=i+1), order_number=i+1, show_values=False, bonus=True, container=section ) md.db.session.add(question) for ans in self.answer.data.split(','): text = ans.strip() md.db.session.add(md.Value( text=text, question=question, allowed_misses=int(len(text) / 4) + 1, points=self.number_of_questions.data-i )) return section
def create(self, quiz: md.Quiz): section = md.Section( user_id=current_user.id, container=quiz, order_number=self.order_number.data, name=self.name.data ) md.db.session.add(section) for i in range(self.number_of_questions.data): question = md.Question( text=self.question_text.data or _('Question %(num)s', num=i+1), order_number=i+1, show_values=True, container=section ) md.db.session.add(question) for j in range(self.number_of_choices.data): md.db.session.add(md.Value( text=_('Answer'), question=question, order_number=j+1, points=0 )) return section
def create(self, quiz: md.Quiz): section = md.Section( user_id=current_user.id, container=quiz, order_number=self.order_number.data, name=self.name.data ) md.db.session.add(section) answers = [x.strip() for x in self.answers.data.split(',')] for i in range(len(answers)): question = md.Question( text=self.question_text.data or _('Question %(num)s', num=i+1), order_number=i+1, show_values=True, container=section ) md.db.session.add(question) for (j, ans) in enumerate(answers): md.db.session.add(md.Value( text=ans, question=question, order_number=j+1, points=0 )) return section
def Delete_Question(Question_name, type): if AUTHORITY_TEST("ADMIN") == False: return render_template("INVALID_AUTHORITY.html", a1=current_user.authority, a2="ADMIN") if type != "NORMAL" and type != "OPTIONAL": return render_template("INVALID_AUTHORITY.html", a1=current_user.authority, a2="ADMIN or STAFF") qp = model.Questions_Pool() q = model.Question() if type == "OPTIONAL": q.set_optional() qp.set_optional() q.Name = Question_name if q.exist() == False: return render_template('Delete_Question.html') form = Delete_Form() if form.validate_on_submit(): if qp.Delete(Question_name) == False: flash("Can not delete question in question pool!") return render_template('Delete_Question.html', form=form, name=Question_name) else: return redirect(url_for('Question_Pool')) return render_template('Delete_Question.html', form=form, name=Question_name)
def Create_Question(type): if (AUTHORITY_TEST("ADMIN") == False) and (AUTHORITY_TEST("STAFF") == False): return render_template("INVALID_AUTHORITY.html", a1=current_user.authority, a2="ADMIN or STAFF") if type != "NORMAL" and type != "OPTIONAL": return render_template("INVALID_AUTHORITY.html", a1=current_user.authority, a2="ADMIN or STAFF") form = Question_Form() if form.validate_on_submit(): q = model.Question() qp = model.Questions_Pool() if type == "OPTIONAL": q.set_optional() qp.set_optional() if q.form_init(form) == False: return render_template('Create_Question.html', form=form) else: if qp.Create(q) == False: flash("Can not create question in question pool!") return render_template('Create_Question.html', form=form) else: return redirect(url_for('Question_Pool')) form.type.data = "C" return render_template('Create_Question.html', form=form)
def __init__(self, question: model.Question = None, block: model.Block = None, *args, **kwargs): if question is None: if block is None or block.id is None: raise ValueError('Block must be provided for new question!') else: latest = block.questions\ .order_by(model.Question.order_number.desc())\ .first() if latest is None: order_number = 1 else: order_number = latest.order_number + 1 question = model.Question(block_id=block.id, order_number=order_number) super().__init__(question, *args, **kwargs)
def writeQuestion(req): info = '' ret_data = {} qid = 0 try: ques = model.Question(title=req['title'], content=req['content'], scan_time=0, agree_time=0, uid=req['uid']) model.db.session.add(ques) model.db.session.commit() qid = ques.id except Exception as e: info = '数据库错误,问题提交失败' logger.error(info) logger.error(e.message) return False, DB_ERR_HAND, info, ret_data try: tabArr = req['tab'].split(';') except KeyError as e: info = '请求错误' logger.error(info) logger.error(e.message) return False, DB_ERR_HAND, info, ret_data tab_list = [] for tab in tabArr: cid = model.db.session.query(model.Category.id).filter_by(category=tab).first() if not cid: tb = model.Category(category=tab, cid=1) model.db.session.add(tb) model.db.session.commit() cid = tb.id else: cid = cid[0] tab_list.append(model.Question_Category_Rela(qid=qid, cid=cid)) model.db.session.add_all(tab_list) model.db.session.commit() return True, SUCCESS, info, ret_data
def Edit_Question(Question_name, type): if AUTHORITY_TEST("ADMIN") == False: return render_template("INVALID_AUTHORITY.html", a1=current_user.authority, a2="ADMIN") if type != "NORMAL" and type != "OPTIONAL": return render_template("INVALID_AUTHORITY.html", a1=current_user.authority, a2="ADMIN or STAFF") qp = model.Questions_Pool() q = model.Question() if type == "OPTIONAL": q.set_optional() qp.set_optional() q.Name = Question_name if q.exist() == False: return render_template('Edit_Question.html') q = qp.Get(Question_name) form = Question_Form() if form.validate_on_submit(): if type == "OPTIONAL": q.set_optional() qp.set_optional() if q.form_edit(form, Question_name) == False: return render_template('Edit_Question.html', form=form) else: new_name = form.name.data q.Name = Question_name if qp.Edit(q, new_name) == False: flash("Can not edit question in question pool!") return render_template('Edit_Question.html', form=form) else: return redirect(url_for('Question_Pool')) form = q.form_create() return render_template('Edit_Question.html', form=form)
def create(self, quiz: md.Quiz): section = md.Section( user_id=current_user.id, container=quiz, order_number=self.order_number.data, name=self.name.data ) md.db.session.add(section) order_number = 1 for i in range(self.number_of_questions.data-1): question = md.Question( text=_('Question %(num)s', num=i+1), order_number=order_number, show_values=False, container=section ) order_number += 1 md.db.session.add(question) md.db.session.add(md.Value( text=_('Answer'), question=question, allowed_misses=3, points=1 )) if i < self.opportunities.data: question = md.Question( text=_('What is the connection? (+%(num)s/-1)', num=self.opportunities.data - i), order_number=order_number, show_values=False, bonus=True, container=section ) order_number += 1 md.db.session.add(question) for ans in self.connection.data.split(','): text = ans.strip() md.db.session.add(md.Value( text=text, question=question, allowed_misses=int(len(text) / 4) + 1, points=self.opportunities.data + 1 - i )) question = md.Question( text=_('What is the connection?'), order_number=order_number, show_values=False, bonus=True, container=section ) md.db.session.add(question) for ans in self.connection.data.split(','): text = ans.strip() md.db.session.add(md.Value( text=text, question=question, allowed_misses=int(len(text) / 4) + 1, points=1 )) return section