Skip to content

Commit

Permalink
Merge pull request #124 from YogeshUpdhyay/main
Browse files Browse the repository at this point in the history
Update jobs endpoint and application creation
  • Loading branch information
hims1911 authored Jun 19, 2024
2 parents ec0a95b + 0473e65 commit 22dfab5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
7 changes: 7 additions & 0 deletions apps/applicants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions apps/jobs/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
36 changes: 31 additions & 5 deletions apps/jobs/views.py
Original file line number Diff line number Diff line change
@@ -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", "is_active", "is_featured"]

class JobViewSets(viewsets.ModelViewSet):
"""
Job object viewsets
Expand All @@ -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"""
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 22dfab5

Please sign in to comment.