Fakultas Ilmu Komputer UI
Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
PMPL
Class Project
Kape
Commits
128dd19a
Commit
128dd19a
authored
Nov 10, 2019
by
Ilham Darmawan Candra Purnama
Browse files
1606882351-101 Refactor class VacancyViewSet pada vacancies.py
parent
49c717b6
Changes
2
Hide whitespace changes
Inline
Side-by-side
core/tests/test_vacancies.py
View file @
128dd19a
...
...
@@ -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.
serializer
s.vacancies
import
VacancySerialize
r
from
core.
view
s.vacancies
import
date_validato
r
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
):
...
...
core/views/vacancies.py
View file @
128dd19a
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
,
time
delta
,
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
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment