示例#1
0
    def post(self, request, *args, **kwargs):
        request_data = request.data.get('data', {})
        validator = CreateDepartmentMembersValidator(data=request_data)
        if not validator.is_valid():
            return Response(console_response(code=1, msg=validator.errors))

        department_id = validator.validated_data.get('department_id')
        member_type = validator.validated_data.get('member_type')

        validator = self.get_create_member_validator(member_type)(
            data=request_data)
        if not validator.is_valid():
            return Response(console_response(code=1, msg=validator.errors))

        member_info = validator.validated_data.get('member_info')
        member, error = AccountService.create(**member_info)
        if error:
            return Response(
                console_response(code=1, ret_set={},
                                 msg='成员创建失败' + str(error)))
        elif DepartmentMemberService.join([member.user], department_id):
            # 创建成功 就加入到指定部门
            new_member = AccountService.get_by_owner(member.user)
            ret_set = DepartmentMemberService.to_dict(new_member)
            return Response(console_response(ret_set=ret_set))

        else:
            AccountService.delete_by_username(member.user.username,
                                              really_delete=True)
            return Response(
                console_response(code=1, ret_set={}, msg='成员加入部门失败'))
示例#2
0
    def post(self, request, *args, **kwargs):
        request_data = request.data.get('data', {})
        validator = CreateDepartmentMembersValidator(data=request_data)
        if not validator.is_valid():
            return Response(console_response(code=1, msg=validator.errors))

        department_id = validator.validated_data.get('department_id')
        member_type = validator.validated_data.get('member_type')

        validator = self.get_create_member_validator(member_type)(
            data=request_data)
        if not validator.is_valid():
            return Response(console_response(code=1, msg=validator.errors))

        member_info = validator.validated_data.get('member_info')

        member, error = AccountService.create(**member_info)
        if error:
            return Response(
                console_response(code=1, ret_set={},
                                 msg='成员创建失败' + str(error)))
        elif DepartmentMemberService.join([member.user], department_id):
            # 创建成功 就加入到指定部门
            if member.type in AccountType.FINANCE_TYPES:
                permission_group_id_list = validator.validated_data.get(
                    'permission_group_id_list')
                new_member = AccountService.get_by_owner(member.user)
                groups = PermissionGroupService.gets(permission_group_id_list)
                count = UserPermissionService.append_groups(new_member, groups)
                ret_set = DepartmentMemberService.to_dict(new_member)
                if count == len(permission_group_id_list):
                    # create_payload = {
                    #     "owner": member.user.username,
                    #     "zone": request_data.get("zone", "bj")
                    # }
                    # create_resp = create_base_net(create_payload)
                    # if create_resp.get("ret_code"):
                    #     return Response(console_response(code=1, msg="创建成员子网失败"))
                    # ret_set["sub_net"] = create_resp.get("ret_set")[0]
                    #
                    return Response(console_response(ret_set=ret_set))
                else:
                    return Response(
                        console_response(code=1,
                                         ret_set=ret_set,
                                         msg='成员初始化权限失败'))
            elif member.type in AccountType.PORTAL_TYPES:
                new_member = AccountService.get_by_owner(member.user)
                ret_set = DepartmentMemberService.to_dict(new_member)
                return Response(console_response(ret_set=ret_set))

        else:
            AccountService.delete_by_username(member.user.username,
                                              really_delete=True)
            return Response(
                console_response(code=1, ret_set={}, msg='成员加入部门失败'))
示例#3
0
    def post(self, request, *args, **kwargs):
        request_data = request.data.get('data', {})
        validator = DescribeDepartmentMemberDetailValidator(data=request_data)
        if not validator.is_valid():
            return Response(console_response(code=1, msg=validator.errors))

        member_id = validator.validated_data.get('member_id')

        member = DepartmentMemberService.get(member_id)
        member_info = DepartmentMemberService.to_dict(member, detail=True)

        return Response(console_response(ret_set=member_info))
