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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 rest(): # 学生用户请假 json = request.get_json() data, errors = RestParaSchema().load(json) if errors: return error_jsonify(InvalidParameters) week = data['week'] time = data['free_time'] schedule = Schedule.query.filter( and_(Schedule.user_id == current_user.id, Schedule.week == week, Schedule.time == time)).first() if schedule is None: return error_jsonify(1002, errors) rest = Rest(user_id=current_user.id, week=week, time=time) session.add(rest) session.commit()
def admin_login(): username = request.args.get("username") password = request.args.get("password") if password is None or password == '': return error_jsonify(10000001) if username is None or username == '': return error_jsonify(10000001) if username == 'wechatterbot': if password == 'buaawechatterbot': data = { 'username': username, 'userId': 1, 'token': generate_token(dictionary[username]) } return jsonify(data) else: return error_jsonify(10000013) else: return error_jsonify(10000012)
def certify_token(username, token): if username not in dictionary: return False, error_jsonify(10000044, status_code=401) key = dictionary[username] token_str = base64.urlsafe_b64decode(token).decode('utf-8') token_list = token_str.split(':') if len(token_list) != 2: return False, error_jsonify(10000042, status_code=401) ts_str = token_list[0] if float(ts_str) < time.time(): # token 过期 return False, error_jsonify(10000043, status_code=401) known_sha1_ts = token_list[1] sha1 = hmac.new(key, ts_str.encode('utf-8'), 'sha1') calc_sha1_ts = sha1.hexdigest() if calc_sha1_ts != known_sha1_ts: # token 验证不成功 return False, error_jsonify(10000044, status_code=401) # token certification success data = {'result': 'success'} return True, jsonify(data)
def query(): s_text = request.args.get("text") s_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 s_id is not None and s_id != '': try: int(s_id) except Exception: return error_jsonify(10000001) statements = list(db.filter_text(id=s_id)) elif s_text is not None and s_text != '': statements = list(db.filter_text(text=s_text)) else: return error_jsonify(10000001) b, result = certify_token(username, token.encode('utf-8')) if not b: return result number = len(statements) code = 1 dict_statements = [] for s in statements: # print(s.text) s = db.Session().merge(s) dict_statements.append(_statement2dict(s)) # 调用数据接口 data = {'code': code, 'number': number, 'statements': dict_statements} # print(data) return jsonify(data)
def data_modify(pk): json = request.get_json() data, error = DataParaSchema().load(json) if error: return error_jsonify(10000001, error) data_c = DataCollection.query.filter_by(id=pk).first() if data_c is None: return error_jsonify(10000018) if data_c.status == 5: return error_jsonify(10000022) data['time'] = data_c.time data['time_id'] = data_c.time_id data['status'] = data_c.status data['user_id'] = data_c.user_id data_c.status = 5 session.add(data_c) new_data = DataCollection(**data) session.add(new_data) session.commit() return jsonify({})
def create(): 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 s_text = data['text'] s_response = data['response'] if s_text == '' or s_response == '': return error_jsonify(10000045) tag_list = [] if 'tags' in data: tags = data['tags'] tag_list = tags.split('+') # 调用数据接口 code = 0 new_statement = db.create_text(text=s_text, in_response_to=s_response, tags=tag_list) if new_statement is None: code = 0 else: code = 1 # 调用数据接口 result = {'code': code, 'statement': _statement2dict(new_statement)} return jsonify(result)
def approve_rest(rest_id): json = request.get_json() data, errors = RestApprovalParaSchema().load(json) if errors: return error_jsonify(InvalidParameters) rest = Rest.query.filter_by(Rest.id == rest_id).first() rest.is_approval = 1 schedule = Schedule.query.filter( and_(Schedule.user_id == rest.user_id, Schedule.week == rest.week, Schedule.time == rest.time)) schedule.is_rest = 1 session.commit()