Skip to content

Commit

Permalink
added update tag
Browse files Browse the repository at this point in the history
  • Loading branch information
kumarvivek1752 committed Apr 13, 2023
1 parent e586d24 commit 66e3748
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
23 changes: 23 additions & 0 deletions app/recipe/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,38 @@ def create(self, validated_data):
"""Create a recipe."""
tags = validated_data.pop('tags', [])
recipe = Recipe.objects.create(**validated_data)
def _get_or_create_tags(self, tags, recipe):
"""Handle getting or creating tags as needed."""
auth_user = self.context['request'].user

for tag in tags:
tag_obj, created = Tag.objects.get_or_create(
user=auth_user,
**tag,
)
recipe.tags.add(tag_obj)


def create(self, validated_data):
"""Create a recipe."""
tags = validated_data.pop('tags', [])
recipe = Recipe.objects.create(**validated_data)
self._get_or_create_tags(tags, recipe)

return recipe

def update(self, instance, validated_data):
"""Update recipe."""
tags = validated_data.pop('tags', None)
if tags is not None:
instance.tags.clear()
self._get_or_create_tags(tags, instance)

for attr, value in validated_data.items():
setattr(instance, attr, value)

instance.save()
return instance



Expand Down
43 changes: 42 additions & 1 deletion app/recipe/tests/test_recipe_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,45 @@ def test_create_recipe_with_existing_tags(self):
name=tag['name'],
user=self.user,
).exists()
self.assertTrue(exists)
self.assertTrue(exists)


def test_create_tag_on_update(self):
"""Test create tag when updating a recipe."""
recipe = create_recipe(user=self.user)

payload = {'tags': [{'name': 'Lunch'}]}
url = detail_url(recipe.id)
res = self.client.patch(url, payload, format='json')

self.assertEqual(res.status_code, status.HTTP_200_OK)
new_tag = Tag.objects.get(user=self.user, name='Lunch')
self.assertIn(new_tag, recipe.tags.all())

def test_update_recipe_assign_tag(self):
"""Test assigning an existing tag when updating a recipe."""
tag_breakfast = Tag.objects.create(user=self.user, name='Breakfast')
recipe = create_recipe(user=self.user)
recipe.tags.add(tag_breakfast)

tag_lunch = Tag.objects.create(user=self.user, name='Lunch')
payload = {'tags': [{'name': 'Lunch'}]}
url = detail_url(recipe.id)
res = self.client.patch(url, payload, format='json')

self.assertEqual(res.status_code, status.HTTP_200_OK)
self.assertIn(tag_lunch, recipe.tags.all())
self.assertNotIn(tag_breakfast, recipe.tags.all())

def test_clear_recipe_tags(self):
"""Test clearing a recipes tags."""
tag = Tag.objects.create(user=self.user, name='Dessert')
recipe = create_recipe(user=self.user)
recipe.tags.add(tag)

payload = {'tags': []}
url = detail_url(recipe.id)
res = self.client.patch(url, payload, format='json')

self.assertEqual(res.status_code, status.HTTP_200_OK)
self.assertEqual(recipe.tags.count(), 0)

0 comments on commit 66e3748

Please sign in to comment.