示例#4
0
    def post(self, request, *args, **kwargs):
        request_data = request.data.get('data', {})
        validator = DisableDepartmentMemberValidator(data=request_data)
        if not validator.is_valid():
            return Response(console_response(code=1, msg=validator.errors))
        member_list = validator.validated_data.get('member_list')

        members = DepartmentMemberService.disable(member_list)
        members_info = []
        for member in members:
            members_info.append(DepartmentMemberService.to_dict(member))

        return Response(console_response(ret_set=members_info))
示例#5
0
    def post(self, request, *args, **kwargs):
        request_data = request.data.get('data', {})
        validator = DescribeDepartmentMemberValidator(data=request_data)
        if not validator.is_valid():
            return Response(console_response(code=1, msg=validator.errors))

        department_id = validator.validated_data.get('department_id')
        page_num = validator.validated_data.get('page_num')
        page_size = validator.validated_data.get('page_size')

        members = DepartmentService.get_members(department_id)

        paged_data = paging(members, page_num, page_size)
        page_num = paged_data['page_num']
        page_size = paged_data['page_size']
        total_page = paged_data['total_page']
        total_item = paged_data['total_item']
        member_list = []
        for member in paged_data['data']:
            member_list.append(DepartmentMemberService.to_dict(member))

        ret_set = dict(page_num=page_num,
                       page_size=page_size,
                       total_page=total_page,
                       total_item=total_item,
                       member_list=member_list)

        return Response(console_response(ret_set=ret_set))
示例#6
0
    def handle(self, *args, **options):

        result = DepartmentService.remove_all()
        if not result:
            logger.error('init failed')
            return
        for department_info in ROOT_DEPARTMENT:
            self.create_root_department(department_info)
            print 'import department: %s(%s) done !' % (
                department_info['department_name'], department_info['department_id'])

        accounts = Account.objects.filter(type=AccountType.TENANT, deleted=False)
        members = [account.user for account in accounts]
        DepartmentMemberService.join(members, 'dep-00000002')

        accounts = Account.objects.filter(type=AccountType.HANKOU, deleted=False)
        members = [account.user for account in accounts]
        DepartmentMemberService.join(members, 'dep-00000001')
示例#7
0
    def post(self, request, *args, **kwargs):
        request_data = request.data.get('data', {})
        validator = RemoveDepartmentMemberValidator(data=request_data)
        if not validator.is_valid():
            return Response(console_response(code=1, msg=validator.errors))
        member_list = validator.validated_data.get('member_list')

        result = DepartmentMemberService.remove(member_list)
        if result:
            return Response(console_response(ret_set=[]))
        else:
            return Response(console_response(code=1, msg='删除用户失败', ret_set=[]))
示例#8
0
    def post(self, request, *args, **kwargs):
        request_data = request.data.get('data', {})
        validator = ChangeDepartmentValidator(data=request_data)
        if not validator.is_valid():
            return Response(console_response(code=1, msg=validator.errors))

        department_id = validator.validated_data.get('department_id')
        member_list = validator.validated_data.get('member_list')
        accounts = AccountService.get_all_by_owner(member_list)
        members = [account.user for account in accounts]
        ret_set = DepartmentMemberService.join(members, department_id)

        return Response(console_response(ret_set=ret_set))
示例#9
0
    def handle(self, *args, **options):
        company_name = raw_input('Create Department:\nInput company name: ')

        if 'clear' == options.get('clear').lower():
            result = DepartmentService.remove_all()
            if not result:
                logger.error('clear failed')
                return
            department = Department(name=company_name,
                                    description=gen_description(),
                                    department_id=FIRST_DEPARTMENT_ID,
                                    path=FIRST_DEPARTMENT_ID,
                                    level=0)
            department.save()
            department.parent_department = department
            department.save()
        else:
            Department.objects.filter(
                department_id=FIRST_DEPARTMENT_ID).update(name=company_name)

        accounts = Account.objects.filter(deleted=False).exclude(
            name='system_image')
        members = [account.user for account in accounts]
        DepartmentMemberService.join(members, FIRST_DEPARTMENT_ID)