Exemple #1
0
    def create(self, request, *args, **kwargs):  # pylint: disable=unused-argument
        '''
        添加子组,从无到有 [POST]
        '''
        parent_group = self.get_object()
        group_data = request.data
        uid = group_data.get('uid', '')
        if 'manager_group' in group_data:
            if parent_group.uid == 'manager':
                if not group_data.get('name', ''):
                    name = "".join(
                        random.choice(string.ascii_lowercase)
                        for _ in range(8))
                    group_data.update(name=name)
            else:
                group_data.pop('manager_group')
        if not uid:
            name = group_data.get('name')
            if not name:
                raise ValidationError({'name': ['this field is required']})
            uid = gen_uid(name=name, cls=Group)
            group_data['uid'] = uid

        cli = CLI()
        group_data.update(parent_uid=self.kwargs['uid'])
        child_group = cli.create_group(group_data)
        cli.add_group_to_group(child_group, parent_group)

        if parent_group.uid == 'intra':
            self._auto_create_manager_group(request, child_group)
        transaction.on_commit(
            lambda: WebhookManager.group_created(child_group))
        return Response(GroupDetailSerializer(child_group).data,
                        status=status.HTTP_201_CREATED)
Exemple #2
0
 def delete(self, request, *args, **kwargs):
     '''
     delete user [DELETE]
     '''
     user = self.get_object()
     CLI().delete_users([user])
     transaction.on_commit(lambda: WebhookManager.user_deleted(user))
     return Response(status=status.HTTP_204_NO_CONTENT)
Exemple #3
0
 def update(self, request, *args, **kwargs):  # pylint: disable=unused-argument
     '''
     更新组信息  [PATCH]
     '''
     group = self.get_object()
     data = json.loads(request.body.decode('utf-8'))
     group = CLI().update_group(group, data)
     transaction.on_commit(lambda: WebhookManager.group_updated(group))
     return Response(self.get_serializer(group).data)
Exemple #4
0
 def perform_destroy(self, instance):
     '''
     删除组 [DELETE]
     '''
     if self.request.query_params.get('ignore_user',
                                      '') in ('true', 'True'):
         CLI().delete_users_from_group(instance.users, instance)
     CLI().delete_group(instance)
     transaction.on_commit(lambda: WebhookManager.group_deleted(instance))
Exemple #5
0
 def update(self, request, *args, **kwargs):  # pylint: disable=unused-argument
     '''
     更新部门信息
     '''
     dept = self.get_object()
     data = json.loads(request.body.decode('utf-8'))
     dept = CLI().update_dept(dept, data)
     transaction.on_commit(lambda: WebhookManager.dept_updated(dept))
     return Response(self.get_serializer(dept).data)
Exemple #6
0
 def update(self, request, *args, **kwargs):  # pylint: disable=unused-argument
     '''
     update user detail [PATCH]
     '''
     user = self.get_object()
     data = request.data
     user = CLI().update_user(user, data)
     transaction.on_commit(lambda: WebhookManager.user_updated(user))
     return Response(UserSerializer(user).data)
Exemple #7
0
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()
        if 'private_email' in serializer.validated_data:
            user.origin = 4
        if 'mobile' in serializer.validated_data:
            user.origin = 3
        user.save()

        cli = CLI(user)
        cli.add_users_to_group([user], Group.get_extern_root())
        data = self.read_serializer_class(user).data
        data.update(token=user.token)
        transaction.on_commit(lambda: WebhookManager.user_registered(user))
        return Response(data, status=status.HTTP_201_CREATED)
Exemple #8
0
 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)
     transaction.on_commit(lambda: WebhookManager.dept_created(child_dept))
     return Response(DeptDetailSerializer(child_dept).data,
                     status=status.HTTP_201_CREATED)
Exemple #9
0
    def create(self, request, *args, **kwargs):  # pylint: disable=unused-argument
        '''
        create user [POST]
        '''
        data = request.data
        user_info = data.get('user', '')

        group_uids = []
        dept_uids = []

        node_uids = data.get('node_uids', [])
        if node_uids:
            for node_uid in node_uids:
                if node_uid.startswith(Dept.NODE_PREFIX):
                    dept_uids.append(node_uid.replace(Dept.NODE_PREFIX, '', 1))
                elif node_uid.startswith(Group.NODE_PREFIX):
                    group_uids.append(node_uid.replace(
                        Group.NODE_PREFIX,
                        '',
                    ))
        else:
            group_uids = data.get('group_uids', [])
            dept_uids = data.get('dept_uids', [])
        cli = CLI()
        password = user_info.pop('password', None)
        # 创建用户
        user = cli.create_user(user_info)
        if password:
            validate_password(password)
            cli.set_user_password(user, password)
        user.origin = 1  # 管理员添加
        user.save()
        # 分配组和部门
        self.assign_user(user, dept_uids=dept_uids, group_uids=group_uids)
        user_serializer = EmployeeSerializer(user)
        transaction.on_commit(lambda: WebhookManager.user_created(user))
        return Response(
            user_serializer.data,
            status=status.HTTP_201_CREATED,
            headers=self.get_success_headers(user_serializer.data),
        )