Skip to content

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

Split get_costs_data into two functions

michaladd39f98a54e
diff --git a/saleor/product/models.py b/saleor/product/models.py
index 55a5b466e8..4adb42981a 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -332,9 +332,8 @@ class ProductVariant(models.Model, Item):
costs = []
margins = []
for stock in self.stock.all():
- costs_data = stock.get_costs_data()
- costs.append(costs_data['costs'])
- margins.append(costs_data['margins'])
+ 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}
@@ -404,15 +403,19 @@ class Stock(models.Model):
def quantity_available(self):
return max(self.quantity - self.quantity_allocated, 0)
- def get_costs_data(self):
+ def get_cost_price(self):
zero_price = Price(0, 0, currency=DEFAULT_CURRENCY)
if not self.cost_price:
- return {'costs': zero_price, 'margins': 0}
+ 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 {'costs': self.cost_price, 'margins': percent}
+ return percent
@python_2_unicode_compatible
diff --git a/templates/dashboard/product/_stock_table.html b/templates/dashboard/product/_stock_table.html
index 61a78db065..2fad321842 100644
--- a/templates/dashboard/product/_stock_table.html
+++ b/templates/dashboard/product/_stock_table.html
@@ -19,7 +19,7 @@
<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_costs_data.margins }}%
+ {{ stock_item.get_margin_for_variant }}%
</td>
</tr>
{% empty %}