Skip to content

Commit

Permalink
Added feature to filter recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermemachado26 committed Feb 27, 2020
1 parent 41bdbdd commit 387b48f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
44 changes: 44 additions & 0 deletions app/recipe/tests/test_recipe_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,47 @@ def test_upload_image_bad_request(self):
res = self.client.post(url, {'image': 'notimage'}, format='multipart')

self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)

def test_filter_recipes_by_tags(self):
"""Test returning recipes with specific tags"""
recipe1 = sample_recipe(user=self.user, title='Thai vegetable curry')
recipe2 = sample_recipe(user=self.user, title='Aubergine with tahini')
tag1 = sample_tag(user=self.user, name='Vegan')
tag2 = sample_tag(user=self.user, name='Vegetarian')
recipe1.tags.add(tag1)
recipe2.tags.add(tag2)
recipe3 = sample_recipe(user=self.user, title='Fish and chips')

res = self.client.get(
RECIPES_URL,
{'tags': f'{tag1.id},{tag2.id}'}
)

serializer1 = RecipeSerializer(recipe1)
serializer2 = RecipeSerializer(recipe2)
serializer3 = RecipeSerializer(recipe3)
self.assertIn(serializer1.data, res.data)
self.assertIn(serializer2.data, res.data)
self.assertNotIn(serializer3.data, res.data)

def test_filter_recipes_by_ingredients(self):
"""Test returning recipes with specific ingredients"""
recipe1 = sample_recipe(user=self.user, title='Posh beans on toast')
recipe2 = sample_recipe(user=self.user, title='Chicken cacciatore')
ingredient1 = sample_ingredient(user=self.user, name='Feta cheese')
ingredient2 = sample_ingredient(user=self.user, name='Chicken')
recipe1.ingredients.add(ingredient1)
recipe2.ingredients.add(ingredient2)
recipe3 = sample_recipe(user=self.user, title='Steak and mushrooms')

res = self.client.get(
RECIPES_URL,
{'ingredients': f'{ingredient1.id},{ingredient2.id}'}
)

serializer1 = RecipeSerializer(recipe1)
serializer2 = RecipeSerializer(recipe2)
serializer3 = RecipeSerializer(recipe3)
self.assertIn(serializer1.data, res.data)
self.assertIn(serializer2.data, res.data)
self.assertNotIn(serializer3.data, res.data)
18 changes: 17 additions & 1 deletion app/recipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,25 @@ class RecipeViewSet(viewsets.ModelViewSet):
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)

def _params_to_ints(self, qs):
"""Convert a list of string IDs to a list of integers"""
return [int(str_id) for str_id in qs.split(',')]

def get_queryset(self):
"""Retrieve the recipe for the authenticated user"""
return self.queryset.filter(user=self.request.user)
tags = self.request.query_params.get('tags')
ingredients = self.request.query_params.get('ingredients')
queryset = self.queryset

if tags:
tag_ids = self._params_to_ints(tags)
queryset = queryset.filter(tags__id__in=tag_ids)

if ingredients:
ingredient_ids = self._params_to_ints(ingredients)
queryset = queryset.filter(ingredients__id__in=ingredient_ids)

return queryset.filter(user=self.request.user)

def get_serializer_class(self, a=None):
"""Return appropriate serializer class"""
Expand Down

0 comments on commit 387b48f

Please sign in to comment.