Implement unified API standard across backend viewsets and serializers, enhancing error handling and response formatting. Update AccountModelViewSet to standardize CRUD operations with success and error responses. Refactor various viewsets to inherit from AccountModelViewSet, ensuring compliance with the new standard. Improve frontend components to handle API responses consistently and update configuration for better user experience.
This commit is contained in:
@@ -13,7 +13,8 @@ class KeywordSerializer(serializers.ModelSerializer):
|
||||
intent = serializers.CharField(read_only=True) # From seed_keyword.intent
|
||||
|
||||
# SeedKeyword relationship
|
||||
seed_keyword_id = serializers.IntegerField(write_only=True, required=True)
|
||||
# Required for create, optional for update (can change seed_keyword or just update other fields)
|
||||
seed_keyword_id = serializers.IntegerField(write_only=True, required=False)
|
||||
seed_keyword = SeedKeywordSerializer(read_only=True)
|
||||
|
||||
# Overrides
|
||||
@@ -50,9 +51,19 @@ class KeywordSerializer(serializers.ModelSerializer):
|
||||
]
|
||||
read_only_fields = ['id', 'created_at', 'updated_at', 'account_id', 'keyword', 'volume', 'difficulty', 'intent']
|
||||
|
||||
def validate(self, attrs):
|
||||
"""Validate that seed_keyword_id is provided for create operations"""
|
||||
# For create operations, seed_keyword_id is required
|
||||
if self.instance is None and 'seed_keyword_id' not in attrs:
|
||||
raise serializers.ValidationError({'seed_keyword_id': 'This field is required when creating a keyword.'})
|
||||
return attrs
|
||||
|
||||
def create(self, validated_data):
|
||||
"""Create Keywords instance with seed_keyword"""
|
||||
seed_keyword_id = validated_data.pop('seed_keyword_id')
|
||||
seed_keyword_id = validated_data.pop('seed_keyword_id', None)
|
||||
if not seed_keyword_id:
|
||||
raise serializers.ValidationError({'seed_keyword_id': 'This field is required when creating a keyword.'})
|
||||
|
||||
try:
|
||||
seed_keyword = SeedKeyword.objects.get(id=seed_keyword_id)
|
||||
except SeedKeyword.DoesNotExist:
|
||||
@@ -63,6 +74,7 @@ class KeywordSerializer(serializers.ModelSerializer):
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
"""Update Keywords instance with seed_keyword"""
|
||||
# seed_keyword_id is optional for updates - only update if provided
|
||||
if 'seed_keyword_id' in validated_data:
|
||||
seed_keyword_id = validated_data.pop('seed_keyword_id')
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user