From 4726deba9770610946e04948eaa7c302fb3cbba9 Mon Sep 17 00:00:00 2001 From: YogeshUpdhyay Date: Tue, 18 Jun 2024 20:59:49 +0530 Subject: [PATCH 1/2] update jobs endpoint for pagination and searching, add has_applied for jobs and validation in application creation --- apps/applicants/views.py | 7 +++++++ apps/jobs/serializers.py | 1 + apps/jobs/views.py | 36 +++++++++++++++++++++++++++++++----- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/apps/applicants/views.py b/apps/applicants/views.py index fd1a77c..3491b34 100644 --- a/apps/applicants/views.py +++ b/apps/applicants/views.py @@ -50,6 +50,13 @@ def post(self, request): job = Job.objects.get(job_id=serializer.data["job_id"]) except Job.DoesNotExist: raise exceptions.NotFound() + + application = Applicants.objects.first(job=job, user=user_profile) + if application is not None: + return Response( + {"msg": "Already Applied!"}, + status=status.HTTP_403_FORBIDDEN + ) application = Applicants(job=job, user=user_profile) application.save() diff --git a/apps/jobs/serializers.py b/apps/jobs/serializers.py index f7a4bb0..8958a2f 100644 --- a/apps/jobs/serializers.py +++ b/apps/jobs/serializers.py @@ -9,6 +9,7 @@ class JobSerializer(serializers.ModelSerializer): """Job object serializer class""" total_applicants = serializers.IntegerField(read_only=True) + has_applied = serializers.BooleanField(read_only=True) class Meta: """ diff --git a/apps/jobs/views.py b/apps/jobs/views.py index d880523..e0a018b 100644 --- a/apps/jobs/views.py +++ b/apps/jobs/views.py @@ -1,22 +1,33 @@ from django.db.models import Count -from django_filters.rest_framework import DjangoFilterBackend +import django_filters.rest_framework as df_filters +from django.db.models import BooleanField, Case, Value, When from drf_spectacular.utils import extend_schema -from rest_framework import exceptions, parsers, status, viewsets +from rest_framework import exceptions, parsers, status, viewsets, filters from rest_framework.views import APIView from rest_framework.decorators import action from rest_framework.response import Response from apps.accounts.permissions import Moderator from apps.jobs.constants import response, values +from apps.userprofile.models import UserProfile from apps.jobs.models import Company, ContactMessage, Job from apps.jobs.serializers import CompanySerializer, ContactUsSerializer, JobSerializer, JobsCountByCategoriesSerializer from apps.jobs.utils.validators import validationClass from apps.utils.responses import InternalServerError +from apps.utils.pagination import DefaultPagination from .utils.user_permissions import UserTypeCheck +class JobsFilter(df_filters.FilterSet): + category = df_filters.BaseInFilter(field_name="category") + job_type = df_filters.BaseInFilter(field_name="job_type") + + class Meta: + model = Job + fields = ["category", "job_type"] + class JobViewSets(viewsets.ModelViewSet): """ Job object viewsets @@ -30,9 +41,24 @@ class JobViewSets(viewsets.ModelViewSet): queryset = Job.objects.annotate(total_applicants=Count("applicants")) serializer_class = JobSerializer - filter_backends = [DjangoFilterBackend] - filterset_fields = ["job_role", "location", "is_active", "category", "is_featured"] + filter_backends = [filters.SearchFilter, filters.OrderingFilter, df_filters.DjangoFilterBackend] + search_fields = ["job_role", "location"] + filterset_class = JobsFilter + pagination_class = DefaultPagination + + def get_queryset(self): + queryset = super().get_queryset() + + if self.request.user.is_authenticated: + queryset = queryset.annotate( + has_applied=Case( + When(applicants__user=UserProfile.objects.get(user=self.request.user), then=Value(True)), + default=Value(False), + output_field=BooleanField(), + ) + ) + return queryset def create(self, request, *args, **kwargs): """Overriding the create method to include permissions""" @@ -172,7 +198,7 @@ class CompanyViewSets(viewsets.ModelViewSet): parser_classes = [parsers.MultiPartParser, parsers.FormParser] # Basic filters - filter_backends = [DjangoFilterBackend] + filter_backends = [df_filters.DjangoFilterBackend] filterset_fields = ["name", "location"] def list(self, request): From 0473e65e8f3772c18cda060556630725ed6e8e49 Mon Sep 17 00:00:00 2001 From: YogeshUpdhyay Date: Tue, 18 Jun 2024 21:04:38 +0530 Subject: [PATCH 2/2] Update filterset fields --- apps/jobs/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/jobs/views.py b/apps/jobs/views.py index e0a018b..40238ab 100644 --- a/apps/jobs/views.py +++ b/apps/jobs/views.py @@ -26,7 +26,7 @@ class JobsFilter(df_filters.FilterSet): class Meta: model = Job - fields = ["category", "job_type"] + fields = ["category", "job_type", "is_active", "is_featured"] class JobViewSets(viewsets.ModelViewSet): """