diff --git a/cookbook/helper/recipe_url_import.py b/cookbook/helper/recipe_url_import.py
index 83831c2ff..9ab62e44a 100644
--- a/cookbook/helper/recipe_url_import.py
+++ b/cookbook/helper/recipe_url_import.py
@@ -95,7 +95,7 @@ def find_recipe_json(ld_json, url, space):
if 'recipeCuisine' in ld_json:
keywords += listify_keywords(ld_json['recipeCuisine'])
try:
- ld_json['keywords'] = parse_keywords(list(set(map(str.casefold, keywords))))
+ ld_json['keywords'] = parse_keywords(list(set(map(str.casefold, keywords))), space)
except TypeError:
pass
@@ -197,7 +197,7 @@ def get_from_scraper(scrape, space):
ingredients = []
for x in scrape.ingredients():
try:
- amount, unit, ingredient, note = parse_ingredient(x)
+ amount, unit, ingredient, note = parse_single_ingredient(x)
if ingredient:
ingredients.append(
{
diff --git a/cookbook/templates/import_json.html b/cookbook/templates/import_json.html
index ce7780560..7d0058fa1 100644
--- a/cookbook/templates/import_json.html
+++ b/cookbook/templates/import_json.html
@@ -137,7 +137,7 @@
this.error = undefined
this.parsed = true
this.loading = true
- this.$http.post("{% url 'api_recipe_from_html' %}", {'html_text': this.html_recipe}, {emulateJSON: true}).then((response) => {
+ this.$http.post("{% url 'api_manual_recipe_from_json' %}", {'html_text': this.html_recipe}, {emulateJSON: true}).then((response) => {
console.log(response.data)
this.recipe_data = response.data['recipe_data'];
this.recipe_tree = response.data['recipe_tree'];
diff --git a/cookbook/templates/import_json_working.html b/cookbook/templates/import_json_working.html
index 1604ece1d..6c2d6fce8 100644
--- a/cookbook/templates/import_json_working.html
+++ b/cookbook/templates/import_json_working.html
@@ -152,7 +152,7 @@
this.error = undefined
this.parsed = true
this.loading = true
- this.$http.post("{% url 'api_recipe_from_html' %}", {'html_text': this.html_recipe}, {emulateJSON: true}).then((response) => {
+ this.$http.post("{% url 'api_manual_recipe_from_json' %}", {'html_text': this.html_recipe}, {emulateJSON: true}).then((response) => {
console.log(response.data)
this.recipe_data = response.data['recipe_data'];
this.recipe_tree = response.data['recipe_tree'];
diff --git a/cookbook/templates/url_import.html b/cookbook/templates/url_import.html
index 59ef707ad..14d1e68a2 100644
--- a/cookbook/templates/url_import.html
+++ b/cookbook/templates/url_import.html
@@ -52,7 +52,7 @@
-
@@ -66,7 +66,7 @@
-
@@ -81,7 +81,7 @@
-
+
@@ -515,7 +515,7 @@
el: '#app',
data: {
remote_url: '',
- json_data: undefined,
+ raw_data: undefined,
keywords: [],
keywords_loading: false,
units: [],
@@ -530,8 +530,6 @@
importing_recipe: false,
recipe_json: undefined,
recipe_tree: undefined,
- recipe_tree1: undefined,
- html_data: undefined,
},
directives: {
tabindex: {
@@ -598,7 +596,7 @@
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) => {
+ this.$http.post("{% url 'api_recipe_from_json' %}", {'json': this.raw_data}, {emulateJSON: true}).then((response) => {
console.log(response.data)
this.recipe_data = response.data;
this.loading = false
@@ -609,11 +607,13 @@
this.makeToast(gettext('Error'), gettext('There was an error loading a resource!') + err.bodyText, 'danger')
})
},
- loadRecipeJson: function () {
- this.recipe_data = undefined
+ loadRecipeHTML: function () {
+ // TODO this needs refactored to get HTML elements
this.error = undefined
this.loading = true
- this.$http.post("{% url 'api_recipe_from_json' %}", {'json': this.json_data}, {emulateJSON: true}).then((response) => {
+ this.parsed = false
+ this.recipe_json['@type'] = "Recipe"
+ this.$http.post("{% url 'api_recipe_from_json' %}", {'html': JSON.stringify(this.raw_data)}, {emulateJSON: true}).then((response) => {
console.log(response.data)
this.recipe_data = response.data;
this.loading = false
@@ -630,10 +630,9 @@
this.error = undefined
this.loading = true
- this.$http.post("{% url 'api_recipe_from_html' %}", {'html_data': this.html_data}, {emulateJSON: true}).then((response) => {
+ this.$http.post("{% url 'api_manual_recipe_from_json' %}", {'data': this.raw_data}, {emulateJSON: true}).then((response) => {
console.log(response.data)
this.recipe_json = response.data['recipe_json'];
- this.recipe_tree1 = JSON.stringify(response.data['recipe_tree'], null, 2);
this.recipe_tree = response.data['recipe_tree'];
this.loading = false
this.parsed = true
@@ -645,23 +644,6 @@
this.makeToast(gettext('Error'), gettext('There was an error loading a resource!') + err.bodyText, 'danger')
})
},
- loadRecipeHTML: function () {
- // TODO this needs refactored to get HTML elements
- this.error = undefined
- this.loading = true
- this.parsed = false
- this.recipe_json['@type'] = "Recipe"
- this.$http.post("{% url 'api_recipe_from_json' %}", {'json': JSON.stringify(this.recipe_json)}, {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 bc28374af..062fe7a44 100644
--- a/cookbook/urls.py
+++ b/cookbook/urls.py
@@ -93,7 +93,7 @@ urlpatterns = [
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-url/', api.recipe_from_url, name='api_recipe_from_url'),
- path('api/recipe-from-html/', api.recipe_from_html, name='api_recipe_from_html'),
+ path('api/recipe-from-html/', api.manual_recipe_from_json, name='api_manual_recipe_from_json'),
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 57b0b5498..5191a293c 100644
--- a/cookbook/views/api.py
+++ b/cookbook/views/api.py
@@ -607,9 +607,17 @@ 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, ''))
+ for ld_json_item in md_json:
+ # recipes type might be wrapped in @graph type
+ if '@graph' in ld_json_item:
+ for x in md_json['@graph']:
+ if '@type' in x and x['@type'] == 'Recipe':
+ md_json = x
+
+ if ('@type' in md_json
+ and md_json['@type'] == 'Recipe'):
+ return JsonResponse(find_recipe_json(md_json, '', request.space))
+
return JsonResponse(
{
'error': True,
@@ -698,14 +706,14 @@ def recipe_from_url(request):
@group_required('user')
-def recipe_from_html(request):
- html_data = request.POST['html_data']
- recipe_json, recipe_tree = get_from_raw(html_data)
+def manual_recipe_from_json(request):
+ json_data = request.POST['data']
+ recipe_json, recipe_tree = get_from_raw(json_data)
if len(recipe_tree) == 0 and len(recipe_json) == 0:
return JsonResponse(
{
'error': True,
- 'msg': _('The requested page refused to provide any information (Status Code 403).') # noqa: E501
+ 'msg': _('No useable data could be found.') # noqa: E501
},
status=400
)
diff --git a/cookbook/views/data.py b/cookbook/views/data.py
index 475454eab..f6fc75c4f 100644
--- a/cookbook/views/data.py
+++ b/cookbook/views/data.py
@@ -132,13 +132,11 @@ def import_url(request):
recipe.steps.add(step)
for kw in data['keywords']:
- # if k := Keyword.objects.filter(name=kw['text'], space=request.space).first():
- # recipe.keywords.add(k)
- # elif data['all_keywords']:
- # k = Keyword.objects.create(name=kw['text'], space=request.space)
- # recipe.keywords.add(k)
- k, created = Keyword.objects.get_or_create(name=kw['text'].strip(), space=request.space)
- recipe.keywords.add(k)
+ if k := Keyword.objects.filter(name=kw['text'], space=request.space).first():
+ recipe.keywords.add(k)
+ elif data['all_keywords']:
+ k = Keyword.objects.create(name=kw['text'])
+ recipe.keywords.add(k)
for ing in data['recipeIngredient']:
ingredient = Ingredient()