From 337cf59425271acf7c6ab2cc05fdf779157ad648 Mon Sep 17 00:00:00 2001 From: Chandrapal Badshah <12944530+Chan9390@users.noreply.github.com> Date: Wed, 30 Apr 2025 15:13:13 +0530 Subject: [PATCH] Revert "Use UniqueTogetherValidator instead of custom validate function" This reverts commit 6df035e0f1c6a4be53bca23c839429b4fbaaf7f5. --- api/src/backend/api/tests/test_views.py | 12 ++++++++++-- api/src/backend/api/v1/serializers.py | 16 +++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/api/src/backend/api/tests/test_views.py b/api/src/backend/api/tests/test_views.py index 2cf93af2ef..92d7002761 100644 --- a/api/src/backend/api/tests/test_views.py +++ b/api/src/backend/api/tests/test_views.py @@ -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"] ) diff --git a/api/src/backend/api/v1/serializers.py b/api/src/backend/api/v1/serializers.py index 504fc6f824..4f8b44ccd2 100644 --- a/api/src/backend/api/v1/serializers.py +++ b/api/src/backend/api/v1/serializers.py @@ -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")