Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Show variants with stock less than given threshold
Marcin Gębala15ac483a1b6e
diff --git a/saleor/dashboard/templates/dashboard/index.html b/saleor/dashboard/templates/dashboard/index.htmlindex 04f88f087d..12a3d6b995 100644--- a/saleor/dashboard/templates/dashboard/index.html+++ b/saleor/dashboard/templates/dashboard/index.html@@ -1,4 +1,5 @@{% extends "dashboard/base.html" %}+{% load i18n %}{% load prices_i18n %}{% block title %}{{ block.super }}{% endblock %}@@ -52,6 +53,37 @@{% endif %}</div></div>+ {% if show_running_out %}+ <div class="panel panel-default">+ <div class="panel-heading">+ <span class="panel-title">{% trans "Running out products" %}</span>+ </div>+ <div class="panel-body">+ {% if running_out %}+ <table class="table">+ <thead>+ <tr>+ <th>{% trans "Product" %}</th>+ <th>{% trans "Variant" %}</th>+ <th>{% trans "Stock" %}</th>+ </tr>+ </thead>+ <tbody>+ {% for variant in running_out %}+ <tr>+ <td><a href="{% url "dashboard:product-update" pk=variant.product.pk %}">{{ variant.product }}</a></td>+ <td>{{ variant.name }}</td>+ <td>{{ variant.stock }}</td>+ </tr>+ {% endfor %}+ </tbody>+ </table>+ {% else %}+ <span>{% trans "No products" %}</span>+ {% endif %}+ </div>+ </div>+ {% endif %}</div></div>{% endblock %}diff --git a/saleor/dashboard/views.py b/saleor/dashboard/views.pyindex 4691962544..f07f8df478 100644--- a/saleor/dashboard/views.py+++ b/saleor/dashboard/views.py@@ -1,7 +1,9 @@+from django.conf import settingsfrom django.contrib.admin.views.decorators import staff_member_required \as _staff_member_requiredfrom django.template.response import TemplateResponsefrom django.utils.decorators import method_decorator+from django.utils.module_loading import import_stringfrom prices import Pricefrom ..order.models import Order, Payment@@ -56,5 +58,23 @@ def index(request):payments = Payment.objects.filter(status='preauth').order_by('-created')for payment in payments:payment.total = Price(payment.total, currency=payment.currency)- ctx = {'preauthorized_payments': payments, 'orders_to_ship': orders_to_ship}++ show_running_out = hasattr(settings, 'STOCKABLE_PRODUCTS')+ running_out = products_running_out() if show_running_out else []++ ctx = {'preauthorized_payments': payments, 'orders_to_ship': orders_to_ship,+ 'show_running_out': show_running_out, 'running_out': running_out}return TemplateResponse(request, 'dashboard/index.html', ctx)+++def products_running_out():+ product_classes = settings.STOCKABLE_PRODUCTS+ try:+ threshold = settings.STOCK_THRESHOLD+ except AttributeError:+ threshold = 10+ products = []+ for cls in product_classes:+ Product = import_string(cls)+ products.extend(Product.objects.filter(stock__lte=threshold))+ return productsdiff --git a/saleor/settings.py b/saleor/settings.pyindex 3139397967..5fab3fdaf8 100644--- a/saleor/settings.py+++ b/saleor/settings.py@@ -228,3 +228,10 @@ TEMPLATE_STRING_IF_INVALID = '<< MISSING VARIABLE >>'MESSAGE_TAGS = {messages.ERROR: 'danger',}++STOCKABLE_PRODUCTS = [+ 'saleor.product.models.BagVariant',+ 'saleor.product.models.ShirtVariant'+]++STOCK_THRESHOLD = 10