Skip to content

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

Use new prices API and templatetags

Michał Ociepka0514e90c447c
diff --git a/requirements.txt b/requirements.txt
index fadee71066..167d67d43c 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -24,16 +24,19 @@ django-model-utils==2.4
django-mptt==0.8.0
django-offsite-storage==0.0.8
django-payments==0.9.1
-django-prices==0.4.10
+django-prices==0.4.11
django-redis==4.3.0
django-selectable==0.9.0
django-versatileimagefield==1.3
django==1.9.2
djangorestframework==3.3.2
+enum34==1.1.2 # via cryptography
fake-factory==0.5.3
google-i18n-address==1.0.7
google-measurement-protocol==0.1.5
idna==2.0 # via cryptography
+ipaddr==2.1.11 # via maxminddb
+ipaddress==1.0.16 # via cryptography
jsonfield==1.0.3
lxml==3.5.0 # via premailer
markdown==2.6.5
@@ -43,7 +46,7 @@ mock==1.3.0
pbr==1.8.1 # via mock
pillow==3.1.1 # via django-versatileimagefield
premailer==2.9.7 # via django-emailit
-prices==0.5.5
+prices==0.5.7
psycopg2==2.6.1
purl==1.2
py==1.4.31 # via pytest
diff --git a/saleor/checkout/templates/checkout/details.html b/saleor/checkout/templates/checkout/details.html
index a149ad9d08..dccdb2b0f0 100644
--- a/saleor/checkout/templates/checkout/details.html
+++ b/saleor/checkout/templates/checkout/details.html
@@ -61,6 +61,7 @@
</div>
<div class="col-sm-4">
+ {% with total=checkout.get_total %}
<table class="table table-condensed">
<tbody>
{% for delivery, delivery_cost, total_with_delivery in checkout.deliveries %}
@@ -99,29 +100,27 @@
{% endif %}
</td>
</tr>
-
- {% with total=checkout.get_total %}
- {% if total.tax or True %}
- <tr>
- <td class="noborder">
- {% trans "Taxes" %}
- </td>
- <td class="noborder text-right">
- {% tax total %}
- </td>
- </tr>
- {% endif %}
+ {% if total.tax or True %}
<tr>
- <th>
- {% trans "Total" %}
- </th>
- <td class="text-right">
- <strong>{% gross total %}</strong>
+ <td class="noborder">
+ {% trans "Taxes" %}
+ </td>
+ <td class="noborder text-right">
+ {% tax total %}
</td>
</tr>
- {% endwith %}
+ {% endif %}
+ <tr>
+ <th>
+ {% trans "Total" %}
+ </th>
+ <td class="text-right">
+ <strong>{% gross total %}</strong>
+ </td>
+ </tr>
</tbody>
</table>
+ {% endwith %}
</div>
</div>
{% endblock content %}
diff --git a/saleor/checkout/templates/checkout/snippets/voucher-form.html b/saleor/checkout/templates/checkout/snippets/voucher-form.html
index fec632bc8c..647f99c693 100644
--- a/saleor/checkout/templates/checkout/snippets/voucher-form.html
+++ b/saleor/checkout/templates/checkout/snippets/voucher-form.html
@@ -1,6 +1,7 @@
{% load bootstrap_field bootstrap_button bootstrap_icon from bootstrap3 %}
{% load trans from i18n %}
-{% load gross net tax from prices_i18n %}
+{% load gross from prices_i18n %}
+{% load discount_amount_for from prices %}
{% if form %}
<tr>
@@ -19,7 +20,7 @@
</form>
</td>
<td class="noborder text-right">
- -&nbsp;{% net discount.amount %}
+ {% gross discount|discount_amount_for:total %}
</td>
{% else %}
<form action="{{ request.path }}?next={{ request.path }}"
diff --git a/saleor/discount/models.py b/saleor/discount/models.py
index b776ef6440..03a3682d6e 100644
--- a/saleor/discount/models.py
+++ b/saleor/discount/models.py
@@ -140,14 +140,10 @@ class Voucher(models.Model):
discount = percentage_discount(
value=self.discount_value, name=smart_text(self))
fixed_discount_value = amount - discount.apply(amount)
- # todo: Fix prces to quantize this discount type
- cent = Decimal('0.01')
- fixed_discount_value = fixed_discount_value.quantize(cent)
discount = FixedDiscount(
amount=fixed_discount_value, name=smart_text(self))
else:
raise NotImplementedError('Unknown discount value type')
- # todo: Fix the price apply method to not return negative values
if discount.amount > amount:
return FixedDiscount(amount, name=smart_text(self))
else:
@@ -271,11 +267,7 @@ class Sale(models.Model):
self._product_has_category_discount(
variant.product, discounted_categories)):
raise NotApplicable('Discount too high for this product')
- discount = self.get_discount()
- after_discount = discount.apply(check_price)
- if after_discount.gross <= 0:
- raise NotApplicable('Discount too high for this product')
- return discount
+ return self.get_discount()
def get_variant_discounts(variant, discounts, **kwargs):
diff --git a/saleor/discount/test_discounts.py b/saleor/discount/test_discounts.py
index d1ee8de987..c2b22a3679 100644
--- a/saleor/discount/test_discounts.py
+++ b/saleor/discount/test_discounts.py
@@ -42,10 +42,10 @@ def test_variant_discounts(product_variant):
high_discount.products.add(product)
final_price = product_variant.get_price_per_item(
discounts=Sale.objects.all())
- assert final_price.gross == 2
+ assert final_price.gross == 0
applied_discount = final_price.history.right
assert isinstance(applied_discount, FixedDiscount)
- assert applied_discount.amount.gross == 8
+ assert applied_discount.amount.gross == 50
@pytest.mark.integration
@@ -65,7 +65,7 @@ def test_percentage_discounts(product_variant):
@pytest.mark.parametrize(
'total, discount_value, discount_type, limit, expected_value', [
('100', 10, Voucher.DISCOUNT_VALUE_FIXED, None, 10),
- ('100.05', 10, Voucher.DISCOUNT_VALUE_PERCENTAGE, 100, '10.01')])
+ ('100.05', 10, Voucher.DISCOUNT_VALUE_PERCENTAGE, 100, 10)])
def test_value_voucher_checkout_discount(settings, total, discount_value,
discount_type, limit, expected_value):
settings.DEFAULT_CURRENCY = 'USD'
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 889ca5a766..de3f192f2d 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -15,7 +15,7 @@ from django.utils.translation import pgettext_lazy
from django_prices.models import PriceField
from payments import PurchasedItem
from payments.models import BasePayment
-from prices import Price
+from prices import Price, FixedDiscount
from satchless.item import ItemLine, ItemSet
from ..core.utils import build_absolute_uri
@@ -109,6 +109,11 @@ class Order(models.Model, ItemSet):
def __str__(self):
return '#%d' % (self.id, )
+ @property
+ def discount(self):
+ return FixedDiscount(
+ amount=self.discount_amount, name=self.discount_name)
+
def get_total(self):
return self.total
diff --git a/saleor/order/templates/order/payment.html b/saleor/order/templates/order/payment.html
index a2718384c4..e7aa48d26b 100644
--- a/saleor/order/templates/order/payment.html
+++ b/saleor/order/templates/order/payment.html
@@ -2,6 +2,7 @@
{% load bootstrap_form from bootstrap3 %}
{% load gross from prices_i18n %}
+{% load discount_amount_for from prices %}
{% load i18n %}
{% load staticfiles %}
@@ -139,7 +140,7 @@
{{ order.discount_name }}
</div>
<div class="panel-body text-right">
- <strong>-&nbsp;{% gross order.discount_amount %}</strong>
+ <strong>{% gross order.discount|discount_amount_for:order.total %}</strong>
</div>
</div>
{% endif %}