def sort_groups_in_group(self, groups, parent_group): ''' 调整一批组在父组中的排序 ''' for group in groups: if group.parent != parent_group: raise ValidationError({'node': ['unrelated']}) Group.sort_as(groups)
def create_group(self, group_info): ''' 创建组 ''' serializer = GroupDetailSerializer(data=group_info) serializer.is_valid(raise_exception=True) serializer.save() group = serializer.instance group.order_no = Group.get_max_order_no(parent=group.parent) + 1 group.save() # 批量创建组权限 # 1) 去重 perm_ids = Perm.valid_objects.values_list('pk', flat=True) exist_perm_ids = GroupPerm.valid_objects.filter( owner=serializer.instance).values_list('perm_id', flat=True) perm_ids = set(perm_ids).difference(exist_perm_ids) # 2) 批量创建 group_perms = [ GroupPerm(owner=serializer.instance, perm_id=x) for x in perm_ids ] GroupPerm.objects.bulk_create(group_perms) return serializer.instance
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)
def move_group_to_group(self, group, parent_group): ''' 将一个已有组移至另一组下 ''' if group == parent_group or parent_group.if_belong_to_group( group, recursive=True): raise ValidationError({'node': ['deadlock']}) if parent_group.top != 'root' and group.top != parent_group.top: raise ValidationError({'node': ["across_scope"]}) group.order_no = Group.get_max_order_no(parent=parent_group) + 1 group.parent = parent_group group.save(update_fields=['order_no', 'parent'])
def add_group_to_group(self, group, parent_group): ''' 将一个新组加入另一个组作为后者子组 ''' if group == parent_group or parent_group.if_belong_to_group( group, recursive=True): raise ValidationError({'node': ['deadlock']}) group.parent = parent_group if parent_group.uid == 'intra': group.top = group.uid else: group.top = parent_group.top group.order_no = Group.get_max_order_no(parent=parent_group) + 1 group.save(update_fields=['order_no', 'parent', 'top'])
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
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) return Response(data, status=status.HTTP_201_CREATED)
def create_group(self, group_info): ''' 创建组 ''' serializer = GroupDetailSerializer(data=group_info) serializer.is_valid(raise_exception=True) serializer.save() group = serializer.instance group.order_no = Group.get_max_order_no(parent=group.parent) + 1 group.save() for perm in Perm.valid_objects.all(): GroupPerm.valid_objects.get_or_create(owner=serializer.instance, perm=perm) return serializer.instance
def create(self, request, *args, **kwargs): ''' qq扫码加绑定 ''' if not AccountConfig.get_current().support_qq_qr_register: return Response({'err_msg': 'qq qr register not allowed'}, HTTP_403_FORBIDDEN) serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) user = self.perform_create(serializer) 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) open_id = serializer.validated_data['user_id'] qq_user = QQUser.valid_objects.create(open_id=open_id, user=user) qq_user.save() return Response(data, status=status.HTTP_201_CREATED)