Example #1
0
def department_create_department(parent_id, name):
    '''
    [
        {"name": "parent_id", "required": 1, "check": "int", "description": "父部门ID"},
        {"name": "name", "required": 1, "check": "string", "description": "部门名"}
    ]
    '''
    # 规定parent_id = -1时,是根部门
    if parent_id == -1:
        parent = Department.get_root_department(current_enterprise.object_id)
    else:
        parent = Department.query.filter(
            Department.enterprise_id == current_enterprise.object_id,
            Department.object_id == parent_id).first()
        if not parent:
            return custom(-1, "部门不存在")
    department = Department(name=name,
                            parent_id=parent.object_id,
                            enterprise_id=current_enterprise.object_id)
    db.session.add(department)

    def callback(dep):
        return dep.to_json()

    return commit_callback(callback=callback, param=(department, ))
Example #2
0
 def wrapper(*args, **kwargs):
     account_id = request.headers.get("AccountID", default=int)
     token = request.headers.get("Token")
     account = Account.query.filter_by(object_id=account_id,
                                       app_token=token).first()
     if not account:
         return custom(-99, "登录失效,请重新登录")
     account_stack.push(account)
     return func(*args, **kwargs)
Example #3
0
 def wrapper(*args, **kwargs):
     if func.__doc__:
         params = json.loads(func.__doc__)
         if request.method == "GET":
             for param in params:
                 status, msg, data = check_required_and_type(
                     param, request.args.get(param["name"]))
                 if not status:
                     return custom(-1, msg)
                 kwargs[param["name"]] = data
         else:
             for param in params:
                 status, msg, data = check_required_and_type(
                     param, request.form.get(param["name"]))
                 if not status:
                     return custom(-1, msg)
                 kwargs[param["name"]] = data
     return func(*args, **kwargs)
Example #4
0
def department_delete_department(department_id):
    '''
    [
        {"name": "department_id", "required": 1, "check": "int", "description": "部门ID"}
    ]
    '''
    department = Department.query.filter(
        Department.enterprise_id == current_enterprise.object_id,
        Department.object_id == department_id).first()
    if not department:
        return custom(-1, "参数错误")
    # 当部门下没有人也没有子部门时,可以删除
    dep_count = department.get_sub_departments_query().count()
    if dep_count:
        return custom(-2, "部门下有子部门,请先删除子部门")
    employee_count = department.get_current_employees_query().count()
    if employee_count:
        return custom(-3, "部门下有员工,请移除部门下所有员工")
    db.session.delete(department)
    return commit()
Example #5
0
def department_get_department_list(department_id, get_employee_info,
                                   sub_department_count, sub_employee_count,
                                   all_sub_department_count,
                                   all_sub_employee_count):
    '''
    [
        {"name": "department_id", "required": 1, "check": "int", "description": "父部门ID"},
        {"name": "get_employee_info", "required": 0, "check": "bool", "description": "是否包含当前部门员工信息"},
        {"name": "sub_department_count", "required": 0, "check": "bool", "description": "部门信息包含一级子部门数"},
        {"name": "sub_employee_count", "required": 0, "check": "bool", "description": "部门信息包含一级子员工数"},
        {"name": "all_sub_department_count", "required": 0, "check": "bool", "description": "部门信息包含所有子部门数"},
        {"name": "all_sub_employee_count", "required": 0, "check": "bool", "description": "部门信息包含所有子员工数"}
    ]
    '''
    if department_id == -1:
        department = Department.get_root_department()
    else:
        department = Department.query.filter(
            Department.enterprise_id == current_enterprise.object_id,
            Department.object_id == department_id).first()
        if not department:
            return custom(-1, "参数错误")
    departments = Department.query.filter(
        Department.enterprise_id == current_enterprise.object_id,
        Department.parent_id == department.object_id).all()
    department_info = []
    for department in departments:
        res = department.to_json()
        if sub_department_count:
            res["sub_department_count"] = department.get_sub_department_query(
            ).count()
        if sub_employee_count:
            res["employee_count"] = department.get_current_employees_query(
            ).count()
        if all_sub_department_count:
            res["all_sub_department_count"] = department.get_all_sub_department_query(
            ).count()
        if all_sub_employee_count:
            res["all_employee_count"] = department.get_all_employees_query(
            ).count()
        department_info.append(res)
    employee_info = []
    if get_employee_info:
        depmems = DepartmentMem.query.options(
            joinedload(DepartmentMem.employee).joinedload(
                Employee.account)).filter(
                    DepartmentMem.department_id == department.object_id).all()
        for depmem in depmems:
            employee_info.append(depmem.to_json())
    return success(data={
        "department_info": department_info,
        "employee_info": employee_info
    })
Example #6
0
 def wrapper(*args, **kwargs):
     enterprise_id = request.headers.get("EnterpriseID", default=int)
     employee = Employee.query.join(Enterprise).options(
         joinedload(Employee.enterprise),
         joinedload(Employee.account)).filter(
             Enterprise.logout == False,
             Employee.enterprise_id == enterprise_id,
             Employee.account_id == current_account.object_id).first()
     if not employee:
         return custom(-100, "您不在该企业中")
     employee_stack.push(employee)
     enterprise_stack.push(employee.enterprise)
     return func(*args, **kwargs)
Example #7
0
def auth_login(phone, passwd):
    '''
    [
        {"name": "phone", "required": 1, "check": "string", "description": "手机号"},
        {"name": "passwd", "required": 1, "check": "string", "description": "密码"}
    ]
    '''
    account = Account.query.filter_by(phone=phone, passwd=Account.md5_passwd(passwd)).first()
    if not account:
        return custom(-1, "账户名或密码错误")
    account.app_token = uuid.uuid4().hex

    def callback(param):
        return param.to_json()

    return commit_callback(callback=callback, param=(account, ))
Example #8
0
def auth_register(phone, passwd, name):
    '''
    [
        {"name": "phone", "required": 1, "check": "string", "description": "手机号"},
        {"name": "passwd", "required": 1, "check": "string", "description": "密码"},
        {"name": "name", "required": 1, "check": "string", "description": "姓名"}
    ]
    '''
    # 是否注册过
    account = Account.query.filter_by(phone=phone).first()
    if account:
        return custom(-1, "该账号已注册")
    account = Account(
        phone=phone,
        passwd=Account.md5_passwd(passwd),
        name=name
    )
    db.session.add(account)

    def callback(param):
        return param.to_json()

    return commit_callback(callback=callback, param=(account, ))