From 215eadb4a02db0047b0e60620a001d71212f2567 Mon Sep 17 00:00:00 2001 From: smilerz Date: Mon, 22 Mar 2021 15:13:01 -0500 Subject: [PATCH] consolidated integration imports into url_import --- cookbook/templates/url_import.html | 54 +++++++++++++++++++++++------- cookbook/views/import_export.py | 12 +++++-- 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/cookbook/templates/url_import.html b/cookbook/templates/url_import.html index 6c4d28a0..c915338a 100644 --- a/cookbook/templates/url_import.html +++ b/cookbook/templates/url_import.html @@ -44,7 +44,7 @@ Manual -
+
@@ -89,13 +100,12 @@ Manual
-
+
-
-
@@ -542,7 +552,8 @@ recipe_json: undefined, recipe_tree: undefined, automatic: true, - recipe_app: 'tandoor' + recipe_app: 'DEFAULT', + recipe_files: [] }, directives: { tabindex: { @@ -660,6 +671,25 @@ this.makeToast(gettext('Error'), gettext('An error occurred while trying to import this recipe!') + err.bodyText, 'danger') }) }, + importAppRecipe: function() { + this.error = undefined + this.loading = true + let formData = new FormData(); + let files = [] + formData.append('type', this.recipe_app); + for( var i = 0; i < this.recipe_files.length; i++ ){ + formData.append('files', this.recipe_files[i]); + } + this.$http.post("{% url 'view_import' %}", formData, {headers: {'Content-Type': 'multipart/form-data'}}).then((response) => { + console.log(response.data) + window.location.href = "{% url 'view_import_response' 1237654 %}".replace('1237654', response.data['import_id']) + }).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') + }) + }, deleteIngredient: function (i) { this.recipe_data.recipeIngredient = this.recipe_data.recipeIngredient.filter(item => item !== i) }, diff --git a/cookbook/views/import_export.py b/cookbook/views/import_export.py index 8e8e03e9..365b30de 100644 --- a/cookbook/views/import_export.py +++ b/cookbook/views/import_export.py @@ -3,7 +3,7 @@ import threading from io import BytesIO from django.contrib import messages -from django.http import HttpResponseRedirect +from django.http import HttpResponseRedirect, JsonResponse from django.shortcuts import render from django.urls import reverse from django.utils.translation import gettext as _ @@ -68,9 +68,15 @@ def import_recipe(request): t.setDaemon(True) t.start() - return HttpResponseRedirect(reverse('view_import_response', args=[il.pk])) + return JsonResponse({'import_id': [il.pk]}) except NotImplementedError: - messages.add_message(request, messages.ERROR, _('Importing is not implemented for this provider')) + return JsonResponse( + { + 'error': True, + 'msg': _('Importing is not implemented for this provider') + }, + status=400 + ) else: form = ImportForm()