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:
IGNY8 VPS (Salman)
2025-11-15 23:04:31 +00:00
parent 5a3706d997
commit 0ec594363c
11 changed files with 593 additions and 113 deletions

View File

@@ -375,9 +375,13 @@ class TasksViewSet(SiteSectorModelViewSet):
class ImagesViewSet(SiteSectorModelViewSet):
"""
ViewSet for managing content images
Unified API Standard v1.0 compliant
"""
queryset = Images.objects.all()
serializer_class = ImagesSerializer
pagination_class = CustomPageNumberPagination
throttle_scope = 'writer'
throttle_classes = [DebugScopedRateThrottle]
filter_backends = [DjangoFilterBackend, filters.OrderingFilter]
ordering_fields = ['created_at', 'position', 'id']
@@ -385,12 +389,37 @@ class ImagesViewSet(SiteSectorModelViewSet):
filterset_fields = ['task_id', 'content_id', 'image_type', 'status']
def perform_create(self, serializer):
"""Override to automatically set account"""
account = getattr(self.request, 'account', None)
if account:
serializer.save(account=account)
else:
serializer.save()
"""Override to automatically set account, site, and sector"""
from rest_framework.exceptions import ValidationError
# Get site and sector from request (set by middleware) or user's active context
site = getattr(self.request, 'site', None)
sector = getattr(self.request, 'sector', None)
if not site:
# Fallback to user's active site if not set by middleware
user = getattr(self.request, 'user', None)
if user and user.is_authenticated and hasattr(user, 'active_site'):
site = user.active_site
if not sector and site:
# Fallback to default sector for the site if not set by middleware
from igny8_core.auth.models import Sector
sector = site.sectors.filter(is_default=True).first()
# Site and sector are required - raise ValidationError if not available
# Use dict format for ValidationError to ensure proper error structure
if not site:
raise ValidationError({"site": ["Site is required for image creation. Please select a site."]})
if not sector:
raise ValidationError({"sector": ["Sector is required for image creation. Please select a sector."]})
# Add site and sector to validated_data so base class can validate access
serializer.validated_data['site'] = site
serializer.validated_data['sector'] = sector
# Call parent to set account and validate access
super().perform_create(serializer)
@action(detail=True, methods=['get'], url_path='file', url_name='image_file')
def serve_image_file(self, request, pk=None):