roo roo roo down the river we go...
This commit is contained in:
0
jobs/__init__.py
Normal file
0
jobs/__init__.py
Normal file
90
jobs/models.py
Normal file
90
jobs/models.py
Normal file
@@ -0,0 +1,90 @@
|
||||
from django.db import models
|
||||
from tenants.models import Tenant
|
||||
from users.models import User
|
||||
|
||||
|
||||
class JobCategory(models.Model):
|
||||
"""
|
||||
Job category for organizing job postings.
|
||||
"""
|
||||
name = models.CharField(max_length=100)
|
||||
description = models.TextField(blank=True)
|
||||
tenant = models.ForeignKey(Tenant, on_delete=models.CASCADE)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class Job(models.Model):
|
||||
"""
|
||||
Job posting model for the recruiting platform.
|
||||
"""
|
||||
STATUS_CHOICES = [
|
||||
('draft', 'Draft'),
|
||||
('published', 'Published'),
|
||||
('closed', 'Closed'),
|
||||
('archived', 'Archived'),
|
||||
]
|
||||
|
||||
title = models.CharField(max_length=200)
|
||||
description = models.TextField()
|
||||
requirements = models.TextField()
|
||||
responsibilities = models.TextField()
|
||||
tenant = models.ForeignKey(Tenant, on_delete=models.CASCADE)
|
||||
posted_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='jobs_posted')
|
||||
|
||||
category = models.ForeignKey(JobCategory, on_delete=models.SET_NULL, null=True, blank=True)
|
||||
location = models.CharField(max_length=200)
|
||||
employment_type = models.CharField(max_length=50, choices=[
|
||||
('full-time', 'Full-time'),
|
||||
('part-time', 'Part-time'),
|
||||
('contract', 'Contract'),
|
||||
('temporary', 'Temporary'),
|
||||
('internship', 'Internship'),
|
||||
('remote', 'Remote'),
|
||||
])
|
||||
|
||||
salary_min = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
|
||||
salary_max = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
|
||||
|
||||
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='draft')
|
||||
is_remote = models.BooleanField(default=False)
|
||||
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
published_at = models.DateTimeField(null=True, blank=True)
|
||||
expires_at = models.DateTimeField(null=True, blank=True)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.title} - {self.tenant.name}"
|
||||
|
||||
@property
|
||||
def is_active(self):
|
||||
return self.status == 'published' and self.expires_at and self.expires_at > self.created_at
|
||||
|
||||
|
||||
class Application(models.Model):
|
||||
"""
|
||||
Job application model to track candidate applications.
|
||||
"""
|
||||
STATUS_CHOICES = [
|
||||
('submitted', 'Submitted'),
|
||||
('reviewed', 'Reviewed'),
|
||||
('shortlisted', 'Shortlisted'),
|
||||
('interviewed', 'Interviewed'),
|
||||
('offered', 'Offered'),
|
||||
('rejected', 'Rejected'),
|
||||
('withdrawn', 'Withdrawn'),
|
||||
]
|
||||
|
||||
job = models.ForeignKey(Job, on_delete=models.CASCADE, related_name='applications')
|
||||
applicant = models.ForeignKey(User, on_delete=models.CASCADE, related_name='applications')
|
||||
cover_letter = models.TextField(blank=True)
|
||||
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='submitted')
|
||||
|
||||
applied_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.applicant.username} - {self.job.title}"
|
||||
40
jobs/serializers.py
Normal file
40
jobs/serializers.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from rest_framework import serializers
|
||||
from .models import Job, Application, JobCategory
|
||||
from users.serializers import UserSerializer
|
||||
from tenants.serializers import TenantSerializer
|
||||
|
||||
|
||||
class JobCategorySerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Serializer for the JobCategory model.
|
||||
"""
|
||||
class Meta:
|
||||
model = JobCategory
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class JobSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Serializer for the Job model.
|
||||
"""
|
||||
posted_by = UserSerializer(read_only=True)
|
||||
tenant = TenantSerializer(read_only=True)
|
||||
category = JobCategorySerializer(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = Job
|
||||
fields = '__all__'
|
||||
read_only_fields = ('posted_by', 'tenant', 'created_at', 'updated_at', 'published_at')
|
||||
|
||||
|
||||
class ApplicationSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Serializer for the Application model.
|
||||
"""
|
||||
job = JobSerializer(read_only=True)
|
||||
applicant = UserSerializer(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = Application
|
||||
fields = '__all__'
|
||||
read_only_fields = ('job', 'applicant', 'applied_at', 'updated_at')
|
||||
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