Fakultas Ilmu Komputer UI

Commit 165186fa authored by Zamil Majdy's avatar Zamil Majdy
Browse files

[#140654507] #12 Add apply vacancy crud

parent 330da9d0
from rest_framework import serializers
from core.serializers.accounts import CompanySerializer, StudentSerializer
from core.models.vacancies import Vacancy
from core.models.vacancies import Vacancy, Application
class VacancySerializer(serializers.ModelSerializer):
......@@ -16,7 +16,7 @@ class ApplicationSerializer(serializers.ModelSerializer):
student = StudentSerializer()
class Meta:
model = Vacancy
model = Application
fields = '__all__'
......
from rest_framework import viewsets
from rest_framework.exceptions import ValidationError
from rest_framework.generics import get_object_or_404
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
......@@ -21,18 +22,20 @@ class VacancyViewSet(viewsets.ModelViewSet):
class ApplicationViewSet(viewsets.GenericViewSet):
serializer_class = VacancySerializer
serializer_class = ApplicationSerializer
permission_classes = [IsAdminOrStudent]
def list(self, request, student_id):
student = get_object_or_404(Student.objects.all(), pk=student_id)
vacancies = self.serializer_class(student.applied_vacancies, many=True, context={'request': request})
return Response(vacancies.data)
applications = Application.objects.filter(student=student)
return Response(ApplicationSerializer(applications, many=True, context={'request': request}).data)
def create(self, request, student_id):
cover_letter = request.data.get('cover_letter')
vacancy = get_object_or_404(Vacancy.objects.all(), pk=request.data.get('vacancy_id'))
student = get_object_or_404(Student.objects.all(), pk=student_id)
if Application.objects.filter(vacancy=vacancy, student=student).exists():
raise ValidationError("You have already applied for the vacancy")
application = Application(vacancy=vacancy, student=student, cover_letter=cover_letter)
application.save()
return Response(ApplicationSerializer(application, context={'request': request}).data)
......@@ -41,6 +44,7 @@ class ApplicationViewSet(viewsets.GenericViewSet):
vacancy = get_object_or_404(Vacancy.objects.all(), pk=pk)
student = get_object_or_404(Student.objects.all(), pk=student_id)
application = get_object_or_404(Application.objects.all(), student=student, vacancy=vacancy)
application.delete()
return Response(ApplicationSerializer(application, context={'request': request}).data)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment