roo roo roo down the river we go...
This commit is contained in:
86
jobs/views.py
Normal file
86
jobs/views.py
Normal file
@@ -0,0 +1,86 @@
|
||||
from rest_framework import generics, permissions, status
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import api_view
|
||||
from .models import Job, Application, JobCategory
|
||||
from .serializers import JobSerializer, ApplicationSerializer, JobCategorySerializer
|
||||
|
||||
|
||||
class JobListView(generics.ListCreateAPIView):
|
||||
"""
|
||||
API view to retrieve list of jobs or create a new job.
|
||||
"""
|
||||
queryset = Job.objects.all()
|
||||
serializer_class = JobSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = Job.objects.all()
|
||||
tenant_id = self.request.query_params.get('tenant', None)
|
||||
if tenant_id is not None:
|
||||
queryset = queryset.filter(tenant_id=tenant_id)
|
||||
return queryset
|
||||
|
||||
|
||||
class JobDetailView(generics.RetrieveUpdateDestroyAPIView):
|
||||
"""
|
||||
API view to retrieve, update or delete a single job.
|
||||
"""
|
||||
queryset = Job.objects.all()
|
||||
serializer_class = JobSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
|
||||
class ApplicationListView(generics.ListCreateAPIView):
|
||||
"""
|
||||
API view to retrieve list of applications or create a new application.
|
||||
"""
|
||||
queryset = Application.objects.all()
|
||||
serializer_class = ApplicationSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
def perform_create(self, serializer):
|
||||
# Set the applicant to the current user
|
||||
serializer.save(applicant=self.request.user)
|
||||
|
||||
|
||||
class ApplicationDetailView(generics.RetrieveUpdateDestroyAPIView):
|
||||
"""
|
||||
API view to retrieve, update or delete a single application.
|
||||
"""
|
||||
queryset = Application.objects.all()
|
||||
serializer_class = ApplicationSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
|
||||
class JobCategoryListView(generics.ListCreateAPIView):
|
||||
"""
|
||||
API view to retrieve list of job categories or create a new category.
|
||||
"""
|
||||
queryset = JobCategory.objects.all()
|
||||
serializer_class = JobCategorySerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
|
||||
@api_view(['POST'])
|
||||
def apply_to_job(request, job_id):
|
||||
"""
|
||||
API endpoint to apply to a specific job.
|
||||
"""
|
||||
try:
|
||||
job = Job.objects.get(pk=job_id)
|
||||
except Job.DoesNotExist:
|
||||
return Response({'error': 'Job not found'}, status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
# Check if user has already applied
|
||||
if Application.objects.filter(job=job, applicant=request.user).exists():
|
||||
return Response({'error': 'Already applied to this job'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
application = Application.objects.create(
|
||||
job=job,
|
||||
applicant=request.user,
|
||||
cover_letter=request.data.get('cover_letter', ''),
|
||||
status='submitted'
|
||||
)
|
||||
|
||||
serializer = ApplicationSerializer(application)
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||
Reference in New Issue
Block a user