diff --git a/core/tests/test_vacancies.py b/core/tests/test_vacancies.py index 9c87d52cf96f0c539c8828d2ff9297d5ff29ab05..f931133b97d1faf92ff0a750b725203bb7a88bbb 100755 --- a/core/tests/test_vacancies.py +++ b/core/tests/test_vacancies.py @@ -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') diff --git a/core/views/vacancies.py b/core/views/vacancies.py index f20275b086cf1370613611f0e7c4660a541d5039..b60289a25693469b1e911013ffbf4a9aa560ba8f 100644 --- a/core/views/vacancies.py +++ b/core/views/vacancies.py @@ -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 = "-" diff --git a/core/views/views_constants.py b/core/views/views_constants.py index c095e9a7de3c7cd645d137722683b9ba0f2890f5..eef143029ced5c1141dd3a4c6236ab958e91ef07 100644 --- a/core/views/views_constants.py +++ b/core/views/views_constants.py @@ -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'