Fakultas Ilmu Komputer UI

Commit 39cbf6f0 authored by FARI QODRI ANDANA's avatar FARI QODRI ANDANA
Browse files

1606875964 202 Sort vacancies by most recent

parent 7a9f7d3b
......@@ -241,12 +241,16 @@ class VacancyTest(APITestCase):
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,
v1 = 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,
v1.created = datetime(2019, 12, 10)
v1.save()
v2 = 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")
working_period="3 Bulan", created=datetime(2018, 12, 10))
v2.created = datetime(2018, 12, 10)
v2.save()
factory = APIRequestFactory()
url_name_asc = '/api/vacancies/?sort=name&order=ascending'
request = factory.get(url_name_asc)
......@@ -274,6 +278,13 @@ class VacancyTest(APITestCase):
VacancySerializer(Vacancy.objects.order_by('-salary'), many=True,
context={'request': request}).data)
url_recent = '/api/vacancies/?sort=recent'
response = self.client.get(url_recent)
request = factory.get(url_recent)
self.assertEqual(dict(response.data)['results'],
VacancySerializer(Vacancy.objects.order_by('-created'), many=True,
context={'request': request}).data)
def test_fail_on_unverified_user_vacancy_list(self):
url = '/api/vacancies/'
response = self.client.post(url, format='json')
......
......@@ -101,8 +101,10 @@ class VacancyViewSet(MultiSerializerViewSetMixin, viewsets.ModelViewSet):
return vacancies
def sort(self, order_by, vacancies, order):
if (order_by is not None) and (order_by in [views_constants.NAME, views_constants.SALARY]) and (
if (order_by is not None) and (order_by in [views_constants.NAME, views_constants.SALARY, views_constants.MOST_RECENT]) and (
order in [views_constants.ORDER_ASCENDING, views_constants.ORDER_DESCENDING]):
if order_by == views_constants.MOST_RECENT:
return vacancies.order_by('-created')
order_query = ""
if order == views_constants.ORDER_DESCENDING:
order_query = "-"
......
......@@ -55,6 +55,7 @@ ERROR = 'error'
IS_VALID = 'is_valid'
MAX_ACCEPTED_APPLICANTS = 'max_accepted_applicants'
NAME = 'name'
MOST_RECENT='recent'
OFFICE_ADDRESS = 'office_address'
OPEN_TIME = 'open_time'
OPENED_ONLY = 'opened_only'
......
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