Fakultas Ilmu Komputer UI

Commit a87a2be5 authored by FARI QODRI ANDANA's avatar FARI QODRI ANDANA
Browse files

1606875964 118 Sort vacancies by name or salary

parent e058c0bd
......@@ -5,11 +5,13 @@ import requests_mock
from django.core.exceptions import ValidationError
from django.contrib.auth.models import User
from rest_framework import status
from rest_framework.test import APITestCase
from rest_framework.test import APITestCase, APIRequestFactory
from rest_framework.response import Response
from core.models.accounts import Company, Student, Supervisor
from core.models.vacancies import Vacancy, Application, VacancyMilestone
from core.views.vacancies import date_validator
from core.serializers.vacancies import VacancySerializer
from core.tests.mocks import mock_csui_oauth_verify, mock_csui_ldap_student, mock_csui_siak_student
......@@ -213,6 +215,40 @@ class VacancyTest(APITestCase):
Vacancy.objects.filter(company__id__in=[new_company.id, new_company2.id])
self.assertEqual(dict(response.data)['count'], Vacancy.objects.count())
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_sort_vacancies(self):
superuser = User.objects.create_superuser('dummy.company', 'dummy.company@company.com', 'lalala123')
self.client.force_authenticate(user=superuser)
new_user = User.objects.create_user('dummy.company3', 'dummy.company3@company.com', 'lalala123')
new_company = Company.objects.create(user=new_user, description="lalala", status=Company.VERIFIED, logo=None,
address=None)
open_time = datetime(2019, 10, 20)
close_time = datetime(2019, 12, 20)
Vacancy.objects.create(company=new_company, verified=True, open_time=open_time, salary=3000000,
description='', close_time=close_time, name='vacancy1', max_accepted_applicants=3, working_period="3 Bulan")
Vacancy.objects.create(company=new_company, verified=True, open_time=open_time, salary=3500000,
description='', close_time=close_time, name='vacancy2', max_accepted_applicants=3, working_period="3 Bulan")
factory = APIRequestFactory()
url_name_asc = '/api/vacancies/?sort=name&order=ascending'
request = factory.get(url_name_asc)
response = self.client.get(url_name_asc, format='json')
self.assertEqual(dict(response.data)['results'], VacancySerializer(Vacancy.objects.order_by('name'), many=True, context={'request': request}).data)
url_salary_asc = '/api/vacancies/?sort=salary'
response = self.client.get(url_salary_asc)
request = factory.get(url_salary_asc)
self.assertEqual(dict(response.data)['results'], VacancySerializer(Vacancy.objects.order_by('salary'), many=True, context={'request': request}).data)
url_name_desc = '/api/vacancies/?sort=name&order=descending'
response = self.client.get(url_name_desc)
request = factory.get(url_name_desc)
self.assertEqual(dict(response.data)['results'], VacancySerializer(Vacancy.objects.order_by('-name'), many=True, context={'request': request}).data)
url_salary_desc = '/api/vacancies/?sort=salary&order=descending'
response = self.client.get(url_salary_desc)
request = factory.get(url_salary_desc)
self.assertEqual(dict(response.data)['results'], VacancySerializer(Vacancy.objects.order_by('-salary'), many=True, context={'request': request}).data)
def test_fail_on_unverified_user_vacancy_list(self):
url = '/api/vacancies/'
......
......@@ -47,6 +47,9 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
def list(self, request, *args, **kwargs):
search = request.query_params['search'] if 'search' in request.query_params else None
verified = request.query_params['verified'] if 'verified' in request.query_params else "True"
order_by = request.GET.get('sort', None)
order = request.GET.get('order', 'ascending')
opened_only = str(request.query_params['opened_only']) \
if 'opened_only' in request.query_params else "False"
companies = [int(x) for x in request.query_params.getlist('company', [])]
......@@ -60,6 +63,7 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
vacancies = self.verifiedFilter(vacancies, verified)
vacancies = self.openedJobFilter(vacancies, opened_only)
vacancies = self.companyFilter(vacancies, companies)
vacancies = self.sort(order_by, vacancies, order)
page = self.paginate_queryset(vacancies)
print(vacancies)
......@@ -86,6 +90,15 @@ 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))
return vacancies
def name_position_validator(self, names):
for name in names.split(" "):
......
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