Skip to content

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

Make is_in_stock methd a cached property

Marcin Gębalaacc274daf81d
diff --git a/saleor/product/models/base.py b/saleor/product/models/base.py
index 2f9be2584a..567ea35247 100644
--- a/saleor/product/models/base.py
+++ b/saleor/product/models/base.py
@@ -8,6 +8,7 @@ from django.core.validators import MinValueValidator, RegexValidator
from django.db.models import Manager, Q
from django.utils.encoding import python_2_unicode_compatible, smart_text
from django.db import models
+from django.utils.functional import cached_property
from django.utils.text import slugify
from django.utils.translation import pgettext_lazy
from django_prices.models import PriceField
@@ -133,6 +134,7 @@ class Product(models.Model, ItemRange):
admin_get_price_max.short_description = pgettext_lazy(
'Product admin page', 'Maximum price')
+ @cached_property
def is_in_stock(self):
return any(variant.is_in_stock() for variant in self)
diff --git a/saleor/product/templates/product/details.html b/saleor/product/templates/product/details.html
index 4efdba300a..0ccb2a99bd 100644
--- a/saleor/product/templates/product/details.html
+++ b/saleor/product/templates/product/details.html
@@ -23,50 +23,48 @@
{{ product }}
</h1>
- {% with in_stock=product.is_in_stock %}
- {% if in_stock %}
- {% with price_range=product|discounted_price_range:request.discounts undiscounted=product.get_price_range %}
- {% with discount=undiscounted.min_price|price_difference:price_range.min_price %}
- <p class="lead" itemprop="offers" itemscope itemtype="http://schema.org/Offer"
- {% if undiscounted.min_price != price_range.min_price %}
- <span class="price undiscounted">{% price_range undiscounted %}</span>
- <br>
- <span class="price">{% price_range price_range %}</span>
- <br>
- <small class="label label-success">{% trans "You save" %} {% gross discount %}</small>
- {% else %}
- <span class="price">{% price_range price_range %}</span>
- {% endif %}
- </p>
- {% endwith %}
+ {% if product.is_in_stock %}
+ {% with price_range=product|discounted_price_range:request.discounts undiscounted=product.get_price_range %}
+ {% with discount=undiscounted.min_price|price_difference:price_range.min_price %}
+ <p class="lead" itemprop="offers" itemscope itemtype="http://schema.org/Offer"
+ {% if undiscounted.min_price != price_range.min_price %}
+ <span class="price undiscounted">{% price_range undiscounted %}</span>
+ <br>
+ <span class="price">{% price_range price_range %}</span>
+ <br>
+ <small class="label label-success">{% trans "You save" %} {% gross discount %}</small>
+ {% else %}
+ <span class="price">{% price_range price_range %}</span>
+ {% endif %}
+ </p>
{% endwith %}
- {% endif %}
+ {% endwith %}
+ {% endif %}
- <hr>
+ <hr>
- <div class="visible-xs">
- <img class="img-responsive" src="{{ product.images.first.image.thumbnail.750x750 }}" alt="">
+ <div class="visible-xs">
+ <img class="img-responsive" src="{{ product.images.first.image.thumbnail.750x750 }}" alt="">
- <hr>
- </div>
+ <hr>
+ </div>
- {% if in_stock %}
- {% block orderform %}
- <form role="form" class="product-form form-vertical" method="post" action=".">
- {% csrf_token %}
- {{ form|as_vertical_form }}
+ {% if product.is_in_stock %}
+ {% block orderform %}
+ <form role="form" class="product-form form-vertical" method="post" action=".">
+ {% csrf_token %}
+ {{ form|as_vertical_form }}
- <div class="form-group">
- <button class="btn btn-lg btn-block btn-primary">
- {% trans "Add to cart" %}
- </button>
- </div>
- </form>
- {% endblock %}
- {% else %}
- <p class="alert alert-warning">{% trans "This product is currently <strong>unavailable</strong>." %}</p>
- {% endif %}
- {% endwith %}
+ <div class="form-group">
+ <button class="btn btn-lg btn-block btn-primary">
+ {% trans "Add to cart" %}
+ </button>
+ </div>
+ </form>
+ {% endblock %}
+ {% else %}
+ <p class="alert alert-warning">{% trans "This product is currently <strong>unavailable</strong>." %}</p>
+ {% endif %}
<hr>