Skip to content

Commit

Permalink
added filter api
Browse files Browse the repository at this point in the history
  • Loading branch information
kumarvivek1752 committed Apr 15, 2023
1 parent d15cd92 commit 4be8285
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 4 deletions.
50 changes: 48 additions & 2 deletions app/recipe/tests/test_ingrediants_api.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""
Tests for the ingredients API.
"""
from decimal import Decimal

from django.contrib.auth import get_user_model
from django.urls import reverse
from django.test import TestCase

from rest_framework import status
from rest_framework.test import APIClient

from core.models import Ingredient
from core.models import Ingredient,Recipe

from recipe.serializers import IngredientSerializer

Expand Down Expand Up @@ -95,4 +97,48 @@ def test_delete_ingredient(self):

self.assertEqual(res.status_code, status.HTTP_204_NO_CONTENT)
ingredients = Ingredient.objects.filter(user=self.user)
self.assertFalse(ingredients.exists())
self.assertFalse(ingredients.exists())



def test_filter_ingredients_assigned_to_recipes(self):
"""Test listing ingedients to those assigned to recipes."""
in1 = Ingredient.objects.create(user=self.user, name='Apples')
in2 = Ingredient.objects.create(user=self.user, name='Turkey')
recipe = Recipe.objects.create(
title='Apple Crumble',
time_minutes=5,
price=Decimal('4.50'),
user=self.user,
)
recipe.ingredients.add(in1)

res = self.client.get(INGREDIENTS_URL, {'assigned_only': 1})

s1 = IngredientSerializer(in1)
s2 = IngredientSerializer(in2)
self.assertIn(s1.data, res.data)
self.assertNotIn(s2.data, res.data)

def test_filtered_ingredients_unique(self):
"""Test filtered ingredients returns a unique list."""
ing = Ingredient.objects.create(user=self.user, name='Eggs')
Ingredient.objects.create(user=self.user, name='Lentils')
recipe1 = Recipe.objects.create(
title='Eggs Benedict',
time_minutes=60,
price=Decimal('7.00'),
user=self.user,
)
recipe2 = Recipe.objects.create(
title='Herb Eggs',
time_minutes=20,
price=Decimal('4.00'),
user=self.user,
)
recipe1.ingredients.add(ing)
recipe2.ingredients.add(ing)

res = self.client.get(INGREDIENTS_URL, {'assigned_only': 1})

self.assertEqual(len(res.data), 1)
47 changes: 46 additions & 1 deletion app/recipe/tests/test_tag_api.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""
Tests for the tags API.
"""
from decimal import Decimal

from django.contrib.auth import get_user_model
from django.urls import reverse
from django.test import TestCase

from rest_framework import status
from rest_framework.test import APIClient

from core.models import Tag
from core.models import Tag,Recipe

from recipe.serializers import TagSerializer

Expand Down Expand Up @@ -95,3 +97,46 @@ def test_delete_tag(self):
self.assertEqual(res.status_code, status.HTTP_204_NO_CONTENT)
tags = Tag.objects.filter(user=self.user)
self.assertFalse(tags.exists())


def test_filter_tags_assigned_to_recipes(self):
"""Test listing tags to those assigned to recipes."""
tag1 = Tag.objects.create(user=self.user, name='Breakfast')
tag2 = Tag.objects.create(user=self.user, name='Lunch')
recipe = Recipe.objects.create(
title='Green Eggs on Toast',
time_minutes=10,
price=Decimal('2.50'),
user=self.user,
)
recipe.tags.add(tag1)

res = self.client.get(TAGS_URL, {'assigned_only': 1})

s1 = TagSerializer(tag1)
s2 = TagSerializer(tag2)
self.assertIn(s1.data, res.data)
self.assertNotIn(s2.data, res.data)

def test_filtered_tags_unique(self):
"""Test filtered tags returns a unique list."""
tag = Tag.objects.create(user=self.user, name='Breakfast')
Tag.objects.create(user=self.user, name='Dinner')
recipe1 = Recipe.objects.create(
title='Pancakes',
time_minutes=5,
price=Decimal('5.00'),
user=self.user,
)
recipe2 = Recipe.objects.create(
title='Porridge',
time_minutes=3,
price=Decimal('2.00'),
user=self.user,
)
recipe1.tags.add(tag)
recipe2.tags.add(tag)

res = self.client.get(TAGS_URL, {'assigned_only': 1})

self.assertEqual(len(res.data), 1)
23 changes: 22 additions & 1 deletion app/recipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ def upload_image(self, request, pk=None):
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


@extend_schema_view(
list=extend_schema(
parameters=[
OpenApiParameter(
'assigned_only',
OpenApiTypes.INT, enum=[0, 1],
description='Filter by items assigned to recipes.',
),
]
)
)

class BaseRecipeAttrViewSet(mixins.DestroyModelMixin,
mixins.UpdateModelMixin,
mixins.ListModelMixin,
Expand All @@ -114,7 +126,16 @@ class BaseRecipeAttrViewSet(mixins.DestroyModelMixin,

def get_queryset(self):
"""Filter queryset to authenticated user."""
return self.queryset.filter(user=self.request.user).order_by('-name')
assigned_only = bool(
int(self.request.query_params.get('assigned_only', 0))
)
queryset = self.queryset
if assigned_only:
queryset = queryset.filter(recipe__isnull=False)

return queryset.filter(
user=self.request.user
).order_by('-name').distinct()

class TagViewSet(BaseRecipeAttrViewSet):
"""Manage tags in the database."""
Expand Down

0 comments on commit 4be8285

Please sign in to comment.