def book_delete(id): try: BookService.delete_book(id) flash(u"已成功删除书籍", 'success') except: flash(u'删除书籍失败,请检查日志', 'danger') return redirect(url_for('admin.book_manage'))
def __init__(self): super().__init__('book_blueprint', __name__, url_prefix='/api/books') self.add_url_rule('', 'books', self.books, methods=['GET', 'POST']) self.add_url_rule('/<book_uid>', 'book', self.book, methods=['GET', 'PUT']) self.book_service = BookService() self.book_schema = BookSchema(exclude=['id'])
def index_delete(book_id, type, id): try: if type == 'section': BookService.delete_section(id) if type == 'unit': BookService.delete_unit(id) flash(u'成功删除章/节', 'success') except: flash(u'删除章/节失败,请检查日志', 'danger') return redirect(url_for('admin.bookindex_update', id=book_id))
def problem_update(id): form = ProblemUpdateForms() proIsExist = ProLibService.get_problem_detailed_info(id) if proIsExist is None: return redirect(url_for('admin.problem_manage')) if request.method == 'GET': problem = ProLibService.get_problem_detailed_info(id) book = BookService.get_book_by_id(problem.belong_to_book) unit = BookService.get_unit_by_id(problem.belong_to_unit) section = BookService.get_section_by_id(problem.belong_to_section) form.type.data = problem.type form.desc_main.data = problem.desc_main form.desc_other.data = problem.desc_other form.author.data = problem.author form.answer.data = problem.answer form.tips.data = problem.tips form.tags.data = problem.tags form.subject.data = problem.subject form.level.data = str(problem.level) form.status.data = str(problem.status) if book: form.b_book_id.data = book.book_id form.b_book.data = book.book_name if unit: form.b_unit_id.data = unit.unit_id form.b_unit.data = unit.unit_name if section: form.b_section_id.data = section.section_id form.b_section.data = section.section_name return render_template('admin/problem/problem_update.html', form=form, active=Config.ADMIN_PAGE_ACTIVE['problem']) if form.book.data == '0': form.book.data = form.b_book_id.data form.unit.data = form.b_unit_id.data form.section.data = form.b_section_id.data #存在js级联查询,不能使用表单验证,如果使用必然不通过 try: ProLibService.update_problem(form, id) flash("Update problem information success", "success") except: flash("Update problem infomation failed", "danger") return redirect(url_for('admin.problem_update', id=id))
def book_add(): form = BookUpdateForm() if request.method == 'GET': return render_template('admin/book/book_add.html', form=form, active=Config.ADMIN_PAGE_ACTIVE['problem']) if form.validate_on_submit(): try: BookService.add_book(form) flash("Add book success", "success") except: flash("Add book field", "danger") return redirect(url_for('admin.book_manage'))
def get_thislevel_json_value(): json_iteminfo = [] book_list = BookService.get_books() for book in book_list: json_iteminfo.append( dict(zip(['itemvalue', 'itemname'], [book.book_id, book.book_name]))) jsonstr = {'itemInfo': json_iteminfo} return json.dumps(jsonstr)
def get_nextlevel_json_value(itemlevel, itemvalue): json_iteminfo = [] if itemlevel == 'second': unit_list = BookService.get_unit_by_book(itemvalue) for unit in unit_list: json_iteminfo.append( dict( zip(['itemvalue', 'itemname'], [unit.unit_id, unit.unit_name]))) if itemlevel == 'third': section_list = BookService.get_section_by_unit(itemvalue) for section in section_list: json_iteminfo.append( dict( zip(['itemvalue', 'itemname'], [section.section_id, section.section_name]))) jsonstr = {'itemInfo': json_iteminfo} return json.dumps(jsonstr)
def book_update(id): form = BookUpdateForm() if request.method == 'GET': book = BookService.get_book_by_id(id) form.book_name.data = book.book_name form.version.data = book.version form.author.data = book.author form.publisher.data = book.publisher form.publish_time.data = book.publish_time form.subject.data = book.subject return render_template('admin/book/book_update.html', form=form, active=Config.ADMIN_PAGE_ACTIVE['problem']) if form.validate_on_submit(): try: BookService.update_book(form, id) flash("Update book information success", "success") except: flash("Update book infomation failed", "danger") return render_template('admin/book/book_update.html', form=form, active=Config.ADMIN_PAGE_ACTIVE['problem'])
def book_manage(page=1, values=''): form = BookSearchForm() if request.method == 'GET': values_json = {} if values != '': values_json = json.loads(base64.b64decode(values)) pagination = BookService.search_bookinfo(values_json, page) return render_template('admin/book/book_manage.html', form=form, values=values, book_list=pagination.items, pagination=pagination, active=Config.ADMIN_PAGE_ACTIVE['problem']) if form.validate_on_submit(): values = base64.b64encode(json.dumps(form.to_dict())) return redirect(url_for('admin.book_manage', page=1, values=values)) return redirect(url_for('admin.book_manage', page=page))
def bookindex_update(id): formUnit = UnitAddForm() formSection = SectionAddForm() #该书若不存在直接禁止访问此页面 bookIsExist = BookService.get_book_by_id(id) if bookIsExist is None: return redirect(url_for('admin.book_manage')) if request.method == 'GET': unit_list = BookService.get_unit_by_book(id) #获取该书的所有章 List = [] #该书所有章节总列表,列表每项为一个字典包含两个键unit,section #unit下只有一个unit对象,section下为一个列表,列表每一项为一个section对象 uid_list = [] #章id列表 uname_list = [] #章名称列表 key = ['unit', 'section'] #遍历每一章,获取每一章对应的所有节存入value列表 for unit in unit_list: uid_list.append(unit.unit_id) uname_list.append(unit.unit_name) value = [unit, BookService.get_section_by_unit(unit.unit_id)] List.append(dict(zip(key, value))) formUnit.bookid.data = id #为右边工具栏“节添加”操作的select赋值 formSection.unitid.choices = zip(uid_list, uname_list) return render_template('admin/book/book_index_update.html', List=List, formUnit=formUnit, formSection=formSection, book_id=id, active=Config.ADMIN_PAGE_ACTIVE['problem']) #章添加的表单合法性验证 if formUnit.submitunit.data: unit = UnitInfo() unit.belong_to_book = formUnit.bookid.data unit.unit_name = formUnit.unitname.data BookService.add_unit(unit) #节添加的表单合法性验证 if formSection.submitsection.data: section = SectionInfo() section.belong_to_unit = formSection.unitid.data section.section_name = formSection.sectionname.data BookService.add_section(section) #获取前端名为update_unit的input按钮的返回值,若有返回值则存在对应表单的操作 if request.form.get('update_unit'): unit = UnitInfo() unit.unit_id = request.form.get('unit_id') unit.unit_name = request.form.get('unit_name') BookService.update_unit(unit) #获取前端名为update_section的input按钮的返回值,若有返回值则存在对应表单的操作 if request.form.get('update_section'): section = SectionInfo() section.section_id = request.form.get('section_id') section.section_name = request.form.get('section_name') BookService.update_section(section) return redirect(url_for('admin.bookindex_update', id=id))
class Books(Blueprint): def __init__(self): super().__init__('book_blueprint', __name__, url_prefix='/api/books') self.add_url_rule('', 'books', self.books, methods=['GET', 'POST']) self.add_url_rule('/<book_uid>', 'book', self.book, methods=['GET', 'PUT']) self.book_service = BookService() self.book_schema = BookSchema(exclude=['id']) def books(self): if request.method == 'GET': books = self.book_service.get_books() return jsonify(self.book_schema.dump(books, many=True)) if request.method == 'POST' and request.is_json: try: book_to_add = self.book_schema.load(request.get_json(), transient=True) except ValidationError as validation_error: return { 'status': 'error', 'error': validation_error.messages }, HTTPStatus.UNPROCESSABLE_ENTITY uid = self.book_service.add_book(book_to_add) return { 'id': uid }, HTTPStatus.CREATED, self.created_book_headers(book_to_add) return { 'status': 'error', 'error': 'Request body type not supported' }, HTTPStatus.UNSUPPORTED_MEDIA_TYPE def book(self, book_uid: str): book = self.book_service.get_book(book_uid) if not book: return { 'status': 'error', 'error': 'Resource not found' }, HTTPStatus.NOT_FOUND if request.method == 'GET': return jsonify(self.book_schema.dump(book)) if request.method == 'PUT' and request.is_json: try: self.book_schema.load(request.get_json(), instance=book, unknown=EXCLUDE) self.book_service.commit_changes() except ValidationError as validation_error: return { 'status': 'error', 'error': validation_error.messages }, HTTPStatus.UNPROCESSABLE_ENTITY return { 'status': 'Book updated' }, HTTPStatus.OK, self.created_book_headers(book) return { 'status': 'error', 'error': 'Request body type not supported' }, HTTPStatus.UNSUPPORTED_MEDIA_TYPE def created_book_headers(self, book: Book) -> dict: return {'ETag': str(hash(book)), 'Location': f'/api/books/{book.uid}'}
def book_service(): return BookService()
def exam_problem(id): form = ExamProblemNumForm() examinfo = ExamService.get_exam_detail_info(id) problem_list_exam = examinfo.problem_list #考试题库题目id列表 problem_list_example = examinfo.problem_list_example #考试样例试卷题目id列表 problem_condition_dict = ExamService.get_problem_condition_dict_byid(id) #考题情况字典 choice = [] blank = [] short_answer = [] choice_ex = [] blank_ex = [] short_answer_ex = [] # 考试题目列表仅仅有id,通过循环获取 # 题目相关信息,下一循环同理 # 区别在于题库和样例试卷的题目不同 for problem in problem_list_exam: problem_detail = ProLibService.get_problem_detailed_info(problem) problem_detail.belong_to_book = BookService.get_book_by_id(problem_detail.belong_to_book).book_name problem_detail.belong_to_unit = BookService.get_unit_by_id(problem_detail.belong_to_unit).unit_name problem_detail.belong_to_section = BookService.get_section_by_id(problem_detail.belong_to_section).section_name if problem_detail.type == Config.PROBLEM_TYPE['choice']: choice.append(problem_detail) elif problem_detail.type == Config.PROBLEM_TYPE['blank']: blank.append(problem_detail) elif problem_detail.type == Config.PROBLEM_TYPE['short_answer']: short_answer.append(problem_detail) for problem in problem_list_example: problem_detail = ProLibService.get_problem_detailed_info(problem) problem_detail.belong_to_book = BookService.get_book_by_id(problem_detail.belong_to_book).book_name problem_detail.belong_to_unit = BookService.get_unit_by_id(problem_detail.belong_to_unit).unit_name problem_detail.belong_to_section = BookService.get_section_by_id(problem_detail.belong_to_section).section_name if problem_detail.type == Config.PROBLEM_TYPE['choice']: choice_ex.append(problem_detail) elif problem_detail.type == Config.PROBLEM_TYPE['blank']: blank_ex.append(problem_detail) elif problem_detail.type == Config.PROBLEM_TYPE['short_answer']: short_answer_ex.append(problem_detail) problem_list_dict = dict((('choice',choice),('blank',blank),('short_answer',short_answer))) problem_list_example_dict = dict((('choice', choice_ex), ('blank', blank_ex), ('short_answer', short_answer_ex))) if request.method == 'GET': form.choice_num.choices = [(i, i) for i in range(len(choice) + 1)] form.blank_num.choices = [(i, i) for i in range(len(blank) + 1)] form.short_answer_num.choices = [(i, i) for i in range(len(short_answer) + 1)] form.choice_num.data = str(problem_condition_dict['choice']['num']) form.choice_score.data = str(problem_condition_dict['choice']['score']) form.blank_num.data = str(problem_condition_dict['blank']['num']) form.blank_score.data = str(problem_condition_dict['blank']['score']) form.short_answer_num.data = str(problem_condition_dict['short_answer']['num']) form.short_answer_score.data = str(problem_condition_dict['short_answer']['num']) form.is_random.data = str(problem_condition_dict['is_random']) return render_template('web/exam_manage/exam_problem.html', form = form, id = id, is_random = problem_condition_dict['is_random'], problem_num_dict = problem_condition_dict, problem_list_dict = problem_list_dict, problem_list_example = problem_list_example_dict, status=Config.STATUS, active=Config.ADMIN_PAGE_ACTIVE['exam']) if request.method == 'POST': ExamService.update_problem_number(form,id) return redirect(url_for('web.exam_problem',id=id))