예제 #1
0
    def patch(self, request, *args, **kwargs):    # pylint: disable=unused-argument
        '''
        调整子组 [PATCH]
        操作包括
        - 排序sort
        - 移动add, 即修改父部门。从无到有的添加,由create负责
        '''
        parent_group = self.get_object()
        data = json.loads(request.body.decode('utf-8'))
        subject = data.get('subject', '')
        if subject not in ['sort', 'add']:
            raise ValidationError({'subject': ['this field must be `sort` or `add`']})

        filters = {}
        if subject == 'sort':
            filters = {'parent': parent_group}

        group_uids = get_patch_scope(request)

        try:
            groups = Group.get_from_pks(pks=group_uids, pk_name='uid', raise_exception=True, **filters)
        except ObjectDoesNotExist as error:
            bad_uid = error.args[0]
            raise ValidationError({'group_uids': ['group:{} invalid'.format(bad_uid)]})

        cli = CLI()
        if subject == 'sort':
            cli.sort_groups_in_group(groups, parent_group)
        elif subject == 'add':
            for group in groups:
                cli.move_group_to_group(group, parent_group)
        return Response(GroupListSerializer(parent_group).data)
예제 #2
0
def get_groups_from_uids(group_uids):
    '''
    根据group uid按顺序返回group list
    '''
    try:
        groups = Group.get_from_pks(pks=group_uids,
                                    pk_name='uid',
                                    raise_exception=True,
                                    is_del=False)
    except ObjectDoesNotExist as error:
        bad_uid = error.args[0]
        raise ValidationError(
            {'group_uids': ['group:{} does not exist'.format(bad_uid)]})
    return groups