diff --git a/open_prices/api/prices/filters.py b/open_prices/api/prices/filters.py index c8835a34..f525de71 100644 --- a/open_prices/api/prices/filters.py +++ b/open_prices/api/prices/filters.py @@ -35,6 +35,9 @@ class PriceFilter(django_filters.FilterSet): created__lte = django_filters.DateTimeFilter( field_name="created", lookup_expr="lte" ) + product__categories_tags__contains = django_filters.CharFilter( + field_name="product__categories_tags", lookup_expr="icontains" + ) class Meta: model = Price diff --git a/open_prices/api/prices/tests.py b/open_prices/api/prices/tests.py index 02fba972..9e2dd53e 100644 --- a/open_prices/api/prices/tests.py +++ b/open_prices/api/prices/tests.py @@ -40,6 +40,15 @@ "osm_lon": "5.7153387", } +PRODUCT_8001505005707 = { + "code": "8001505005707", + "product_name": "Nocciolata", + "categories_tags": ["en:breakfasts", "en:spreads"], + "labels_tags": ["en:no-gluten", "en:organic"], + "brands_tags": ["rigoni-di-asiago"], + "price_count": 15, +} + class PriceListApiTest(TestCase): @classmethod @@ -113,11 +122,13 @@ def setUpTestData(cls): cls.user_proof_receipt = ProofFactory( type=proof_constants.TYPE_RECEIPT, owner=cls.user_session.user.user_id ) + cls.product = ProductFactory(**PRODUCT_8001505005707) cls.user_price = PriceFactory( **PRICE_8001505005707, receipt_quantity=2, proof_id=cls.user_proof_receipt.id, owner=cls.user_session.user.user_id, + product=cls.product, ) PriceFactory( type=price_constants.TYPE_CATEGORY, @@ -298,6 +309,13 @@ def test_price_list_filter_by_created(self): response = self.client.get(url) self.assertEqual(response.data["total"], 0) + def test_price_list_filter_by_product_categories_tags(self): + self.assertEqual(Price.objects.count(), 5) + url = self.url + "?product__categories_tags__contains=en:breakfasts" + with self.assertNumQueries(1 + 1): + response = self.client.get(url) + self.assertEqual(response.data["total"], 1) + class PriceDetailApiTest(TestCase): @classmethod