Skip to content

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

Move Stock get_cost_price, get_margin_for_variant and ProductVariant get_costs_data to utils

michal9bb1b6e598bb
diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.py
index 4fb7623373..0c985c8829 100644
--- a/saleor/dashboard/product/views.py
+++ b/saleor/dashboard/product/views.py
@@ -13,7 +13,7 @@ from ...product.models import (
Product, ProductAttribute, ProductClass, ProductImage, ProductVariant,
Stock, StockLocation)
from ...product.utils import (
- get_availability, get_purchase_cost_and_gross_margins)
+ get_availability, get_product_costs_data, get_variant_costs_data)
from ...settings import DASHBOARD_PAGINATE_BY
from ..views import staff_member_required, superuser_required
from . import forms
@@ -157,7 +157,7 @@ def product_detail(request, pk):
images = product.images.all()
availability = get_availability(product)
sale_price = availability.price_range
- purchase_cost, gross_margin = get_purchase_cost_and_gross_margins(product)
+ purchase_cost, gross_margin = get_product_costs_data(product)
gross_price_range = product.get_gross_price_range()
# no_variants is True for product classes that doesn't require variant.
@@ -400,8 +400,10 @@ def variant_details(request, product_pk, variant_pk):
stock = variant.stock.all()
images = variant.images.all()
+ costs_data = get_variant_costs_data(variant)
ctx = {'images': images, 'product': product, 'stock': stock,
- 'variant': variant}
+ 'variant': variant, 'costs': costs_data['costs'],
+ 'margins': costs_data['margins']}
return TemplateResponse(
request,
'dashboard/product/product_variant/detail.html',
diff --git a/saleor/dashboard/templatetags/utils.py b/saleor/dashboard/templatetags/utils.py
index 69d5b23ad0..5e0a2283bd 100644
--- a/saleor/dashboard/templatetags/utils.py
+++ b/saleor/dashboard/templatetags/utils.py
@@ -9,6 +9,7 @@ except ImportError:
from urllib import urlencode
from django.template import Library
+from ...product.utils import get_margin_for_variant, get_variant_costs_data
register = Library()
@@ -45,3 +46,14 @@ def paginate(context, page_obj, num_of_pages=5):
context['next_section'] = (2 * num_of_pages) + 1
context['previous_section'] = (-2 * num_of_pages) - 1
return context
+
+
+def margin_for_variant(stock):
+ return get_margin_for_variant(stock)
+
+
+def margins_for_variant(variant):
+ margins = get_variant_costs_data(variant)['margins']
+ return margins
diff --git a/saleor/product/models.py b/saleor/product/models.py
index 4adb42981a..ce4108aa72 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -328,16 +328,6 @@ class ProductVariant(models.Model, Item):
if stock:
return stock.cost_price
- def get_costs_data(self):
- costs = []
- margins = []
- for stock in self.stock.all():
- costs.append(stock.get_cost_price())
- margins.append(stock.get_margin_for_variant())
- costs = sorted(costs, key=lambda x: x.gross)
- margins = sorted(margins)
- return {'costs': costs, 'margins': margins}
-
@python_2_unicode_compatible
class StockLocation(models.Model):
@@ -403,20 +393,6 @@ class Stock(models.Model):
def quantity_available(self):
return max(self.quantity - self.quantity_allocated, 0)
- def get_cost_price(self):
- zero_price = Price(0, 0, currency=DEFAULT_CURRENCY)
- if not self.cost_price:
- return zero_price
- return self.cost_price
-
- def get_margin_for_variant(self):
- if not self.cost_price:
- return 0
- price = self.variant.get_price_per_item()
- margin = price - self.cost_price
- percent = round((margin.gross / price.gross) * 100, 0)
- return percent
-
@python_2_unicode_compatible
class ProductAttribute(models.Model):
diff --git a/saleor/product/utils.py b/saleor/product/utils.py
index 92ce2c383b..f4d6e421b8 100644
--- a/saleor/product/utils.py
+++ b/saleor/product/utils.py
@@ -316,7 +316,7 @@ def get_variant_availability_status(variant):
return VariantAvailabilityStatus.AVAILABLE
-def get_purchase_cost_and_gross_margins(product):
+def get_product_costs_data(product):
zero_price = Price(0, 0, currency=DEFAULT_CURRENCY)
zero_price_range = PriceRange(zero_price, zero_price)
purchase_costs_range = zero_price_range
@@ -344,7 +344,34 @@ def get_cost_data_from_variants(variants):
costs = []
margins = []
for variant in variants:
- costs_data = variant.get_costs_data()
+ costs_data = get_variant_costs_data(variant)
costs += costs_data['costs']
margins += costs_data['margins']
return sort_cost_data(costs, margins)
+
+
+def get_variant_costs_data(variant):
+ costs = []
+ margins = []
+ for stock in variant.stock.all():
+ costs.append(get_cost_price(stock))
+ margins.append(get_margin_for_variant(stock))
+ costs = sorted(costs, key=lambda x: x.gross)
+ margins = sorted(margins)
+ return {'costs': costs, 'margins': margins}
+
+
+def get_cost_price(stock):
+ zero_price = Price(0, 0, currency=DEFAULT_CURRENCY)
+ if not stock.cost_price:
+ return zero_price
+ return stock.cost_price
+
+
+def get_margin_for_variant(stock):
+ if not stock.cost_price:
+ return 0
+ price = stock.variant.get_price_per_item()
+ margin = price - stock.cost_price
+ percent = round((margin.gross / price.gross) * 100, 0)
+ return percent
diff --git a/templates/dashboard/product/_stock_table.html b/templates/dashboard/product/_stock_table.html
index 2fad321842..65b1bf78f3 100644
--- a/templates/dashboard/product/_stock_table.html
+++ b/templates/dashboard/product/_stock_table.html
@@ -1,5 +1,6 @@
{% load i18n %}
{% load prices_i18n %}
+{% load utils %}
<table class="bordered highlight responsive data-table">
<thead>
@@ -19,7 +20,12 @@
<td>{{ stock_item.quantity_allocated }}</td>
<td class="right-align">{% if stock_item.cost_price %}{% gross stock_item.cost_price %}{% else %}-{% endif %}</td>
<td class="right-align">
- {{ stock_item.get_margin_for_variant }}%
+ {% margin_for_variant stock=stock_item as margin%}
+ {% if margin %}
+ {{ margin }}%
+ {% else %}
+ -
+ {% endif %}
</td>
</tr>
{% empty %}
diff --git a/templates/dashboard/product/detail.html b/templates/dashboard/product/detail.html
index 0b565ea538..dfcfbdb973 100644
--- a/templates/dashboard/product/detail.html
+++ b/templates/dashboard/product/detail.html
@@ -4,6 +4,7 @@
{% load prices_i18n %}
{% load staticfiles %}
{% load status %}
+{% load utils %}
{% block title %}{{ product }} - {{ block.super }}{% endblock %}
@@ -115,17 +116,14 @@
<td class="wide">{% render_variant_availability_status variant %}</td>
<td class="right-align">{% gross variant.get_price_per_item %}</td>
<td class="right-align">
- {% with variant.get_costs_data.margins as margins %}
- {% if margins|length > 1 %}
- {{ margins|first }}% - {{ margins|last }}%
- {% else %}
- {% if variant.get_costs_data.margins %}
- {{ margins|first }}%
- {% else %}
- 0%
- {% endif %}
- {% endif %}
- {% endwith %}
+ {% margins_for_variant variant=variant as margins %}
+ {% if margins|length > 1 %}
+ {{ margins|first }}% - {{ margins|last }}%
+ {% elif margins %}
+ {{ margins|first }}%
+ {% else %}
+ -
+ {% endif %}
</td>
{% if perms.product.edit_product %}
<td><a href="{% url 'dashboard:variant-update' product_pk=product.pk variant_pk=variant.pk %}" class="btn btn-flat">{% trans "Edit" %}</a></td>
diff --git a/templates/dashboard/product/product_variant/detail.html b/templates/dashboard/product/product_variant/detail.html
index e518f46eb9..06c6d13118 100644
--- a/templates/dashboard/product/product_variant/detail.html
+++ b/templates/dashboard/product/product_variant/detail.html
@@ -60,15 +60,13 @@
</div>
<div class="col s7 m6">
<p class="text-padding right-align">
- {% with variant.get_costs_data.costs as costs %}
- {% if costs|length > 1 %}
- {% gross costs|first %} - {% gross costs|last %}
- {% elif costs|length == 1 %}
- {% gross costs|first %}
- {% else %}
- -
- {% endif %}
- {% endwith %}
+ {% if costs|length > 1 %}
+ {% gross costs|first %} - {% gross costs|last %}
+ {% elif costs|length == 1 %}
+ {% gross costs|first %}
+ {% else %}
+ -
+ {% endif %}
</p>
</div>
<div class="col s5 m6">
@@ -76,17 +74,15 @@
</div>
<div class="col s7 m6">
<p class="text-padding right-align">
- {% with variant.get_costs_data.margins as margins %}
- {% if margins|length > 1 %}
- {{ margins|first }}% - {{ margins|last }}%
+ {% if margins|length > 1 %}
+ {{ margins|first }}% - {{ margins|last }}%
+ {% else %}
+ {% if variant.get_costs_data.margins %}
+ {{ margins|first }}%
{% else %}
- {% if variant.get_costs_data.margins %}
- {{ margins|first }}%
- {% else %}
- 0%
- {% endif %}
+ -
{% endif %}
- {% endwith %}
+ {% endif %}
</p>
</div>
</div>