def look_room_status(): """ 查看教室状态 :return: """ rev_json = request.args look_date, look_building = rev_json.get('date'), rev_json.get('building') if look_date is None or look_building is None: return send_json(-101, '缺少必要参数') apply_records = Apply.query.filter( and_(Apply.use_date == look_date, Apply.building == look_building)).all() result_data = dict() for one_apply in apply_records: if one_apply.room_name not in result_data.keys(): result_data[one_apply.room_name] = list() result_data[one_apply.room_name].append({ 'begin_time': one_apply.begin_time, 'end_time': one_apply.end_time, 'activity': one_apply.activity_name, 'organization': one_apply.applicant_name }) return send_json(0, result_data)
def account_list_GET(rev_json, cur_org): """ 处理GET请求 :param rev_json: 接受的json数据,cur_org:执行目前操作的管理员 :return: 运行正确时返回需要发送的json中的data字段的值,其type为列表 若出现错误则返回一个元组,分别为需要发送给前端的code和data字段值 """ # 获取参数 start_id, end_id = rev_json.get('start_id'), rev_json.get('end_id') if start_id is None or end_id is None: # 检查是否缺失参数 return -101, '缺少必需参数' try: start_id = int(start_id) end_id = int(end_id) except Exception: return send_json(-102, 'start_id 或 end_id 值错误') # 计算真正应返回的账号id范围 db_count = Administrator.query.count() # 账号的数量 if start_id > db_count: return -102, '超过最大公告数量' if end_id > db_count: end_id = db_count # 从数据库中查询相应记录 administrators = Administrator.query.offset(db_count - end_id).limit(end_id - start_id + 1).all() res = [ dict(account=record.account, grade=record.grade, name=record.name, phone=record.phone) for record in administrators if record.org == cur_org ] return res
def apply_detail(apply_id): # GET: if request.method == 'GET': apply = Apply.query.get(apply_id) if apply is None: return send_json(-102, '查询不到该申请') result_data = { 'room_num': apply.room_name, 'applicant': apply.applicant_id, 'applicant_name': apply.applicant_name, 'begin_time': apply.begin_time, 'end_time': apply.end_time, 'request': apply.request, 'check_status': apply.check_status, 'note': apply.note, 'verifier_name': apply.verifier_name, 'teacher_name': apply.teacher_name, 'material': apply.material, 'org': apply.org, 'building': apply.building, 'floor': apply.floor, 'room_name': apply.room_name } return send_json(0, result_data) # POST: else: rev_json = request.get_json(silent=True) verifier_id, is_pass = rev_json.get('verifier_id'), rev_json.get( 'is_pass') if verifier_id is None or is_pass is None: return send_json(-101, '缺少必要参数') if is_pass not in {0, 1, 2}: return send_json(-102, 'is_pass参数不符合要求') admin_account = session.get( 'admin_login') # 经adm_login_required装饰器验证后,登录的管理员一定存在 cur_admin = Administrator.query.get(admin_account) apply = Apply.query.get(apply_id) if apply is None: return send_json(-102, '查询不到该教室') apply.verifier_name = cur_admin.name apply.org = cur_admin.org apply.check_status = {0: '审核通过', 1: '审核失败', 2: '待审核'}.get(is_pass) for k, v in rev_json.items: if k in ('note', 'building', 'floor', 'room_name'): apply.__setattr__(k, v) try: db.session.commit() except Exception as e: record_exception(e) return send_json(101, '数据库异常') return send_json(0, {'tip': '审批成功'})
def Adm_room_info(room_id): """ GET:查看教室信息 POST:修改教室信息 :param room_id: :return: """ if request.method == 'GET': room = Room.query.get(room_id) result_data = { 'building': room.building, 'floor': room.floor, 'room_name': room.room_name, 'org': room.org, 'permissible': room.permissible, 'picture': room.picture, 'description': room.description } return send_json(0, result_data) else: rev_json = request.get_json(silent=True) opr_type = rev_json.get('type') if opr_type is None: return send_json(-101, '缺少type参数') room = Room.query.get(room_id) room_name = room.room_name if opr_type == 'delete': db.session.delete(room) try: db.session.commit() except Exception as e: db.session.rollback() record_exception(e) return send_json(-101, '数据库异常') # return send_json(0, '教室 {} 信息删除成功'.format(room_name)) return send_json(0, '操作成功') elif opr_type == 'update': optional_args = { 'building', 'floor', 'room_name', 'picture', 'max_num', 'description', 'permissible' } for k, v in rev_json.items(): if k in optional_args: room.__setattr__(k, v) try: db.session.commit() except Exception as e: db.session.rollback() record_exception(e) return send_json(-101, '数据库异常') # return send_json(0, '教室 {} 信息更新成功'.format(room_name)) return send_json(0, '操作成功') else: return send_json(-102, '对数据库的操作类型错误')
def adm_timetable_handler(): """ GET:查看时间表 POST: 修改时间表 :return: """ if request.method == 'GET': records = Timetable.query.all() result_data = [[ '第{}节'.format(record.class_id), turn_to_string(record.begin_time), turn_to_string(record.end_time) ] for record in records] return send_json(0, result_data) # 当 method 为 POST 时 else: new_timetable = request.get_json(silent=True).get('timetable') if new_timetable is None: return {-101, '缺少必要参数'} # 删除旧时间表 old_timetable = Timetable.query.all() for klass in old_timetable: db.session.delete(klass) # 加入新时间表 for klass in new_timetable: new_record = Timetable() new_record.class_id = klass[0] new_record.begin_time = turn_to_int(klass[1]) new_record.end_time = turn_to_int(klass[2]) # new_record = Timetable(klass[0], turn_to_int(klass[1]), turn_to_int(klass[2])) db.session.add(new_record) # 提交到数据库 try: db.session.commit() except Exception as e: record_exception(e) return send_json(101, '数据库异常') return send_json(0, {'tip': '更新成功'})
def Adm_room_GET(rev_json, org): """ 处理Adm_room视图函数的GET请求 :param rev_json: 接收到的json数据。 :return: 需要发送给前端的结果 """ start_id, end_id = rev_json.get('start_id'), rev_json.get('end_id') # 验证参数的有效性 if None in (start_id, end_id): return jsonify(code=-101, tip='缺少必要参数') try: start_id = int(start_id) end_id = int(end_id) except Exception: return send_json(-102, 'start_id 或 end_id 值错误') room_nums = Room.query.count() if start_id > room_nums: return jsonify(code=-102, tip='超过教室最大数量') if end_id > room_nums: end_id = room_nums # 查询出所有符合条件的教室 rooms = Room.query.offset(start_id - 1).limit(end_id - start_id + 1).all() res_data = rooms_to_data(rooms, org) return jsonify(code=0, data=res_data)
def acquire_apply_list(): """ GET:获取申请列表 POST:修改申请列表 :return: """ # 当 method 为 GET 时 if request.method == 'GET': # 获取参数 rev_json = request.args apply_status_type = rev_json.get('type') start_cursor, end_cursor = rev_json.get('start_cursor'), rev_json.get( 'end_cursor') building = rev_json.get('building') if None in {apply_status_type, start_cursor, end_cursor}: return send_json(-101, '缺少必要参数') start_cursor, end_cursor = int(start_cursor), int(end_cursor) # 进行查询 applies = Apply.query # 首先根据审核状态查询 if apply_status_type == 'uncheck': applies = applies.filter(Apply.check_status == "待审核") elif apply_status_type == 'checked': applies = applies.filter( or_(Apply.check_status == "审核通过", Apply.check_status == "审核失败")) else: return send_json(-101, "type 参数错误") if building is not None: applies = applies.filter(Apply.building == building) # 根据教学楼查询 apply_num = applies.count() applies = applies.order_by(Apply.apply_id.desc()) # 对所有查询进行倒序排序 if start_cursor > apply_num: return send_json(0, []) end_cursor = end_cursor if end_cursor <= apply_num else apply_num # 防止end_id越界 applies = applies.offset(start_cursor - 1).limit(end_cursor - start_cursor + 1) apply_records = applies.all() result_data = [{ 'activity': apply_record.activity_name, 'organization': apply_record.applicant_name, 'time': apply_record.apply_time.strftime('%Y-%m-%d'), 'building': apply_record.building, 'room_name': apply_record.room_name } for apply_record in apply_records] return send_json(0, result_data) # 当 method 为 POST 时 else: rev_json = request.get_json(silent=True) if rev_json is None: return send_json(-101, '缺少必需参数') apply_list = rev_json.get('applys') if type(apply_list) is not list: return send_json(-101, '缺少必需参数') if len(apply_list) == 0: return send_json(0, '待修改列表数据为空') admin = Administrator.query.get(session.get('admin_login')) success_list = list() for one_apply in apply_list: activity_name = one_apply.get('activity_name') use_date = one_apply.get('date') begin_time = one_apply.get('begin_time') end_time = one_apply.get('end_time') room_name = one_apply.get('room_num', '不指定') building = one_apply.get('building', '不指定') floor = one_apply.get('floor', '不指定') applicant_name = one_apply.get('applicant_name') applicant_phone = one_apply.get('applicant_phone') if None in { activity_name, use_date, begin_time, end_time, applicant_phone, applicant_name }: return send_json(-101, '缺少必需参数') one_apply_record = Apply(apply_id=0, activity_name=activity_name, applicant_id='', applicant_name=applicant_name, applicant_phone=applicant_phone, apply_time=datetime.now(), use_date=use_date, begin_time=begin_time, end_time=end_time, people_count=-1, room_name=room_name, building=building, floor=floor, teacher_name='', check_status='审核通过', org=admin.org, verifier_name=admin.name) db.session.add(one_apply_record) try: db.session.commit() success_list.append(one_apply) except Exception as e: db.session.rollback() record_exception(e) return send_json(0, '数据库异常') return send_json(0, success_list)