Skip to content

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

Add shipping dashboard

Michał Ociepka3db2f3503b09
diff --git a/saleor/dashboard/shipping/__init__.py b/saleor/dashboard/shipping/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/dashboard/shipping/forms.py b/saleor/dashboard/shipping/forms.py
new file mode 100644
index 0000000000..61060e59a8
--- /dev/null
+++ b/saleor/dashboard/shipping/forms.py
@@ -0,0 +1,18 @@
+from __future__ import unicode_literals
+
+from django import forms
+from django.forms import inlineformset_factory
+
+from ...shipping.models import ShippingMethod, ShippingMethodCountry
+
+
+class ShippingMethodForm(forms.ModelForm):
+
+ class Meta:
+ model = ShippingMethod
+ exclude = []
+
+
+ShippingMethodCountryFormSet = inlineformset_factory(
+ ShippingMethod, ShippingMethodCountry, fields=['country_code', 'price'],
+ can_delete=True, extra=2, min_num=1, validate_min=True)
diff --git a/saleor/dashboard/shipping/urls.py b/saleor/dashboard/shipping/urls.py
new file mode 100644
index 0000000000..30283b7388
--- /dev/null
+++ b/saleor/dashboard/shipping/urls.py
@@ -0,0 +1,12 @@
+from django.conf.urls import url
+
+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+)/$', views.shipping_method_detail, name='shipping-method-detail'),
+ url(r'^(?P<pk>\d+)/delete/$',
+ views.shipping_method_delete, name='shipping-method-delete'),
+]
+
diff --git a/saleor/dashboard/shipping/views.py b/saleor/dashboard/shipping/views.py
new file mode 100644
index 0000000000..50684bb0ad
--- /dev/null
+++ b/saleor/dashboard/shipping/views.py
@@ -0,0 +1,59 @@
+from __future__ import unicode_literals
+
+from django.contrib import messages
+from django.contrib.admin.views.decorators import staff_member_required
+from django.shortcuts import get_object_or_404, redirect
+from django.template.response import TemplateResponse
+from django.utils.translation import ugettext_lazy as _
+
+from ...core.utils import get_paginator_items
+
+from ...shipping.models import ShippingMethod
+from .forms import ShippingMethodForm, ShippingMethodCountryFormSet
+
+
+@staff_member_required
+def shipping_method_list(request):
+ methods = ShippingMethod.objects.prefetch_related('price_per_country').all()
+ methods = get_paginator_items(methods, 30, request.GET.get('page'))
+ ctx = {'shipping_methods': methods}
+ return TemplateResponse(request, 'dashboard/shipping/method_list.html', ctx)
+
+
+def shipping_method_edit(request, method):
+ form = ShippingMethodForm(request.POST or None, instance=method)
+ formset = ShippingMethodCountryFormSet(request.POST or None, instance=method)
+ if form.is_valid() and formset.is_valid():
+ method = form.save()
+ formset.save()
+ msg = _('%s method saved') % method
+ messages.success(request, msg)
+ return redirect('dashboard:shipping-methods')
+ ctx = {'shipping_method_form': form,
+ 'price_per_country_formset': formset, 'shipping_method': method}
+ return TemplateResponse(request, 'dashboard/shipping/method_form.html', ctx)
+
+
+@staff_member_required
+def shipping_method_add(request):
+ method = ShippingMethod()
+ return shipping_method_edit(request, method)
+
+
+@staff_member_required
+def shipping_method_detail(request, pk):
+ method = get_object_or_404(ShippingMethod, pk=pk)
+ return shipping_method_edit(request, method)
+
+
+@staff_member_required
+def shipping_method_delete(request, pk):
+ shipping_method = get_object_or_404(ShippingMethod, pk=pk)
+ if request.method == 'POST':
+ shipping_method.delete()
+ messages.success(
+ request, _('%(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/method_delete.html', ctx)
diff --git a/saleor/dashboard/templates/dashboard/base.html b/saleor/dashboard/templates/dashboard/base.html
index 9f50ca9c4b..beb57c08c3 100644
--- a/saleor/dashboard/templates/dashboard/base.html
+++ b/saleor/dashboard/templates/dashboard/base.html
@@ -53,7 +53,7 @@
<img src="{% static "img/dashboard-logo.svg" %}" alt="Saleor">
</a>
</li>
- <li class="nav-home"><a href="{% url 'dashboard:index' %}">Home</a></li>
+ <li class="nav-home"><a href="{% url 'dashboard:index' %}">{% trans "Home" %}</a></li>
<li>
<ul class="collapsible collapsible-accordion" data-collapsible="accordion">
<li>
@@ -80,6 +80,10 @@
</ul>
</div>
</li>
+
+ <li class="divider"></li>
+
+ <li class="{% block menu_delivery_class %}{% endblock %}"><a href="{% url 'dashboard:shipping-methods' %}">{% trans "Shipping methods" %}</a></li>
</ul>
</li>
</ul>
diff --git a/saleor/dashboard/templates/dashboard/shipping/method_delete.html b/saleor/dashboard/templates/dashboard/shipping/method_delete.html
new file mode 100644
index 0000000000..f01932d4a8
--- /dev/null
+++ b/saleor/dashboard/templates/dashboard/shipping/method_delete.html
@@ -0,0 +1,44 @@
+{% extends "dashboard/base.html" %}
+{% load materializecss %}
+{% load i18n %}
+
+{% block title %}{% trans "Delete" %} {{ shipping_method }}{% endblock %}
+
+{% block body_class %}body-shipping-methods{% endblock %}
+
+{% block page_title %}{% trans "Delete" %} {{ shipping_method }}{% endblock %}
+
+{% block menu_delivery_class %}active{% endblock %}
+
+{% block breadcrumbs %}
+ <ul class="breadcrumbs breadcrumbs--with-menu">
+ <li><a href="{% url 'dashboard:shipping-methods' %}"
+ class="breadcrumbs--ellipsed-item">{% trans "Subscription methods" %}</a>
+ </li>
+ <li><a href="{% url 'dashboard:shipping-method-detail' pk=shipping_method.pk %}"
+ class="breadcrumbs--ellipsed-item">{{ shipping_method }}</a>
+ </li>
+ <li>
+ <span class="breadcrumbs--ellipsed-item">{% trans "Delete" %}</span>
+ </li>
+ </ul>
+{% endblock %}
+
+{% block menu_shipping_class %} active{% endblock %}
+
+{% block content %}
+ <div class="row">
+ <div class="col s12 m5">
+ <div class="card-panel">
+ {% blocktrans with shipping_method_name=shipping_method %}
+ Are you sure you want to delete the {{ shipping_method_name }} method?
+ {% endblocktrans %}
+ </div>
+ </div>
+ </div>
+ <form method="post">
+ {% csrf_token %}
+ <button type="submit" class="btn red">{% trans "Delete" %}</button>
+ </div>
+ </form>
+{% endblock %}
diff --git a/saleor/dashboard/templates/dashboard/shipping/method_form.html b/saleor/dashboard/templates/dashboard/shipping/method_form.html
new file mode 100644
index 0000000000..31832c3012
--- /dev/null
+++ b/saleor/dashboard/templates/dashboard/shipping/method_form.html
@@ -0,0 +1,88 @@
+{% extends "dashboard/base.html" %}
+{% load materializecss %}
+{% load i18n %}
+{% load mptt_tags %}
+
+
+{% block title %}
+ {% trans "Shipping methods" %}
+ {% if shipping_method.pk %}
+ - {{ shipping_method }}
+ {% else %}
+ - {% trans 'Add new method' %}
+ {% endif %}
+ - {{ block.super }}
+{% endblock %}
+
+{% block body_class %}body-shipping-methods{% endblock %}
+
+{% block menu_delivery_class %}active{% endblock %}
+
+{% block page_title %}{% if shipping_method.pk %}{{ shipping_method }}{% else %}
+ {% trans 'Add new method' %}
+{% endif %}{% endblock %}
+
+{% block breadcrumbs %}
+ <ul class="breadcrumbs breadcrumbs--with-menu">
+ <li><a href="{% url 'dashboard:shipping-methods' %}"
+ class="breadcrumbs--ellipsed-item">{% trans "Shipping methods" %}</a>
+ </li>
+ <li>
+ {% if shipping_method.pk %}
+ <span class="breadcrumbs--ellipsed-item">{{ shipping_method }}</span>
+ {% else %}
+ <span class="breadcrumbs--ellipsed-item">{% trans 'Add new method' %}</span>
+ {% endif %}
+ </li>
+ </ul>
+{% endblock %}
+
+{% block menu_shipping_class %} active{% endblock %}
+
+{% block content %}
+ <form method="post" id="form-method">
+ {% csrf_token %}
+ {{ price_per_country_formset.management_form }}
+ <div class="row">
+ {{ shipping_method_form|materializecss }}
+ </div>
+ <h4 class="light">{% trans "Price per country" %}</h4>
+
+ <div class="card-panel">
+ {% for form in price_per_country_formset %}
+ <div class="row">
+ {{ form.id }}
+ {{ form.country_code|materializecss:"input-field s12 l6" }}
+ {{ form.price|materializecss:"input-field s12 l5" }}
+ {{ form.DELETE|materializecss:"input-field s12 l1" }}
+ </div>
+ {% if form.non_field_errors %}
+ {% for error in form.non_field_errors %}
+ <p class="help-block materialize-red-text">{{ error }}</p>
+ {% endfor %}
+ {% endif %}
+ {% endfor %}
+ </div>
+
+
+ <div class="row">
+ {% if shipping_method.pk %}
+ <div class="col s12 l6">
+ <a href="{% url 'dashboard:shipping-method-delete' pk=shipping_method.pk %}"
+ class="btn red">{% trans "Delete" %}</a>
+ </div>
+ <div class="col s12 l6 right-align">
+ <a href="{% url 'dashboard:shipping-methods' %}"
+ class="btn-flat">{% trans "Cancel" %}</a>
+ <button type="submit" class="btn">{% trans "Update" %}</button>
+ </div>
+ {% else %}
+ <div class="col s12 right-align">
+ <a href="{% url 'dashboard:shipping-methods' %}"
+ class="btn-flat">{% trans "Cancel" %}</a>
+ <button type="submit" class="btn">{% trans "Create" %}</button>
+ </div>
+ {% endif %}
+ </div>
+ </form>
+{% endblock %}
diff --git a/saleor/dashboard/templates/dashboard/shipping/method_list.html b/saleor/dashboard/templates/dashboard/shipping/method_list.html
new file mode 100644
index 0000000000..4e8ed8a0f0
--- /dev/null
+++ b/saleor/dashboard/templates/dashboard/shipping/method_list.html
@@ -0,0 +1,70 @@
+{% extends "dashboard/base.html" %}
+{% load i18n %}
+{% load prices_i18n %}
+{% load status %}
+
+{% block title %}{% trans "Shipping methods" %} - {{ block.super }}{% endblock %}
+
+{% block body_class %}body-shipping-methods{% endblock %}
+
+{% block menu_delivery_class %}active{% endblock %}
+
+{% block breadcrumbs %}
+ <ul class="breadcrumbs">
+ <li>{% trans "Shipping" %}</li>
+ <li>{% trans "methods" %}</li>
+ </ul>
+{% endblock %}
+
+{% block header_extra %}
+ <div class="fixed-action-btn">
+ <a class="btn-fab btn-floating btn-large waves-effect waves-light teal"
+ title="{% trans "Add method" %}"
+ href="{% url 'dashboard:shipping-method-add' %}">
+ <i class="mdi-content-add"></i>
+ </a>
+ </div>
+{% endblock %}
+
+{% block content %}
+ {% if shipping_methods %}
+ <div class="card">
+ <div class="data-table-container">
+ <table class="bordered hoverable responsive data-table">
+ <thead>
+ <tr>
+ <th>{% trans "Name" %}</th>
+ <th>{% trans "Price range" %}</th>
+ <th class="wide">{% trans "Countries" %}</th>
+ </tr>
+ </thead>
+ <tbody>
+ {% for method in shipping_methods %}
+ <tr>
+ <td>
+ <a href="{% url 'dashboard:shipping-method-detail' pk=method.pk %}">{{ method }}</a>
+ </td>
+ <td>
+ {% if not method.price_range %}
+ -
+ {% elif method.price_range.min_price == method.price_range.max_price %}
+ {% gross method.price_range.min_price %}
+ {% else %}
+ {% gross method.price_range.min_price %} -
+ {% gross method.price_range.max_price %}
+ {% endif %}
+ </td>
+ <td>{{ method.countries|join:", " }}</td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+ </div>
+ </div>
+
+ {% include "dashboard/includes/_pagination.html" %}
+ {% else %}
+ <p>{% trans "No shipping methods found." %}</p>
+ {% endif %}
+
+{% endblock %}
diff --git a/saleor/dashboard/urls.py b/saleor/dashboard/urls.py
index 8552b7bdca..f94f78cf69 100644
--- a/saleor/dashboard/urls.py
+++ b/saleor/dashboard/urls.py
@@ -7,6 +7,7 @@ from .order.urls import urlpatterns as order_urls
from .payments.urls import urlpatterns as payments_urls
from .product.urls import urlpatterns as product_urls
from .discount.urls import urlpatterns as discount_urls
+from .shipping.urls import urlpatterns as shipping_urls
urlpatterns = [
@@ -17,4 +18,5 @@ urlpatterns = [
url(r'^payments/', include(payments_urls)),
url(r'^customers/', include(customer_urls)),
url(r'^discounts/', include(discount_urls)),
+ url(r'^shipping/', include(shipping_urls)),
]