diff --git a/cookbook/helper/recipe_html_import.py b/cookbook/helper/recipe_html_import.py index f9882778..9140d076 100644 --- a/cookbook/helper/recipe_html_import.py +++ b/cookbook/helper/recipe_html_import.py @@ -118,7 +118,7 @@ def get_recipe_from_source(text, url, space): temp_tree.append(node) if '@type' in el and el['@type'] == 'Recipe': - recipe_json = helper.find_recipe_json(el, None, space) + recipe_json = helper.find_recipe_json(el, url, space) recipe_tree += [{'name': 'ld+json', 'children': temp_tree}] else: recipe_tree += [{'name': 'json', 'children': temp_tree}] diff --git a/cookbook/helper/recipe_url_import.py b/cookbook/helper/recipe_url_import.py index 1b0828d4..0951ace2 100644 --- a/cookbook/helper/recipe_url_import.py +++ b/cookbook/helper/recipe_url_import.py @@ -86,6 +86,9 @@ def find_recipe_json(ld_json, url, space): else: ld_json['recipeInstructions'] = "" + if url: + ld_json['recipeInstructions'] += "\nImported from " + url + if 'image' in ld_json: ld_json['image'] = parse_image(ld_json['image']) else: diff --git a/cookbook/templates/url_import.html b/cookbook/templates/url_import.html index 6dd3ec33..c4775fa0 100644 --- a/cookbook/templates/url_import.html +++ b/cookbook/templates/url_import.html @@ -30,18 +30,18 @@ title="{% trans 'Drag me to your bookmarks to import recipes from anywhere' %}"> {% trans 'Bookmark Me!' %} - - URL - App - Source - Text - File + + URL + App + Source + Text + File - + Automatic @@ -625,13 +625,13 @@ recipe_data: undefined, error: undefined, loading: false, - preview: {{preview}}, + preview: undefined, preview_type: 'json', all_keywords: false, importing_recipe: false, - recipe_json: {{recipe_json|safe}}, - recipe_tree: {{recipe_tree|safe}}, - recipe_html: {{recipe_html|safe}}, + recipe_json: undefined, + recipe_tree: undefined, + recipe_html: undefined, automatic: true, show_blank: false, blank_field: '', @@ -650,7 +650,12 @@ this.searchKeywords('') this.searchUnits('') this.searchIngredients('') - + let uri = window.location.search.substring(1); + let params = new URLSearchParams(uri); + q = params.get("id") + if (q) { + this.loadBookmarklet(q) + } }, methods: { makeToast: function (title, message, variant = null) { @@ -706,7 +711,7 @@ this.error = undefined this.loading = true this.preview = false - this.$http.post("{% url 'api_recipe_from_source' %}", {'data': this.source_data, 'auto':this.automatic}, {emulateJSON: true}).then((response) => { + this.$http.post("{% url 'api_recipe_from_source' %}", {'data': this.source_data, 'url': this.remote_url, 'auto':this.automatic}, {emulateJSON: true}).then((response) => { console.log(response.data) if (this.automatic) { this.recipe_data = response.data['recipe_json']; @@ -731,6 +736,26 @@ this.makeToast(gettext('Error'), msg, 'danger') }) }, + loadBookmarklet: function(id_bkmk) { + let uri = window.location.search.substring(1); + let params = new URLSearchParams(uri); + q = params.get("id") + console.log(q) + this.error = undefined + this.loading = true + this.$http.get("{% url 'api:bookmarkletimport-list' %}?id=" + id_bkmk ).then((response) => { + console.log(response.data) + this.automatic = false + this.source_data = response.data[0]['html'] + this.remote_url = response.data[0]['url'] + this.loadSource() + }).catch((err) => { + this.error = err.data + this.loading = false + console.log(err) + this.makeToast(gettext('Error'), gettext('There was an error loading bookmarklet!') + err.bodyText, 'danger') + }) + }, loadRecipeManual: function () { this.error = undefined this.preview = false @@ -931,7 +956,7 @@ console.log(response) let new_ingredient={ unit: {id: Math.random() * 1000, text: response.body.unit}, - amount: response.body.amount, + amount: String(response.body.amount), ingredient: {id: Math.random() * 1000, text: response.body.food}, note: response.body.note } diff --git a/cookbook/views/api.py b/cookbook/views/api.py index f56d216a..173a6557 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -621,6 +621,8 @@ def recipe_from_source(request, url=None, url_text=None): auto = request.POST['auto'] else: auto = 'true' + if 'url' in request.POST: + url = request.POST['url'] recipe_json, recipe_tree, recipe_html, images = get_recipe_from_source(json_data, url, request.space) if len(recipe_tree) == 0 and len(recipe_json) == 0: diff --git a/cookbook/views/data.py b/cookbook/views/data.py index 45ddec3b..cbedab0e 100644 --- a/cookbook/views/data.py +++ b/cookbook/views/data.py @@ -183,8 +183,13 @@ def import_url(request): pass return HttpResponse(reverse('view_recipe', args=[recipe.pk])) + + if 'id' in request.GET: + context = {'bookmarklet': 25} + else: + context = {} - return render(request, 'url_import.html', {}) + return render(request, 'url_import.html', context) class Object(object):