From 618eac93b0355822cf2b50e977f82df278171057 Mon Sep 17 00:00:00 2001 From: HimanshuSharma-Angel Date: Fri, 14 Jun 2024 02:08:14 +0530 Subject: [PATCH] api to get applied jobs added for candidate and sending user_type in token payload --- apps/accounts/views.py | 1 + apps/applicants/serializers.py | 22 ++++++++++++++++++ apps/applicants/urls.py | 3 ++- apps/applicants/views.py | 41 ++++++++++++++++++++++++++++++++-- 4 files changed, 64 insertions(+), 3 deletions(-) diff --git a/apps/accounts/views.py b/apps/accounts/views.py index 06a880a..8dec6d0 100644 --- a/apps/accounts/views.py +++ b/apps/accounts/views.py @@ -55,6 +55,7 @@ def get_tokens_for_user(user): return { "refresh": str(refresh), "access": str(refresh.access_token), + "user_type": user.user_type } @staticmethod diff --git a/apps/applicants/serializers.py b/apps/applicants/serializers.py index e39cbbf..047e0f8 100644 --- a/apps/applicants/serializers.py +++ b/apps/applicants/serializers.py @@ -29,3 +29,25 @@ class ApplyToJobSerializer(serializers.Serializer): class UpdateApplicationStatusSerializer(serializers.Serializer): application_id = serializers.CharField(required=True) status = serializers.ChoiceField(choices=constants.STATUS_CHOICES, required=True) + + + + + + +class AppliedJobSerializer(serializers.ModelSerializer): + job = JobSerializer(read_only=True) + + class Meta: + model = Applicants + fields = [ + 'id', + 'job', + 'created_at', + 'updated_at', + 'is_deleted', + 'is_active', + 'status' + ] + + diff --git a/apps/applicants/urls.py b/apps/applicants/urls.py index af86f92..3b94ec6 100644 --- a/apps/applicants/urls.py +++ b/apps/applicants/urls.py @@ -3,11 +3,12 @@ from apps.applicants.views import ( AllApplicantsOfCompany, ApplyToJob, - UpdateApplicationStatus, + UpdateApplicationStatus, GetAppliedJobs, ) urlpatterns = [ path("applicants/", AllApplicantsOfCompany.as_view(), name="applicants"), + path("applied_jobs/",GetAppliedJobs.as_view(), name="applied_jobs"), path("apply/job", ApplyToJob.as_view(), name="applytojob"), path( "application/updatestatus", diff --git a/apps/applicants/views.py b/apps/applicants/views.py index 90dacd6..fd1a77c 100644 --- a/apps/applicants/views.py +++ b/apps/applicants/views.py @@ -8,7 +8,7 @@ from apps.applicants.serializers import ( ApplicantModelSerializer, ApplyToJobSerializer, - UpdateApplicationStatusSerializer, + UpdateApplicationStatusSerializer, AppliedJobSerializer, ) from apps.jobs.models import Job from apps.userprofile.models import UserProfile @@ -71,7 +71,7 @@ def post(self, request): serializer.is_valid(raise_exception=True) if not Applicants.objects.filter(id=serializer.data["application_id"]).update( - status=serializer.data["status"] + status=serializer.data["status"] ): raise exceptions.NotFound() @@ -79,3 +79,40 @@ def post(self, request): {"msg": "Success", "detail": "Status updated successfully."}, status=status.HTTP_200_OK, ) + + +class GetAppliedJobs(APIView): + """ + get applied jobs will return all the applied jobs associated with that candidate + """ + permission_classes = [permissions.IsAuthenticated, IsProfileCompleted] + + @extend_schema( + responses={200: ApplicantModelSerializer(many=True)}, + tags=["applied_jobs"] + ) + def get(self, request): + """List all users that belong to the company, or a specific applicant by ID""" + job_id = request.query_params.get('job_id') + + # finding out the user_id associated with logged in user_id + user_id = UserProfile.objects.get(user_id=request.user.id) + + if job_id: + try: + applicant = Applicants.objects.get(user_id=user_id, job_id=job_id) + return Response( + AppliedJobSerializer(applicant).data, + status=status.HTTP_200_OK + ) + except Applicants.DoesNotExist: + return Response( + {"detail": "Applicant not found."}, + status=status.HTTP_404_NOT_FOUND + ) + else: + applicants = Applicants.objects.filter(user_id=user_id).order_by('-created_at') + return Response( + AppliedJobSerializer(applicants, many=True).data, + status=status.HTTP_200_OK + )