Skip to content

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

Delete formsets for Shipping methods

michal8e8544953566
diff --git a/saleor/dashboard/shipping/forms.py b/saleor/dashboard/shipping/forms.py
index 61060e59a8..3853d33199 100644
--- a/saleor/dashboard/shipping/forms.py
+++ b/saleor/dashboard/shipping/forms.py
@@ -1,7 +1,6 @@
from __future__ import unicode_literals
from django import forms
-from django.forms import inlineformset_factory
from ...shipping.models import ShippingMethod, ShippingMethodCountry
@@ -13,6 +12,8 @@ class ShippingMethodForm(forms.ModelForm):
exclude = []
-ShippingMethodCountryFormSet = inlineformset_factory(
- ShippingMethod, ShippingMethodCountry, fields=['country_code', 'price'],
- can_delete=True, extra=2, min_num=1, validate_min=True)
+class ShippingMethodCountryForm(forms.ModelForm):
+
+ class Meta:
+ model = ShippingMethodCountry
+ exclude = ['shipping_method']
diff --git a/saleor/dashboard/shipping/urls.py b/saleor/dashboard/shipping/urls.py
index f931a22c04..e654c354ba 100644
--- a/saleor/dashboard/shipping/urls.py
+++ b/saleor/dashboard/shipping/urls.py
@@ -4,11 +4,21 @@ from . import views
urlpatterns = [
url(r'^$', views.shipping_method_list, name='shipping-methods'),
- url(r'^add/$', views.shipping_method_add, name='shipping-method-add'),
- url(r'^(?P<pk>\d+)/update/$', views.shipping_method_update,
+ url(r'^add/$', views.shipping_method_edit, name='shipping-method-add'),
+ url(r'^(?P<pk>\d+)/update/$', views.shipping_method_edit,
name='shipping-method-update'),
url(r'^(?P<pk>\d+)/$', views.shipping_method_detail,
name='shipping-method-detail'),
url(r'^(?P<pk>\d+)/delete/$',
views.shipping_method_delete, name='shipping-method-delete'),
+
+ url(r'^(?P<shipping_method_pk>\d+)/country/add/$',
+ views.shipping_method_country_edit,
+ name='shipping-method-country-add'),
+ url(r'^(?P<shipping_method_pk>\d+)/country/(?P<country_pk>\d+)/update/$',
+ views.shipping_method_country_edit,
+ name='shipping-method-country-edit'),
+ url(r'^(?P<shipping_method_pk>\d+)/country/(?P<country_pk>\d+)/delete/$',
+ views.shipping_method_country_delete,
+ name='shipping-method-country-delete'),
]
diff --git a/saleor/dashboard/shipping/views.py b/saleor/dashboard/shipping/views.py
index 4aa45c2197..0fca28e260 100644
--- a/saleor/dashboard/shipping/views.py
+++ b/saleor/dashboard/shipping/views.py
@@ -7,10 +7,11 @@ from django.template.response import TemplateResponse
from django.utils.translation import pgettext_lazy
from ...core.utils import get_paginator_items
-from ...shipping.models import ShippingMethod
+
from ..views import staff_member_required
+from ...shipping.models import ShippingMethod, ShippingMethodCountry
from ...settings import DASHBOARD_PAGINATE_BY
-from .forms import ShippingMethodForm, ShippingMethodCountryFormSet
+from .forms import ShippingMethodForm, ShippingMethodCountryForm
@staff_member_required
@@ -26,35 +27,23 @@ def shipping_method_list(request):
@staff_member_required
@permission_required('shipping.edit_shipping')
-def shipping_method_edit(request, method):
+def shipping_method_edit(request, pk=None):
+ if pk:
+ method = get_object_or_404(ShippingMethod, pk=pk)
+ else:
+ method = ShippingMethod()
form = ShippingMethodForm(request.POST or None, instance=method)
- formset = ShippingMethodCountryFormSet(request.POST or None, instance=method)
- if form.is_valid() and formset.is_valid():
- shipping_method = form.save()
- formset.save()
+ if form.is_valid():
+ method = form.save()
msg = pgettext_lazy(
- 'Dashboard message', '%(shipping_method_name)s method saved') % {
- 'shipping_method_name': shipping_method}
+ 'dashboard message', 'Updated shipping method') \
+ if pk else pgettext_lazy(
+ 'Dashboard message', 'Added shipping method')
messages.success(request, msg)
- return redirect(
- 'dashboard:shipping-method-detail', pk=shipping_method.pk)
- ctx = {'shipping_method_form': form,
- 'price_per_country_formset': formset, 'shipping_method': method}
- return TemplateResponse(request, 'dashboard/shipping/form.html', ctx)
-
-
-@staff_member_required
-@permission_required('shipping.edit_shipping')
-def shipping_method_add(request):
- method = ShippingMethod()
- return shipping_method_edit(request, method)
-
-
-@staff_member_required
-@permission_required('shipping.edit_shipping')
-def shipping_method_update(request, pk):
- method = get_object_or_404(ShippingMethod, pk=pk)
- return shipping_method_edit(request, method)
+ return redirect('dashboard:shipping-method-detail', pk=method.pk)
+ ctx = {'form': form, 'shipping_method': method}
+ return TemplateResponse(
+ request, 'dashboard/shipping/form.html', ctx)
@staff_member_required
@@ -78,8 +67,55 @@ def shipping_method_delete(request, pk):
messages.success(
request,
pgettext_lazy(
- 'Dashboard message', '%(shipping_method_name)s successfully deleted') % {
+ 'Dashboard message',
+ '%(shipping_method_name)s successfully deleted') % {
'shipping_method_name': shipping_method})
return redirect('dashboard:shipping-methods')
ctx = {'shipping_method': shipping_method}
- return TemplateResponse(request, 'dashboard/shipping/modal/confirm_delete.html', ctx)
+ return TemplateResponse(
+ request, 'dashboard/shipping/modal/confirm_delete.html', ctx)
+
+
+@staff_member_required
+@permission_required('shipping.edit_shipping')
+def shipping_method_country_edit(request, shipping_method_pk, country_pk=None):
+ if country_pk:
+ method = get_object_or_404(ShippingMethodCountry, pk=country_pk)
+ else:
+ method = ShippingMethodCountry(shipping_method_id=shipping_method_pk)
+ form = ShippingMethodCountryForm(request.POST or None, instance=method)
+ if form.is_valid():
+ method = form.save()
+ msg = pgettext_lazy(
+ 'dashboard message', 'Updated shipping method') \
+ if country_pk else pgettext_lazy(
+ 'Dashboard message', 'Added shipping method')
+ messages.success(request, msg)
+ return redirect(
+ 'dashboard:shipping-method-detail', pk=shipping_method_pk)
+ ctx = {'form': form, 'shipping_method_pk': shipping_method_pk}
+ return TemplateResponse(
+ request,
+ 'dashboard/shipping/country/edit.html',
+ ctx)
+
+
+@staff_member_required
+@permission_required('shipping.edit_shipping')
+def shipping_method_country_delete(
+ request, shipping_method_pk, country_pk=None):
+ country = get_object_or_404(ShippingMethodCountry, pk=country_pk)
+ if request.method == 'POST':
+ country.delete()
+ messages.success(
+ request,
+ pgettext_lazy(
+ 'Dashboard message',
+ 'Removed shipping method %s') %
+ (country,))
+ return redirect(
+ 'dashboard:shipping-method-detail', pk=shipping_method_pk)
+ return TemplateResponse(
+ request,
+ 'dashboard/shipping/modal/country_confirm_delete.html',
+ {'country': country, 'shipping_method_pk': shipping_method_pk})
diff --git a/templates/dashboard/product/product_attribute/detail.html b/templates/dashboard/product/product_attribute/detail.html
index 36b0255798..4ba2a04f4b 100644
--- a/templates/dashboard/product/product_attribute/detail.html
+++ b/templates/dashboard/product/product_attribute/detail.html
@@ -62,10 +62,12 @@
<span class="card-title">{% trans "Attribute values" %}</span>
</div>
{% if perms.product.edit_product %}
- <div class="data-table-header-action">
- <a href="{% url 'dashboard:product-attribute-value-add' attribute_pk=attribute.pk %}"" class="btn-data-table btn btn-flat">{% trans "Add" %}</a>
- </div>
- {% endif %}
+ <div class="data-table-header-action">
+ <a href="{% url 'dashboard:product-attribute-value-add' attribute_pk=attribute.pk %}" class="btn-data-table btn btn-flat">
+ {% trans "Add" %}
+ </a>
+ </div>
+ {% endif %}
<div class="data-table-container">
<table class="bordered highlight responsive data-table">
<thead>
@@ -92,8 +94,12 @@
</td>
{% if perms.product.edit_product %}
<td class="right-align">
- <a href="{% url 'dashboard:product-attribute-value-update' attribute_pk=attribute.pk value_pk=value.pk %}" class="btn btn-flat">{% trans 'Edit' context 'Attribute choice value edit action' %}</a>
- <a class="btn btn-flat modal-trigger-custom" href="#base-modal" data-href="{% url 'dashboard:product-attribute-value-delete' attribute_pk=attribute.pk value_pk=value.pk %}">{% trans 'Remove' context 'Attribute choice value edit action' %}</a>
+ <a href="{% url 'dashboard:product-attribute-value-update' attribute_pk=attribute.pk value_pk=value.pk %}" class="btn btn-flat">
+ {% trans 'Edit' context 'Attribute choice value edit action' %}
+ </a>
+ <a class="btn btn-flat modal-trigger-custom" href="#base-modal" data-href="{% url 'dashboard:product-attribute-value-delete' attribute_pk=attribute.pk value_pk=value.pk %}">
+ {% trans 'Remove' context 'Attribute choice value edit action' %}
+ </a>
</td>
{% endif %}
</tr>
diff --git a/templates/dashboard/shipping/country/edit.html b/templates/dashboard/shipping/country/edit.html
new file mode 100644
index 0000000000..2ba84d74e7
--- /dev/null
+++ b/templates/dashboard/shipping/country/edit.html
@@ -0,0 +1,102 @@
+{% extends "dashboard/base.html" %}
+{% load i18n %}
+{% load materializecss %}
+{% load staticfiles %}
+
+{% block title %}
+ {% if attribute.pk %}
+ {% trans "Edit" context "Dashboard attributes list" %} - {{ attribute }}
+ {% else %}
+ {% trans "Add new attribute" context "Attribute detail page title" %}
+ {% endif %}
+ - {{ block.super }}
+{% endblock %}
+
+{% block body_class %}body-attributes{% endblock %}
+
+{% block menu_attributes_class %}active{% endblock %}
+
+{% block header_menu %}
+ {% url "dashboard:product-attributes" as url %}
+ {% include "dashboard/includes/_back-link.html" with url=url %}
+{% endblock %}
+
+
+{% block breadcrumbs %}
+ <ul class="breadcrumbs breadcrumbs--history">
+ <li>
+ <a href="{% url 'dashboard:product-attributes' %}" class="breadcrumbs--ellipsed-item">
+ {% trans "Attributes" context "Dashboard attributes list" %}
+ </a>
+ </li>
+ <li class="back-mobile">
+ <a href="{% url 'dashboard:product-attributes' %}">
+ <svg data-src="{% static "dashboard/images/arrow_left.svg" %}" fill="#fff" width="20px" height="20px" />
+ </a>
+ </li>
+ {% if attribute.pk %}
+ <li>
+ <a href="{% url 'dashboard:product-attribute-detail' attribute.pk %}" class="breadcrumbs--ellipsed-item">
+ {{ attribute }}
+ </a>
+ </li>
+ <li>
+ <span class="breadcrumbs--ellipsed-item">
+ {% trans "Edit" context "Attribute detail breadcrumbs" %}
+ </span>
+ </li>
+ {% else %}
+ <li>
+ <span class="breadcrumbs--ellipsed-item">
+ {% trans "Add new attribute" context "Attribute detail breadcrumbs" %}
+ </span>
+ </li>
+ {% endif %}
+ </ul>
+{% endblock %}
+
+{% block menu_catalogue_class %} active{% endblock %}
+
+{% block content %}
+ <div class="row">
+ <div class="col m12 l9">
+ <div class="card">
+ <form method="post" enctype="multipart/form-data" id="form-attributes" novalidate>
+ {% csrf_token %}
+ <div class="card-content">
+ <span class="card-title">
+ {% trans "Attribute values" context "Attribute values form title" %}
+ </span>
+ {% for field in form %}
+ <div class="row">
+ <div class="col s12 l8" >
+ <div class="row">
+ {{ field|materializecss }}
+ </div>
+ </div>
+ </div>
+ {% endfor %}
+ </div>
+ <div class="card-action right-align">
+ {% if value.pk %}
+ <a href="{% url 'dashboard:shipping-method-detail' pk=shipping_method_pk %}" class="btn btn-flat">
+ {% trans "Cancel" context "Dashboard cancel action" %}
+ </a>
+ <button type="submit" class="btn waves-effect waves-light">
+ {% trans "Save" context "Dashboard update action" %}
+ </button>
+ {% else %}
+ <a href="{% url 'dashboard:shipping-method-detail' pk=shipping_method_pk %}" class="btn btn-flat">
+ {% trans "Cancel" context "Dashboard cancel action" %}
+ </a>
+ <button type="submit" class="btn waves-effect waves-light">
+ {% trans "Create" context "Dashboard create action" %}
+ </button>
+ {% endif %}
+ </div>
+ </form>
+ </div>
+ </div>
+ <div class="col m12 l3 help-box"></div>
+ </div>
+{% endblock %}
diff --git a/templates/dashboard/shipping/detail.html b/templates/dashboard/shipping/detail.html
index c2f77d9394..cdbf144cc3 100644
--- a/templates/dashboard/shipping/detail.html
+++ b/templates/dashboard/shipping/detail.html
@@ -73,6 +73,11 @@
{% trans "Price per country" context "Title of the per-country shipping price list" %}
</span>
</div>
+ <div class="data-table-header-action">
+ <a href="{% url 'dashboard:shipping-method-country-add' shipping_method_pk=shipping_method.pk %}" class="btn-data-table btn btn-flat">
+ {% trans "Add" %}
+ </a>
+ </div>
<div class="data-table-container">
<table class="bordered highlight responsive data-table">
<thead>
@@ -80,14 +85,24 @@
<th>
{% trans "Country" context "Header of the per-country shipping price list" %}
</th>
- <th>{% trans "Price" context "Header of the per-country shipping price list" %}</th>
+ <th>
+ {% trans "Price" context "Header of the per-country shipping price list" %}
+ </th>
</tr>
</thead>
<tbody>
- {% for price in method_countries %}
+ {% for country in method_countries %}
<tr>
- <td>{{ price.get_country_code_display }}</td>
- <td>{% gross price.price %}</td>
+ <td>{{ country.get_country_code_display }}</td>
+ <td>{% gross country.price %}</td>
+ <td class="right-align">
+ <a href="{% url 'dashboard:shipping-method-country-edit' shipping_method_pk=shipping_method.pk country_pk=country.pk %}" class="btn btn-flat">
+ {% trans 'Edit' context 'Attribute choice value edit action' %}
+ </a>
+ <a class="btn btn-flat modal-trigger-custom" href="#base-modal" data-href="{% url 'dashboard:shipping-method-country-delete' shipping_method_pk=shipping_method.pk country_pk=country.pk %}">
+ {% trans 'Remove' context 'Attribute choice value edit action' %}
+ </a>
+ </td>
</tr>
{% empty %}
<tr>
diff --git a/templates/dashboard/shipping/form.html b/templates/dashboard/shipping/form.html
index 4e66ab1883..1eba56081b 100644
--- a/templates/dashboard/shipping/form.html
+++ b/templates/dashboard/shipping/form.html
@@ -68,53 +68,10 @@
<form method="post" id="form-method">
<div class="card-content">
{% csrf_token %}
- {{ price_per_country_formset.management_form }}
<div class="row">
- {{ shipping_method_form|materializecss }}
+ {{ form|materializecss }}
</div>
- <span class="card-title">{% trans "Price per country" context "Shipping method form text" %}</span>
</div>
- <div class="formset-table-container">
- <table class="formset-table">
- <thead>
- <tr>
- <th>
- {% trans "Country" context "Shipping method country list" %}
- </th>
- <th>
- {% trans "Price" context "Shipping method country list" %}
- </th>
- <th>
- {% trans "Delete" context "Dashboard delete action" %}
- </th>
- </tr>
- </thead>
- <tbody>
- {% for form in price_per_country_formset %}
- <tr>
- <td>
- {% for hidden in form.hidden_fields %}
- {{ hidden }}
- {% endfor %}
- {{ form.country_code|materializecss:"input-field s12" }}
- {% if form.non_field_errors %}
- {% for error in form.non_field_errors %}
- <p class="help-block materialize-red-text">{{ error }}</p>
- {% endfor %}
- {% endif %}
- </td>
- <td>
- {{ form.price|materializecss }}
- </td>
- <td>
- {{ form.DELETE|materializecss }}
- </td>
- </tr>
- {% endfor %}
- </tbody>
- </table>
- </div>
-
<div class="card-action right-align">
{% if shipping_method.pk %}
<a href="{% url 'dashboard:shipping-method-detail' shipping_method.pk %}" class="btn btn-flat">
diff --git a/templates/dashboard/shipping/modal/country_confirm_delete.html b/templates/dashboard/shipping/modal/country_confirm_delete.html
new file mode 100644
index 0000000000..e37b9f2fdc
--- /dev/null
+++ b/templates/dashboard/shipping/modal/country_confirm_delete.html
@@ -0,0 +1,19 @@
+{% extends "dashboard/base_modal_confirm_delete.html" %}
+
+{% load i18n %}
+
+{% block action_url %}
+{% url "dashboard:shipping-method-country-delete" shipping_method_pk=shipping_method_pk country_pk=country.pk %}
+{% endblock %}
+
+{% block title %}
+{% blocktrans trimmed context "Modal delete attribute text" %}
+Delete shipping {{ country }}
+{% endblocktrans %}
+{% endblock title %}
+
+{% block content %}
+{% blocktrans trimmed context "Modal delete attribute text" %}
+Are you sure you want to delete shipping <strong>{{ country }}</strong>?
+{% endblocktrans %}
+{% endblock content %}