Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Drop django rest framework usage

Anna Siwik55c589a3cac7
diff --git a/requirements.in b/requirements.in
index 272a76f747..2e9851c3db 100644
--- a/requirements.in
+++ b/requirements.in
@@ -12,7 +12,6 @@ django-payments>=0.11.0
django-prices>=0.4.11
django-prices-openexchangerates>=0.1.12
django-redis>=4.0.0
-djangorestframework>=3.1
django-storages>=1.4.1
django-templated-email>=2.2.0
django-versatileimagefield>=1.0.1
diff --git a/requirements.txt b/requirements.txt
index f8d2a551f0..085589de45 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -64,7 +64,7 @@ pyjwt==1.5.3 # via django-payments, social-auth-core
pytest-django==3.1.2
pytest==3.2.2
python-dateutil==2.6.1 # via botocore, faker
-python3-openid==3.1.0 # via social-auth-core
+python-openid==2.2.5 # via social-auth-core
pytz==2017.2 # via babel, django
redis==2.10.6 # via django-redis
requests-oauthlib==0.8.0 # via social-auth-core
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index 95700a490b..ee75ade7b2 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -11,8 +11,8 @@ from django.utils.translation import pgettext_lazy
from ...product.models import (
AttributeChoiceValue, Product, ProductAttribute, ProductClass,
ProductImage, ProductVariant, Stock, StockLocation, VariantImage)
-from .widgets import ImagePreviewWidget
from ...search import index as search_index
+from .widgets import ImagePreviewWidget
class ProductClassSelectorForm(forms.Form):
@@ -288,3 +288,18 @@ class AttributeChoiceValueForm(forms.ModelForm):
AttributeChoiceValueFormset = inlineformset_factory(
ProductAttribute, AttributeChoiceValue, form=AttributeChoiceValueForm,
extra=1)
+
+
+class UploadImageForm(forms.ModelForm):
+ class Meta:
+ model = ProductImage
+ fields = ('image', )
+
+ def __init__(self, *args, **kwargs):
+ self.product = kwargs.pop('product')
+ super(UploadImageForm, self).__init__(*args, **kwargs)
+
+ def save(self, commit=True):
+ image = super(UploadImageForm, self).save(commit=commit)
+ image.product = self.product
+ return image.save()
diff --git a/saleor/dashboard/product/urls.py b/saleor/dashboard/product/urls.py
index 9fcd40ed5b..e319b04316 100644
--- a/saleor/dashboard/product/urls.py
+++ b/saleor/dashboard/product/urls.py
@@ -59,9 +59,9 @@ urlpatterns = [
url(r'^(?P<product_pk>[0-9]+)/images/(?P<img_pk>[0-9]+)/delete/$',
views.product_image_delete, name='product-image-delete'),
url(r'^(?P<product_pk>[0-9]+)/images/reorder/$',
- api.reorder_product_images, name='product-images-reorder'),
+ views.ajax_reorder_product_images, name='product-images-reorder'),
url(r'^(?P<product_pk>[0-9]+)/images/upload/$',
- api.upload_image, name='product-images-upload'),
+ views.ajax_upload_image, name='product-images-upload'),
url(r'attributes/$',
views.attribute_list, name='product-attributes'),
diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.py
index 03973e6b6b..e310317766 100644
--- a/saleor/dashboard/product/views.py
+++ b/saleor/dashboard/product/views.py
@@ -1,5 +1,6 @@
from __future__ import unicode_literals
+import json
from django.contrib import messages
from django.contrib.auth.decorators import permission_required
from django.http import JsonResponse
@@ -7,7 +8,7 @@ from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.utils.http import is_safe_url
from django.utils.translation import pgettext_lazy
-from django.views.decorators.http import require_http_methods
+from django.views.decorators.http import require_POST
from ...core.utils import get_paginator_items
from ...product.models import (
@@ -555,3 +556,36 @@ def stock_location_delete(request, location_pk):
request,
'dashboard/product/stock_location/modal_confirm_delete.html',
ctx)
+
+
+@require_POST
+@staff_member_required
+def ajax_reorder_product_images(request, product_pk):
+ product = get_object_or_404(Product, pk=product_pk)
+ data = json.loads(request.body)
+ order = data["order"]
+ for order, pk in enumerate(order):
+ try:
+ img = product.images.get(pk=pk)
+ except ProductImage.DoesNotExist:
+ pass
+ else:
+ img.order = order
+ img.save()
+ return JsonResponse({}, status=200)
+
+
+@require_POST
+@staff_member_required
+def ajax_upload_image(request, product_pk):
+ product = get_object_or_404(Product, pk=product_pk)
+ form = forms.UploadImageForm(
+ request.POST or None, request.FILES or None, product=product)
+ status = 200
+ if form.is_valid():
+ image = form.save()
+ ctx = {'id': image.pk, 'image': image, 'order': image.order}
+ elif form.errors:
+ status = 400
+ ctx = {'errors': form.errors}
+ return JsonResponse(ctx, status=200)
diff --git a/saleor/static/dashboard/js/dashboard.js b/saleor/static/dashboard/js/dashboard.js
index 5439894caf..1d63de6af8 100644
--- a/saleor/static/dashboard/js/dashboard.js
+++ b/saleor/static/dashboard/js/dashboard.js
@@ -134,7 +134,7 @@ $(document).ready(function () {
});
});
Dropzone.options.productImageForm = {
- paramName: 'image',
+ paramName: 'image_0',
maxFilesize: 20,
previewsContainer: '.product-gallery',
thumbnailWidth: 400,