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:
@@ -1,12 +1,16 @@
|
||||
"""
|
||||
ViewSets for Settings Models
|
||||
Unified API Standard v1.0 compliant
|
||||
"""
|
||||
from rest_framework import viewsets, status, permissions
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
from django.db import transaction
|
||||
from igny8_core.api.base import AccountModelViewSet
|
||||
from igny8_core.api.response import success_response, error_response
|
||||
from igny8_core.api.authentication import JWTAuthentication, CSRFExemptSessionAuthentication
|
||||
from igny8_core.api.pagination import CustomPageNumberPagination
|
||||
from igny8_core.api.throttles import DebugScopedRateThrottle
|
||||
from .settings_models import SystemSettings, AccountSettings, UserSettings, ModuleSettings, AISettings
|
||||
from .settings_serializers import (
|
||||
SystemSettingsSerializer, AccountSettingsSerializer, UserSettingsSerializer,
|
||||
@@ -14,14 +18,18 @@ from .settings_serializers import (
|
||||
)
|
||||
|
||||
|
||||
class SystemSettingsViewSet(viewsets.ModelViewSet):
|
||||
class SystemSettingsViewSet(AccountModelViewSet):
|
||||
"""
|
||||
ViewSet for managing system-wide settings (admin only for write operations)
|
||||
Unified API Standard v1.0 compliant
|
||||
"""
|
||||
queryset = SystemSettings.objects.all()
|
||||
serializer_class = SystemSettingsSerializer
|
||||
permission_classes = [permissions.IsAuthenticated] # Require authentication
|
||||
authentication_classes = [JWTAuthentication, CSRFExemptSessionAuthentication]
|
||||
pagination_class = CustomPageNumberPagination
|
||||
throttle_scope = 'system'
|
||||
throttle_classes = [DebugScopedRateThrottle]
|
||||
|
||||
def get_permissions(self):
|
||||
"""Admin only for write operations, read for authenticated users"""
|
||||
@@ -43,23 +51,28 @@ class SystemSettingsViewSet(viewsets.ModelViewSet):
|
||||
try:
|
||||
setting = SystemSettings.objects.get(key=pk)
|
||||
except SystemSettings.DoesNotExist:
|
||||
return Response(
|
||||
{'error': 'Setting not found'},
|
||||
status=status.HTTP_404_NOT_FOUND
|
||||
return error_response(
|
||||
error='Setting not found',
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
request=request
|
||||
)
|
||||
|
||||
serializer = self.get_serializer(setting)
|
||||
return Response(serializer.data)
|
||||
return success_response(data=serializer.data, request=request)
|
||||
|
||||
|
||||
class AccountSettingsViewSet(AccountModelViewSet):
|
||||
"""
|
||||
ViewSet for managing account-level settings
|
||||
Unified API Standard v1.0 compliant
|
||||
"""
|
||||
queryset = AccountSettings.objects.all()
|
||||
serializer_class = AccountSettingsSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
authentication_classes = [JWTAuthentication, CSRFExemptSessionAuthentication]
|
||||
pagination_class = CustomPageNumberPagination
|
||||
throttle_scope = 'system'
|
||||
throttle_classes = [DebugScopedRateThrottle]
|
||||
|
||||
def get_queryset(self):
|
||||
"""Get settings for current account"""
|
||||
@@ -76,13 +89,14 @@ class AccountSettingsViewSet(AccountModelViewSet):
|
||||
try:
|
||||
setting = queryset.get(key=pk)
|
||||
except AccountSettings.DoesNotExist:
|
||||
return Response(
|
||||
{'error': 'Setting not found'},
|
||||
status=status.HTTP_404_NOT_FOUND
|
||||
return error_response(
|
||||
error='Setting not found',
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
request=request
|
||||
)
|
||||
|
||||
serializer = self.get_serializer(setting)
|
||||
return Response(serializer.data)
|
||||
return success_response(data=serializer.data, request=request)
|
||||
|
||||
def perform_create(self, serializer):
|
||||
"""Set account automatically"""
|
||||
@@ -99,14 +113,18 @@ class AccountSettingsViewSet(AccountModelViewSet):
|
||||
serializer.save(account=account)
|
||||
|
||||
|
||||
class UserSettingsViewSet(viewsets.ModelViewSet):
|
||||
class UserSettingsViewSet(AccountModelViewSet):
|
||||
"""
|
||||
ViewSet for managing user-level settings
|
||||
Unified API Standard v1.0 compliant
|
||||
"""
|
||||
queryset = UserSettings.objects.all()
|
||||
serializer_class = UserSettingsSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
authentication_classes = [JWTAuthentication, CSRFExemptSessionAuthentication]
|
||||
pagination_class = CustomPageNumberPagination
|
||||
throttle_scope = 'system'
|
||||
throttle_classes = [DebugScopedRateThrottle]
|
||||
|
||||
def get_queryset(self):
|
||||
"""Get settings for current user and account"""
|
||||
@@ -130,13 +148,14 @@ class UserSettingsViewSet(viewsets.ModelViewSet):
|
||||
try:
|
||||
setting = queryset.get(key=pk)
|
||||
except UserSettings.DoesNotExist:
|
||||
return Response(
|
||||
{'error': 'Setting not found'},
|
||||
status=status.HTTP_404_NOT_FOUND
|
||||
return error_response(
|
||||
error='Setting not found',
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
request=request
|
||||
)
|
||||
|
||||
serializer = self.get_serializer(setting)
|
||||
return Response(serializer.data)
|
||||
return success_response(data=serializer.data, request=request)
|
||||
|
||||
def perform_create(self, serializer):
|
||||
"""Set user and account automatically"""
|
||||
@@ -155,11 +174,15 @@ class UserSettingsViewSet(viewsets.ModelViewSet):
|
||||
class ModuleSettingsViewSet(AccountModelViewSet):
|
||||
"""
|
||||
ViewSet for managing module-specific settings
|
||||
Unified API Standard v1.0 compliant
|
||||
"""
|
||||
queryset = ModuleSettings.objects.all()
|
||||
serializer_class = ModuleSettingsSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
authentication_classes = [JWTAuthentication, CSRFExemptSessionAuthentication]
|
||||
pagination_class = CustomPageNumberPagination
|
||||
throttle_scope = 'system'
|
||||
throttle_classes = [DebugScopedRateThrottle]
|
||||
|
||||
def get_queryset(self):
|
||||
"""Get settings for current account, optionally filtered by module"""
|
||||
@@ -174,7 +197,7 @@ class ModuleSettingsViewSet(AccountModelViewSet):
|
||||
"""Get all settings for a specific module"""
|
||||
queryset = self.get_queryset().filter(module_name=module_name)
|
||||
serializer = self.get_serializer(queryset, many=True)
|
||||
return Response(serializer.data)
|
||||
return success_response(data=serializer.data, request=request)
|
||||
|
||||
def retrieve(self, request, pk=None):
|
||||
"""Get setting by key (pk can be key string)"""
|
||||
@@ -189,18 +212,20 @@ class ModuleSettingsViewSet(AccountModelViewSet):
|
||||
try:
|
||||
setting = queryset.get(module_name=module_name, key=pk)
|
||||
except ModuleSettings.DoesNotExist:
|
||||
return Response(
|
||||
{'error': 'Setting not found'},
|
||||
status=status.HTTP_404_NOT_FOUND
|
||||
return error_response(
|
||||
error='Setting not found',
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
request=request
|
||||
)
|
||||
else:
|
||||
return Response(
|
||||
{'error': 'Setting not found'},
|
||||
status=status.HTTP_404_NOT_FOUND
|
||||
return error_response(
|
||||
error='Setting not found',
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
request=request
|
||||
)
|
||||
|
||||
serializer = self.get_serializer(setting)
|
||||
return Response(serializer.data)
|
||||
return success_response(data=serializer.data, request=request)
|
||||
|
||||
def perform_create(self, serializer):
|
||||
"""Set account automatically"""
|
||||
@@ -220,11 +245,15 @@ class ModuleSettingsViewSet(AccountModelViewSet):
|
||||
class AISettingsViewSet(AccountModelViewSet):
|
||||
"""
|
||||
ViewSet for managing AI-specific settings
|
||||
Unified API Standard v1.0 compliant
|
||||
"""
|
||||
queryset = AISettings.objects.all()
|
||||
serializer_class = AISettingsSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
authentication_classes = [JWTAuthentication, CSRFExemptSessionAuthentication]
|
||||
pagination_class = CustomPageNumberPagination
|
||||
throttle_scope = 'system'
|
||||
throttle_classes = [DebugScopedRateThrottle]
|
||||
|
||||
def get_queryset(self):
|
||||
"""Get AI settings for current account"""
|
||||
@@ -241,13 +270,14 @@ class AISettingsViewSet(AccountModelViewSet):
|
||||
try:
|
||||
setting = queryset.get(integration_type=pk)
|
||||
except AISettings.DoesNotExist:
|
||||
return Response(
|
||||
{'error': 'AI Setting not found'},
|
||||
status=status.HTTP_404_NOT_FOUND
|
||||
return error_response(
|
||||
error='AI Setting not found',
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
request=request
|
||||
)
|
||||
|
||||
serializer = self.get_serializer(setting)
|
||||
return Response(serializer.data)
|
||||
return success_response(data=serializer.data, request=request)
|
||||
|
||||
def perform_create(self, serializer):
|
||||
"""Set account automatically"""
|
||||
|
||||
Reference in New Issue
Block a user