Exemplo n.º 1
0
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()
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
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()
Exemplo n.º 4
0
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()
Exemplo n.º 5
0
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()
Exemplo n.º 6
0
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()
Exemplo n.º 7
0
def delete_user():
    json_data = request.get_json()
    if not json_data:
        return ParameterException(msg="数据为空")
    user_id = json_data.get('id')
    if not user_id:
        return ParameterException(msg="缺少账号ID")
    account = User.query.filter_by(id=user_id).first_or_404()
    with db.auto_commit():
        db.session.delete(account)
    return DeleteSuccess()
Exemplo n.º 8
0
def change_password():
    json_data = request.get_json()
    if not json_data:
        ParameterException(msg="数据为空")
    try:
        data = user_schema.load(json_data, partial=True)
    except ValidationError as err:
        return ParameterException(msg=err.messages)
    user = User.query.filter_by(id=g.user.uid).first_or_404()
    with db.auto_commit():
        user.password = data['password']
    return Success()
Exemplo n.º 9
0
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()
Exemplo n.º 10
0
def get_operate_log():
    page = request.args.get('page')
    page_size = request.args.get('pageSize')
    operate_type = request.args.get('type')
    start_time = request.args.get('startTime')
    end_time = request.args.get('endTime')
    if not page or not page_size:
        return ParameterException(msg="缺少分页信息")
    try:
        page = int(page)
        page_size = int(page_size)
    except ValueError:
        return ParameterException("当前页或页数格式错误")
    user_id = g.user.uid
    if operate_type and not start_time and not end_time:
        operate_log_list_pagination = OperateLog.query.filter(
            OperateLog.operateType.like(f'%{operate_type}%'),
            OperateLog.operatorId == user_id,
            OperateLog.status == 1).paginate(page,
                                             per_page=page_size,
                                             error_out=False)
    elif start_time and end_time and not operate_type:
        operate_log_list_pagination = OperateLog.query.filter(
            and_(OperateLog.create_time >= time_str2timestamp(start_time),
                 OperateLog.create_time <= time_str2timestamp(end_time),
                 OperateLog.operatorId == user_id,
                 OperateLog.status == 1)).paginate(page,
                                                   per_page=page_size,
                                                   error_out=False)
    elif operate_type and start_time and end_time:
        operate_log_list_pagination = OperateLog.query.filter(
            and_(OperateLog.create_time >= time_str2timestamp(start_time),
                 OperateLog.create_time <= time_str2timestamp(end_time),
                 OperateLog.operateType.like(f'%{operate_type}%'),
                 OperateLog.status == 1)).paginate(page,
                                                   per_page=page_size,
                                                   error_out=False)
    else:
        operate_log_list_pagination = OperateLog.query.filter_by().paginate(
            page, per_page=page_size, error_out=False)
    result = {'data': []}
    for record in operate_log_list_pagination.items:
        json = {
            'id': record.id,
            'type': record.operateType,
            'content': record.operateContent,
            'time': record.create_datetime.strftime('%Y-%m-%d %H:%M:%S')
        }
        result['data'].append(json)
    result['total'] = operate_log_list_pagination.total
    return jsonify(result)
Exemplo n.º 11
0
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()
Exemplo n.º 12
0
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()
Exemplo n.º 13
0
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()
Exemplo n.º 14
0
def get_department():
    department_id = request.args.get('id')
    if not department_id:
        return ParameterException()
    department = Department.query.filter_by(id=department_id).first_or_404()
    result = department_schema.dump(department)
    return jsonify(result)
Exemplo n.º 15
0
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()
Exemplo n.º 16
0
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()
Exemplo n.º 17
0
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()
Exemplo n.º 18
0
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()
Exemplo n.º 19
0
def get_all_operate_log():
    page = request.args.get('page')
    page_size = request.args.get('pageSize')
    params = []
    if not page or not page_size:
        return ParameterException(msg="缺少分页信息")
    try:
        page = int(page)
        page_size = int(page_size)
    except ValueError:
        return ParameterException("当前页或页数格式错误")
    if request.args.get('type'):
        operate_type = request.args.get('type')
        params.append(OperateLog.operateType.like(f'%{operate_type}%'))
    if request.args.get('startTime') and request.args.get('endTime'):
        start_time = request.args.get('startTime')
        end_time = request.args.get('endTime')
        params.append(OperateLog.create_time >= time_str2timestamp(start_time))
        params.append(OperateLog.create_time <= time_str2timestamp(end_time))
    if request.args.get('operator'):
        operator_id = request.args.get('operator')
        params.append(OperateLog.operatorId == operator_id)
    params.append(OperateLog.status == 1)
    operate_log_list_pagination = OperateLog.query.filter(*params).paginate(
        page, per_page=page_size, error_out=False)
    result = {'data': []}
    for record in operate_log_list_pagination.items:
        json = {
            'id': record.id,
            'operateAccount': record.user.username,
            'type': record.operateType,
            'content': record.operateContent,
            'time': record.create_datetime.strftime('%Y-%m-%d %H:%M:%S')
        }
        if not record.user.holderId:
            json['accountHolder'] = record.user.holder + '(管理员)'
        else:
            json['accountHolder'] = record.user.holder_info.name
        result['data'].append(json)
    result['total'] = operate_log_list_pagination.total
    return jsonify(result)
Exemplo n.º 20
0
def get_undone_assess_program_done_assess_people_list():
    program_id = request.args.get('id')
    if not program_id:
        return ParameterException(msg="缺少项目ID")
    record_list = EmployeeTrainingRecord.query.filter_by(
        trainingProgramId=program_id, isFinishAssess=True).all()
    result = employee_training_records_schema.dump(record_list)
    for index, item in enumerate(result):
        item['employeeName'] = record_list[index].employee_info.name
        item['employeeDepartment'] = record_list[
            index].employee_info.department.name
    return jsonify(result)
Exemplo n.º 21
0
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)
Exemplo n.º 22
0
def get_program_detail():
    program_id = request.args.get('id')
    just_participant = request.args.get('justParticipant')
    if not program_id:
        return ParameterException('缺少项目ID')
    program = TrainingProgram.query.filter_by(id=program_id).first_or_404()
    if not just_participant:
        result = training_program_schema.dump(program)
        result['participant'] = []
        for item in program.trainingPeople:
            json = {
                'id': item.employee_info.id,
                'name': item.employee_info.name,
                'type': item.employee_info.type,
                'phone': item.employee_info.phone,
                'email': item.employee_info.email,
                'departmentName': item.employee_info.department.name
            }
            result['participant'].append(json)
    else:
        result = [item.employeeId for item in program.trainingPeople]
    return jsonify(result)