From ac2ab620502b9ad9c025edc1b6d6ac0632bde5d3 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Tue, 12 Jul 2022 21:14:51 +0200 Subject: [PATCH] moved import functions to proper api function --- cookbook/templates/import.html | 26 ------------------ cookbook/templates/javascript_urls.html | 18 ------------- cookbook/urls.py | 2 +- cookbook/views/api.py | 36 +++++++++++++++++++++++-- cookbook/views/import_export.py | 36 ------------------------- 5 files changed, 35 insertions(+), 83 deletions(-) delete mode 100644 cookbook/templates/import.html delete mode 100644 cookbook/templates/javascript_urls.html diff --git a/cookbook/templates/import.html b/cookbook/templates/import.html deleted file mode 100644 index d36a8adc..00000000 --- a/cookbook/templates/import.html +++ /dev/null @@ -1,26 +0,0 @@ -{% extends "base.html" %} -{% load crispy_forms_filters %} -{% load i18n %} -{% load static %} - -{% block title %}{% trans 'Import Recipes' %}{% endblock %} - -{% block extra_head %} - {{ form.media }} -{% endblock %} - - -{% block content %} -

{% trans 'Import' %}

-
-
-
- {% csrf_token %} - {{ form|crispy }} - -
-
-
- -{% endblock %} \ No newline at end of file diff --git a/cookbook/templates/javascript_urls.html b/cookbook/templates/javascript_urls.html deleted file mode 100644 index 5eb1ca0a..00000000 --- a/cookbook/templates/javascript_urls.html +++ /dev/null @@ -1,18 +0,0 @@ - - - \ No newline at end of file diff --git a/cookbook/urls.py b/cookbook/urls.py index f852846b..b4de540f 100644 --- a/cookbook/urls.py +++ b/cookbook/urls.py @@ -73,7 +73,7 @@ urlpatterns = [ path('ingredient-editor/', views.ingredient_editor, name='view_ingredient_editor'), path('abuse/', views.report_share_abuse, name='view_report_share_abuse'), - path('import/', import_export.import_recipe, name='view_import'), + path('api/import/', api.import_files, name='view_import'), path('import-response//', import_export.import_response, name='view_import_response'), path('export/', import_export.export_recipe, name='view_export'), path('export-response//', import_export.export_response, name='view_export_response'), diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 111048cf..c8b0cce6 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -2,6 +2,7 @@ import io import json import mimetypes import re +import threading import traceback import uuid from collections import OrderedDict @@ -44,6 +45,7 @@ from rest_framework.throttling import AnonRateThrottle from rest_framework.viewsets import ViewSetMixin from treebeard.exceptions import InvalidMoveToDescendant, InvalidPosition, PathOverflow +from cookbook.forms import ImportForm from cookbook.helper import recipe_url_import as helper from cookbook.helper.HelperFunctions import str2bool from cookbook.helper.image_processing import handle_image @@ -51,7 +53,7 @@ from cookbook.helper.ingredient_parser import IngredientParser from cookbook.helper.permission_helper import (CustomIsAdmin, CustomIsGuest, CustomIsOwner, CustomIsOwnerReadOnly, CustomIsShare, CustomIsShared, CustomIsSpaceOwner, CustomIsUser, group_required, - is_space_owner, switch_user_active_space) + is_space_owner, switch_user_active_space, above_space_limit) from cookbook.helper.recipe_search import RecipeFacet, RecipeSearch from cookbook.helper.recipe_url_import import get_from_youtube_scraper, get_images_from_soup from cookbook.helper.scrapers.scrapers import text_scraper @@ -85,6 +87,7 @@ from cookbook.serializer import (AutomationSerializer, BookmarkletImportListSeri SyncLogSerializer, SyncSerializer, UnitSerializer, UserFileSerializer, UserNameSerializer, UserPreferenceSerializer, UserSpaceSerializer, ViewLogSerializer) +from cookbook.views.import_export import get_integration from recipes import settings @@ -720,7 +723,7 @@ class RecipeViewSet(viewsets.ModelViewSet): 'Query string matched (fuzzy) against recipe name. In the future also fulltext search.')), QueryParam(name='keywords', description=_( 'ID of keyword a recipe should have. For multiple repeat parameter. Equivalent to keywords_or'), - qtype='int'), + qtype='int'), QueryParam(name='keywords_or', description=_('Keyword IDs, repeat for multiple. Return recipes with any of the keywords'), qtype='int'), @@ -1258,6 +1261,35 @@ def download_file(request, file_id): return Response({}, status=status.HTTP_400_BAD_REQUEST) +@api_view(['POST']) +# @schema(AutoSchema()) #TODO add proper schema +@permission_classes([CustomIsUser]) +def import_files(request): + """ + function to handle files passed by application importer + """ + limit, msg = above_space_limit(request.space) + if limit: + return Response({'error': msg}, status=status.HTTP_400_BAD_REQUEST) + + form = ImportForm(request.POST, request.FILES) + if form.is_valid() and request.FILES != {}: + try: + integration = get_integration(request, form.cleaned_data['type']) + + il = ImportLog.objects.create(type=form.cleaned_data['type'], created_by=request.user, space=request.space) + files = [] + for f in request.FILES.getlist('files'): + files.append({'file': io.BytesIO(f.read()), 'name': f.name}) + t = threading.Thread(target=integration.do_import, args=[files, il, form.cleaned_data['duplicates']]) + t.setDaemon(True) + t.start() + + return Response({'import_id': il.pk}, status=status.HTTP_200_OK) + except NotImplementedError: + return Response({'error': True, 'msg': _('Importing is not implemented for this provider')}, status=status.HTTP_400_BAD_REQUEST) + + def get_recipe_provider(recipe): if recipe.storage.method == Storage.DROPBOX: return Dropbox diff --git a/cookbook/views/import_export.py b/cookbook/views/import_export.py index d81777fc..d4885949 100644 --- a/cookbook/views/import_export.py +++ b/cookbook/views/import_export.py @@ -82,42 +82,6 @@ def get_integration(request, export_type): return Cookmate(request, export_type) -@group_required('user') -def import_recipe(request): - limit, msg = above_space_limit(request.space) - if limit: - messages.add_message(request, messages.WARNING, msg) - return HttpResponseRedirect(reverse('index')) - - if request.method == "POST": - form = ImportForm(request.POST, request.FILES) - if form.is_valid() and request.FILES != {}: - try: - integration = get_integration(request, form.cleaned_data['type']) - - il = ImportLog.objects.create(type=form.cleaned_data['type'], created_by=request.user, space=request.space) - files = [] - for f in request.FILES.getlist('files'): - files.append({'file': BytesIO(f.read()), 'name': f.name}) - t = threading.Thread(target=integration.do_import, args=[files, il, form.cleaned_data['duplicates']]) - t.setDaemon(True) - t.start() - - return JsonResponse({'import_id': il.pk}) - except NotImplementedError: - return JsonResponse( - { - 'error': True, - 'msg': _('Importing is not implemented for this provider') - }, - status=400 - ) - else: - form = ImportForm() - - return render(request, 'import.html', {'form': form}) - - @group_required('user') def export_recipe(request): if request.method == "POST":