def get_queryset(self): q = self.request.query_params.get('q', None) if q: return Restaurant.get_searched_list(q=q) # querystring에서 type, price, district를 찾아 딕셔너리 형태로 Key에 대입, 없을경우 None객체를 넣음 filter_fields = { 'restaurant_type': self.request.query_params.get('type', None), 'average_price': self.request.query_params.get('price', None), 'district': self.request.query_params.get('district', None), } return Restaurant.get_filtered_list(filter_fields=filter_fields)
def test_get_restaurant_list_with_params(self): user = self.create_user() num = randint(1, 100) for i in range(num): self.create_restaurant(user=user) url = reverse(self.URL_RESTAURANT_LIST_NAME) params = { 'type': CHOICES_RESTAURANT_TYPE[randint(0, len(CHOICES_RESTAURANT_TYPE) - 1)][0], 'price': CHOICES_PRICE[randint(0, len(CHOICES_PRICE) - 1)][0], } # 필터된 리스트와 비교를 하기 위해 filter된 Restaurant List를 받아옴 filtered_restaurant = Restaurant.objects.filter(restaurant_type=params['type'], average_price=params['price']) response = self.client.get(url, params) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['count'], filtered_restaurant.count()) # 대문자 검색 테스트 search_param_uppercase = { 'q': 'DUMMY' } searched_upper_restaurant = Restaurant.get_searched_list(q=search_param_uppercase['q']) upper_response = self.client.get(url, search_param_uppercase) self.assertEqual(upper_response.status_code, status.HTTP_200_OK) self.assertEqual(upper_response.data['count'], searched_upper_restaurant.count()) # 소문자 검색 테스트 search_param_lowercase = { 'q': 'dummy' } searched_lower_restaurant = Restaurant.get_searched_list(q=search_param_lowercase['q']) lower_response = self.client.get(url, search_param_lowercase) self.assertEqual(lower_response.status_code, status.HTTP_200_OK) self.assertEqual(lower_response.data['count'], searched_lower_restaurant.count()) # 대문자 검색과 소문자 검색 결과가 같은지 테스트 self.assertEqual(upper_response.data['count'], lower_response.data['count'])
def restaurant_list_view(request): if request.method == "GET": q = request.GET.get('q', None) if q: restaurant = Restaurant.get_searched_list(q=q) return render(request, 'restaurant/list.html', {'list': restaurant}) filter_fileds = { 'restaurant_type': request.GET.get('type', None), 'average_price': request.GET.get('price', None), 'district': request.GET.get('district', None), } restaurant = Restaurant.get_filtered_list(filter_fields=filter_fileds) return render(request, 'restaurant/list.html', {'list': restaurant}) else: raise Http404