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
1799ceac
Commit
1799ceac
authored
Nov 15, 2019
by
Ichlasul Affan
Browse files
Eliminate Code Smells and Duplications on Backend Codes - 2nd Iteration (1606895606 -
#83
)
parent
0e8f280d
Changes
2
Hide whitespace changes
Inline
Side-by-side
core/tests/test_create_vacancies.py
View file @
1799ceac
...
...
@@ -111,7 +111,7 @@ class CreateAndUpdateVacancyTest(APITestCase):
url
=
'/api/vacancies/'
response
=
self
.
client
.
post
(
url
,
{
'company'
:
new_company
.
pk
,
'open_time'
:
self
.
today
,
'close_time'
:
self
.
tomorrow
,
'name'
:
'new
_
vacancy'
,
'close_time'
:
self
.
tomorrow
,
'name'
:
'new
vacancy'
,
'description'
:
'new_vacancy '
,
'amount'
:
'sepuluh'
,
'salary'
:
'dua juta'
,
'requirements'
:
'new_vacancy'
,
'max_accepted_applicants'
:
3
,
'working_period'
:
'3 Bulan'
},
format
=
'json'
)
...
...
@@ -128,7 +128,7 @@ class CreateAndUpdateVacancyTest(APITestCase):
url
=
'/api/vacancies/'
response
=
self
.
client
.
post
(
url
,
{
'company'
:
new_company
.
pk
,
'open_time'
:
self
.
today
,
'close_time'
:
self
.
tomorrow
,
'name'
:
'new
_
vacancy'
,
'close_time'
:
self
.
tomorrow
,
'name'
:
'new
vacancy'
,
'description'
:
'new_vacancy '
,
'amount'
:
'sepuluh'
,
'benefits'
:
'new_vacancy'
,
'max_accepted_applicants'
:
3
,
'working_period'
:
'3 Bulan'
},
format
=
'json'
)
...
...
@@ -149,7 +149,7 @@ class CreateAndUpdateVacancyTest(APITestCase):
url
=
'/api/vacancies/'
+
str
(
new_vacancy
.
pk
)
+
'/'
response
=
self
.
client
.
patch
(
url
,
{
'open_time'
:
self
.
today
,
'close_time'
:
self
.
tomorrow
,
'name'
:
'new
_
vacancy
2
'
,
'description'
:
'new_vacancy2'
,
'name'
:
'new
vacancy'
,
'description'
:
'new_vacancy2'
,
'responsibilities'
:
'new_vacancy2'
,
'requirements'
:
'new_vacancy2'
,
'max_accepted_applicants'
:
3
,
'working_period'
:
'3 Bulan'
},
format
=
'json'
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
...
...
@@ -166,7 +166,7 @@ class CreateAndUpdateVacancyTest(APITestCase):
url
=
'/api/vacancies/'
+
str
(
new_vacancy
.
pk
)
+
'/'
response
=
self
.
client
.
patch
(
url
,
{
'open_time'
:
self
.
today
,
'close_time'
:
self
.
tomorrow
,
'name'
:
'new
_
vacancy
2
'
,
'description'
:
'new_vacancy2'
,
'amount'
:
10
,
'name'
:
'new
vacancy'
,
'description'
:
'new_vacancy2'
,
'amount'
:
10
,
'salary'
:
2
,
'responsibilities'
:
'new_vacancy2'
,
'requirements'
:
'new_vacancy2'
,
'max_accepted_applicants'
:
3
,
'working_period'
:
'3 Bulan'
},
format
=
'json'
)
...
...
@@ -186,7 +186,7 @@ class CreateAndUpdateVacancyTest(APITestCase):
max_accepted_applicants
=
3
)
url
=
'/api/vacancies/'
+
str
(
new_vacancy
.
pk
)
+
'/'
response
=
self
.
client
.
patch
(
url
,
{
'open_time'
:
self
.
today
,
'close_time'
:
self
.
tomorrow
,
'name'
:
'new
_
vacancy
2
'
,
response
=
self
.
client
.
patch
(
url
,
{
'open_time'
:
self
.
today
,
'close_time'
:
self
.
tomorrow
,
'name'
:
'new
vacancy'
,
'description'
:
'new_vacancy2'
,
'amount'
:
10
,
'benefits'
:
'new_vacancy2'
,
'responsibilities'
:
'new_vacancy2'
,
'requirements'
:
'new_vacancy'
,
'working_period'
:
'3 Bulan'
,
'max_accepted_applicants'
:
3
},
format
=
'json'
)
...
...
@@ -207,7 +207,7 @@ class CreateAndUpdateVacancyTest(APITestCase):
url
=
'/api/vacancies/'
+
str
(
new_vacancy
.
pk
)
+
'/'
response
=
self
.
client
.
patch
(
url
,
{
'open_time'
:
self
.
today
,
'close_time'
:
self
.
tomorrow
,
'name'
:
'new
_
vacancy
2
'
,
'description'
:
'new_vacancy2'
,
'amount'
:
10
,
'name'
:
'new
vacancy'
,
'description'
:
'new_vacancy2'
,
'amount'
:
10
,
'responsibilities'
:
'new_vacancy2'
,
'requirements'
:
'new_vacancy2'
,
'max_accepted_applicants'
:
3
,
'working_period'
:
'3 Bulan'
},
format
=
'json'
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
...
...
@@ -246,7 +246,7 @@ class CreateAndUpdateVacancyTest(APITestCase):
url
=
'/api/vacancies/'
+
str
(
new_vacancy
.
pk
)
+
'/'
response
=
self
.
client
.
patch
(
url
,
{
'open_time'
:
self
.
today
,
'close_time'
:
self
.
yesterday
,
'name'
:
'new
_
vacancy
2
'
,
'description'
:
'new_vacancy2'
,
'amount'
:
'sepuluh'
,
'name'
:
'new
vacancy'
,
'description'
:
'new_vacancy2'
,
'amount'
:
'sepuluh'
,
'salary'
:
'dua juta'
,
'requirements'
:
'new_vacancy'
,
'max_accepted_applicants'
:
3
,
'working_period'
:
'3 Bulan'
},
format
=
'json'
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_400_BAD_REQUEST
)
...
...
@@ -262,7 +262,7 @@ class CreateAndUpdateVacancyTest(APITestCase):
url
=
'/api/vacancies/'
response
=
self
.
client
.
post
(
url
,
{
'company'
:
new_company
.
pk
,
'open_time'
:
self
.
today
,
'close_time'
:
self
.
today
,
'name'
:
'new
_
vacancy'
,
'description'
:
'new_vacancy'
,
'close_time'
:
self
.
today
,
'name'
:
'new
vacancy'
,
'description'
:
'new_vacancy'
,
'max_accepted_applicants'
:
3
,
'working_period'
:
'3 Bulan'
},
format
=
'json'
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_400_BAD_REQUEST
)
...
...
@@ -278,7 +278,7 @@ class CreateAndUpdateVacancyTest(APITestCase):
url
=
'/api/vacancies/'
response
=
self
.
client
.
post
(
url
,
{
'company'
:
new_company
.
pk
,
'open_time'
:
datetime
.
fromtimestamp
(
0
),
'close_time'
:
datetime
.
fromtimestamp
(
0
),
'name'
:
'new
_
vacancy'
,
'close_time'
:
datetime
.
fromtimestamp
(
0
),
'name'
:
'new
vacancy'
,
'description'
:
'new_vacancy'
,
'max_accepted_applicants'
:
3
,
'working_period'
:
'3 Bulan'
},
format
=
'json'
)
...
...
@@ -299,7 +299,7 @@ class CreateAndUpdateVacancyTest(APITestCase):
url
=
'/api/vacancies/'
+
str
(
new_vacancy
.
pk
)
+
'/'
response
=
self
.
client
.
patch
(
url
,
{
'open_time'
:
self
.
today
,
'close_time'
:
self
.
today
,
'name'
:
'new
_
vacancy
2
'
,
'description'
:
'new_vacancy2'
,
'name'
:
'new
vacancy'
,
'description'
:
'new_vacancy2'
,
'requirements'
:
'new_vacancy2'
,
'max_accepted_applicants'
:
3
,
'working_period'
:
'3 Bulan'
},
format
=
'json'
)
...
...
@@ -319,7 +319,7 @@ class CreateAndUpdateVacancyTest(APITestCase):
response
=
self
.
client
.
patch
(
url
,
{
'open_time'
:
datetime
.
fromtimestamp
(
0
),
'close_time'
:
datetime
.
fromtimestamp
(
0
),
'name'
:
'new
_
vacancy
2
'
,
'description'
:
'new_vacancy2'
,
'amount'
:
'sepuluh'
,
'name'
:
'new
vacancy'
,
'description'
:
'new_vacancy2'
,
'amount'
:
'sepuluh'
,
'requirements'
:
'new_vacancy2'
,
'max_accepted_applicants'
:
3
,
'working_period'
:
'3 Bulan'
},
format
=
'json'
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_400_BAD_REQUEST
)
...
...
@@ -338,7 +338,7 @@ class CreateAndUpdateVacancyTest(APITestCase):
max_accepted_applicants
=
3
)
url
=
'/api/vacancies/'
+
str
(
new_vacancy
.
pk
)
+
'/'
response
=
self
.
client
.
patch
(
url
,
{
'open_time'
:
self
.
today
,
'close_time'
:
self
.
tomorrow
,
'name'
:
'new
_
vacancy
2
'
,
response
=
self
.
client
.
patch
(
url
,
{
'open_time'
:
self
.
today
,
'close_time'
:
self
.
tomorrow
,
'name'
:
'new
vacancy'
,
'description'
:
'new_vacancy2'
,
'amount'
:
'sepuluh'
,
'benefits'
:
'new_vacancy2'
,
'requirements'
:
'new_vacancy'
,
'working_period'
:
'3 Bulan'
,
'max_accepted_applicants'
:
3
},
format
=
'json'
)
...
...
@@ -359,7 +359,7 @@ class CreateAndUpdateVacancyTest(APITestCase):
url
=
'/api/vacancies/'
+
str
(
new_vacancy
.
pk
)
+
'/'
response
=
self
.
client
.
patch
(
url
,
{
'open_time'
:
self
.
today
,
'close_time'
:
self
.
tomorrow
,
'name'
:
'new
_
vacancy
2
'
,
'description'
:
'new_vacancy2'
,
'name'
:
'new
vacancy'
,
'description'
:
'new_vacancy2'
,
'responsibilities'
:
'new_vacancy2'
,
'amount'
:
'sepuluh'
,
'requirements'
:
'new_vacancy2'
,
'max_accepted_applicants'
:
3
,
'working_period'
:
'3 Bulan'
},
format
=
'json'
)
...
...
@@ -398,7 +398,7 @@ class CreateAndUpdateVacancyTest(APITestCase):
url
=
'/api/vacancies/'
+
str
(
new_vacancy
.
pk
)
+
'/'
response
=
self
.
client
.
patch
(
url
,
{
'open_time'
:
self
.
yesterday
,
'close_time'
:
self
.
today
,
'name'
:
'new
_
vacancy
2
'
,
'description'
:
'new_vacancy2'
,
'name'
:
'new
vacancy'
,
'description'
:
'new_vacancy2'
,
'responsibilities'
:
'new_vacancy2'
,
'requirements'
:
'new_vacancy2'
,
'max_accepted_applicants'
:
3
,
'working_period'
:
'3 Bulan'
},
format
=
'json'
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_400_BAD_REQUEST
)
...
...
@@ -418,7 +418,7 @@ class CreateAndUpdateVacancyTest(APITestCase):
url
=
'/api/vacancies/'
+
str
(
new_vacancy
.
pk
)
+
'/'
response
=
self
.
client
.
patch
(
url
,
{
'open_time'
:
self
.
today
,
'close_time'
:
self
.
tomorrow
,
'name'
:
'new
_
vacancy
2
'
,
'description'
:
'new_vacancy2'
,
'amount'
:
10
,
'name'
:
'new
vacancy'
,
'description'
:
'new_vacancy2'
,
'amount'
:
10
,
'responsibilities'
:
'new_vacancy2'
,
'requirements'
:
'new_vacancy2'
,
'max_accepted_applicants'
:
3
,
'working_period'
:
'3 Bulan'
},
format
=
'json'
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
...
...
core/views/vacancies.py
View file @
1799ceac
...
...
@@ -92,133 +92,108 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
if
len
(
companies
)
>
0
:
vacancies
=
vacancies
.
filter
(
company__id__in
=
companies
)
return
vacancies
def
sort
(
self
,
order_by
,
vacancies
,
order
):
if
order_by
is
not
None
and
order_by
in
[
'name'
,
'salary'
]:
if
order
in
[
'ascending'
,
'descending'
]:
order_query
=
""
if
order
==
'descending'
:
order_query
=
"-"
return
vacancies
.
order_by
(
'{}{}'
.
format
(
order_query
,
order_by
))
if
(
order_by
is
not
None
)
and
(
order_by
in
[
'name'
,
'salary'
])
and
(
order
in
[
'ascending'
,
'descending'
]):
order_query
=
""
if
order
==
'descending'
:
order_query
=
"-"
return
vacancies
.
order_by
(
'{}{}'
.
format
(
order_query
,
order_by
))
return
vacancies
def
name_position_validator
(
self
,
names
):
for
name
in
names
.
split
(
" "
):
if
not
name
.
isalpha
():
raise
serializers
.
ValidationError
(
"Name must alphabets only"
)
raise
ValidationError
(
"Name must alphabets only"
)
return
name
def
salary_validator
(
self
,
salary
):
if
not
isinstance
(
salary
,
int
):
raise
serializers
.
ValidationError
(
"Salary must number only"
)
raise
ValidationError
(
"Salary must number only"
)
def
amount_validator
(
self
,
amount
):
if
not
isinstance
(
amount
,
int
):
raise
serializers
.
ValidationError
(
"
a
mount must number only"
)
raise
ValidationError
(
"
A
mount must number only"
)
def
create
(
self
,
request
):
data
=
request
.
data
print
(
"[LOG] data: "
+
str
(
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'
]
self
.
name_position_validator
(
name
)
salary
=
data
.
get
(
'salary'
)
or
0
self
.
salary_validator
(
salary
)
description
=
data
[
'description'
]
working_period
=
data
[
'working_period'
]
tag
=
''
try
:
tag
=
data
[
'tag'
]
except
:
print
(
"Tag leaved in blank"
)
max_accepted_applicants
=
data
[
'max_accepted_applicants'
]
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'
]
if
'amount'
in
data
:
if
isinstance
(
data
[
'amount'
],
int
):
vacancy
.
amount
=
data
[
'amount'
]
self
.
amount_validator
(
data
[
'amount'
])
else
:
return
Response
(
status
=
status
.
HTTP_400_BAD_REQUEST
)
if
'responsibilities'
in
data
:
if
isinstance
(
data
[
'responsibilities'
],
basestring
):
vacancy
.
responsibilities
=
data
[
'responsibilities'
]
else
:
return
Response
(
status
=
status
.
HTTP_400_BAD_REQUEST
)
def
responsibilities_validator
(
self
,
responsibilities
):
if
not
isinstance
(
responsibilities
,
basestring
):
raise
ValidationError
(
"Responsibilities must be string"
)
def
recruiter_activity_validator
(
self
,
recruiter_activity
):
enum_recruiter_activity
=
[
'Selalu'
,
'Sering'
,
'Kadang'
,
'Jarang'
,
'Tidak Pernah'
]
if
'recruiter_activity'
in
data
:
if
isinstance
(
data
[
'recruiter_activity'
],
basestring
):
if
data
[
'recruiter_activity'
]
in
enum_recruiter_activity
:
vacancy
.
recruiter_activity
=
data
[
'recruiter_activity'
]
else
:
return
Response
({
'error'
:
'Tingkat Respons Perekrut harus salah '
if
(
not
isinstance
(
recruiter_activity
,
basestring
))
or
(
recruiter_activity
not
in
enum_recruiter_activity
):
raise
ValidationError
(
'Tingkat Respons Perekrut harus salah '
'satu dari Selalu, Sering, Kadang, '
'Jarang, dan Tidak Pernah!'
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
else
:
return
Response
({
'error'
:
'Tingkat Respons Perekrut harus salah '
'satu dari Selalu, Sering, Kadang, '
'Jarang, dan Tidak Pernah!'
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
'Jarang, dan Tidak Pernah!'
)
def
date_range_validator
(
self
,
open_time
,
close_time
):
validity
=
date_validator
(
open_time
,
close_time
)
if
not
validity
[
'is_valid'
]:
raise
ValidationError
(
validity
[
'error'
])
def
__get_company
(
self
,
company_id
):
company_set
=
Company
.
objects
.
filter
(
id
=
company_id
)
if
len
(
company_set
)
==
0
:
raise
ValidationError
(
'No company exists with that ID'
)
return
company_set
[
0
]
def
__parse_vacancy_data
(
self
,
raw_data
):
data
=
{
"open_time"
:
raw_data
[
'open_time'
],
"close_time"
:
raw_data
[
'close_time'
],
"name"
:
raw_data
[
'name'
],
"salary"
:
raw_data
.
get
(
'salary'
,
0
),
"description"
:
raw_data
[
'description'
],
"working_period"
:
raw_data
[
'working_period'
],
"max_accepted_applicants"
:
raw_data
[
'max_accepted_applicants'
]
}
if
'benefits'
in
raw_data
:
data
[
'benefits'
]
=
raw_data
[
'benefits'
]
if
'amount'
in
raw_data
:
data
[
'amount'
]
=
raw_data
[
'amount'
]
if
'responsibilities'
in
raw_data
:
data
[
'responsibilities'
]
=
raw_data
[
'responsibilities'
]
if
'recruiter_activity'
in
raw_data
:
data
[
'recruiter_activity'
]
=
raw_data
[
'recruiter_activity'
]
return
data
def
__validate_vacancy_data
(
self
,
data
):
self
.
date_range_validator
(
data
[
'open_time'
],
data
[
'close_time'
])
self
.
name_position_validator
(
data
[
'name'
])
self
.
salary_validator
(
data
[
'salary'
])
if
data
.
get
(
'amount'
):
self
.
amount_validator
(
data
[
'amount'
])
if
data
.
get
(
'responsibilities'
):
self
.
responsibilities_validator
(
data
[
'responsibilities'
])
if
data
.
get
(
'recruiter_activity'
):
self
.
recruiter_activity_validator
(
data
[
'recruiter_activity'
])
vacancy
.
save
()
def
create
(
self
,
request
):
print
(
"[LOG] data: "
+
str
(
request
.
data
))
try
:
data
=
self
.
__parse_vacancy_data
(
request
.
data
)
data
[
'company'
]
=
self
.
__get_company
(
request
.
data
[
'company'
])
data
[
'tag'
]
=
request
.
data
.
get
(
'tag'
,
''
)
self
.
__validate_vacancy_data
(
data
)
vacancy
=
Vacancy
(
**
data
)
vacancy
.
save
()
except
ValidationError
as
e
:
print
(
"[LOG] error: "
+
str
(
e
))
return
Response
({
"error"
:
str
(
e
.
detail
[
0
])},
status
=
status
.
HTTP_400_BAD_REQUEST
)
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'
]
salary
=
data
.
get
(
'salary'
)
or
0
close_time
=
data
[
'close_time'
]
name
=
data
[
'name'
]
description
=
data
[
'description'
]
working_period
=
data
[
'working_period'
]
requirements
=
data
[
'requirements'
]
max_accepted_applicants
=
data
[
'max_accepted_applicants'
]
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
vacancy
.
salary
=
salary
vacancy
.
description
=
description
vacancy
.
max_accepted_applicants
=
max_accepted_applicants
vacancy
.
working_period
=
working_period
vacancy
.
responsibilities
=
data
.
get
(
'responsibilities'
)
if
'benefits'
in
data
:
vacancy
.
benefits
=
data
[
'benefits'
]
vacancy
.
requirements
=
requirements
if
'amount'
in
data
:
if
isinstance
(
data
[
'amount'
],
int
):
vacancy
.
amount
=
data
[
'amount'
]
else
:
return
Response
(
status
=
status
.
HTTP_400_BAD_REQUEST
)
enum_recruiter_activity
=
[
'Selalu'
,
'Sering'
,
'Kadang'
,
'Jarang'
,
'Tidak Pernah'
]
if
'recruiter_activity'
in
data
:
if
isinstance
(
data
[
'recruiter_activity'
],
basestring
):
if
data
[
'recruiter_activity'
]
in
enum_recruiter_activity
:
vacancy
.
recruiter_activity
=
data
[
'recruiter_activity'
]
else
:
return
Response
({
'error'
:
'Tingkat Respons Perekrut harus salah '
'satu dari Selalu, Sering, Kadang, '
'Jarang, dan Tidak Pernah!'
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
else
:
return
Response
({
'error'
:
'Tingkat Respons Perekrut harus salah '
'satu dari Selalu, Sering, Kadang, '
'Jarang, dan Tidak Pernah!'
},
status
=
status
.
HTTP_400_BAD_REQUEST
)
vacancy
.
save
()
print
(
"[LOG] data: "
+
str
(
request
.
data
))
try
:
data
=
self
.
__parse_vacancy_data
(
request
.
data
)
self
.
__validate_vacancy_data
(
data
)
vacancy
=
Vacancy
.
objects
.
get
(
pk
=
pk
)
for
(
key
,
value
)
in
data
.
items
():
setattr
(
vacancy
,
key
,
value
)
vacancy
.
save
()
except
ValidationError
as
e
:
print
(
"[LOG] error: "
+
str
(
e
))
return
Response
({
"error"
:
str
(
e
.
detail
[
0
])},
status
=
status
.
HTTP_400_BAD_REQUEST
)
return
Response
(
status
=
status
.
HTTP_200_OK
)
@
detail_route
(
permission_classes
=
[
IsAdminOrCompany
])
...
...
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