def validate(self, attrs):
        remove = attrs.get('remove')
        role = attrs.get('role')
        organization = attrs.get('organization')
        username = attrs.get('username')

        # check if roles are downgrading and the user is the last admin
        if username and (remove or role != OwnerRole.name):
            user = User.objects.get(username=username)

            owners = _get_owners(organization)
            if user in owners and len(owners) <= 1:
                raise serializers.ValidationError(
                    _("Organization cannot be without an owner"))

        return attrs
    def validate(self, attrs):
        remove = attrs.get('remove')
        role = attrs.get('role')
        organization = attrs.get('organization')
        username = attrs.get('username')

        # check if roles are downgrading and the user is the last admin
        if username and (remove or role != OwnerRole.name):
            user = User.objects.get(username=username)

            owners = _get_owners(organization)
            if user in owners and len(owners) <= 1:
                raise serializers.ValidationError(
                    _("Organization cannot be without an owner"))

        return attrs
예제 #3
0
 def _set_organization_role_to_user(org, user, role_cls):
     owners = _get_owners(organization)
     if user in owners and len(owners) <= 1:
         raise ValidationError(_("Organization cannot be without an owner"))
     else:
         role_cls.add(user, organization)