def _sync_scim(url): users = User.objects.all() user_srl = UserSerializer(users, many=True) print('---user data', user_srl.data) user_resp = requests.post(os.path.join(url, 'users'), json=user_srl.data) depts = Dept.objects.all() dept_srl = DeptDetailSerializer(depts, many=True) print('---user data', dept_srl.data) dept_resp = requests.post(os.path.join(url, 'groups'), json=dept_srl.data)
def create_dept(self, dept_info): ''' 创建部门 ''' serializer = DeptDetailSerializer(data=dept_info) serializer.is_valid(raise_exception=True) serializer.save() dept = serializer.instance dept.order_no = Dept.get_max_order_no(parent=dept.parent) + 1 dept.save() # 批量创建组权限 # 1) 去重 perm_ids = Perm.valid_objects.values_list('pk', flat=True) exist_perm_ids = DeptPerm.valid_objects.filter( owner=serializer.instance).values_list('perm_id', flat=True) perm_ids = set(perm_ids).difference(exist_perm_ids) # 2) 批量创建 dept_perms = [ DeptPerm(owner=serializer.instance, perm_id=x) for x in perm_ids ] DeptPerm.objects.bulk_create(dept_perms) return serializer.instance
def update_dept(self, dept, dept_info): ''' 更新部门信息 ''' serializer = DeptDetailSerializer(dept, data=dept_info, partial=True) serializer.is_valid(raise_exception=True) serializer.save() return serializer.instance
def create(self, request, *args, **kwargs): # pylint: disable=unused-argument ''' 添加子部门,从无到有 [POST] ''' parent_dept = self.get_object() dept_data = json.loads(request.body.decode('utf-8')) uid = dept_data.get('uid', '') if not uid: name = dept_data.get('name') uid = gen_uid(name=name, cls=Dept) dept_data['uid'] = uid dept_data.update(parent_uid=self.kwargs['uid']) cli = CLI() child_dept = cli.create_dept(dept_data) cli.add_dept_to_dept(child_dept, parent_dept) return Response(DeptDetailSerializer(child_dept).data, status=status.HTTP_201_CREATED)
def create_dept(self, dept_info): ''' 创建部门 ''' serializer = DeptDetailSerializer(data=dept_info) serializer.is_valid(raise_exception=True) serializer.save() dept = serializer.instance dept.order_no = Dept.get_max_order_no(parent=dept.parent) + 1 dept.save() for perm in Perm.valid_objects.all(): DeptPerm.valid_objects.create(owner=serializer.instance, perm=perm) return dept
def generate_dept_payload(dept): data = DeptDetailSerializer(dept).data return json.dumps(data)