def validate(self, attrs): attrs['content_type'] = ContentType.objects.get_for_model( model=self.context['content_object']) attrs['object_id'] = self.context['content_object'].pk try: attrs['role'] = Role.objects.get(pk=attrs.pop('role_pk')) except Role.DoesNotExist as exception: raise ValidationError(force_text(exception)) permissions_pk_list = attrs.pop('permissions_pk_list', None) permissions_result = [] if permissions_pk_list: for pk in permissions_pk_list.split(','): try: permission = Permission.get(pk=pk) except KeyError: raise ValidationError(_('No such permission: %s') % pk) else: # Accumulate valid stored permission pks permissions_result.append(permission.pk) instance = AccessControlList(**attrs) try: instance.full_clean() except DjangoValidationError as exception: raise ValidationError(exception) # Add a queryset of valid stored permissions so that they get added # after the ACL gets created. attrs['permissions'] = StoredPermission.objects.filter( pk__in=permissions_result) return attrs
def validate(self, attrs): permissions_pk_list = attrs.pop('permission_pk', None) permissions_result = [] if permissions_pk_list: for pk in permissions_pk_list.split(','): try: permission = Permission.get(pk=pk) except KeyError: raise ValidationError(_('No such permission: %s') % pk) else: # Accumulate valid stored permission pks permissions_result.append(permission.pk) attrs['permissions'] = StoredPermission.objects.filter( pk__in=permissions_result) return attrs