def addComplete(qt_name): q = Question() qt = QuestionType.query.filter_by(name=qt_name).first() print qt.name form = AddQuestionForm() q.context = form.context.data subject = '数据结构' q.subject_id = Subject.query.filter_by(name=subject).first().id if qt_name == '选择题': q.options = form.options.data q.answer = form.answer.data q.degree = int(request.form['kn_point_degree']) q.kn_point_counts = int(request.form['kn_point_counts']) q.kn_point_id = KnowledgePoint.query.filter_by( name=request.form['kn_point_id_1']).first().id q.question_type_id = qt.id db.session.add(q) db.session.commit() qt_name = '' return render_template('ques/add.html', form=form, qt_name=qt_name, message='添加成功')
def insert_questions(): f = open('/home/zhuzw/git/flasky/sql/data.txt', 'r') lines = f.readlines() f.close() for line in lines: print line tmp = line.replace("\n", "").split("|") q = Question() q.context = tmp[0] q.options = tmp[1] q.answer = tmp[2] qt = QuestionType.query.filter_by(name=tmp[3]).first() q.question_type = qt.id q.kn_point = tmp[4] q.degree = tmp[5] q.accuracy = 0 q.appearance = 0 u = User.query.filter_by(username="******").first() q.professor_id = u.id if q.question_type is "选择题": q.score = 3 elif q.question_type is "判断题": q.score = 2 elif q.question_type is "填空题": q.score = 5 elif q.question_type is "主观题": q.score = 10 else: print "error" print line sys.exit() db.session.add(q) db.session.commit()
def upload(): if request.method == 'GET': return render_template('ques/upload.html') elif request.method == 'POST': f = request.files['file'] fname = secure_filename(f.filename) filename = os.path.join(UPLOAD_FOLDER, fname) f.save(filename) print 'path:', filename datas = excel_table_byindex(filename) for row in datas: ques = Question() ques.context = row['context'] ques.answer = row['answer'] ques.degree = row['degree'] if row['ques_type'] == '选择题': ques.options = row['options'] ques.subject_id = Subject.query.filter_by( name=row['subject']).first().id ques.kn_point_id = KnowledgePoint.query.filter_by( name=row['kn_points']).first().id ques.question_type_id = QuestionType.query.filter_by( name=row['ques_type']).first().id ques.professor_id = User.query.filter_by( username='******').first().id db.session.add(ques) db.session.commit() return render_template('ques/message.html', message='上传成功')