Fakultas Ilmu Komputer UI

Commit 9778061a authored by Ilham Darmawan Candra Purnama's avatar Ilham Darmawan Candra Purnama
Browse files

Merge branch '1606882351-101' into 'master'

1606882351-101 Refactor class VacancyViewSet pada vacancies.py

See merge request !117
parents 49c717b6 128dd19a
Pipeline #24434 passed with stages
in 10 minutes and 46 seconds
......@@ -9,7 +9,7 @@ from rest_framework.test import APITestCase
from core.models.accounts import Company, Student, Supervisor
from core.models.vacancies import Vacancy, Application, VacancyMilestone
from core.serializers.vacancies import VacancySerializer
from core.views.vacancies import date_validator
class ApplicationTests(APITestCase):
@requests_mock.Mocker()
......@@ -486,6 +486,20 @@ class VacancyTest(APITestCase):
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):
......
import requests
from datetime import datetime
from django.utils import timezone
from django.conf import settings
from django.db.models import Q
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.generics import get_object_or_404
from rest_framework.pagination import PageNumberPagination
......@@ -16,15 +12,27 @@ from core.lib.mixins import MultiSerializerViewSetMixin
from core.lib.permissions import IsAdminOrStudent, IsAdminOrCompany, IsAdminOrVacancyOwner, AsAdminOrSupervisor, \
VacancyApprovalPermission, IsAdminOrVacancyOwnerOrAuthenticatedReadOnly
from core.models import Student, Company
from core.models.vacancies import Vacancy, Application, VacancyMilestone, ReasonRejected
from core.serializers.vacancies import VacancySerializer, ApplicationSerializer, ApplicationStatusSerializer, \
PostVacancySerializer, VacancyVerifiedSerializer, SupervisorStudentApplicationSerializer, \
from core.models.vacancies import Vacancy, Application, ReasonRejected
from core.serializers.vacancies import VacancySerializer, ApplicationSerializer, ApplicationStatusSerializer,\
VacancyVerifiedSerializer, SupervisorStudentApplicationSerializer, \
VacancyMilestoneSerializer
from core.views.accounts import StudentViewSet
from datetime import datetime, timedelta, time
from datetime import datetime, time
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):
queryset = Vacancy.objects.all()
serializer_class = VacancySerializer
......@@ -104,16 +112,13 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
tag = data['tag']
except:
print("Tag leaved in blank")
# if close_time < open_time:
# raise ValidationError('Waktu tutup lowongan harus lebih dari waktu buka lowongan!')
max_accepted_applicants = data['max_accepted_applicants']
if open_time < str(datetime.today()):
return Response({'error': 'Waktu buka lowongan harus lebih dari hari ini!'}, status=status.HTTP_400_BAD_REQUEST)
elif close_time < open_time:
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!')
date_validity = date_validator(open_time, close_time)
if not date_validity['is_valid']:
return Response({'error': date_validity['error']}, status=date_validity['status'])
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:
vacancy.benefits = data['benefits']
......@@ -142,12 +147,9 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
working_period = data['working_period']
requirements = data['requirements']
max_accepted_applicants = data['max_accepted_applicants']
if open_time < str(datetime.today()):
return Response({'error': 'Waktu buka lowongan harus lebih dari hari ini!'}, status=status.HTTP_400_BAD_REQUEST)
elif close_time < open_time:
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!')
date_validity = date_validator(open_time, close_time)
if not date_validity['is_valid']:
return Response({'error': date_validity['error']}, status=date_validity['status'])
vacancy.open_time = open_time
vacancy.close_time = close_time
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