diff --git a/cookbook/templates/url_import.html b/cookbook/templates/url_import.html
index 858e09581..c6f90af8e 100644
--- a/cookbook/templates/url_import.html
+++ b/cookbook/templates/url_import.html
@@ -88,6 +88,19 @@
id="id_btn_app"> {% trans 'Import' %}
+
+
@@ -695,9 +708,20 @@
this.makeToast(gettext('Error'), msg, 'danger')
})
},
- showRecipe: function() {
- this.preview = false
- this.recipe_data = this.recipe_json
+ 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) {
diff --git a/cookbook/urls.py b/cookbook/urls.py
index 03c6a60ee..925e8bfb1 100644
--- a/cookbook/urls.py
+++ b/cookbook/urls.py
@@ -93,7 +93,8 @@ urlpatterns = [
path('api/sync_all/', api.sync_all, name='api_sync'),
path('api/log_cooking//', api.log_cooking, name='api_log_cooking'),
path('api/plan-ical///', api.get_plan_ical, name='api_get_plan_ical'),
- path('api/recipe-from-source/', api.recipe_from_source, name='api_recipe_from_source'),
+ 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'),
diff --git a/cookbook/views/api.py b/cookbook/views/api.py
index f7f90eef7..f48dbb0e6 100644
--- a/cookbook/views/api.py
+++ b/cookbook/views/api.py
@@ -34,15 +34,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_html_import import get_recipe_from_source
-from cookbook.helper.recipe_url_import import get_from_scraper
-
group_required)
-from cookbook.helper.recipe_search import search_recipes
-from cookbook.helper.recipe_url_import import get_from_html, get_from_scraper, find_recipe_json
-
+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,
@@ -609,14 +602,28 @@ def get_plan_ical(request, from_date, to_date):
@group_required('user')
-def recipe_from_source(request):
- url = request.POST.get('url', None)
- data = request.POST.get('data', None)
- mode = request.POST.get('mode', None)
- auto = request.POST.get('auto', 'true')
+def recipe_from_json(request):
+ mjson = request.POST['json']
- HEADERS = {
- "User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7"
+ 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']
+
+ headers = {
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36' # noqa: E501
}
if (not url and not data) or (mode == 'url' and not url) or (mode == 'source' and not data):