Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add CRUD operations for ProductImage model
Marcin Gębala53d8bd878e24
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.pyindex ae7013619c..a6fbbd071a 100644--- a/saleor/dashboard/product/forms.py+++ b/saleor/dashboard/product/forms.py@@ -76,3 +76,9 @@ def get_variant_formset(product):return BagVariantFormsetelse:raise ValueError('Unknown product')+++class ProductImageForm(forms.ModelForm):+ class Meta:+ model = ProductImage+ exclude = ('product',)\ No newline at end of filediff --git a/saleor/dashboard/product/urls.py b/saleor/dashboard/product/urls.pyindex 9643e501e9..7a7b8ac135 100644--- a/saleor/dashboard/product/urls.py+++ b/saleor/dashboard/product/urls.py@@ -12,4 +12,13 @@ urlpatterns = patterns(url(r'^add/(?P<cls_name>[-\w]+)/$',views.product_details, name='product-add'),url(r'^(?P<pk>[0-9]+)/delete/$',- views.ProductDeleteView.as_view(), name='product-delete'),)+ views.ProductDeleteView.as_view(), name='product-delete'),++ url(r'^(?P<product_pk>[0-9]+)/images/$',+ views.product_images_list, name='product-images'),+ url(r'^(?P<product_pk>[0-9]+)/images/(?P<img_pk>[0-9]+)/update/$',+ views.product_image_edit, name='product-image-update'),+ url(r'^(?P<product_pk>[0-9]+)/images/add/$',+ views.product_image_edit, name='product-image-add'),+ url(r'^(?P<product_pk>[0-9]+)/images/(?P<img_pk>[0-9]+)/delete/$',+ views.product_image_delete, name='product-image-delete'))diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.pyindex d07f43d53f..258845a503 100644--- a/saleor/dashboard/product/views.py+++ b/saleor/dashboard/product/views.py@@ -8,11 +8,11 @@ from django.template.response import TemplateResponsefrom django.utils.translation import ugettext_lazy as _from django.views.generic import DeleteView-from ...product.models import Product+from ...product.models import Product, ProductImagefrom ..utils import paginatefrom ..views import StaffMemberOnlyMixin, staff_member_requiredfrom .forms import (ProductImageFormSet, ProductClassForm, get_product_form,- get_product_cls_by_name, get_variant_formset)+ get_product_cls_by_name, get_variant_formset, ProductImageForm)@staff_member_required@@ -73,3 +73,50 @@ class ProductDeleteView(StaffMemberOnlyMixin, DeleteView):result = self.delete(request, *args, **kwargs)messages.success(request, _('Deleted product %s') % self.object)return result+++@staff_member_required+def product_images_list(request, product_pk):+ product = get_object_or_404(Product, pk=product_pk)+ images = product.images.all()+ ctx = {'product': product, 'images': images}+ return TemplateResponse(request, 'dashboard/product/product_images_list.html', ctx)+++@staff_member_required+def product_image_edit(request, product_pk, img_pk=None):+ product = get_object_or_404(Product, pk=product_pk)+ if img_pk:+ product_image = get_object_or_404(ProductImage, pk=img_pk)+ title = product_image.image.name+ else:+ product_image = ProductImage(product=product)+ title = _('Add image')+ form = ProductImageForm(request.POST or None, request.FILES or None,+ instance=product_image)+ if form.is_valid():+ product_image = form.save()+ if img_pk:+ msg = _('Updated image %s') % product_image.image.name+ else:+ msg = _('Added image %s') % product_image.image.name+ messages.success(request, msg)+ return redirect('dashboard:product-images', product_pk=product.pk)+ else:+ if form.errors:+ messages.error(request, _('Your submitted data was not valid - '+ 'please correct the errors below'))+ ctx = {'product': product, 'product_image': product_image, 'title': title, 'form': form}+ return TemplateResponse(request, 'dashboard/product/product_image_form.html', ctx)+++@staff_member_required+def product_image_delete(request, product_pk, img_pk):+ product = get_object_or_404(Product, pk=product_pk)+ product_image = get_object_or_404(ProductImage, pk=img_pk)+ if request.method == 'POST':+ product_image.delete()+ messages.success(request, _('Deleted image %s') % product_image.image.name)+ return redirect('dashboard:product-images', product_pk=product.pk)+ ctx = {'product': product, 'product_image': product_image}+ return TemplateResponse(request, 'dashboard/product/product_image_confirm_delete.html', ctx)diff --git a/saleor/dashboard/templates/dashboard/product/product_image_confirm_delete.html b/saleor/dashboard/templates/dashboard/product/product_image_confirm_delete.htmlnew file mode 100644index 0000000000..b997a69a56--- /dev/null+++ b/saleor/dashboard/templates/dashboard/product/product_image_confirm_delete.html@@ -0,0 +1,21 @@+{% extends "dashboard/base.html" %}+{% load i18n %}++{% block title %}{% trans "Delete image" %} - {{ block.super }}{% endblock %}++{% block content %}+ <div class="row">+ <h3>{% trans "Are you sure you want to delete the following image?" %}</h3>+ </div>+ <form action="" method="post">+ {% csrf_token %}+ <div class="row">+ <div>{{ product_image.image.name }}</div>+ <div><img src="{{ product_image.image.crop.50x50}}" alt=""></div>+ </div>+ <div class="row">+ <button class="btn waves-effect red" type="submit"><i class="glyphicon glyphicon-trash"></i> {% trans "Delete" %}</button>+ <a class="btn waves-effect" href="{{ request.META.HTTP_REFERER }}"><i class="glyphicon glyphicon-remove"></i> {% trans "Cancel" %}</a>+ </div>+ </form>+{% endblock %}diff --git a/saleor/dashboard/templates/dashboard/product/product_image_form.html b/saleor/dashboard/templates/dashboard/product/product_image_form.htmlnew file mode 100644index 0000000000..c9b3ba88bc--- /dev/null+++ b/saleor/dashboard/templates/dashboard/product/product_image_form.html@@ -0,0 +1,33 @@+{% extends "dashboard/base.html" %}+{% load i18n %}+{% load static %}++{% block title %}{{ title }} - {% trans "Images" %} - {{ block.super }}{% endblock %}++{% block page_title %}{{ title }}{% endblock %}++{% block content %}+ <form method="post" id="product-form" enctype="multipart/form-data">+ <div class="row">+ <div class="col l8">+ {% csrf_token %}+ {{ form }}+ </div>+ </div>+ <div class="row">+ <div class="col l8">+ {% if product_image.pk %}+ <span class="right">+ <a href="{% url 'dashboard:product-image-delete' product_pk=product.pk img_pk=product_image.pk %}" class="btn waves-effect red"><i class="glyphicon glyphicon-trash"></i> {% trans "Delete" %}</a>+ </span>+ {% endif %}+ {% if product_image.pk %}+ <button type="submit" class="btn waves-effect"><i class="glyphicon glyphicon-refresh"></i> {% trans "Update" %}</button>+ {% else %}+ <button type="submit" class="btn waves-effect"><i class="glyphicon glyphicon-upload"></i> {% trans "Add" %}</button>+ {% endif %}+ <a href="{% url 'dashboard:product-images' product_pk=product.pk %}" class="btn waves-effect"><i class="glyphicon glyphicon-remove"></i> {% trans "Cancel" %}</a>+ </div>+ </div>+ </form>+{% endblock %}\ No newline at end of filediff --git a/saleor/dashboard/templates/dashboard/product/product_images_list.html b/saleor/dashboard/templates/dashboard/product/product_images_list.htmlnew file mode 100644index 0000000000..3133d70664--- /dev/null+++ b/saleor/dashboard/templates/dashboard/product/product_images_list.html@@ -0,0 +1,24 @@+{% extends "dashboard/base.html" %}+{% load i18n %}+{% load bootstrap %}+{% load static %}++{% block title %}{% trans "Images" %} - {{ block.super }}{% endblock %}++{% block page_title %}{% trans "Images" %}{% endblock %}++{% block content %}+ <a class="btn waves-effect" href="{% url 'dashboard:product-image-add' product_pk=product.pk %}">{% trans 'Add image' %}</a>+ <ul class="collection">+ {% for product_image in images %}+ <li class="collection-item">+ <img src="{{ product_image.image.crop.50x50 }}" alt="">+ <span>{{ product_image.image.name }}</span>+ <div class="right">+ <a href="{% url 'dashboard:product-image-update' product_pk=product.pk img_pk=product_image.pk %}">{% trans 'Edit' %}</a>+ <a href="{% url 'dashboard:product-image-delete' product_pk=product.pk img_pk=product_image.pk %}">{% trans 'Delete' %}</a>+ </div>+ </li>+ {% endfor %}+ </ul>+{% endblock %}\ No newline at end of file