Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Improvements after review comments
Anna Siwik852a988080f5
diff --git a/saleor/cart/models.py b/saleor/cart/models.pyindex a4c0b649d6..e7526181a5 100644--- a/saleor/cart/models.py+++ b/saleor/cart/models.py@@ -243,15 +243,9 @@ class CartLine(models.Model):def get_total(self, discounts=None, taxes=None):"""Return the total price of this line."""- amount = self.get_price_per_item(discounts, taxes) * self.quantity+ amount = self.quantity * self.variant.get_price(discounts, taxes)return amount.quantize(CENTS)- # pylint: disable=W0221- def get_price_per_item(self, discounts=None, taxes=None):- """Return the unit price of the line."""- return self.variant.get_price_per_item(- discounts=discounts, taxes=taxes)-def is_shipping_required(self):"""Return `True` if the related product variant requires shipping."""return self.variant.is_shipping_required()diff --git a/saleor/cart/utils.py b/saleor/cart/utils.pyindex 7eac997fdf..fe249688b8 100644--- a/saleor/cart/utils.py+++ b/saleor/cart/utils.py@@ -65,7 +65,7 @@ def get_product_variants_and_prices(cart, product):if cart_line.variant.product_id == product.id)for line in lines:for dummy_i in range(line.quantity):- yield line.variant, line.get_price_per_item()+ yield line.variant, line.variant.get_price()def get_category_variants_and_prices(cart, root_category):diff --git a/saleor/cart/views.py b/saleor/cart/views.pyindex 06e9ad7b5e..254cbdbe3a 100644--- a/saleor/cart/views.py+++ b/saleor/cart/views.py@@ -40,7 +40,7 @@ def index(request, cart):initial=initial, discounts=discounts)cart_lines.append({'variant': line.variant,- 'get_price_per_item': line.get_price_per_item(discounts, taxes),+ 'get_price': line.variant.get_price(discounts, taxes),'get_total': line.get_total(discounts, taxes),'form': form})diff --git a/saleor/core/utils/__init__.py b/saleor/core/utils/__init__.pyindex a2eafe41f1..7f2c9b24cf 100644--- a/saleor/core/utils/__init__.py+++ b/saleor/core/utils/__init__.py@@ -26,6 +26,8 @@ ZERO_TAXED_MONEY = TaxedMoney(net=Money(0, settings.DEFAULT_CURRENCY),gross=Money(0, settings.DEFAULT_CURRENCY))+DEFAULT_TAX_RATE_NAME = 'standard'+georeader = geolite2.reader()logger = logging.getLogger(__name__)@@ -106,7 +108,7 @@ def get_taxes_for_country(country):if tax_rates is None:return None- taxes = {'standard': {+ taxes = {DEFAULT_TAX_RATE_NAME: {'value': tax_rates['standard_rate'],'tax': get_tax_for_rate(tax_rates)}}if tax_rates['reduced_rates']:@@ -135,7 +137,7 @@ def apply_tax_to_price(taxes, rate_name, base):if rate_name in taxes:tax_to_apply = taxes[rate_name]['tax']else:- tax_to_apply = taxes['standard']['tax']+ tax_to_apply = taxes[DEFAULT_TAX_RATE_NAME]['tax']keep_gross = Site.objects.get_current().settings.include_taxes_in_pricesreturn tax_to_apply(base, keep_gross=keep_gross)diff --git a/saleor/dashboard/order/utils.py b/saleor/dashboard/order/utils.pyindex cef6989bfe..e017f730f2 100644--- a/saleor/dashboard/order/utils.py+++ b/saleor/dashboard/order/utils.py@@ -82,7 +82,7 @@ def get_product_variants_and_prices(order, product):for dummy_i in range(line.quantity):variant = line.variantif variant:- yield variant, variant.get_price_per_item()+ yield variant, variant.get_price()def get_category_variants_and_prices(order, root_category):diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.pyindex af47ad43eb..5c4e183840 100644--- a/saleor/dashboard/product/forms.py+++ b/saleor/dashboard/product/forms.py@@ -13,6 +13,7 @@ from mptt.forms import TreeNodeChoiceFieldfrom . import ProductBulkActionfrom ...core.i18n import VAT_RATE_TYPE_TRANSLATIONS+from ...core.utils import DEFAULT_TAX_RATE_NAMEfrom ...product.models import (AttributeChoiceValue, Category, Collection, Product, ProductAttribute,ProductImage, ProductType, ProductVariant, VariantImage)@@ -56,7 +57,7 @@ class ProductTypeSelectorForm(forms.Form):def get_tax_rate_type_choices():- rate_types = get_tax_rate_types() + ['standard']+ rate_types = get_tax_rate_types() + [DEFAULT_TAX_RATE_NAME]return [(rate_type, VAT_RATE_TYPE_TRANSLATIONS.get(rate_type, rate_type))for rate_type in rate_types]diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.pyindex 8a9cb33342..3fca8665cf 100644--- a/saleor/dashboard/product/views.py+++ b/saleor/dashboard/product/views.py@@ -621,7 +621,7 @@ def ajax_available_variants_list(request):def get_variant_label(variant, discounts):return '%s, %s, %s' % (variant.sku, variant.display_product(),- prices_i18n.amount(variant.get_price_per_item(discounts).gross))+ prices_i18n.amount(variant.get_price(discounts).gross))available_products = Product.objects.available_products().prefetch_related('category',diff --git a/saleor/data_feeds/google_merchant.py b/saleor/data_feeds/google_merchant.pyindex 091b6422a3..52965ae43a 100644--- a/saleor/data_feeds/google_merchant.py+++ b/saleor/data_feeds/google_merchant.py@@ -106,7 +106,7 @@ def item_tax(item, discounts):Read more:https://support.google.com/merchants/answer/6324454"""- price = item.get_price_per_item(discounts=discounts)+ price = item.get_price(discounts=discounts)return 'US::%s:y' % price.tax@@ -146,12 +146,12 @@ def item_google_product_category(item, category_paths):def item_price(item):- price = item.get_price_per_item(discounts=None)+ price = item.get_price(discounts=None)return '%s %s' % (price.gross.amount, price.currency)def item_sale_price(item, discounts):- sale_price = item.get_price_per_item(discounts=discounts)+ sale_price = item.get_price(discounts=discounts)return '%s %s' % (sale_price.gross.amount, sale_price.currency)diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 46e057878e..bd31b05339 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -5,11 +5,12 @@ from django.db.models import Ffrom django.shortcuts import get_object_or_404, redirectfrom prices import Money-from saleor.order.models import OrderLinefrom ..account.utils import store_user_addressfrom ..core.exceptions import InsufficientStock+from ..core.utils import DEFAULT_TAX_RATE_NAMEfrom ..dashboard.order.utils import get_voucher_discount_for_orderfrom ..order import FulfillmentStatus, OrderStatus+from ..order.models import OrderLinefrom ..product.utils import allocate_stock, deallocate_stock, increase_stock@@ -137,7 +138,7 @@ def get_variant_tax_rate(variant, taxes=None):if rate_name in taxes:tax = taxes[rate_name]else:- tax = taxes['standard']+ tax = taxes[DEFAULT_TAX_RATE_NAME]return tax['value']@@ -161,7 +162,7 @@ def add_variant_to_order(returnif quantity_not_fulfilled > variant.quantity_available:raise InsufficientStock(variant)- price = variant.get_price_per_item(discounts, taxes=taxes)+ price = variant.get_price(discounts, taxes=taxes)order.lines.create(product_name=variant.display_product(),product_sku=variant.sku,diff --git a/saleor/product/forms.py b/saleor/product/forms.pyindex 5f9faf43ba..2cebd77d04 100644--- a/saleor/product/forms.py+++ b/saleor/product/forms.py@@ -18,7 +18,7 @@ class VariantChoiceField(forms.ModelChoiceField):'%(variant_label)s - %(price)s') % {'variant_label': variant_label,'price': amount(- obj.get_price_per_item(discounts=self.discounts).gross)}+ obj.get_price(discounts=self.discounts).gross)}return labeldef update_field_data(self, variants, cart):diff --git a/saleor/product/models.py b/saleor/product/models.pyindex 55c585dd1c..a1814edec4 100644--- a/saleor/product/models.py+++ b/saleor/product/models.py@@ -13,12 +13,12 @@ from django.utils.translation import pgettext_lazyfrom django_prices.models import MoneyFieldfrom mptt.managers import TreeManagerfrom mptt.models import MPTTModel-from prices import Money, TaxedMoney, TaxedMoneyRange+from prices import TaxedMoney, TaxedMoneyRangefrom text_unidecode import unidecodefrom versatileimagefield.fields import PPOIField, VersatileImageFieldfrom ..core.exceptions import InsufficientStock-from ..core.utils import apply_tax_to_price+from ..core.utils import apply_tax_to_price, DEFAULT_TAX_RATE_NAMEfrom ..discount.utils import calculate_discounted_pricefrom ..seo.models import SeoModel@@ -69,7 +69,8 @@ class ProductType(models.Model):variant_attributes = models.ManyToManyField('ProductAttribute', related_name='product_variant_types', blank=True)is_shipping_required = models.BooleanField(default=False)- tax_rate = models.CharField(max_length=128, default='standard', blank=True)+ tax_rate = models.CharField(+ max_length=128, default=DEFAULT_TAX_RATE_NAME, blank=True)class Meta:app_label = 'product'@@ -157,20 +158,10 @@ class Product(SeoModel):first_image = self.images.first()return first_image.image if first_image else None- def get_attribute(self, pk):- return self.attributes.get(smart_text(pk))-- def set_attribute(self, pk, value_pk):- self.attributes[smart_text(pk)] = smart_text(value_pk)-- def get_price_per_item(self, item, discounts=None, taxes=None):- return item.get_price_per_item(discounts, taxes)-def get_price_range(self, discounts=None, taxes=None):if self.variants.exists():prices = [- self.get_price_per_item(- variant, discounts=discounts, taxes=taxes)+ variant.get_price(discounts=discounts, taxes=taxes)for variant in self]return TaxedMoneyRange(min(prices), max(prices))price = calculate_discounted_price(self, self.price, discounts)@@ -217,7 +208,7 @@ class ProductVariant(models.Model):if quantity > self.quantity_available:raise InsufficientStock(self)- def get_price_per_item(self, discounts=None, taxes=None):+ def get_price(self, discounts=None, taxes=None):price = self.price_override or self.product.priceprice = calculate_discounted_price(self.product, price, discounts)if not self.product.charge_taxes:@@ -237,7 +228,7 @@ class ProductVariant(models.Model):'product_name': str(self),'product_id': self.product.pk,'variant_id': self.pk,- 'unit_price': str(self.get_price_per_item().gross)}+ 'unit_price': str(self.get_price().gross)}def is_shipping_required(self):return self.product.product_type.is_shipping_requireddiff --git a/saleor/product/templatetags/discount.py b/saleor/product/templatetags/discount.pydeleted file mode 100644index 4773d7201c..0000000000--- a/saleor/product/templatetags/discount.py+++ /dev/null@@ -1,18 +0,0 @@-from django import template--register = template.Library()---def discounted_price(item, discounts):- return item.get_price_per_item(discounts=discounts)---def discounted_price_range(item, discounts):- return item.get_price_range(discounts=discounts)---def price_difference(price1, price2):- return price1 - price2diff --git a/saleor/product/utils/costs.py b/saleor/product/utils/costs.pyindex 50edcdbf2c..6644029ea9 100644--- a/saleor/product/utils/costs.py+++ b/saleor/product/utils/costs.py@@ -59,7 +59,7 @@ def get_margin_for_variant(variant):variant_cost = variant.get_total()if variant_cost is None:return None- price = variant.get_price_per_item()+ price = variant.get_price()margin = price - variant_costpercent = round((margin.gross / price.gross) * 100, 0)return percentdiff --git a/saleor/product/utils/variants_picker.py b/saleor/product/utils/variants_picker.pyindex 134ccf33a3..6c680cd941 100644--- a/saleor/product/utils/variants_picker.py+++ b/saleor/product/utils/variants_picker.py@@ -20,8 +20,8 @@ def get_variant_picker_data(filter_available_variants = defaultdict(list)for variant in variants:- price = variant.get_price_per_item(discounts, taxes)- price_undiscounted = variant.get_price_per_item(taxes=taxes)+ price = variant.get_price(discounts, taxes)+ price_undiscounted = variant.get_price(taxes=taxes)if local_currency:price_local_currency = to_local_currency(price, local_currency)else:diff --git a/saleor/seo/schema/product.py b/saleor/seo/schema/product.pyindex 18c4106c27..18cf4369ec 100644--- a/saleor/seo/schema/product.py+++ b/saleor/seo/schema/product.py@@ -30,7 +30,7 @@ def product_json_ld(product, attributes=None):'offers': []}for variant in product.variants.all():- price = variant.get_price_per_item()+ price = variant.get_price()in_stock = Trueif not product.is_available() or not variant.is_in_stock():in_stock = Falsediff --git a/saleor/settings.py b/saleor/settings.pyindex be529129b7..8160cf5d26 100644--- a/saleor/settings.py+++ b/saleor/settings.py@@ -272,7 +272,7 @@ AUTH_USER_MODEL = 'account.User'LOGIN_URL = '/account/login/'-DEFAULT_COUNTRY = 'US' # fixme: revert back to US before merge+DEFAULT_COUNTRY = 'US'DEFAULT_CURRENCY = 'USD'DEFAULT_DECIMAL_PLACES = get_currency_fraction(DEFAULT_CURRENCY)AVAILABLE_CURRENCIES = [DEFAULT_CURRENCY]diff --git a/saleor/shipping/utils.py b/saleor/shipping/utils.pyindex 8cbb4d38bc..a36c8e4404 100644--- a/saleor/shipping/utils.py+++ b/saleor/shipping/utils.py@@ -1,7 +1,7 @@from django.contrib.sites.models import Sitefrom prices import MoneyRange-from ..core.utils import apply_tax_to_price+from ..core.utils import apply_tax_to_price, DEFAULT_TAX_RATE_NAMEdef get_taxed_shipping_price(price, taxes=None):@@ -10,7 +10,7 @@ def get_taxed_shipping_price(price, taxes=None):Site.objects.get_current().settings.charge_taxes_on_shipping)if not charge_taxes:taxes = None- return apply_tax_to_price(taxes, 'standard', price)+ return apply_tax_to_price(taxes, DEFAULT_TAX_RATE_NAME, price)def get_shipment_options(country_code, taxes=None):diff --git a/templates/cart_dropdown.html b/templates/cart_dropdown.htmlindex dc0c249324..c97b300561 100644--- a/templates/cart_dropdown.html+++ b/templates/cart_dropdown.html@@ -20,7 +20,7 @@<h3>{{ line.product }}<span>x{{ line.quantity }}</span>- <p>{{ line.variant }}</p>+ <p>{{ line.variant }} ({{ line.line_total.net|amount:'html' }} + {{ line.line_total.tax|amount:'html' }} VAT)</p></h3></a></div>@@ -50,6 +50,11 @@</p></h3></div>+ <div class="col-md-12">+ <div class="small text-right">+ ({{ total.net|amount:'html' }} + {{ total.tax|amount:'html' }} VAT)+ </div>+ </div></div><div class="row cart-dropdown__actions"><div class="col-md-1"></div>diff --git a/templates/checkout/details.html b/templates/checkout/details.htmlindex 030fe424eb..473c2cee16 100644--- a/templates/checkout/details.html+++ b/templates/checkout/details.html@@ -117,6 +117,11 @@<div class="col-4"><h3 class="float-right">{{ total.gross|amount:'html' }}</h3></div>+ <div class="col-md-12">+ <p class="small text-right">+ ({{ total.net|amount:'html' }} + {{ total.tax|amount:'html' }} VAT)+ </p>+ </div></div>{% endwith %}</div>diff --git a/templates/dashboard/product/detail.html b/templates/dashboard/product/detail.htmlindex 37deaa0347..abec90e0da 100644--- a/templates/dashboard/product/detail.html+++ b/templates/dashboard/product/detail.html@@ -116,7 +116,7 @@<td>{{ variant.sku }}</td><td>{{ variant }}</td><td class="wide">{% render_variant_availability_status variant %}</td>- <td class="right-align">{{ variant.get_price_per_item.gross|amount }}</td>+ <td class="right-align">{{ variant.get_price.gross|amount }}</td><td class="right-align">{% margins_for_variant variant=variant as margins %}{% if margins|length > 1 %}diff --git a/templates/dashboard/product/product_variant/detail.html b/templates/dashboard/product/product_variant/detail.htmlindex 41dc887f82..c285109e11 100644--- a/templates/dashboard/product/product_variant/detail.html+++ b/templates/dashboard/product/product_variant/detail.html@@ -53,7 +53,7 @@<p class="text-padding">{% trans "Sale price" context "Dashboard variant details view" %}</p></div><div class="col s7 m6">- <p class="text-padding right-align">{{ variant.get_price_per_item.gross|amount }}</p>+ <p class="text-padding right-align">{{ variant.get_price.gross|amount }}</p></div><div class="col s5 m6"><p class="text-padding">{% trans "Cost price" context "Dashboard variant details view" %}</p>diff --git a/tests/test_cart.py b/tests/test_cart.pyindex f6106f3da7..fff91c5dfc 100644--- a/tests/test_cart.py+++ b/tests/test_cart.py@@ -356,12 +356,9 @@ def test_cart_counter(monkeypatch):def test_get_product_variants_and_prices():- variant = Mock(product_id=1, id=1)+ variant = Mock(product_id=1, id=1, get_price=Mock(return_value=10))cart = MagicMock(spec=Cart)- cart.lines.all.return_value = [- Mock(- quantity=1, variant=variant,- get_price_per_item=Mock(return_value=10))]+ cart.lines.all.return_value = [Mock(quantity=1, variant=variant)]variants = list(utils.get_product_variants_and_prices(cart, variant))assert variants == [(variant, 10)]diff --git a/tests/test_discounts.py b/tests/test_discounts.pyindex e07141ff95..ae90b3b822 100644--- a/tests/test_discounts.py+++ b/tests/test_discounts.py@@ -44,7 +44,7 @@ def test_variant_discounts(product):type=DiscountValueType.FIXED,value=50)high_discount.products.add(product)- final_price = variant.get_price_per_item(discounts=Sale.objects.all())+ final_price = variant.get_price(discounts=Sale.objects.all())assert final_price.gross == Money(0, 'USD')@@ -56,7 +56,7 @@ def test_percentage_discounts(product):type=DiscountValueType.PERCENTAGE,value=50)discount.products.add(product)- final_price = variant.get_price_per_item(discounts=[discount])+ final_price = variant.get_price(discounts=[discount])assert final_price.gross == Money(5, 'USD')diff --git a/tests/test_product.py b/tests/test_product.pyindex af5e332803..4a7d7bf510 100644--- a/tests/test_product.py+++ b/tests/test_product.py@@ -438,7 +438,7 @@ def test_create_product_thumbnails(('10.00', True, True, False, '8.13', '10.00'),('15.00', True, False, True, '10.00', '10.00'),('15.00', True, True, True, '8.13', '10.00')])-def test_get_price_per_item(+def test_get_price(product_type, default_category, taxes, sale, product_price,include_taxes_in_prices, include_taxes, include_discounts,product_net, product_gross, site_settings):@@ -450,7 +450,7 @@ def test_get_price_per_item(price=Money(product_price, 'USD'))variant = product.variants.create()- price = variant.get_price_per_item(+ price = variant.get_price(taxes=taxes if include_taxes else None,discounts=Sale.objects.all() if include_discounts else None)@@ -458,7 +458,7 @@ def test_get_price_per_item(net=Money(product_net, 'USD'), gross=Money(product_gross, 'USD'))-def test_product_get_price_per_item_variant_has_no_price(+def test_product_get_price_variant_has_no_price(product_type, default_category, taxes, site_settings):site_settings.include_taxes_in_prices = Falsesite_settings.save()@@ -468,13 +468,13 @@ def test_product_get_price_per_item_variant_has_no_price(price=Money('10.00', 'USD'))variant = product.variants.create()- price = variant.get_price_per_item(taxes=taxes)+ price = variant.get_price(taxes=taxes)assert price == TaxedMoney(net=Money('10.00', 'USD'), gross=Money('12.30', 'USD'))-def test_product_get_price_per_item_variant_with_price(+def test_product_get_price_variant_with_price(product_type, default_category, taxes, site_settings):site_settings.include_taxes_in_prices = Falsesite_settings.save()@@ -484,7 +484,7 @@ def test_product_get_price_per_item_variant_with_price(price=Money('10.00', 'USD'))variant = product.variants.create(price_override=Money('20.00', 'USD'))- price = variant.get_price_per_item(taxes=taxes)+ price = variant.get_price(taxes=taxes)assert price == TaxedMoney(net=Money('20.00', 'USD'), gross=Money('24.60', 'USD'))@@ -527,7 +527,7 @@ def test_product_get_price_range_no_variants(assert price == TaxedMoneyRange(start=expected_price, stop=expected_price)-def test_product_get_price_per_item_do_not_charge_taxes(+def test_product_get_price_do_not_charge_taxes(product_type, default_category, taxes, sale):product = models.Product.objects.create(product_type=product_type,@@ -536,8 +536,7 @@ def test_product_get_price_per_item_do_not_charge_taxes(charge_taxes=False)variant = product.variants.create()- price = variant.get_price_per_item(- taxes=taxes, discounts=Sale.objects.all())+ price = variant.get_price(taxes=taxes, discounts=Sale.objects.all())assert price == TaxedMoney(net=Money('5.00', 'USD'), gross=Money('5.00', 'USD'))