Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add dashboard views to inspect taxes
Anna Siwik105625477fc6
diff --git a/saleor/core/i18n.py b/saleor/core/i18n.pyindex f57147e77a..d78786e9de 100644--- a/saleor/core/i18n.py+++ b/saleor/core/i18n.py@@ -1,4 +1,9 @@from django.utils.translation import pgettext_lazy+from django_countries import countries++ANY_COUNTRY = ''+ANY_COUNTRY_DISPLAY = pgettext_lazy('Country choice', 'Rest of World')+COUNTRY_CODE_CHOICES = [(ANY_COUNTRY, ANY_COUNTRY_DISPLAY)] + list(countries)VAT_RATE_TYPE_TRANSLATIONS = {'accommodation': pgettext_lazy('VAT rate type', 'accommodation'),diff --git a/saleor/core/templatetags/dashboard.py b/saleor/core/templatetags/dashboard.pyindex 4d6b2b67e2..dd83818d20 100644--- a/saleor/core/templatetags/dashboard.py+++ b/saleor/core/templatetags/dashboard.py@@ -1,8 +1,15 @@from django.template import Library+from ...core.utils import get_country_name_by_code+register = Library()@register.simple_tagdef get_formset_form(formset, index):return formset.forms[index]+++def get_country_by_code(country_code):+ return get_country_name_by_code(country_code)diff --git a/saleor/core/utils/__init__.py b/saleor/core/utils/__init__.pyindex 9e9ae4de35..19783833c1 100644--- a/saleor/core/utils/__init__.py+++ b/saleor/core/utils/__init__.py@@ -21,6 +21,7 @@ from prices import Money, MoneyRange, TaxedMoney, TaxedMoneyRangefrom versatileimagefield.image_warmer import VersatileImageFieldWarmerfrom ...account.models import User+from ...core.i18n import COUNTRY_CODE_CHOICESZERO_TAXED_MONEY = TaxedMoney(net=Money(0, settings.DEFAULT_CURRENCY),@@ -203,3 +204,10 @@ def create_thumbnails(pk, model, size_set, image_attr=None):if failed_to_create:logger.error('Failed to generate thumbnails',extra={'paths': failed_to_create})+++def get_country_name_by_code(country_code):+ country_name = next(+ (name for code, name in COUNTRY_CODE_CHOICES if code == country_code),+ country_code)+ return country_namediff --git a/saleor/dashboard/discount/forms.py b/saleor/dashboard/discount/forms.pyindex 09cd2266ad..e56a0a54ef 100644--- a/saleor/dashboard/discount/forms.py+++ b/saleor/dashboard/discount/forms.py@@ -7,10 +7,11 @@ from django.utils.translation import pgettext_lazyfrom django_prices.forms import MoneyFieldfrom prices import Money+from ...core.i18n import COUNTRY_CODE_CHOICESfrom ...discount import DiscountValueType, VoucherApplyToProductfrom ...discount.models import Sale, Voucherfrom ...product.models import Product-from ...shipping.models import COUNTRY_CODE_CHOICES, ShippingMethodCountry+from ...shipping.models import ShippingMethodCountryfrom ..forms import AjaxSelect2ChoiceField, AjaxSelect2MultipleChoiceFielddiff --git a/saleor/dashboard/shipping/filters.py b/saleor/dashboard/shipping/filters.pyindex 33ce165443..21d477cf24 100644--- a/saleor/dashboard/shipping/filters.py+++ b/saleor/dashboard/shipping/filters.py@@ -2,8 +2,9 @@ from django.utils.translation import npgettext, pgettext_lazyfrom django_filters import (CharFilter, ChoiceFilter, OrderingFilter, RangeFilter)+from ...core.i18n import COUNTRY_CODE_CHOICESfrom ...core.filters import SortedFilterSet-from ...shipping.models import COUNTRY_CODE_CHOICES, ShippingMethod+from ...shipping.models import ShippingMethodSORT_BY_FIELDS = {'name': pgettext_lazy('Group list sorting option', 'name')}diff --git a/saleor/dashboard/taxes/__init__.py b/saleor/dashboard/taxes/__init__.pynew file mode 100644index 0000000000..e69de29bb2diff --git a/saleor/dashboard/taxes/filters.py b/saleor/dashboard/taxes/filters.pynew file mode 100644index 0000000000..7f5aa0feea--- /dev/null+++ b/saleor/dashboard/taxes/filters.py@@ -0,0 +1,47 @@+from django import forms+from django.utils.translation import npgettext, pgettext_lazy+from django_filters import ChoiceFilter, OrderingFilter+from django_prices_vatlayer.models import VAT++from ...core.filters import SortedFilterSet+from ...core.utils import get_country_name_by_code++SORT_BY_FIELDS = {+ 'country_code': pgettext_lazy(+ 'Product list sorting option', 'country_code')}+++def get_country_choices_for_vat():+ choices = [+ (country_code, get_country_name_by_code(country_code))+ for country_code in VAT.objects.values_list('country_code', flat=True)]+ return choices+++class TaxFilter(SortedFilterSet):+ country_code = ChoiceFilter(+ label=pgettext_lazy(+ 'Taxes list filter label', 'Country name'),+ empty_label=pgettext_lazy('Filter empty choice label', 'All'),+ widget=forms.Select)+ sort_by = OrderingFilter(+ label=pgettext_lazy('Taxes list sorting form', 'Sort by'),+ fields=SORT_BY_FIELDS.keys(),+ field_labels=SORT_BY_FIELDS)++ class Meta:+ model = VAT+ fields = []++ def __init__(self, *args, **kwargs):+ super().__init__(*args, **kwargs)+ self.filters['country_code'].extra.update({+ 'choices': get_country_choices_for_vat()})++ def get_summary_message(self):+ counter = self.qs.count()+ return npgettext(+ 'Number of matching records in the dashboard taxes list',+ 'Found %(counter)d matching country',+ 'Found %(counter)d matching countries',+ number=counter) % {'counter': counter}diff --git a/saleor/dashboard/taxes/urls.py b/saleor/dashboard/taxes/urls.pynew file mode 100644index 0000000000..0269bdfd14--- /dev/null+++ b/saleor/dashboard/taxes/urls.py@@ -0,0 +1,9 @@+from django.conf.urls import url++from . import views++urlpatterns = [+ url(r'^$', views.tax_list, name='taxes'),+ url(r'^(?P<country_code>[A-Z]{2})/details/$', views.tax_details,+ name='tax-details'),+]diff --git a/saleor/dashboard/taxes/views.py b/saleor/dashboard/taxes/views.pynew file mode 100644index 0000000000..46d83a5f49--- /dev/null+++ b/saleor/dashboard/taxes/views.py@@ -0,0 +1,33 @@+from django.conf import settings+from django.shortcuts import get_object_or_404+from django.template.response import TemplateResponse+from django_countries.fields import Country+from django_prices_vatlayer.models import VAT++from ...core.i18n import VAT_RATE_TYPE_TRANSLATIONS+from ...core.utils import get_paginator_items, get_taxes_for_country+from ...dashboard.taxes.filters import TaxFilter+from ...dashboard.views import staff_member_required+++@staff_member_required+def tax_list(request):+ taxes = VAT.objects.order_by('country_code')+ tax_filter = TaxFilter(request.GET, queryset=taxes)+ taxes = get_paginator_items(+ tax_filter.qs, settings.DASHBOARD_PAGINATE_BY, request.GET.get('page'))+ ctx = {+ 'taxes': taxes, 'filter_set': tax_filter,+ 'is_empty': not tax_filter.queryset.exists()}+ return TemplateResponse(request, 'dashboard/taxes/list.html', ctx)+++@staff_member_required+def tax_details(request, country_code):+ tax = get_object_or_404(VAT, country_code=country_code)+ tax_rates = get_taxes_for_country(Country(country_code))+ tax_rates = [+ (VAT_RATE_TYPE_TRANSLATIONS.get(rate_name, rate_name), tax['value'])+ for rate_name, tax in tax_rates.items()]+ ctx = {'tax': tax, 'tax_rates': sorted(tax_rates)}+ return TemplateResponse(request, 'dashboard/taxes/details.html', ctx)diff --git a/saleor/dashboard/urls.py b/saleor/dashboard/urls.pyindex 9ecc820357..142612b995 100644--- a/saleor/dashboard/urls.py+++ b/saleor/dashboard/urls.py@@ -14,6 +14,7 @@ from .search.urls import urlpatterns as search_urlsfrom .shipping.urls import urlpatterns as shipping_urlsfrom .sites.urls import urlpatterns as site_urlsfrom .staff.urls import urlpatterns as staff_urls+from .taxes.urls import urlpatterns as taxes_urlsurlpatterns = [url(r'^$', core_views.index, name='index'),@@ -31,4 +32,5 @@ urlpatterns = [url(r'^shipping/', include(shipping_urls)),url(r'^style-guide/', core_views.styleguide, name='styleguide'),url(r'^search/', include(search_urls)),+ url(r'^taxes/', include(taxes_urls)),]diff --git a/saleor/shipping/models.py b/saleor/shipping/models.pyindex 39bb76f915..2d459552d8 100644--- a/saleor/shipping/models.py+++ b/saleor/shipping/models.py@@ -6,17 +6,13 @@ from django.db import modelsfrom django.db.models import Qfrom django.utils.safestring import mark_safefrom django.utils.translation import pgettext_lazy-from django_countries import countriesfrom django_prices.models import MoneyFieldfrom prices import MoneyRange+from ..core.i18n import ANY_COUNTRY, COUNTRY_CODE_CHOICESfrom ..core.utils import format_moneyfrom ..shipping.utils import get_taxed_shipping_price-ANY_COUNTRY = ''-ANY_COUNTRY_DISPLAY = pgettext_lazy('Country choice', 'Rest of World')-COUNTRY_CODE_CHOICES = [(ANY_COUNTRY, ANY_COUNTRY_DISPLAY)] + list(countries)-class ShippingMethod(models.Model):diff --git a/templates/dashboard/base.html b/templates/dashboard/base.htmlindex 91c95fc8ab..6cbacb09b3 100644--- a/templates/dashboard/base.html+++ b/templates/dashboard/base.html@@ -236,6 +236,11 @@</a></li>{% endif %}+ <li class="{% block menu_taxes_class %}{% endblock %}">+ <a href="{% url 'dashboard:taxes' %}">+ {% trans "Taxes" context "Dashboard taxes" %}+ </a>+ </li>{% if perms.page.view_page %}<li class="{% block menu_pages_class %}{% endblock %}"><a href="{% url 'dashboard:page-list' %}">diff --git a/templates/dashboard/taxes/details.html b/templates/dashboard/taxes/details.htmlnew file mode 100644index 0000000000..b60ddecf8e--- /dev/null+++ b/templates/dashboard/taxes/details.html@@ -0,0 +1,78 @@+{% extends 'dashboard/base.html' %}++{% load get_country_by_code from dashboard %}+{% load i18n %}+{% load staticfiles %}++{% block title %}+ {% get_country_by_code tax.country_code as country_name %}+ {{ country_name }} tax rates - {{ block.super }}+{% endblock %}++{% block menu_taxes_class %} active{% endblock %}++{% block breadcrumbs %}+ <ul class="breadcrumbs breadcrumbs--history">+ <li>+ <a href="{% url 'dashboard:taxes' %}">+ {% trans "Taxes" context "Tax details breadcrumbs" %}+ </a>+ </li>+ <li class="back-mobile">+ <a href="{% url 'dashboard:taxes' %}"><i class="material-icons">arrow_back</i></a>+ </li>+ <li>+ <span class="breadcrumbs--elipsed-item">+ {% get_country_by_code tax.country_code as country_name %}+ {{ country_name }}+ </span>+ </li>+ </ul>+{% endblock %}++{% block content %}+ <div class="row">+ <div class="col m12 l9">+ <div class="card">+ <div class="card-content">+ <span class="card-title">+ {% get_country_by_code tax.country_code as country_name %}+ {% blocktrans trimmed with country_name=country_name context "Country tax rates list title" %}+ Tax rates in {{ country_name }}+ {% endblocktrans %}+ </span>+ </div>+ <div class="data-table-container">+ <table class="bordered highlight responsive data-table">+ <thead>+ <tr>+ <th>+ {% trans "Name" context "Country tax rates list header" %}+ </th>+ <th class="right-align">+ {% trans "Value" context "Country tax rates list header" %}+ </th>+ </tr>+ </thead>+ <tbody>+ {% for name, tax_rate in tax_rates %}+ <tr>+ <td>{{ name }}</td>+ <td class="right-align">+ {% blocktrans with tax_rate=tax_rate context "Order line tax rate value" %}+ {{ tax_rate }} %+ {% endblocktrans %}+ </td>+ </tr>+ {% empty %}+ <tr>+ <td colspan="2">{% trans "No tax rates" context "Empty country tax rates list" %}</td>+ </tr>+ {% endfor %}+ </tbody>+ </table>+ </div>+ </div>+ </div>+ </div>+{% endblock %}diff --git a/templates/dashboard/taxes/list.html b/templates/dashboard/taxes/list.htmlnew file mode 100644index 0000000000..3777c9270d--- /dev/null+++ b/templates/dashboard/taxes/list.html@@ -0,0 +1,72 @@+{% extends "dashboard/base.html" %}+{% load i18n %}+{% load get_country_by_code from dashboard %}+{% load utils %}++{% block title %}+ {% trans "Taxes" context "Dashboard taxes list" %} - {{ block.super }}+{% endblock %}++{% block menu_taxes_class %}active{% endblock %}++{% block breadcrumbs %}+ <ul class="breadcrumbs">+ <li class="visible-s">+ {% trans "Taxes" context "Dashboard taxes list" %}+ </li>+ </ul>+{% endblock %}++{% block content %}+ <div class="row">+ <div class="col s12 l9">+ {% if taxes %}+ <div class="card">+ <div class="data-table-container">+ <table class="bordered highlight responsive data-table">+ <thead>+ <tr>+ {% trans "Country code" context "Taxes table header" as label %}+ {% sorting_header 'country_code' label %}++ <th class="wide">+ {% trans "Country name" context "Taxes table header" %}+ </th>+ </tr>+ </thead>+ <tbody>+ {% for tax in taxes %}+ <tr data-action-go="{% url 'dashboard:tax-details' country_code=tax.country_code %}">+ <td>+ {{ tax.country_code }}+ </td>+ <td>+ {% get_country_by_code tax.country_code as country_name %}+ {{ country_name }}+ </td>+ </tr>+ {% endfor %}+ </tbody>+ </table>+ </div>+ </div>+ {% paginate taxes %}+ {% else %}+ {% if is_empty %}+ {% include 'dashboard/taxes/zero_page.html' %}+ {% else %}+ <div class="col s12">+ <div class="not-found">+ <p class="grey-text">+ {% trans "No taxes found." context "Empty taxes message" %}+ </p>+ </div>+ </div>+ {% endif %}+ {% endif %}+ </div>+ <div class="col s12 l3" id="filters">+ {% if not is_empty %}{% filters filter_set %}{% endif %}+ </div>+ </div>+{% endblock %}diff --git a/templates/dashboard/taxes/zero_page.html b/templates/dashboard/taxes/zero_page.htmlnew file mode 100644index 0000000000..dad89b7508--- /dev/null+++ b/templates/dashboard/taxes/zero_page.html@@ -0,0 +1,10 @@+{% extends 'dashboard/base_zero_page.html' %}+{% load i18n %}++{% block title %}{% trans "No taxes found." %}{% endblock %}+{% block content %}+ {% blocktrans %}+ Taxes define tax rates used in European Union countries and are fetched automatically.<br>+ To enable fetching taxes, please provide your access key to vatlayer API in settings file.+ {% endblocktrans %}+{% endblock %}