@@ -94,7 +95,7 @@
delimiters: ['[[', ']]'],
el: '#app',
data: {
- raw_recipe: '',
+ html_recipe: '',
keywords: [],
keywords_loading: false,
units: [],
@@ -151,7 +152,7 @@
this.error = undefined
this.parsed = true
this.loading = true
- this.$http.post("{% url 'api_recipe_from_raw' %}", {'raw_text': this.raw_recipe}, {emulateJSON: true}).then((response) => {
+ this.$http.post("{% url 'api_recipe_from_html' %}", {'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 1cd0fae6..1187dc83 100644
--- a/cookbook/templates/url_import.html
+++ b/cookbook/templates/url_import.html
@@ -8,8 +8,14 @@
{% include 'include/vue_base.html' %}
+
+
+
-
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
{% trans 'Preview Import' %}
@@ -74,6 +99,169 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
[[recipe_json.description]]
+
+
+
+
+
+
+
+
+
![Recipe Image]()
+
+
+
+
+
+
+
+
+
[[recipe_json.servings]]
+
+
+
+
+
+
+
+
[[recipe_json.prepTime]]
+
+
+
+
+
+
+
+
[[recipe_json.cookTime]]
+
+
+
+
+
+
+
+
+
+
[[i.amount]] [[i.unit.text]] [[i.ingredient.text]] [[i.note]]
+
+
+
+
+
+
+
+
[[recipe_json.recipeInstructions]]
+
+
+
+
+
+
{% trans 'Import' %}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ {% verbatim %}
+ [[_.model.name]]
+ {% endverbatim %}
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -82,6 +270,10 @@
+
+
+
+
@@ -326,9 +518,13 @@
recipe_data: undefined,
error: undefined,
loading: false,
+ parsed: false,
all_keywords: false,
importing_recipe: false,
- json_data: '',
+ recipe_json: undefined,
+ recipe_tree: undefined,
+ recipe_tree1: undefined,
+ html_data: undefined,
},
directives: {
tabindex: {
@@ -410,6 +606,58 @@
this.makeToast(gettext('Error'), gettext('There was an error loading a resource!') + err.bodyText, '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')
+ })
+ },
+ loadPreviewRaw: function () {
+ this.recipe_json = undefined
+ this.recipe_tree = undefined
+ this.error = undefined
+
+ this.loading = true
+ this.$http.post("{% url 'api_recipe_from_html' %}", {'html_data': this.html_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
+ }).catch((err) => {
+ this.error = err.data
+ this.loading = false
+ this.parsed = false
+ console.log(err)
+ this.makeToast(gettext('Error'), gettext('There was an error loading a resource!') + err.bodyText, 'danger')
+ })
+ },
+ loadRecipeHTML: function () {
+ 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')
@@ -517,6 +765,93 @@
this.makeToast(gettext('Error'), gettext('There was an error loading a resource!') + err.bodyText, 'danger')
})
},
+ deleteNode: function (node ,item, e) {
+ e.stopPropagation()
+ var index = node.parentItem.indexOf(item)
+ node.parentItem.splice(index, 1)
+ },
+ deletePreview: function(field) {
+ switch (field) {
+ case 'name':
+ this.recipe_json.name=""
+ break;
+ case 'description':
+ this.recipe_json.description=""
+ break;
+ case 'image':
+ this.recipe_json.image=""
+ break;
+ case 'keywords':
+ this.recipe_json.keywords=[]
+ break;
+ case 'servings':
+ this.recipe_json.servings=""
+ break;
+ case 'prepTime':
+ this.recipe_json.prepTime=""
+ break;
+ case 'cookTime':
+ this.recipe_json.cookTime=""
+ break;
+ case 'ingredients':
+ this.recipe_json.recipeIngredient=[]
+ break;
+ case 'instructions':
+ this.recipe_json.recipeInstructions=""
+ break;
+ }
+ },
+ itemClick: function (node, item, e) {
+ this.makeToast(gettext('Details'), node.model.value, 'info')
+ },
+ itemDragStart (node,item,e) {
+ if (node.model.children.length > 0) {
+ e.dataTransfer.setData('hasChildren', true)
+ }
+ e.dataTransfer.setData('value', node.model.value)
+
+ },
+ replacePreview: function(field, e) {
+ v = e.dataTransfer.getData('value')
+ if (e.dataTransfer.getData('hasChildren')) {
+ this.makeToast(gettext('Error'), gettext('Items with children cannot be dropped here!') , 'danger')
+ return
+ }
+ switch (field) {
+ case 'name':
+ this.recipe_json.name=v
+ break;
+ case 'description':
+ this.recipe_json.description=v
+ break;
+ case 'image':
+ this.recipe_json.image=v
+ break;
+ case 'keywords':
+ this.recipe_json.keywords.push(v)
+ break;
+ case 'servings':
+ this.recipe_json.servings=v
+ break;
+ case 'prepTime':
+ this.recipe_json.prepTime=v
+ break;
+ case 'cookTime':
+ this.recipe_json.cookTime=v
+ break;
+ case 'ingredients':
+ let new_ingredient={
+ unit: {id: Math.random() * 1000, text: ""},
+ amount: "",
+ ingredient: {id: Math.random() * 1000, text: v}
+ }
+ this.recipe_json.recipeIngredient=[new_ingredient]
+ break;
+ case 'instructions':
+ this.recipe_json.recipeInstructions=this.recipe_json.recipeInstructions.concat(v)
+ break;
+ }
+ }
}
});
diff --git a/cookbook/views/api.py b/cookbook/views/api.py
index ac18edba..135ffb06 100644
--- a/cookbook/views/api.py
+++ b/cookbook/views/api.py
@@ -29,7 +29,7 @@ from cookbook.helper.permission_helper import (CustomIsAdmin, CustomIsGuest,
CustomIsShared, CustomIsUser,
group_required)
from cookbook.helper.recipe_url_import import get_from_html, find_recipe_json
-from cookbook.helper.recipe_html_import import get_from_html
+from cookbook.helper.recipe_html_import import get_from_raw
from cookbook.models import (CookLog, Food, Ingredient, Keyword, MealPlan,
MealType, Recipe, RecipeBook, ShoppingList,
ShoppingListEntry, ShoppingListRecipe, Step,
@@ -610,11 +610,20 @@ def recipe_from_url_old(request):
@group_required('user')
def recipe_from_html(request):
html_data = request.POST['html_data']
- recipe_json, recipe_tree = get_from_html(html_data)
- return JsonResponse({
- 'recipe_tree': recipe_tree,
- 'recipe_json': recipe_json
- })
+ recipe_json, recipe_tree = get_from_raw(html_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
+ },
+ status=400
+ )
+ else:
+ return JsonResponse({
+ 'recipe_tree': recipe_tree,
+ 'recipe_json': recipe_json
+ })
@group_required('admin')