diff --git a/cookbook/helper/shopping_helper.py b/cookbook/helper/shopping_helper.py index c2e3d5458..d7ff0b634 100644 --- a/cookbook/helper/shopping_helper.py +++ b/cookbook/helper/shopping_helper.py @@ -177,8 +177,9 @@ class RecipeShoppingEditor(): existing = self._shopping_list_recipe.entries.filter(ingredient__in=ingredients).values_list('ingredient__pk', flat=True) add_ingredients = ingredients.exclude(id__in=existing) + entries = [] for i in [x for x in add_ingredients if x.food]: - ShoppingListEntry.objects.create( + entry = ShoppingListEntry( list_recipe=self._shopping_list_recipe, food=i.food, unit=i.unit, @@ -187,6 +188,12 @@ class RecipeShoppingEditor(): created_by=self.created_by, space=self.space, ) + entries.append(entry) + + ShoppingListEntry.objects.bulk_create(entries) + for e in entries: + if e.food.shopping_lists.count() > 0: + e.shopping_lists.set(e.food.shopping_lists.all()) # deletes shopping list entries not in ingredients list def _delete_ingredients(self, ingredients=None): diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 4dd0b2d9c..37ad9b69a 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -1992,19 +1992,22 @@ class ShoppingListRecipeViewSet(LoggingMixin, viewsets.ModelViewSet): if serializer.is_valid(): entries = [] for e in serializer.validated_data['entries']: - entries.append( - ShoppingListEntry( - list_recipe_id=obj.pk, - amount=e['amount'], - unit_id=e['unit_id'], - food_id=e['food_id'], - ingredient_id=e['ingredient_id'], - created_by_id=request.user.id, - space_id=request.space.id, - ) + entry = ShoppingListEntry( + list_recipe_id=obj.pk, + amount=e['amount'], + unit_id=e['unit_id'], + food_id=e['food_id'], + ingredient_id=e['ingredient_id'], + created_by_id=request.user.id, + space_id=request.space.id, ) + entries.append(entry) ShoppingListEntry.objects.bulk_create(entries) + for e in entries: + if e.food.shopping_lists.count() > 0: + e.shopping_lists.set(e.food.shopping_lists.all()) + ConnectorManager.add_work(ActionType.CREATED, *entries) return Response(serializer.validated_data) else: