From b9f49cad451a7e2e719e0f13735bd343d77eb434 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Wed, 4 Dec 2024 18:21:19 +0100 Subject: [PATCH] ingredient from string API --- cookbook/views/api.py | 11 +++++++- .../components/display/ShoppingListView.vue | 4 +-- vue3/src/components/inputs/StepEditor.vue | 27 ++++++++++++++++--- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 1d519db18..51b654646 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -2098,4 +2098,13 @@ def ingredient_from_string(request): ingredient_parser = IngredientParser(request, False) amount, unit, food, note = ingredient_parser.parse(text) - return JsonResponse({'amount': amount, 'unit': unit, 'food': food, 'note': note}, status=200) + ingredient = {'amount': amount, 'unit': None, 'food': None, 'note': note} + if food: + food, created = Food.objects.get_or_create(space=request.space, name=food) + ingredient['food'] = {'name': food.name, 'id': food.id} + + if unit: + unit, created = Unit.objects.get_or_create(space=request.space, name=unit) + ingredient['unit'] = {'name': unit.name, 'id': unit.id} + + return JsonResponse(ingredient, status=200) diff --git a/vue3/src/components/display/ShoppingListView.vue b/vue3/src/components/display/ShoppingListView.vue index 6cd09a2f0..926d4baed 100644 --- a/vue3/src/components/display/ShoppingListView.vue +++ b/vue3/src/components/display/ShoppingListView.vue @@ -269,8 +269,8 @@ function addIngredient() { api.apiIngredientFromStringCreate({ingredientString: {text: ingredientInput.value} as IngredientString}).then(r => { useShoppingStore().createObject({ amount: Math.max(r.amount, 1), - unit: (r.unit != null) ? {name: r.unit} as Unit : null, - food: {name: r.food} as Food, + unit: r.unit, + food: r.food, } as ShoppingListEntry, true) ingredientInput.value = '' diff --git a/vue3/src/components/inputs/StepEditor.vue b/vue3/src/components/inputs/StepEditor.vue index 8547abc24..ec51ba508 100644 --- a/vue3/src/components/inputs/StepEditor.vue +++ b/vue3/src/components/inputs/StepEditor.vue @@ -110,7 +110,7 @@ - {{$t('Add')}} + {{ $t('Add') }} @@ -118,7 +118,7 @@