Fakultas Ilmu Komputer UI

Commit bbe25ae3 authored by Kemas Khaidar Ali Indrakusuma's avatar Kemas Khaidar Ali Indrakusuma
Browse files

Merge branch '1606831035-29' into 'master'

1606831035-29 Validasi menambahkan atau update lowongan pekerjaan agar close_time tidak kurang dari dari open_time

See merge request !4
parents ae8e8cc8 e891c101
Pipeline #21823 passed with stages
in 141 minutes and 37 seconds
from datetime import datetime
import requests_mock
from django.contrib.auth.models import User
from rest_framework import status
from rest_framework.test import APITestCase
from core.models.accounts import Company, Student, Supervisor
from core.models.vacancies import Vacancy, Application
class CreateAndUpdateVacancyTest(APITestCase):
def test_new_vacancy_success(self):
superuser = User.objects.create_superuser('dummy.company', 'dummy.company@company.com', 'lalala123')
new_company = Company.objects.create(user=superuser, description="lalalaz", status=Company.VERIFIED, logo=None,
address=None)
self.client.force_authenticate(user=superuser)
url = '/api/vacancies/'
response = self.client.post(url, {'company': new_company.pk, 'open_time': datetime.fromtimestamp(0),
'close_time': datetime.today(), 'name': 'new_vacancy', 'description': 'new_vacancy '}, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
vacancies = Vacancy.objects.count()
self.assertEqual(vacancies,1)
def test_new_vacancy_failed(self):
superuser = User.objects.create_superuser('dummy.company', 'dummy.company@company.com', 'lalala123')
new_company = Company.objects.create(user=superuser, description="lalalaz", status=Company.VERIFIED, logo=None,
address=None)
self.client.force_authenticate(user=superuser)
url = '/api/vacancies/'
response = self.client.post(url, {'company': new_company.pk, 'open_time': datetime.today(),
'close_time': datetime.fromtimestamp(0), 'name': 'new_vacancy', 'description': 'new_vacancy'}, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
vacancies = Vacancy.objects.count()
self.assertEqual(vacancies,0)
def test_update_vacancy_success(self):
superuser = User.objects.create_superuser('dummy.company', 'dummy.company@company.com', 'lalala123')
new_company = Company.objects.create(user=superuser, description="lalalaz", status=Company.VERIFIED, logo=None,
address=None)
self.client.force_authenticate(user=superuser)
new_vacancy = Vacancy.objects.create(company=new_company, verified=False, open_time=datetime.fromtimestamp(0),
description="lalala", close_time=datetime.today(), name='new_company')
url = '/api/vacancies/' + str(new_vacancy.pk) + '/'
response = self.client.patch(url, {'open_time': datetime.fromtimestamp(0), 'close_time': datetime.today(),
'name': 'new_vacancy2', 'description': 'new_vacancy2'}, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_update_vacancy_failed(self):
superuser = User.objects.create_superuser('dummy.company', 'dummy.company@company.com', 'lalala123')
new_company = Company.objects.create(user=superuser, description="lalalaz", status=Company.VERIFIED, logo=None,
address=None)
self.client.force_authenticate(user=superuser)
new_vacancy = Vacancy.objects.create(company=new_company, verified=False, open_time=datetime.fromtimestamp(0),
description="lalala", close_time=datetime.today(), name='new_company')
url = '/api/vacancies/' + str(new_vacancy.pk) + '/'
response = self.client.patch(url, {'open_time': datetime.today(), 'close_time': datetime.fromtimestamp(0),
'name': 'new_vacancy2', 'description': 'new_vacancy2'}, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
\ No newline at end of file
......@@ -21,9 +21,9 @@ from core.views.accounts import StudentViewSet
class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
queryset = Vacancy.objects.all()
serializer_class = VacancySerializer
serializer_action_classes = {
'create': PostVacancySerializer
}
# serializer_action_classes = {
# 'create': PostVacancySerializer
# }
permission_classes = [IsAdminOrCompany]
pagination_class = PageNumberPagination
......@@ -44,6 +44,38 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
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):
data = request.data
company_set = Company.objects.filter(id=data['company'])
if len(company_set) == 0:
raise ValidationError('no company')
company = company_set[0]
open_time = data['open_time']
close_time = data['close_time']
name = data['name']
description = data['description']
if close_time < open_time:
raise ValidationError('Waktu tutup lowongan harus lebih dari waktu buka lowongan!')
vacancy = Vacancy(company=company, open_time=open_time, close_time=close_time, name=name, description=description)
vacancy.save()
return Response(status=status.HTTP_200_OK)
def partial_update(self, request, pk):
data = request.data
vacancy = Vacancy.objects.get(pk=pk)
open_time = data['open_time']
close_time = data['close_time']
name = data['name']
description = data['description']
if close_time < open_time:
raise ValidationError('Waktu tutup lowongan harus lebih dari waktu buka lowongan!')
vacancy.open_time = open_time
vacancy.close_time = close_time
vacancy.name = name
vacancy.description = description
vacancy.save()
return Response(status=status.HTTP_200_OK)
@detail_route(permission_classes=[IsAdminOrCompany])
def count(self, request, pk=None):
vacancy = self.get_object()
......
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