Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use our own view to handle file upload in GraphQL
Marcin Gębala52122d2ee53f
diff --git a/requirements.in b/requirements.inindex 2103dd3b91..3993a1968b 100644--- a/requirements.in+++ b/requirements.in@@ -34,7 +34,6 @@ freezegun>=0.3.9google-measurement-protocol>=1.0.0google-i18n-address>=2.3.0graphene-django>=2.0.dev-graphene-file-upload==0.1.1jsonfield>=2.0.2Markdown>=2.4maxminddbdiff --git a/requirements.txt b/requirements.txtindex 8fbb559427..fe38d08924 100644--- a/requirements.txt+++ b/requirements.txt@@ -59,7 +59,6 @@ freezegun==0.3.10google-i18n-address==2.3.0google-measurement-protocol==1.0.0graphene-django==2.0.0-graphene-file-upload==0.1.1graphene==2.0.1 # via django-graphql-jwt, graphene-djangographql-core==2.0 # via graphene, graphql-relaygraphql-relay==0.4.5 # via graphenediff --git a/saleor/graphql/core/mutations.py b/saleor/graphql/core/mutations.pyindex e50b4ced06..e3cbc6f54f 100644--- a/saleor/graphql/core/mutations.py+++ b/saleor/graphql/core/mutations.py@@ -4,12 +4,12 @@ import graphenefrom django.core.exceptions import ImproperlyConfigured, ValidationErrorfrom graphene.types.mutation import MutationOptionsfrom graphene_django.registry import get_global_registry-from graphene_file_upload import Uploadfrom graphql_jwt import ObtainJSONWebToken, Verifyfrom graphql_jwt.exceptions import GraphQLJWTError, PermissionDeniedfrom ...account import modelsfrom ..account.types import User+from ..file_upload.types import Uploadfrom ..utils import get_node, get_nodesfrom .types import Errordiff --git a/saleor/graphql/file_upload/types.py b/saleor/graphql/file_upload/types.pynew file mode 100644index 0000000000..59e48d173f--- /dev/null+++ b/saleor/graphql/file_upload/types.py@@ -0,0 +1,15 @@+import graphene+++class Upload(graphene.types.Scalar):+ @staticmethod+ def serialize(value):+ return value++ @staticmethod+ def parse_literal(node):+ return node++ @staticmethod+ def parse_value(value):+ return valuediff --git a/saleor/graphql/file_upload/views.py b/saleor/graphql/file_upload/views.pynew file mode 100644index 0000000000..065442a94f--- /dev/null+++ b/saleor/graphql/file_upload/views.py@@ -0,0 +1,86 @@+from graphene_django.views import GraphQLView+import json+import graphene++# This class is modified verion of the `ModifiedGraphQLView` class from+# `graphene-file-upload` (https://github.com/lmcgartland/graphene-file-upload).+++class FileUploadGraphQLView(GraphQLView):++ @staticmethod+ def get_graphql_params(request, data):+ content_type = GraphQLView.get_content_type(request)++ # Only check multipart/form-data if content_type is not None (this is+ # not checked in the original version from `graphene-file-upload`).+ if content_type and 'multipart/form-data' in content_type:+ query, variables, operation_name, id = super(+ FileUploadGraphQLView, FileUploadGraphQLView).get_graphql_params(+ request, data)+ operations = data.get('operations')+ files_map = data.get('map')+ try:+ operations = json.loads(operations)+ files_map = json.loads(files_map)+ variables = operations.get('variables')+ for file_key in files_map:+ # file key is which file it is in the form-data+ file_instances = files_map[file_key]+ for file_instance in file_instances:+ test = obj_set(operations, file_instance, file_key, False)+ query = operations.get('query')+ variables = operations.get('variables')+ except Exception as e:+ raise e+ else:+ query, variables, operation_name, id = super(+ FileUploadGraphQLView, FileUploadGraphQLView).get_graphql_params(+ request, data)+ return query, variables, operation_name, id+++def getKey(key):+ try:+ intKey = int(key)+ return intKey+ except:+ return key+++def getShallowProperty(obj, prop):+ if type(prop) is int:+ return obj[prop]++ try:+ return obj.get(prop)+ except:+ return None+++def obj_set(obj, path, value, doNotReplace):+ if type(path) is int:+ path = [path]+ if path is None or len(path) == 0:+ return obj+ if isinstance(path, str):+ newPath = list(map(getKey, path.split('.')))+ return obj_set(obj, newPath, value, doNotReplace )++ currentPath = path[0]+ currentValue = getShallowProperty(obj, currentPath)++ if len(path) == 1:+ if currentValue is None or not doNotReplace:+ obj[currentPath] = value++ if currentValue is None:+ try:+ if type(path[1]) == int:+ obj[currentPath] = []+ else:+ obj[currentPath] = {}+ except Exception as e:+ pass++ return obj_set(obj[currentPath], path[1:], value, doNotReplace)diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.pyindex 54df580974..364fbc2264 100644--- a/saleor/graphql/product/mutations/products.py+++ b/saleor/graphql/product/mutations/products.py@@ -1,13 +1,13 @@import graphenefrom django.template.defaultfilters import slugifyfrom graphene.types import InputObjectType-from graphene_file_upload import Uploadfrom graphql_jwt.decorators import permission_requiredfrom ....product import modelsfrom ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutationfrom ...core.types import Decimal, Error, SeoInputfrom ...core.utils import clean_seo_fields+from ...file_upload.types import Uploadfrom ...utils import get_attributes_dict_from_list, get_node, get_nodesfrom ..types import Collection, Product, ProductImagediff --git a/saleor/urls.py b/saleor/urls.pyindex ec9440eabb..13c56248fe 100644--- a/saleor/urls.py+++ b/saleor/urls.py@@ -6,7 +6,6 @@ from django.contrib.sitemaps.views import sitemapfrom django.contrib.staticfiles.views import servefrom django.views.decorators.csrf import csrf_exemptfrom django.views.i18n import JavaScriptCatalog, set_language-from graphene_file_upload import ModifiedGraphQLViewfrom .account.urls import urlpatterns as account_urlsfrom .checkout.urls import (@@ -16,6 +15,7 @@ from .core.urls import urlpatterns as core_urlsfrom .dashboard.urls import urlpatterns as dashboard_urlsfrom .data_feeds.urls import urlpatterns as feed_urlsfrom .graphql.api import schema+from .graphql.file_upload.views import FileUploadGraphQLViewfrom .order.urls import urlpatterns as order_urlsfrom .page.urls import urlpatterns as page_urlsfrom .product.urls import urlpatterns as product_urls@@ -26,7 +26,7 @@ handler404 = 'saleor.core.views.handle_404'non_translatable_urlpatterns = [url(r'^dashboard/',include((dashboard_urls, 'dashboard'), namespace='dashboard')),- url(r'^graphql/', csrf_exempt(ModifiedGraphQLView.as_view(+ url(r'^graphql/', csrf_exempt(FileUploadGraphQLView.as_view(schema=schema, graphiql=settings.DEBUG)), name='api'),url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps},name='django.contrib.sitemaps.views.sitemap'),