From 386834f409bec8bc2db214306e58adbffb32e557 Mon Sep 17 00:00:00 2001 From: Patrick Pirker Date: Wed, 3 Mar 2021 21:37:39 +0100 Subject: [PATCH] Allow recipes to be imported from json directly --- cookbook/templates/url_import.html | 27 +++++++++++++++++++++++++++ cookbook/urls.py | 1 + cookbook/views/api.py | 21 +++++++++++++++++++-- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/cookbook/templates/url_import.html b/cookbook/templates/url_import.html index 64c4ec0e..57dddcb7 100644 --- a/cookbook/templates/url_import.html +++ b/cookbook/templates/url_import.html @@ -29,6 +29,18 @@ +
+
+
+ +
+ +
+
+
+
@@ -369,6 +381,21 @@ this.makeToast(gettext('Error'), msg, 'danger') }) }, + loadRecipeJson: function () { + this.recipe_data = undefined + this.error = undefined + this.loading = true + this.$http.post("{% url 'api_recipe_from_json' %}", {'json': this.json_data}, {emulateJSON: true}).then((response) => { + console.log(response.data) + this.recipe_data = response.data; + this.loading = false + }).catch((err) => { + this.error = err.data + this.loading = false + console.log(err) + this.makeToast(gettext('Error'), gettext('There was an error loading a resource!') + err.bodyText, 'danger') + }) + }, importRecipe: function () { if (this.recipe_data.name.length > 128) { this.makeToast(gettext('Error'), gettext('Recipe name is longer than 128 characters'), 'danger') diff --git a/cookbook/urls.py b/cookbook/urls.py index bbe97dac..925e8bfb 100644 --- a/cookbook/urls.py +++ b/cookbook/urls.py @@ -95,6 +95,7 @@ urlpatterns = [ path('api/plan-ical///', api.get_plan_ical, name='api_get_plan_ical'), path('api/recipe-from-url/', api.recipe_from_url, name='api_recipe_from_url'), path('api/recipe-from-json/', api.recipe_from_json, name='api_recipe_from_json'), + path('api/backup/', api.get_backup, name='api_backup'), path('api/ingredient-from-string/', api.ingredient_from_string, name='api_ingredient_from_string'), path('dal/keyword/', dal.KeywordAutocomplete.as_view(), name='dal_keyword'), diff --git a/cookbook/views/api.py b/cookbook/views/api.py index dc254241..07a29bd3 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -27,8 +27,8 @@ from cookbook.helper.ingredient_parser import parse from cookbook.helper.permission_helper import (CustomIsAdmin, CustomIsGuest, CustomIsOwner, CustomIsShare, CustomIsShared, CustomIsUser, - group_required, share_link_valid) -from cookbook.helper.recipe_url_import import get_from_html, get_from_scraper, find_recipe_json + group_required) +from cookbook.helper.recipe_url_import import get_from_html, find_recipe_json from cookbook.models import (CookLog, Food, Ingredient, Keyword, MealPlan, MealType, Recipe, RecipeBook, ShoppingList, ShoppingListEntry, ShoppingListRecipe, Step, @@ -533,6 +533,23 @@ def get_plan_ical(request, from_date, to_date): return response +@group_required('user') +def recipe_from_json(request): + mjson = request.POST['json'] + + md_json = json.loads(mjson) + if ('@type' in md_json + and md_json['@type'] == 'Recipe'): + return JsonResponse(find_recipe_json(md_json, '')) + return JsonResponse( + { + 'error': True, + 'msg': _('Could not parse correctly...') + }, + status=400 + ) + + @group_required('user') def recipe_from_url(request): url = request.POST['url']