def data_compare(): json = request.get_json() data, errors = DataCompareParaSchema().load(json) if errors: return error_jsonify(10000001, errors) data_list_1 = DataCollection.query.filter(or_(DataCollection.status == 2, DataCollection.status == 3), DataCollection.time_id == data['id_1']).all() data_list_2 = DataCollection.query.filter(or_(DataCollection.status == 2, DataCollection.status == 3), DataCollection.time_id == data['id_2']).all() res = [] if not data_list_1 or not data_list_2: return error_jsonify(10000020) tmp_1 = {'sum': 0, 'filing': 0, 'check': 0, 'diff': 0, 'percent': 0} for i in data_list_1: tmp_1['filing'] = tmp_1['filing'] + i.filing tmp_1['check'] = tmp_1['check'] + i.check tmp_1['sum'] = len(data_list_1) tmp_1['diff'] = tmp_1['check'] - tmp_1['filing'] tmp_1['percent'] = int(float(tmp_1['diff']) / tmp_1['filing'] * 100) res.append(tmp_1) tmp_2 = {'sum': 0, 'filing': 0, 'check': 0, 'diff': 0, 'percent': 0} for i in data_list_2: tmp_2['filing'] = tmp_2['filing'] + i.filing tmp_2['check'] = tmp_2['check'] + i.check tmp_2['sum'] = len(data_list_2) tmp_2['diff'] = tmp_2['check'] - tmp_2['filing'] tmp_2['percent'] = int(float(tmp_2['diff']) / tmp_2['filing'] * 100) res.append(tmp_2) return jsonify(res)
def query_rule(): r_text = request.args.get("text") r_id = request.args.get("id") token = request.args.get("token") username = request.args.get("username") if token is None or token == '': return error_jsonify(10000001, status_code=400) if username is None or username == '': return error_jsonify(10000001, status_code=400) # 调用数据接口 if r_id != '' and r_id is not None: try: int(r_id) except Exception: return error_jsonify(10000001) rules = list(db.filter_rules(id=r_id)) elif r_text != '' and r_text is not None: rules = list(db.filter_rules(text=r_text)) else: return error_jsonify(10000001) b, result = certify_token(username, token.encode('utf-8')) if not b: return result number = len(rules) code = 1 dict_rules = [] for r in rules: dict_rules.append(_rule2dict(r)) # 调用数据接口 data = {'code': code, 'number': number, 'rules': dict_rules} return jsonify(data)
def info_get(): cur_id = current_user.id tmp_info = Info.query.filter_by(user_id=cur_id).first() if tmp_info is None: tmp_info = {} json, error = InfoParaSchema().dump(tmp_info) return jsonify(json)
def arrange(): # 管理员排班审批 json = request.get_json() data, errors = ArrangeParaSchema().load(json) if errors: return error_jsonify(InvalidParameters, errors) user_id = data['user_id'] time = bin(data['time']).replace('0b', '') # 将time转化为二进制数字符串 time_list = [i for i in range(len(time)) if time[i] == '1'] # 提取所有有空的时间段 info = Info.query.filter_by(Info.user_id == user_id).first() if info is None: return error_jsonify(NoStudentInfo, errors) now = datetime.now() year = now.strftime('%Y') month = int(now.strftime('%m')) for schedule_time in time_list: for wk in range(1, 21): schedule = Schedule( user_id=user_id, year=year, term=False if 2 <= month <= 7 else True, # 判断学期 week=wk + 1, time=schedule_time, department=info.department_id, position=info.position_id ) session.add(schedule) session.commit() return jsonify({})
def delete(pk): user = User.query.filter_by(id=pk).first() if user is None: return error_jsonify(10000012) session.delete(user) session.commit() return jsonify({})
def register(): """ 注册视图,只接受POST消息,根据发来的用户名和密码 进行注册,参数不对返回400,如果账户已有的话返回401 :return: HTTP状态码和json信息 """ json = request.get_json() data, errors = AccountParaSchema().load(json) if errors: return error_jsonify(InvalidArguments, errors, 400) if current_user.isAdmin != 2: return error_jsonify(10000003) username = data['name'] password_md5 = data['password'] is_admin = data['isAdmin'] area = data['area'] if User.is_exist(username, area): return error_jsonify(AccountAlreadyExist, status_code=400) if is_admin == PERMISSION_CITY: if User.query.filter_by(isAdmin=PERMISSION_CITY, area=area).count(): return error_jsonify(AccountAlreadyExist) new_user = User(name=username, password=password_md5, isAdmin=is_admin, area=area) session.add(new_user) session.commit() return jsonify({})
def info(): # 学生用户获取个人信息 info = Info.query.filter_by(user_id=current_user.id).first() if info is None: return error_jsonify(NoStudentInfo) ret = { 'username': current_user.username, 'sex': info.sex, 'student_id': info.student_id, 'financial_difficulties': info.financial_difficulties, 'phone': info.phone, 'email': info.email, 'name': info.name, 'school': info.school, 'work': info.work, 'department_id': info.department_id, 'position_id': info.position_id, 'experience': info.experience, 'skill': info.skill, 'free_time': info.free_time, 'on_position': info.on_position } return jsonify(ret)
def login(): """ 最基本的登录视图,只能通过post发送登录信息 如果发送的参数不对返回400,用户不存在和密码错误返回401, :return: HTTP状态码和json信息 """ json = request.get_json() data, errors = AccountParaSchema(exclude=('is_admin', 'area')).load(json) if errors: return error_jsonify(InvalidArguments, errors, 400) username = data['name'] password_md5 = data['password'] attempt_user = User.query.filter_by(name=username).first() if attempt_user is None: return error_jsonify(AccountDoesNotExist, status_code=400) else: if attempt_user.has_right_password(password_md5): login_user(attempt_user) return jsonify({ "is_admin": attempt_user.isAdmin, "area": attempt_user.area }) else: return error_jsonify(PasswordIsNotCorrect, status_code=400)
def pass_or_not(): # 审核是否通过 json = request.get_json() data, errors = PassParaSchema().load(json) if errors: return error_jsonify(10000001, errors) data_need = DataCollection.query.filter_by(id=data['id']) if data_need.first() is None: return error_jsonify(10000020) data_update = {} if current_user.isAdmin == 1: # 市级审核 if data['status'] == 0: data_update['status'] = 4 else: data_update['status'] = 2 elif current_user.isAdmin == 2: # 省级审核 if data['status'] == 0: data_update['status'] = 4 else: data_update['status'] = 3 else: return error_jsonify(10000003) data_need.update(data_update) session.commit() return jsonify({})
def schedule(): # 学生用户查看排班表 json = request.get_json() data, errors = ScheduleParaSchema().load(json) week = data['week'] department = data['department'] position = data['position'] if week is None or department is None or position is None: return error_jsonify(InvalidParameters) students = Schedule.query.filter( and_(Schedule.week == week, Schedule.department == department, Schedule.position == position)).all() names = [] for student in students: name = Info.query.filter_by( student.user_id == Info.user_id).first().name if name is None: names.append('无') else: names.append(name) return jsonify(names)
def create_rule(): try: data = json.loads(request.get_data(as_text=True)) except ValueError: return error_jsonify(10000041) except Exception: traceback.print_exc() return error_jsonify(10000002) check_keys = ('text', 'response', 'username', 'token') if not all(k in data for k in check_keys): return error_jsonify(10000001) b, result = certify_token(data['username'], data['token'].encode('utf-8')) if not b: return result r_text = data['text'] r_response = data['response'] if r_text == '' or r_response == '': return error_jsonify(10000045) # 调用数据接口 code = 0 new_rule = db.create_rule(text=r_text, in_response_to=r_response) # new_rule = {'text': r_text, 'in_response_to': r_response} if new_rule is None: code = 0 else: code = 1 # 调用数据接口 data = {'code': code, 'rule': _rule2dict(new_rule)} return jsonify(data)
def info_report(): json = request.get_json() data, errors = DataParaSchema().load(json) if errors: return error_jsonify(10000001, errors) tmp_info = Info.query.filter_by( user_id=current_user.id).first() # 找到企业信息中是否有当前用户的信息 if tmp_info is None: return error_jsonify(10000021) now = datetime.datetime.now() data['time'] = now admin_user = User.query.filter_by(isAdmin=2).first() # 找到省管理员账户id report_time = ReportTime.query.filter( and_(ReportTime.user_id == admin_user.id, ReportTime.start_time <= now, ReportTime.end_time >= now)).first() # 找到省级管理员设置的提交时间中符合条件的 if report_time: # 如果在当前时间段 tmp_data = DataCollection.query.filter_by(user_id=current_user.id, time_id=report_time.id) # 找到企业填报的符合条件的数据 if tmp_data: # 上报 if tmp_data.first().status != 0: return error_jsonify(10000016) data['status'] = 1 tmp_data.update(data) session.commit() else: # 没有找到说明没有保存 return error_jsonify(10000015) else: # 现在不在任何可以填报的时间段内 return error_jsonify(10000014) return jsonify({})
def info_record_get(): now = datetime.datetime.now() admin_user = User.query.filter_by(isAdmin=2).first() # 找到省管理员账户id report_time = ReportTime.query.filter( and_(ReportTime.user_id == admin_user.id, ReportTime.start_time <= now, ReportTime.end_time >= now)).first() if report_time: tmp_data = DataCollection.query.filter_by( user_id=current_user.id, time_id=report_time.id).first() # 找到企业填报的符合条件的数据 if tmp_data: data_need, errors = DataParaSchema(exclude=('id', 'name')).dump(tmp_data) return jsonify(data_need) else: return jsonify({})
def data_compare_get(): time_list = ReportTime.query.all() res = [] for i in time_list: tmp = {'start': i.start_time.date().strftime("%Y-%m-%d"), 'end': i.end_time.date().strftime("%Y-%m-%d"), 'id': i.id} res.append(tmp) return jsonify(res)
def time_get(): # 获得所有的设定时间段 if current_user.isAdmin != 2: # 只能省级管理员 return error_jsonify(10000003) res = ReportTime.query.all() data_need, errors = TimeParaSchema(many=True).dump(res) if errors: return error_jsonify(10000001) return jsonify(data_need)
def get_info(): json = request.get_json() data, errors = InfoSearchSchema().load(json) if errors: return error_jsonify(10000001, errors) area = u'%{}%'.format(data.get('area')) infos = Info.query.filter(Info.area.like(area)) if current_user.isAdmin == PERMISSION_CITY: infos.filter(Info.area.like(u'%{}%'.format(current_user.area))) json = InfoParaSchema(many=True).dump(infos.all()) return jsonify(json.data)
def time_manage_delete(id): # 省级管理员删除时间段 if current_user.isAdmin != 2: # 只能省级管理员 return error_jsonify(10000003) data_need = ReportTime.query.filter_by(id=id).first() if data_need is None: return error_jsonify(10000017) session.delete(data_need) session.commit() return jsonify({})
def notice_manage_delete(id): # 删除id对应的通知 if current_user.isAdmin == 0: # 只能为管理员 return error_jsonify(10000003) data_need = Notice.query.filter_by(id=id).first() if data_need is None: return error_jsonify(10000017) session.delete(data_need) session.commit() return jsonify({})
def notification_get(): # 管理员获得通知 if current_user.isAdmin == 0: # 只能为管理员 return error_jsonify(10000003) if current_user.isAdmin == 2: # 如果是省级管理员 res = Notice.query.all() # 获得所有通知 if current_user.isAdmin == 1: # 市级管理员 res = Notice.query.filter_by(user_id=current_user.id).all() data_need, errors = NoticeParaSchema(many=True).dump(res) if errors: return error_jsonify(10000001, errors) return jsonify(data_need)
def time_manage(): # 省级管理员设定上交开始时间,结束时间 json = request.get_json() data, errors = TimeParaSchema().load(json) if errors: return error_jsonify(10000001, errors) if current_user.isAdmin != 2: # 只能省级管理员 return error_jsonify(10000003) new_time = ReportTime(start_time=data['start_time'], end_time=data['end_time'], user_id=current_user.id) session.add(new_time) session.commit() return jsonify({})
def modify(pk): json = request.json data, error = AccountParaSchema(exclude=('is_admin', 'id', 'area')).load(json) if error: return error_jsonify(10000001, error) user = User.query.filter_by(id=pk).first() if user is None: return error_jsonify(10000012) for k, v in data.items(): setattr(user, k, v) session.add(user) session.commit() return jsonify({})
def login(): """ 最基本的登录视图,只能通过post发送登录信息 如果发送的参数不对返回400 :return: HTTP状态码和json信息 """ json = request.get_json() data, errors = AccountParaSchema().load(json) if errors: return error_jsonify(InvalidParameters, errors, 400) username = data['username'] attempt_user = User.query.filter_by(username=username).first() if attempt_user is None: is_admin = True if 'admin' in username else False new_user = User(username=username, is_admin=is_admin) session.add(new_user) session.commit() login_user(new_user) return jsonify({}) else: login_user(attempt_user) return jsonify({})
def time_manage_id(id): # 省级管理员更改上交开始时间,结束时间 json = request.get_json() data, errors = TimeParaSchema().load(json) if errors: return error_jsonify(10000001, errors) if current_user.isAdmin != 2: # 只能省级管理员 return error_jsonify(10000003) data_need = ReportTime.query.filter_by(id=id) if data_need.first() is None: return error_jsonify(10000018) data_need.update(data) session.commit() return jsonify({})
def info_get(): args = request.args data, errors = DataGetParaSchema().load(args) if errors: return error_jsonify(10000001) res = DataCollection.query.filter( and_(DataCollection.user_id == current_user.id, DataCollection.time >= data['start'], DataCollection.time <= data['end'])).all() data_need, errors = DataParaSchema(many=True, exclude=('id', 'name')).dump(res) if errors: return error_jsonify(10000001) return jsonify(data_need)
def find_data(): # 返回所有当前用户可以审核的条目 if current_user.isAdmin == 0: return error_jsonify(10000003) res = [] if current_user.isAdmin == 1: area = current_user.area user_list = User.query.filter_by(area=area).all() # 找到所有地区相同的user for i in user_list: # 循环 if i.id == current_user.id: continue data_tmp = DataCollection.query.filter( and_(DataCollection.user_id == i.id, DataCollection.status == 1)).all() # 找到这个企业填报的信息 if len(data_tmp) != 0: for j in data_tmp: tmp, errors = DataCheckParaSchema().dump(j) res.append(tmp) return jsonify(res) if current_user.isAdmin == 2: data_list = DataCollection.query.filter_by(status=2).all() for i in data_list: tmp, errors = DataCheckParaSchema().dump(i) tmp['name'] = Info.query.filter_by(user_id=i.user_id).first().name res.append(tmp) return jsonify(res)
def notification_manage(): # 管理员设定通知 if current_user.isAdmin == 0: # 只能为管理员 return error_jsonify(10000003) json = request.get_json() data, errors = NoticeParaSchema().load(json) if errors: return error_jsonify(10000001, errors) now = datetime.datetime.now() data['created_at'] = now data['source'] = '山东省人力资源管理部门' data['user_id'] = current_user.id new_data = Notice(**data) session.add(new_data) session.commit() return jsonify({})
def check_user(): try: data = json.loads(request.get_data(as_text=True)) except ValueError: return error_jsonify(10000041) except Exception: traceback.print_exc() return error_jsonify(10000002) check_keys = ('username', 'token') if not all(k in data for k in check_keys): return error_jsonify(10000001) username = data['username'] b, result = certify_token(username, data['token'].encode('utf-8')) if b: return jsonify({'code': 1}) else: return result
def notice_manage_id(id): # 更改管理员获得的通知 if current_user.isAdmin == 0: # 只能为管理员 return error_jsonify(10000003) json = request.get_json() data, errors = NoticeParaSchema().load(json) if errors: return error_jsonify(10000001, errors) data_need = Notice.query.filter_by(id=id) if data_need.first() is None: # 没有这个id,更改失败 return error_jsonify(10000018) data_need.update(data) session.commit() return jsonify({})
def info_record(): json = request.get_json() data, errors = InfoParaSchema().load(json) if errors: return error_jsonify(InvalidArguments, errors, 400) else: data['user_id'] = current_user.id tmp_info = Info.query.filter_by(user_id=current_user.id) if tmp_info.first() is not None: tmp_info.update(data) session.commit() else: new_info = Info(**data) session.add(new_info) session.commit() return jsonify({})
def update(): try: data = json.loads(request.get_data(as_text=True)) except ValueError: return error_jsonify(10000041) except Exception: traceback.print_exc() return error_jsonify(10000002) check_keys = ('id', 'text', 'response', 'username', 'token') if not all(k in data for k in check_keys): return error_jsonify(10000001) b, result = certify_token(data['username'], data['token'].encode('utf-8')) if not b: return result s_id = data['id'] if s_id == '': return error_jsonify(10000046) try: int(s_id) except Exception: return error_jsonify(10000001) text = data['text'] response = data['response'] if text == '' or response == '': return error_jsonify(10000045) tag_list = [] if 'tags' in data: tags = data['tags'] tag_list = tags.split('+') # 调用数据接口 code = 1 new_statement = Statement(text=text, in_response_to=response, id=s_id, tags=tag_list) db.update_text(new_statement) # 调用数据接口 result = {'code': code, 'statement': _statement2dict(new_statement)} return jsonify(result)