Fakultas Ilmu Komputer UI

Commit 9132fe98 authored by Joshua Casey's avatar Joshua Casey
Browse files

[#144502159] [REFACTOR] #37 added paginations and other info for list APIs

parent ec9e30c7
...@@ -7,10 +7,33 @@ from core.serializers.accounts import StudentSerializer, CompanySerializer ...@@ -7,10 +7,33 @@ from core.serializers.accounts import StudentSerializer, CompanySerializer
class VacancySerializer(serializers.ModelSerializer): class VacancySerializer(serializers.ModelSerializer):
company = CompanySerializer() company = CompanySerializer()
status = serializers.SerializerMethodField('_get_app_status')
bookmarked = serializers.SerializerMethodField('_get_bookmarked_status')
def _get_app_status(self, instance):
try:
request = self.context.get("request")
student = request.user.student
app = Application.objects.get(vacancy=instance, student=student)
return app.status
except:
return None
return None
def _get_bookmarked_status(self, instance):
try:
request = self.context.get("request")
if request.user.student.bookmarked_vacancies.filter(pk=instance.id).exists():
return True
return False
except:
return None
return None
class Meta: class Meta:
model = Vacancy model = Vacancy
fields = '__all__' fields = ['company', 'verified', 'open_time', 'description', 'close_time', 'created', 'updated', 'name', \
'status', 'bookmarked']
class PostVacancySerializer(serializers.ModelSerializer): class PostVacancySerializer(serializers.ModelSerializer):
...@@ -35,11 +58,3 @@ class ApplicationStatusSerializer(serializers.ModelSerializer): ...@@ -35,11 +58,3 @@ class ApplicationStatusSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Application model = Application
fields = ['status'] fields = ['status']
class VacancyApplicationSerializer(serializers.ModelSerializer):
vacancy = VacancySerializer()
class Meta:
model = Application
fields = ['cover_letter', 'vacancy', 'status']
...@@ -6,13 +6,13 @@ from rest_framework.exceptions import ValidationError ...@@ -6,13 +6,13 @@ from rest_framework.exceptions import ValidationError
from rest_framework.generics import get_object_or_404 from rest_framework.generics import get_object_or_404
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.pagination import PageNumberPagination
from core.lib.mixins import MultiSerializerViewSetMixin from core.lib.mixins import MultiSerializerViewSetMixin
from core.lib.permissions import IsAdminOrStudent, IsAdminOrCompany, IsAdminOrVacancyOwner from core.lib.permissions import IsAdminOrStudent, IsAdminOrCompany, IsAdminOrVacancyOwner
from core.models import Student, Company from core.models import Student, Company
from core.models.vacancies import Vacancy, Application from core.models.vacancies import Vacancy, Application
from core.serializers.vacancies import VacancySerializer, ApplicationSerializer, ApplicationStatusSerializer, \ from core.serializers.vacancies import VacancySerializer, ApplicationSerializer, ApplicationStatusSerializer, \
VacancyApplicationSerializer, PostVacancySerializer PostVacancySerializer
class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet): class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
...@@ -22,6 +22,7 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet): ...@@ -22,6 +22,7 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
'create': PostVacancySerializer 'create': PostVacancySerializer
} }
permission_classes = [IsAdminOrCompany] permission_classes = [IsAdminOrCompany]
pagination_class = PageNumberPagination
def get_permissions(self): def get_permissions(self):
if self.action in ["retrieve", "list"]: if self.action in ["retrieve", "list"]:
...@@ -33,7 +34,9 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet): ...@@ -33,7 +34,9 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
verified = request.query_params['verified'] if 'verified' in request.query_params else "True" verified = request.query_params['verified'] if 'verified' in request.query_params else "True"
if verified.lower() in ("yes", "true", "t", "1"): if verified.lower() in ("yes", "true", "t", "1"):
vacancies = vacancies.filter(verified=True) vacancies = vacancies.filter(verified=True)
page = self.paginate_queryset(vacancies)
if page is not None:
return self.get_paginated_response(VacancySerializer(page, many=True, context={'request': request}).data)
return Response(VacancySerializer(vacancies, many=True, context={'request': request}).data) return Response(VacancySerializer(vacancies, many=True, context={'request': request}).data)
@detail_route(permission_classes=[IsAdminOrCompany]) @detail_route(permission_classes=[IsAdminOrCompany])
...@@ -47,15 +50,20 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet): ...@@ -47,15 +50,20 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
class ApplicationViewSet(viewsets.GenericViewSet): class ApplicationViewSet(viewsets.GenericViewSet):
serializer_class = ApplicationSerializer serializer_class = ApplicationSerializer
permission_classes = [IsAdminOrStudent] permission_classes = [IsAdminOrStudent]
pagination_class = PageNumberPagination
def list(self, request, student_id): def list(self, request, student_id):
""" """
Get list of a student {student_id}'s applications Get list of a student {student_id}'s applied vacancies
--- ---
""" """
student = get_object_or_404(Student.objects.all(), pk=student_id) student = get_object_or_404(Student.objects.all(), pk=student_id)
applications = Application.objects.filter(student=student) vacancy_ids = Application.objects.filter(student=student).values('vacancy')
return Response(VacancyApplicationSerializer(applications, many=True, context={'request': request}).data) vacancies = Vacancy.objects.filter(id__in=vacancy_ids)
page = self.paginate_queryset(vacancies)
if page is not None:
return self.get_paginated_response(VacancySerializer(page, many=True, context={'request': request}).data)
return Response(VacancySerializer(vacancies, many=True, context={'request': request}).data)
def create(self, request, student_id): def create(self, request, student_id):
""" """
...@@ -92,6 +100,7 @@ class ApplicationViewSet(viewsets.GenericViewSet): ...@@ -92,6 +100,7 @@ class ApplicationViewSet(viewsets.GenericViewSet):
class CompanyApplicationViewSet(viewsets.GenericViewSet): class CompanyApplicationViewSet(viewsets.GenericViewSet):
queryset = Application.objects.all() queryset = Application.objects.all()
permission_classes = [IsAdminOrCompany] permission_classes = [IsAdminOrCompany]
pagination_class = PageNumberPagination
def list(self, request, company_id): def list(self, request, company_id):
""" """
...@@ -103,6 +112,9 @@ class CompanyApplicationViewSet(viewsets.GenericViewSet): ...@@ -103,6 +112,9 @@ class CompanyApplicationViewSet(viewsets.GenericViewSet):
applications = Application.objects.filter(vacancy__in=vacancies) applications = Application.objects.filter(vacancy__in=vacancies)
if 'status' in request.query_params: if 'status' in request.query_params:
applications = applications.filter(status=request.query_params['status']) applications = applications.filter(status=request.query_params['status'])
page = self.paginate_queryset(applications)
if page is not None:
return self.get_paginated_response(ApplicationSerializer(page, many=True, context={'request': request}).data)
return Response(ApplicationSerializer(applications, many=True, context={'request': request}).data) return Response(ApplicationSerializer(applications, many=True, context={'request': request}).data)
...@@ -137,6 +149,7 @@ class CompanyApplicationStatusViewSet(viewsets.GenericViewSet): ...@@ -137,6 +149,7 @@ class CompanyApplicationStatusViewSet(viewsets.GenericViewSet):
class CompanyVacanciesViewSet(viewsets.GenericViewSet): class CompanyVacanciesViewSet(viewsets.GenericViewSet):
queryset = Vacancy.objects.all() queryset = Vacancy.objects.all()
pagination_class = PageNumberPagination
def list(self, request, company_id): def list(self, request, company_id):
""" """
...@@ -145,6 +158,9 @@ class CompanyVacanciesViewSet(viewsets.GenericViewSet): ...@@ -145,6 +158,9 @@ class CompanyVacanciesViewSet(viewsets.GenericViewSet):
""" """
company = get_object_or_404(Company.objects.all().order_by('-updated'), pk=company_id) company = get_object_or_404(Company.objects.all().order_by('-updated'), pk=company_id)
vacancies = Vacancy.objects.filter(company=company) vacancies = Vacancy.objects.filter(company=company)
page = self.paginate_queryset(vacancies)
if page is not None:
return self.get_paginated_response(VacancySerializer(page, many=True, context={'request': request}).data)
return Response(VacancySerializer(vacancies, many=True, context={'request': request}).data) return Response(VacancySerializer(vacancies, many=True, context={'request': request}).data)
...@@ -158,8 +174,11 @@ class BookmarkedVacancyByStudentViewSet(viewsets.GenericViewSet): ...@@ -158,8 +174,11 @@ class BookmarkedVacancyByStudentViewSet(viewsets.GenericViewSet):
--- ---
""" """
student = get_object_or_404(Student.objects.all(), pk=student_id) student = get_object_or_404(Student.objects.all(), pk=student_id)
vacancies = self.serializer_class(student.bookmarked_vacancies, many=True, context={'request': request}) vacancies = student.bookmarked_vacancies.all()
return Response(vacancies.data) page = self.paginate_queryset(vacancies)
if page is not None:
return self.get_paginated_response(VacancySerializer(page, many=True, context={'request': request}).data)
return Response(VacancySerializer(vacancies, many=True, context={'request': request}).data)
def create(self, request, student_id): def create(self, request, student_id):
""" """
......
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