update
This commit is contained in:
@@ -2,6 +2,7 @@ from rest_framework import viewsets, status, filters
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.permissions import AllowAny, IsAdminUser
|
||||
from rest_framework.parsers import MultiPartParser, FormParser, JSONParser
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
from django.shortcuts import get_object_or_404
|
||||
import logging
|
||||
@@ -63,6 +64,7 @@ class JobApplicationViewSet(viewsets.ModelViewSet):
|
||||
|
||||
queryset = JobApplication.objects.all()
|
||||
serializer_class = JobApplicationSerializer
|
||||
parser_classes = [MultiPartParser, FormParser, JSONParser]
|
||||
filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter]
|
||||
filterset_fields = ['job', 'status']
|
||||
search_fields = ['first_name', 'last_name', 'email', 'job__title']
|
||||
@@ -85,20 +87,51 @@ class JobApplicationViewSet(viewsets.ModelViewSet):
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
"""Submit a job application"""
|
||||
serializer = self.get_serializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
try:
|
||||
# Build data dict - Django QueryDict returns lists, so we need to get first item
|
||||
data = {}
|
||||
|
||||
# Get POST data (text fields)
|
||||
for key in request.POST.keys():
|
||||
value = request.POST.get(key)
|
||||
data[key] = value
|
||||
|
||||
# Get FILES data
|
||||
for key in request.FILES.keys():
|
||||
file_obj = request.FILES.get(key)
|
||||
data[key] = file_obj
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error parsing request: {str(e)}")
|
||||
return Response(
|
||||
{'error': 'Error parsing request data'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
serializer = self.get_serializer(data=data)
|
||||
|
||||
if not serializer.is_valid():
|
||||
logger.error(f"Validation errors: {serializer.errors}")
|
||||
return Response(
|
||||
{'error': 'Validation failed', 'details': serializer.errors},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
try:
|
||||
# Save the application
|
||||
application = serializer.save()
|
||||
|
||||
# Send email notifications
|
||||
email_service = CareerEmailService()
|
||||
email_service.send_application_confirmation(application)
|
||||
email_service.send_application_notification_to_admin(application)
|
||||
|
||||
logger.info(f"New job application received: {application.full_name} for {application.job.title}")
|
||||
|
||||
# Try to send email notifications (non-blocking - don't fail if emails fail)
|
||||
try:
|
||||
email_service = CareerEmailService()
|
||||
email_service.send_application_confirmation(application)
|
||||
email_service.send_application_notification_to_admin(application)
|
||||
logger.info(f"Email notifications sent successfully for application {application.id}")
|
||||
except Exception as email_error:
|
||||
# Log email error but don't fail the application submission
|
||||
logger.warning(f"Failed to send email notifications for application {application.id}: {str(email_error)}")
|
||||
|
||||
return Response(
|
||||
{
|
||||
'message': 'Application submitted successfully',
|
||||
@@ -106,6 +139,7 @@ class JobApplicationViewSet(viewsets.ModelViewSet):
|
||||
},
|
||||
status=status.HTTP_201_CREATED
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error submitting job application: {str(e)}", exc_info=True)
|
||||
return Response(
|
||||
|
||||
Reference in New Issue
Block a user