default list when adding trough mealplan/recipe

This commit is contained in:
vabene1111
2025-12-04 08:07:32 +01:00
parent 17de37b9fc
commit 0039654d40
2 changed files with 21 additions and 11 deletions

View File

@@ -177,8 +177,9 @@ class RecipeShoppingEditor():
existing = self._shopping_list_recipe.entries.filter(ingredient__in=ingredients).values_list('ingredient__pk', flat=True) existing = self._shopping_list_recipe.entries.filter(ingredient__in=ingredients).values_list('ingredient__pk', flat=True)
add_ingredients = ingredients.exclude(id__in=existing) add_ingredients = ingredients.exclude(id__in=existing)
entries = []
for i in [x for x in add_ingredients if x.food]: for i in [x for x in add_ingredients if x.food]:
ShoppingListEntry.objects.create( entry = ShoppingListEntry(
list_recipe=self._shopping_list_recipe, list_recipe=self._shopping_list_recipe,
food=i.food, food=i.food,
unit=i.unit, unit=i.unit,
@@ -187,6 +188,12 @@ class RecipeShoppingEditor():
created_by=self.created_by, created_by=self.created_by,
space=self.space, 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 # deletes shopping list entries not in ingredients list
def _delete_ingredients(self, ingredients=None): def _delete_ingredients(self, ingredients=None):

View File

@@ -1992,19 +1992,22 @@ class ShoppingListRecipeViewSet(LoggingMixin, viewsets.ModelViewSet):
if serializer.is_valid(): if serializer.is_valid():
entries = [] entries = []
for e in serializer.validated_data['entries']: for e in serializer.validated_data['entries']:
entries.append( entry = ShoppingListEntry(
ShoppingListEntry( list_recipe_id=obj.pk,
list_recipe_id=obj.pk, amount=e['amount'],
amount=e['amount'], unit_id=e['unit_id'],
unit_id=e['unit_id'], food_id=e['food_id'],
food_id=e['food_id'], ingredient_id=e['ingredient_id'],
ingredient_id=e['ingredient_id'], created_by_id=request.user.id,
created_by_id=request.user.id, space_id=request.space.id,
space_id=request.space.id,
)
) )
entries.append(entry)
ShoppingListEntry.objects.bulk_create(entries) 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) ConnectorManager.add_work(ActionType.CREATED, *entries)
return Response(serializer.validated_data) return Response(serializer.validated_data)
else: else: