def add_training_program(): json_data = request.get_json() if not json_data: return ParameterException(msg='数据为空') training_people_list = json_data['trainingPeople'] del json_data['trainingPeople'] try: data = training_program_schema.load(json_data) if TrainingProgram.query.filter_by(name=data['name'], beginDate=data['beginDate'], endDate=data['endDate']).first(): raise DatabaseRecordRepeat(msg='重复的培训项目') except ValidationError as err: return ParameterException(err.messages) except DatabaseRecordRepeat as err: return ParameterException(err.msg) if data['beginDate'] < datetime.date.today(): return ParameterException(msg="开始时间错误") if data['beginDate'] > data['endDate']: return ParameterException(msg="开始时间不能大于结束时间") with db.auto_commit(): training_program = TrainingProgram() for key, value in data.items(): setattr(training_program, key, value) training_program.isEnd = False training_people = [] for item in training_people_list: employee_training_record = EmployeeTrainingRecord() employee_training_record.employeeId = item employee_training_record.isFinishAssess = False training_people.append(employee_training_record) training_program.trainingPeople = training_people db.session.add(training_program) OperateLog.write_log(g.user.uid, "培训相关操作", "添加培训项目") return Success()
def add_job_level(): json_data = request.get_json() if not json_data: return ParameterException() try: data = job_level_schema.load(json_data) except ValidationError as err: return ParameterException(msg=err.messages) job_level = JobLevel.query.filter_by(name=data['name']).first() if job_level and job_level.status: return ParameterException(msg="重复职位!") elif job_level and job_level.status == 0: with db.auto_commit(): for key, value in data.items(): setattr(job_level, key, value) job_level.status = 1 db.session.add(job_level) else: with db.auto_commit(): job_level = JobLevel() for key, value in data.items(): setattr(job_level, key, value) db.session.add(job_level) OperateLog.write_log(g.user.uid, '职位操作', f'添加{job_level.name}职位') return Success()
def assess(): json_data = request.get_json() if not json_data: return ParameterException(msg="数据为空") record_id = json_data.get('id') if not record_id: return ParameterException(msg="缺少记录ID") del json_data['id'] try: data = employee_training_record_schema.load(json_data) except ValidationError as err: return ParameterException(err.messages) record = EmployeeTrainingRecord.query.filter_by( id=record_id).first_or_404() if record.isFinishAssess: return ParameterException(msg="重复评价") with db.auto_commit(): for key, value in data.items(): setattr(record, key, value) record.isFinishAssess = True record.finishAssessTime = datetime.datetime.now() undone_finish_assess_list = EmployeeTrainingRecord.query.filter_by( trainingProgramId=record.trainingProgramId, isFinishAssess=False).all() if len(undone_finish_assess_list) == 0: with db.auto_commit(): program = TrainingProgram.query.filter_by( id=record.trainingProgramId).first_or_404() program.isFinishAssess = True OperateLog.write_log( g.user.uid, '培训评价操作', f'对{program.name}参与人 {record.employee_info.name} 作出评价') return Success()
def modify_training_people(): json_data = request.get_json() if not json_data: return ParameterException(msg="数据为空") if not json_data.get('id'): return ParameterException(msg="项目ID为空") for item in json_data['trainingPeople']: if not isinstance(item, int): return ParameterException(msg="数据格式错误") program = TrainingProgram.query.filter_by( id=json_data['id']).first_or_404() if program.isPublish: return ParameterException(msg="已发布的项目不能修改") with db.auto_commit(): before_training_people = EmployeeTrainingRecord.query.filter_by( trainingProgramId=json_data['id']).all() temp_list = [item.employeeId for item in before_training_people] no_change = set(temp_list) & set(json_data['trainingPeople']) will_delete = list(set(temp_list) - no_change) will_add = list(set(json_data['trainingPeople']) - no_change) for item in before_training_people: if item.employeeId in will_delete: db.session.delete(item) for item in will_add: employee_training_record = EmployeeTrainingRecord() employee_training_record.employeeId = item employee_training_record.isFinishAssess = False db.session.add(employee_training_record) program.trainingPeople.append(employee_training_record) OperateLog.write_log(g.user.uid, "培训相关操作", "编辑参与人员") return Success()
def add_user(): json_data = request.get_json() del json_data['confirmPassword'] if not json_data: return ParameterException() try: data = user_schema.load(json_data) except ValidationError as err: return ParameterException(msg=err.messages) roles_list = [] for item in data['roles']: role = Role.query.filter_by(name=item['name']).first() if role: roles_list.append(role) else: return ParameterException(msg="没有此项权限!") try: with db.auto_commit(): user = User() user.username = data['username'] user.password = data['password'] if data['holderId']: user.holderId = data['holderId'] user.roles = roles_list except IntegrityError: return ParameterException(msg="用户名已存在") OperateLog.write_log(g.user.uid, '账户操作', f'添加账户{user.username}') return Success()
def delete_department(): department_id = request.get_json()['id'] if not department_id: return ParameterException() department = Department.query.filter_by(id=department_id).first_or_404() if len(department.children) > 0: return ParameterException(msg="该部门员工数不为零,不能删除") with db.auto_commit(): department.status = 0 OperateLog.write_log(g.user.uid, '部门操作', f'删除{department.name}部门') return DeleteSuccess()
def delete_job_level(): job_level_id = request.get_json()['id'] if not job_level_id: return ParameterException() job_level = JobLevel.query.filter_by(id=job_level_id).first_or_404() if len(job_level.employee) > 0: return ParameterException(msg="删除失败,该职位员工数量不为零!") with db.auto_commit(): job_level.status = 0 OperateLog.write_log(g.user.uid, '职位操作', f'删除{job_level.name}职位') return DeleteSuccess()
def modify_job_level(): json_data = request.get_json() if not json_data: return ParameterException() try: data = job_level_schema.load(json_data) except ValidationError as err: return ParameterException(msg=err.messages) job_level = JobLevel.query.filter_by(id=data['id']).first_or_404() with db.auto_commit(): for key, value in data.items(): setattr(job_level, key, value) OperateLog.write_log(g.user.uid, '职位操作', f'修改{job_level.name}职位') return Success()
def delete_training_program(): program_id = request.get_json()['id'] if not program_id: return ParameterException(msg="缺少项目ID") program = TrainingProgram.query.filter_by(id=program_id).first_or_404() if program.isPublish: return ParameterException(msg="已发布的项目不可删除") with db.auto_commit(): program.status = 0 training_people = EmployeeTrainingRecord.query.filter_by( trainingProgramId=program.id) for item in training_people: item.status = 0 OperateLog.write_log(g.user.uid, "培训相关操作", "删除培训项目") return DeleteSuccess()
def modify_department(): json_data = request.get_json() print(json_data) if not json_data: return ParameterException() try: data = department_schema.load(json_data) except ValidationError as err: return ParameterException(msg=err.messages) department = Department.query.filter_by(id=data['id']).first_or_404() with db.auto_commit(): for key, value in data.items(): if value and key != "id": setattr(department, key, value) OperateLog.write_log(g.user.uid, '部门操作', f'修改{department.name}部门') return Success()
def publish_program(): program_id = request.get_json()['id'] if not program_id: return ParameterException("未指定项目ID") program = TrainingProgram.query.filter_by(id=program_id).first_or_404() if program.isPublish: return ParameterException("项目已发布") elif program.beginDate < datetime.date.today(): return ParameterException("当前时间已超过项目开始时间,请先修改时间后再发布") elif len(program.trainingPeople) == 0: return ParameterException("当前培训项目没人参加,请先选择参与者") else: with db.auto_commit(): program.isPublish = True program.isFinishAssess = False OperateLog.write_log(g.user.uid, "培训相关操作", "发布培训项目") return Success()
def modify_program_base(): json_data = request.get_json() program_id = json_data['programId'] del json_data['programId'] if not json_data: return ParameterException(msg="数据为空") try: data = training_program_schema.load(json_data) except ValidationError as err: return ParameterException(msg=err.messages) program = TrainingProgram.query.filter_by(id=program_id).first_or_404() if program.isPublish: return ParameterException(msg="已发布的项目不能修改") with db.auto_commit(): for key, value in data.items(): setattr(program, key, value) OperateLog.write_log(g.user.uid, "培训相关操作", "编辑培训项目基本信息") return Success()
def add_department(): json_data = request.get_json() # print(json_data) if not json_data: return ParameterException() try: data = department_schema.load(json_data) if Department.query.filter_by(name=data['name']).first(): raise DatabaseRecordRepeat() except ValidationError as err: return ParameterException(msg=err.messages) except DatabaseRecordRepeat as err: return ParameterException(msg=err.msg) with db.auto_commit(): department = Department() for key, value in data.items(): setattr(department, key, value) db.session.add(department) OperateLog.write_log(g.user.uid, '部门操作', f'添加{department.name}部门') return Success()
def modify_user(): json_data = request.get_json() if not json_data: return ParameterException() if not json_data.get('id'): return ParameterException(msg="缺少要修改账户的ID") try: account_id = int(json_data.get('id')) except ValueError: return ParameterException("账户ID格式错误") user = User.query.filter_by(id=account_id).first_or_404() del json_data['id'] try: data = user_schema.load(json_data, partial=True) except ValidationError as err: return ParameterException(msg=err.messages) with db.auto_commit(): user.password = data['password'] OperateLog.write_log(g.user.uid, '账户操作', f'修改账户{user.username}的密码') return Success()
def login(): json_data = request.get_json() if not json_data: return ParameterException() try: data = user_schema.load(json_data) except ValidationError as err: return ParameterException(msg=err.messages) identity = User.verify(data['username'], data['password']) expiration = current_app.config['TOKEN_EXPIRATION'] token = generate_auth_token(identity['uid'], data['username'], identity['roles'], identity['holderId'], expiration) user = User.query.filter_by(username=data['username']).first() with db.auto_commit(): user.lastLoginIp = request.remote_addr t = { 'token': token.decode('ascii') } OperateLog.write_log(user.id, '账户操作', '登录') return jsonify(t)