Fakultas Ilmu Komputer UI

Commit ef66ae2f authored by Muhammad Ilham Peruzzi's avatar Muhammad Ilham Peruzzi
Browse files

change open_time value because there is bug in newer python and change...

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