From 19708dbc64a18e00ffd45a6210d38008f0464b80 Mon Sep 17 00:00:00 2001 From: smilerz Date: Mon, 14 Feb 2022 17:28:16 -0600 Subject: [PATCH 1/7] paste list of ingredients --- cookbook/views/views.py | 1 + .../apps/RecipeEditView/RecipeEditView.vue | 44 ++++++++++++++++++- vue/src/locales/en.json | 5 ++- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/cookbook/views/views.py b/cookbook/views/views.py index 8a1462a25..6fefc11ab 100644 --- a/cookbook/views/views.py +++ b/cookbook/views/views.py @@ -12,6 +12,7 @@ from django.contrib.auth.models import Group from django.contrib.auth.password_validation import validate_password from django.core.exceptions import ValidationError from django.db.models import Avg, Q, Sum +from django.db.models.functions import Lower from django.http import HttpResponseRedirect, JsonResponse from django.shortcuts import get_object_or_404, redirect, render from django.urls import reverse, reverse_lazy diff --git a/vue/src/apps/RecipeEditView/RecipeEditView.vue b/vue/src/apps/RecipeEditView/RecipeEditView.vue index 0bd118fa6..771cef262 100644 --- a/vue/src/apps/RecipeEditView/RecipeEditView.vue +++ b/vue/src/apps/RecipeEditView/RecipeEditView.vue @@ -210,6 +210,18 @@ {{ $t("File") }} + + {{ $t("paste_ingredients") }} + @@ -516,6 +528,17 @@ + + + + + @@ -574,7 +597,8 @@ export default { recipes_loading: false, message: "", options_limit: 25, - + paste_ingredients: undefined, + paste_step: undefined, show_file_create: false, step_for_file_create: undefined, } @@ -942,6 +966,24 @@ export default { energy: function () { return energyHeading() }, + appendIngredients: function () { + let ing_list = this.paste_ingredients.split(/\r?\n/) + let step = this.recipe.steps.findIndex((x) => x.id == this.paste_step) + this.recipe.steps[step].ingredients_visible = true + ing_list.forEach((ing) => { + this.genericPostAPI("api_ingredient_from_string", { text: ing }).then((result) => { + let unit = null + if (result.data.unit !== "") { + unit = { name: result.data.unit } + } + this.recipe.steps[step].ingredients.push({ + amount: result.data.amount, + unit: unit, + food: { name: result.data.food }, + }) + }) + }) + }, }, } diff --git a/vue/src/locales/en.json b/vue/src/locales/en.json index 9bf72a48c..8942d03e6 100644 --- a/vue/src/locales/en.json +++ b/vue/src/locales/en.json @@ -296,5 +296,8 @@ "food_recipe_help": "Linking a recipe here will include the linked recipe in any other recipe that use this food", "Foods": "Foods", "review_shopping": "Review shopping entries before saving", - "view_recipe": "View Recipe" + "view_recipe": "View Recipe", + "paste_ingredients_placeholder": "Paste ingredient list here...", + "paste_ingredients": "Paste Ingredients", + "ingredient_list": "Ingredient List" } From ad6d99800e14c9170acf88d25e59fb1b2588981b Mon Sep 17 00:00:00 2001 From: smilerz Date: Wed, 16 Feb 2022 08:35:04 -0600 Subject: [PATCH 2/7] include note from ingredient API --- vue/src/apps/RecipeEditView/RecipeEditView.vue | 1 + 1 file changed, 1 insertion(+) diff --git a/vue/src/apps/RecipeEditView/RecipeEditView.vue b/vue/src/apps/RecipeEditView/RecipeEditView.vue index 771cef262..874acdd98 100644 --- a/vue/src/apps/RecipeEditView/RecipeEditView.vue +++ b/vue/src/apps/RecipeEditView/RecipeEditView.vue @@ -980,6 +980,7 @@ export default { amount: result.data.amount, unit: unit, food: { name: result.data.food }, + note: result.data.note, }) }) }) From 8b94bf1333b794660035adefbe384635c24dbec1 Mon Sep 17 00:00:00 2001 From: smilerz Date: Wed, 16 Feb 2022 13:22:53 -0600 Subject: [PATCH 3/7] retain order of pasted ingredients --- vue/src/apps/RecipeEditView/RecipeEditView.vue | 3 +++ 1 file changed, 3 insertions(+) diff --git a/vue/src/apps/RecipeEditView/RecipeEditView.vue b/vue/src/apps/RecipeEditView/RecipeEditView.vue index 874acdd98..1eb9c4949 100644 --- a/vue/src/apps/RecipeEditView/RecipeEditView.vue +++ b/vue/src/apps/RecipeEditView/RecipeEditView.vue @@ -969,6 +969,7 @@ export default { appendIngredients: function () { let ing_list = this.paste_ingredients.split(/\r?\n/) let step = this.recipe.steps.findIndex((x) => x.id == this.paste_step) + let order = 0 this.recipe.steps[step].ingredients_visible = true ing_list.forEach((ing) => { this.genericPostAPI("api_ingredient_from_string", { text: ing }).then((result) => { @@ -981,8 +982,10 @@ export default { unit: unit, food: { name: result.data.food }, note: result.data.note, + order: order, }) }) + order++ }) }, }, From d68a89a32ca808cad36d51823c8118637ed16e01 Mon Sep 17 00:00:00 2001 From: smilerz Date: Wed, 16 Feb 2022 14:07:56 -0600 Subject: [PATCH 4/7] catch empty lines in paste_ingredients --- .../apps/RecipeEditView/RecipeEditView.vue | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/vue/src/apps/RecipeEditView/RecipeEditView.vue b/vue/src/apps/RecipeEditView/RecipeEditView.vue index 1eb9c4949..973b57689 100644 --- a/vue/src/apps/RecipeEditView/RecipeEditView.vue +++ b/vue/src/apps/RecipeEditView/RecipeEditView.vue @@ -969,23 +969,24 @@ export default { appendIngredients: function () { let ing_list = this.paste_ingredients.split(/\r?\n/) let step = this.recipe.steps.findIndex((x) => x.id == this.paste_step) - let order = 0 + let order = Math.max(...this.recipe.steps[step].ingredients.map((x) => x.order)) + 1 this.recipe.steps[step].ingredients_visible = true ing_list.forEach((ing) => { - this.genericPostAPI("api_ingredient_from_string", { text: ing }).then((result) => { - let unit = null - if (result.data.unit !== "") { - unit = { name: result.data.unit } - } - this.recipe.steps[step].ingredients.push({ - amount: result.data.amount, - unit: unit, - food: { name: result.data.food }, - note: result.data.note, - order: order, + if (ing.trim() !== "") { + this.genericPostAPI("api_ingredient_from_string", { text: ing }).then((result) => { + let unit = null + if (result.data.unit !== "") { + unit = { name: result.data.unit } + } + this.recipe.steps[step].ingredients.splice(order, 0, { + amount: result.data.amount, + unit: unit, + food: { name: result.data.food }, + note: result.data.note, + }) }) - }) - order++ + order++ + } }) }, }, From 57dec86b0629949a5997a115be33b5a163549d9f Mon Sep 17 00:00:00 2001 From: smilerz Date: Wed, 16 Feb 2022 14:17:41 -0600 Subject: [PATCH 5/7] handle empty ingredient list when pasting --- vue/src/apps/RecipeEditView/RecipeEditView.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vue/src/apps/RecipeEditView/RecipeEditView.vue b/vue/src/apps/RecipeEditView/RecipeEditView.vue index 973b57689..e644924c6 100644 --- a/vue/src/apps/RecipeEditView/RecipeEditView.vue +++ b/vue/src/apps/RecipeEditView/RecipeEditView.vue @@ -969,7 +969,7 @@ export default { appendIngredients: function () { let ing_list = this.paste_ingredients.split(/\r?\n/) let step = this.recipe.steps.findIndex((x) => x.id == this.paste_step) - let order = Math.max(...this.recipe.steps[step].ingredients.map((x) => x.order)) + 1 + let order = Math.max(...(this.recipe.steps[step].ingredients.map((x) => x.order), 0)) + 1 this.recipe.steps[step].ingredients_visible = true ing_list.forEach((ing) => { if (ing.trim() !== "") { From 5f3d5afc37afba88e45a2c3c5c54f6fcefbae9a3 Mon Sep 17 00:00:00 2001 From: smilerz Date: Wed, 16 Feb 2022 14:18:23 -0600 Subject: [PATCH 6/7] typo --- vue/src/apps/RecipeEditView/RecipeEditView.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vue/src/apps/RecipeEditView/RecipeEditView.vue b/vue/src/apps/RecipeEditView/RecipeEditView.vue index e644924c6..d005853e8 100644 --- a/vue/src/apps/RecipeEditView/RecipeEditView.vue +++ b/vue/src/apps/RecipeEditView/RecipeEditView.vue @@ -969,7 +969,7 @@ export default { appendIngredients: function () { let ing_list = this.paste_ingredients.split(/\r?\n/) let step = this.recipe.steps.findIndex((x) => x.id == this.paste_step) - let order = Math.max(...(this.recipe.steps[step].ingredients.map((x) => x.order), 0)) + 1 + let order = Math.max(...this.recipe.steps[step].ingredients.map((x) => x.order), 0) + 1 this.recipe.steps[step].ingredients_visible = true ing_list.forEach((ing) => { if (ing.trim() !== "") { From 21c6f819a0c452b01541e4c1bcabfaf27454e477 Mon Sep 17 00:00:00 2001 From: smilerz Date: Wed, 16 Feb 2022 14:19:58 -0600 Subject: [PATCH 7/7] start counting at zero --- vue/src/apps/RecipeEditView/RecipeEditView.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vue/src/apps/RecipeEditView/RecipeEditView.vue b/vue/src/apps/RecipeEditView/RecipeEditView.vue index d005853e8..14ccd0c3f 100644 --- a/vue/src/apps/RecipeEditView/RecipeEditView.vue +++ b/vue/src/apps/RecipeEditView/RecipeEditView.vue @@ -969,7 +969,7 @@ export default { appendIngredients: function () { let ing_list = this.paste_ingredients.split(/\r?\n/) let step = this.recipe.steps.findIndex((x) => x.id == this.paste_step) - let order = Math.max(...this.recipe.steps[step].ingredients.map((x) => x.order), 0) + 1 + let order = Math.max(...this.recipe.steps[step].ingredients.map((x) => x.order), -1) + 1 this.recipe.steps[step].ingredients_visible = true ing_list.forEach((ing) => { if (ing.trim() !== "") {