Fakultas Ilmu Komputer UI

Commit 6227c703 authored by Muhammad Ilham Peruzzi's avatar Muhammad Ilham Peruzzi
Browse files

Merge branch '1606823475-34' into 'master'

1606823475-34 Extract Method on CompanyApplicationViewSet Class

See merge request !11
parents e68e63c4 ef66ae2f
Pipeline #21843 passed with stages
in 192 minutes and 7 seconds
from datetime import datetime
from django.utils import timezone
import requests_mock
from django.contrib.auth.models import User
......@@ -70,8 +71,8 @@ class ApplicationTests(APITestCase):
new_user = User.objects.create_user('dummy.company', 'dummy.company@company.com', 'lalala123')
new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
address=None)
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
description="lalala", close_time=datetime.today())
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
description="lalala", close_time=timezone.now())
url = '/api/students/' + str(student_id) + '/applied-vacancies/'
response = self.client.post(url, {'vacancy_id': new_vacancy.pk, 'cover_letter': 'this is a cover letter.'},
......@@ -144,8 +145,8 @@ class BookmarkApplicationTests(APITestCase):
new_user = User.objects.create_user('dummy.company2', 'dummy.compan2y@company.com', 'lalala123')
new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
address=None)
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
description="lalala", close_time=datetime.today())
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
description="lalala", close_time=timezone.now())
url = '/api/students/' + str(student_id) + '/bookmarked-vacancies/'
response = self.client.post(url, {'vacancy_id': new_vacancy.pk}, format='json')
......@@ -253,8 +254,8 @@ class CompanyListsTests(APITestCase):
new_user2 = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
new_student = Student.objects.create(user=new_user2, npm=1234123412)
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
description="lalala", close_time=datetime.today())
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
description="lalala", close_time=timezone.now())
new_app = Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
self.client.force_authenticate(new_user)
......@@ -271,8 +272,8 @@ class CompanyListsTests(APITestCase):
new_user2 = User.objects.create_user('dummy.student', 'dummy.company3@company.com', 'lalala123')
new_student = Student.objects.create(user=new_user2, npm=1234123412)
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
description="lalala", close_time=datetime.today())
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
description="lalala", close_time=timezone.now())
new_app = Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
new_user3 = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
......@@ -293,8 +294,8 @@ class CompanyListsTests(APITestCase):
new_user2 = User.objects.create_user('dummy.student', 'dummy.company3@company.com', 'lalala123')
new_student = Student.objects.create(user=new_user2, npm=1234123412)
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
description="lalala", close_time=datetime.today())
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
description="lalala", close_time=timezone.now())
new_app = Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
new_user3 = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
......@@ -315,8 +316,8 @@ class CompanyListsTests(APITestCase):
new_user2 = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
new_student = Student.objects.create(user=new_user2, npm=1234123412)
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
description="lalala", close_time=datetime.today())
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
description="lalala", close_time=timezone.now())
new_app = Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
self.client.force_authenticate(new_user)
......@@ -333,8 +334,8 @@ class CompanyListsTests(APITestCase):
new_user2 = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
new_student = Student.objects.create(user=new_user2, npm=1234123412)
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
description="lalala", close_time=datetime.today())
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
description="lalala", close_time=timezone.now())
new_app = Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
self.client.force_authenticate(new_user)
......@@ -352,8 +353,8 @@ class CompanyListsTests(APITestCase):
new_user2 = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
new_student = Student.objects.create(user=new_user2, npm=1234123412)
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
description="lalala", close_time=datetime.today())
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
description="lalala", close_time=timezone.now())
new_app = Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
self.client.force_authenticate(new_user)
......@@ -371,8 +372,8 @@ class CompanyListsTests(APITestCase):
new_user2 = User.objects.create_user('dummy.company4', 'dummy.company4@company.com', 'lalala123')
new_student = Student.objects.create(user=new_user2, npm=1234123412)
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
description="lalala", close_time=datetime.today())
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(1541319300.0),
description="lalala", close_time=timezone.now())
new_app = Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
self.client.force_authenticate(new_user)
......@@ -410,8 +411,8 @@ class SupervisorApprovalTests(APITestCase):
new_user2 = User.objects.create_user('dummy.company2', 'dummy.compan2y@company.com', 'lalala123')
new_company2 = Company.objects.create(user=new_user2, description="lalala", status=Company.VERIFIED, logo=None,
address=None)
new_vacancy2 = Vacancy.objects.create(company=new_company2, verified=False, open_time=datetime.fromtimestamp(0),
description="lalala", close_time=datetime.today())
new_vacancy2 = Vacancy.objects.create(company=new_company2, verified=False, open_time=datetime.fromtimestamp(1541319300.0),
description="lalala", close_time=timezone.now())
url = '/api/vacancies/' + str(new_vacancy2.pk) + '/verify/'
response = self.client.patch(url, {'verified': True}, format='json')
......@@ -426,8 +427,8 @@ class SupervisorApprovalTests(APITestCase):
address=None)
self.client.force_authenticate(user=new_user)
new_vacancy = Vacancy.objects.create(company=new_company, verified=False, open_time=datetime.fromtimestamp(0),
description="lalala", close_time=datetime.today())
new_vacancy = Vacancy.objects.create(company=new_company, verified=False, open_time=datetime.fromtimestamp(1541319300.0),
description="lalala", close_time=timezone.now())
url = '/api/vacancies/' + str(new_vacancy.pk) + '/verify/'
response = self.client.patch(url, format='json')
......
......@@ -187,6 +187,11 @@ class StudentApplicationViewSet(viewsets.GenericViewSet):
application.delete()
return Response(ApplicationSerializer(application, context={'request': request}).data)
class StatusError(Exception):
pass
class UnauthorizeError(Exception):
pass
class CompanyApplicationViewSet(viewsets.GenericViewSet):
queryset = Application.objects.all()
......@@ -198,18 +203,25 @@ class CompanyApplicationViewSet(viewsets.GenericViewSet):
Get list of company {company_id}'s applications
---
"""
company = get_object_or_404(Company.objects.all(), pk=company_id)
if not request.user.is_superuser and request.user != company.user:
try:
company = self.__get_company_list_by_company_id(request, company_id)
vacancies = self.__get_vacancy_list_filter_by_company(company)
applications = self.__get_application_list_filter_by_vacancies(vacancies)
st = self.__get_status_from_request_param(request)
if st is not None:
st = int(st)
self.__validating_application_status(st)
applications = applications.filter(status=st)
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)
except UnauthorizeError:
return Response({"error": "forbidden"}, status=status.HTTP_403_FORBIDDEN)
vacancies = Vacancy.objects.filter(company=company)
applications = Application.objects.filter(vacancy__in=vacancies)
if 'status' in request.query_params:
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)
except (StatusError, ValueError):
return Response({"error": "status must be an integer between 0 and 4"}, \
status=status.HTTP_400_BAD_REQUEST)
@detail_route(methods=["get"])
def by_vacancy(self, request, company_id, pk=None):
......@@ -217,32 +229,63 @@ class CompanyApplicationViewSet(viewsets.GenericViewSet):
Get list of company {company_id}'s applications by vacancy {id}
---
"""
if pk is None:
return list(self, request, company_id)
company = get_object_or_404(Company.objects.all().order_by('-updated'), pk=company_id)
if not request.user.is_superuser and request.user != company.user:
return Response({"error": "forbidden"}, status=status.HTTP_403_FORBIDDEN)
vacancy = get_object_or_404(Vacancy.objects.all(), pk=pk)
if vacancy.company != company:
return Response({"error": "forbidden"}, status=status.HTTP_403_FORBIDDEN)
applications = Application.objects.filter(vacancy=vacancy)
st = request.query_params.get('status', None)
if st is not None:
try:
try:
if pk is None:
return list(self, request, company_id)
company = self.__get_company_list_by_company_id(request, company_id)
vacancy = self.__get_vacancy_list_by_pk(pk, company)
applications = self.__get_aplication_by_vacancy(vacancy)
st = self.__get_status_from_request_param(request)
if st is not None:
st = int(st)
if st < 0 or st > 4:
return Response({"error": "status must be an integer between 0 and 4"},
status=status.HTTP_400_BAD_REQUEST)
self.__validating_application_status(st)
applications = applications.filter(status=st)
except:
return Response({"error": "status must be an integer between 0 and 4"}, \
status=status.HTTP_400_BAD_REQUEST)
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)
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)
except UnauthorizeError:
return Response({"error": "forbidden"}, status=status.HTTP_403_FORBIDDEN)
except (StatusError, ValueError):
return Response({"error": "status must be an integer between 0 and 4"}, \
status=status.HTTP_400_BAD_REQUEST)
def __get_company_list_by_company_id(self, request, company_id):
company = get_object_or_404(Company.objects.all().order_by('-updated'), pk=company_id)
if not self.__validating_user(request, company):
raise UnauthorizeError
return company
def __validating_user(self, request, company):
return request.user.is_superuser or request.user == company.user
def __get_vacancy_list_by_pk(self, pk, company):
vacancy = get_object_or_404(Vacancy.objects.all(), pk=pk)
if not self.__validating_vacancy(vacancy, company):
raise UnauthorizeError
return vacancy
def __validating_vacancy(self, vacancy, company):
return vacancy.company == company
def __get_vacancy_list_filter_by_company(self, company):
return Vacancy.objects.filter(company=company)
def __get_application_list_filter_by_vacancies(self, vacancies):
return Application.objects.filter(vacancy__in=vacancies)
def __get_aplication_by_vacancy(self, vacancy):
return Application.objects.filter(vacancy=vacancy)
def __get_status_from_request_param(self, request):
return request.query_params.get('status', None)
def __validating_application_status(self, status):
list_status = {0 : "NEW", 1 : "READ", 2 : "BOOKMARKED", 3 : "REJECTED", 4: "ACCEPTED"}
if status not in list_status:
raise StatusError
return True
class CompanyVacanciesViewSet(viewsets.GenericViewSet):
queryset = Vacancy.objects.all()
......
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