115 lines
4.1 KiB
Python
115 lines
4.1 KiB
Python
from rest_framework import viewsets, status
|
|
from rest_framework.decorators import action
|
|
from rest_framework.response import Response
|
|
from rest_framework.permissions import AllowAny, IsAuthenticated
|
|
from django.db.models import Q
|
|
from django.utils import timezone
|
|
from .models import ContactUs, Composition, Campaign
|
|
from .serializers import ContactUsSerializer, CompositionSerializer, CampaignSerializer
|
|
|
|
|
|
class ContactUsViewSet(viewsets.ModelViewSet):
|
|
"""
|
|
ViewSet for ContactUs model.
|
|
Provides CRUD operations for contact form submissions.
|
|
"""
|
|
queryset = ContactUs.objects.all()
|
|
serializer_class = ContactUsSerializer
|
|
permission_classes = [AllowAny] # Allow anyone to submit contact forms
|
|
|
|
def get_permissions(self):
|
|
"""
|
|
Override to allow GET (list, retrieve) and POST (create) for anyone.
|
|
"""
|
|
if self.action in ['list', 'retrieve', 'create']:
|
|
return [AllowAny()]
|
|
return [IsAuthenticated()]
|
|
|
|
@action(detail=False, methods=['get'])
|
|
def by_category(self, request):
|
|
"""Get contacts filtered by category."""
|
|
category = request.query_params.get('category', None)
|
|
if category:
|
|
contacts = self.queryset.filter(category=category)
|
|
serializer = self.get_serializer(contacts, many=True)
|
|
return Response(serializer.data)
|
|
return Response(
|
|
{'error': 'Category parameter is required.'},
|
|
status=status.HTTP_400_BAD_REQUEST
|
|
)
|
|
|
|
|
|
class CompositionViewSet(viewsets.ModelViewSet):
|
|
"""
|
|
ViewSet for Composition model.
|
|
Provides CRUD operations for compositions.
|
|
"""
|
|
queryset = Composition.objects.all()
|
|
serializer_class = CompositionSerializer
|
|
permission_classes = [AllowAny] # Public read access
|
|
|
|
def get_permissions(self):
|
|
"""
|
|
Override to allow GET (list, retrieve) and POST (create) for anyone.
|
|
"""
|
|
if self.action in ['list', 'retrieve', 'create']:
|
|
return [AllowAny()]
|
|
return [IsAuthenticated()]
|
|
|
|
def get_serializer_context(self):
|
|
"""Add request to serializer context for image URL generation."""
|
|
context = super().get_serializer_context()
|
|
context['request'] = self.request
|
|
return context
|
|
|
|
|
|
class CampaignViewSet(viewsets.ModelViewSet):
|
|
"""
|
|
ViewSet for Campaign model.
|
|
Provides CRUD operations for campaigns.
|
|
"""
|
|
queryset = Campaign.objects.all()
|
|
serializer_class = CampaignSerializer
|
|
permission_classes = [AllowAny] # Public read access
|
|
|
|
def get_permissions(self):
|
|
"""
|
|
Override to allow GET (list, retrieve) and POST (create) for anyone.
|
|
"""
|
|
if self.action in ['list', 'retrieve', 'create', 'active', 'upcoming', 'ended']:
|
|
return [AllowAny()]
|
|
return [IsAuthenticated()]
|
|
|
|
def get_serializer_context(self):
|
|
"""Add request to serializer context for image URL generation."""
|
|
context = super().get_serializer_context()
|
|
context['request'] = self.request
|
|
return context
|
|
|
|
@action(detail=False, methods=['get'])
|
|
def active(self, request):
|
|
"""Get all currently active campaigns."""
|
|
now = timezone.now()
|
|
active_campaigns = self.queryset.filter(
|
|
start_time__lte=now,
|
|
end_time__gte=now
|
|
)
|
|
serializer = self.get_serializer(active_campaigns, many=True)
|
|
return Response(serializer.data)
|
|
|
|
@action(detail=False, methods=['get'])
|
|
def upcoming(self, request):
|
|
"""Get all upcoming campaigns."""
|
|
now = timezone.now()
|
|
upcoming_campaigns = self.queryset.filter(start_time__gt=now)
|
|
serializer = self.get_serializer(upcoming_campaigns, many=True)
|
|
return Response(serializer.data)
|
|
|
|
@action(detail=False, methods=['get'])
|
|
def ended(self, request):
|
|
"""Get all ended campaigns."""
|
|
now = timezone.now()
|
|
ended_campaigns = self.queryset.filter(end_time__lt=now)
|
|
serializer = self.get_serializer(ended_campaigns, many=True)
|
|
return Response(serializer.data)
|