Fakultas Ilmu Komputer UI

Commit 128dd19a authored by Ilham Darmawan Candra Purnama's avatar Ilham Darmawan Candra Purnama
Browse files

1606882351-101 Refactor class VacancyViewSet pada vacancies.py

parent 49c717b6
...@@ -9,7 +9,7 @@ from rest_framework.test import APITestCase ...@@ -9,7 +9,7 @@ from rest_framework.test import APITestCase
from core.models.accounts import Company, Student, Supervisor from core.models.accounts import Company, Student, Supervisor
from core.models.vacancies import Vacancy, Application, VacancyMilestone from core.models.vacancies import Vacancy, Application, VacancyMilestone
from core.serializers.vacancies import VacancySerializer from core.views.vacancies import date_validator
class ApplicationTests(APITestCase): class ApplicationTests(APITestCase):
@requests_mock.Mocker() @requests_mock.Mocker()
...@@ -486,6 +486,20 @@ class VacancyTest(APITestCase): ...@@ -486,6 +486,20 @@ class VacancyTest(APITestCase):
self.assertNotEqual(response, "Pendaftaran ditutup") self.assertNotEqual(response, "Pendaftaran ditutup")
def test_vacancy_valid_date(self):
self.assertEqual(True, date_validator(str(datetime.today() + timedelta(days=1)), str(datetime.today() + timedelta(days=2)))['is_valid'])
def test_vacancy_invalid_date(self):
self.assertEqual(
status.HTTP_400_BAD_REQUEST,
date_validator(str(datetime.today() - timedelta(days=1)), str(datetime.today()))['status'])
self.assertEqual(status.HTTP_400_BAD_REQUEST,
date_validator(str(datetime.today()), str(datetime.today() - timedelta(days=1)))['status'])
self.assertRaisesMessage(status.HTTP_400_BAD_REQUEST,
date_validator(str(datetime.today()), str(datetime.today()))['status'])
class CompanyListsTests(APITestCase): class CompanyListsTests(APITestCase):
......
import requests
from datetime import datetime
from django.utils import timezone from django.utils import timezone
from django.conf import settings
from django.db.models import Q from django.db.models import Q
from rest_framework import viewsets, status from rest_framework import viewsets, status
from rest_framework.decorators import detail_route, permission_classes from rest_framework.decorators import detail_route
from rest_framework.exceptions import ValidationError 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.pagination import PageNumberPagination from rest_framework.pagination import PageNumberPagination
...@@ -16,15 +12,27 @@ from core.lib.mixins import MultiSerializerViewSetMixin ...@@ -16,15 +12,27 @@ from core.lib.mixins import MultiSerializerViewSetMixin
from core.lib.permissions import IsAdminOrStudent, IsAdminOrCompany, IsAdminOrVacancyOwner, AsAdminOrSupervisor, \ from core.lib.permissions import IsAdminOrStudent, IsAdminOrCompany, IsAdminOrVacancyOwner, AsAdminOrSupervisor, \
VacancyApprovalPermission, IsAdminOrVacancyOwnerOrAuthenticatedReadOnly VacancyApprovalPermission, IsAdminOrVacancyOwnerOrAuthenticatedReadOnly
from core.models import Student, Company from core.models import Student, Company
from core.models.vacancies import Vacancy, Application, VacancyMilestone, ReasonRejected from core.models.vacancies import Vacancy, Application, ReasonRejected
from core.serializers.vacancies import VacancySerializer, ApplicationSerializer, ApplicationStatusSerializer, \ from core.serializers.vacancies import VacancySerializer, ApplicationSerializer, ApplicationStatusSerializer,\
PostVacancySerializer, VacancyVerifiedSerializer, SupervisorStudentApplicationSerializer, \ VacancyVerifiedSerializer, SupervisorStudentApplicationSerializer, \
VacancyMilestoneSerializer VacancyMilestoneSerializer
from core.views.accounts import StudentViewSet from core.views.accounts import StudentViewSet
from datetime import datetime, timedelta, time from datetime import datetime, time
from rest_framework import serializers from rest_framework import serializers
def date_validator(open_time, close_time):
if open_time < str(datetime.today()):
return {'is_valid': False, 'error': 'Waktu buka lowongan harus lebih dari hari ini!', 'status': status.HTTP_400_BAD_REQUEST}
elif close_time < open_time:
return {'is_valid': False, 'error': 'Waktu tutup lowongan harus lebih dari waktu buka lowongan!', 'status': status.HTTP_400_BAD_REQUEST}
elif close_time == open_time:
raise ValidationError('Waktu tutup dan buka lowongan tidak boleh sama!')
else:
return {'is_valid': True, 'error':'', 'status': status.HTTP_200_OK}
class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet): class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
queryset = Vacancy.objects.all() queryset = Vacancy.objects.all()
serializer_class = VacancySerializer serializer_class = VacancySerializer
...@@ -104,16 +112,13 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet): ...@@ -104,16 +112,13 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
tag = data['tag'] tag = data['tag']
except: except:
print("Tag leaved in blank") print("Tag leaved in blank")
# if close_time < open_time: # if close_time < open_time:
# raise ValidationError('Waktu tutup lowongan harus lebih dari waktu buka lowongan!') # raise ValidationError('Waktu tutup lowongan harus lebih dari waktu buka lowongan!')
max_accepted_applicants = data['max_accepted_applicants'] max_accepted_applicants = data['max_accepted_applicants']
if open_time < str(datetime.today()): date_validity = date_validator(open_time, close_time)
return Response({'error': 'Waktu buka lowongan harus lebih dari hari ini!'}, status=status.HTTP_400_BAD_REQUEST) if not date_validity['is_valid']:
elif close_time < open_time: return Response({'error': date_validity['error']}, status=date_validity['status'])
return Response({'error': 'Waktu tutup lowongan harus lebih dari waktu buka lowongan!'}, status=status.HTTP_400_BAD_REQUEST)
elif close_time == open_time:
raise ValidationError('Waktu tutup dan buka lowongan tidak boleh sama!')
vacancy = Vacancy(company=company, open_time=open_time, close_time=close_time, name=name, description=description, salary=salary, working_period=working_period, tag=tag, max_accepted_applicants=max_accepted_applicants) vacancy = Vacancy(company=company, open_time=open_time, close_time=close_time, name=name, description=description, salary=salary, working_period=working_period, tag=tag, max_accepted_applicants=max_accepted_applicants)
if 'benefits' in data: if 'benefits' in data:
vacancy.benefits = data['benefits'] vacancy.benefits = data['benefits']
...@@ -142,12 +147,9 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet): ...@@ -142,12 +147,9 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
working_period = data['working_period'] working_period = data['working_period']
requirements = data['requirements'] requirements = data['requirements']
max_accepted_applicants = data['max_accepted_applicants'] max_accepted_applicants = data['max_accepted_applicants']
if open_time < str(datetime.today()): date_validity = date_validator(open_time, close_time)
return Response({'error': 'Waktu buka lowongan harus lebih dari hari ini!'}, status=status.HTTP_400_BAD_REQUEST) if not date_validity['is_valid']:
elif close_time < open_time: return Response({'error': date_validity['error']}, status=date_validity['status'])
return Response({'error': 'Waktu tutup lowongan harus lebih dari waktu buka lowongan!'}, status=status.HTTP_400_BAD_REQUEST)
elif close_time == open_time:
raise ValidationError('Waktu tutup dan buka lowongan tidak boleh sama!')
vacancy.open_time = open_time vacancy.open_time = open_time
vacancy.close_time = close_time vacancy.close_time = close_time
vacancy.name = name vacancy.name = name
......
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