Revert "Use UniqueTogetherValidator instead of custom validate function"

This reverts commit 6df035e0f1c6a4be53bca23c839429b4fbaaf7f5.
This commit is contained in:
Chandrapal Badshah
2025-04-30 15:13:13 +05:30
parent 896df5776f
commit 337cf59425
2 changed files with 19 additions and 9 deletions
+10 -2
View File
@@ -5485,8 +5485,16 @@ class TestLighthouseConfigViewSet:
assert any(field in error["source"]["pointer"] for error in errors)
def test_lighthouse_config_create_duplicate(
self, authenticated_client, valid_config_payload, lighthouse_config_fixture
self, authenticated_client, valid_config_payload
):
# Create first config
response = authenticated_client.post(
reverse("lighthouseconfig-list"),
data=valid_config_payload,
content_type=API_JSON_CONTENT_TYPE,
)
assert response.status_code == status.HTTP_201_CREATED
# Try to create second config for same tenant
response = authenticated_client.post(
reverse("lighthouseconfig-list"),
@@ -5495,7 +5503,7 @@ class TestLighthouseConfigViewSet:
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert (
"Lighthouse configuration already exists for this tenant"
"AI configuration already exists for this tenant"
in response.json()["errors"][0]["detail"]
)
+9 -7
View File
@@ -7,7 +7,6 @@ from django.contrib.auth.models import update_last_login
from django.contrib.auth.password_validation import validate_password
from drf_spectacular.utils import extend_schema_field
from jwt.exceptions import InvalidKeyError
from rest_framework.validators import UniqueTogetherValidator
from rest_framework_json_api import serializers
from rest_framework_json_api.serializers import ValidationError
from rest_framework_simplejwt.exceptions import TokenError
@@ -2182,13 +2181,16 @@ class LighthouseConfigCreateSerializer(RLSSerializer, BaseWriteSerializer):
"business_context",
"is_active",
]
validators = [
UniqueTogetherValidator(
queryset=LighthouseConfig.objects.all(),
fields=["name"],
message="Lighthouse configuration already exists for this tenant.",
def validate(self, attrs):
tenant_id = self.context.get("request").tenant_id
if LighthouseConfig.objects.filter(tenant_id=tenant_id).exists():
raise serializers.ValidationError(
{
"tenant_id": "AI configuration already exists for this tenant. Use PUT to update."
}
)
]
return super().validate(attrs)
def create(self, validated_data):
api_key = validated_data.pop("api_key")