Permalink
Browse files

Merge pull request #163 from elbaschid/master

#56148: reduced SQL queries for products page / #56029: edit link on product page
  • Loading branch information...
2 parents 1ccbec2 + 4c212b2 commit cebeb5a781acd24492f8944398140a0b9bc956de @codeinthehole codeinthehole committed May 2, 2012
@@ -28,7 +28,7 @@ class BasketView(ModelFormSetView):
template_name='basket/basket.html'
def get_queryset(self):
- return self.request.basket.lines.all()
+ return self.request.basket.all_lines()
def get_default_shipping_method(self, basket):
return Repository().get_default_shipping_method(self.request.user, self.request.basket)
@@ -69,7 +69,7 @@ def get_context_data(self, **kwargs):
if self.request.user.is_authenticated():
try:
saved_basket = self.basket_model.saved.get(owner=self.request.user)
- saved_queryset = saved_basket.lines.all().select_related('product', 'product__stockrecord')
+ saved_queryset = saved_basket.all_lines().select_related('product', 'product__stockrecord')
SavedFormset = modelformset_factory(self.model, form=SavedLineForm, extra=0, can_delete=True)
formset = SavedFormset(queryset=saved_queryset)
context['saved_formset'] = formset
@@ -250,7 +250,7 @@ def get(self, request, *args, **kwargs):
def get_queryset(self):
try:
saved_basket = self.basket_model.saved.get(owner=self.request.user)
- return saved_basket.lines.all().select_related('product', 'product__stockrecord')
+ return saved_basket.all_lines().select_related('product', 'product__stockrecord')
except self.basket_model.DoesNotExist:
return []
@@ -228,7 +228,10 @@ def is_top_level(self):
@property
def is_group(self):
u"""Return True if this is a top level product and has more than 0 variants"""
- return self.is_top_level and self.variants.count() > 0
+ # use len() instead of count() in this specific instance
+ # as variants are highly likely to be used after this
+ # which reduces the amount of SQL queries required
+ return self.is_top_level and len(self.variants.all()) > 0
@property
def is_variant(self):
@@ -14,39 +14,39 @@ class ProductDetailView(DetailView):
view_signal = product_viewed
template_folder = "catalogue"
_product = None
-
+
def get_object(self):
if not self._product:
self._product = super(ProductDetailView, self).get_object()
return self._product
-
+
def get(self, request, **kwargs):
"""
Ensure that the correct URL is used
"""
product = self.get_object()
- correct_path = product.get_absolute_url()
+ correct_path = product.get_absolute_url()
if correct_path != request.path:
return HttpResponsePermanentRedirect(correct_path)
response = super(ProductDetailView, self).get(request, **kwargs)
-
+
# Send signal to record the view of this product
self.view_signal.send(sender=self, product=product, user=request.user, request=request, response=response)
return response;
def get_template_names(self):
"""
Return a list of possible templates.
-
+
We try 2 options before defaulting to catalogue/detail.html:
1). detail-for-upc-<upc>.html
2). detail-for-class-<classname>.html
-
+
This allows alternative templates to be provided for a per-product
and a per-item-class basis.
- """
+ """
product = self.get_object()
- names = ['%s/detail-for-upc-%s.html' % (self.template_folder, product.upc),
+ names = ['%s/detail-for-upc-%s.html' % (self.template_folder, product.upc),
'%s/detail-for-class-%s.html' % (self.template_folder, product.product_class.name.lower()),
'%s/detail.html' % (self.template_folder)]
return names
@@ -59,7 +59,7 @@ class ProductCategoryView(ListView):
context_object_name = "products"
template_name = 'catalogue/browse.html'
paginate_by = 20
-
+
def get_categories(self):
slug = self.kwargs['category_slug']
try:
@@ -69,19 +69,21 @@ def get_categories(self):
categories = list(category.get_descendants())
categories.append(category)
return categories
-
+
def get_context_data(self, **kwargs):
context = super(ProductCategoryView, self).get_context_data(**kwargs)
categories = self.get_categories()
context['categories'] = categories
context['category'] = categories[-1]
context['summary'] = categories[-1].name
- return context
+ return context
def get_queryset(self):
return product_model.browsable.filter(categories__in=self.get_categories()).distinct()
+from django.views.decorators.cache import cache_page
+from django.utils.decorators import method_decorator
class ProductListView(ListView):
"""
@@ -98,17 +100,27 @@ def get_search_query(self):
return q.strip() if q else q
def get_base_queryset(self):
- return self.model.browsable.all()
+ return self.model.browsable.select_related(
+ 'product_class',
+ 'stockrecord',
+ 'stockrecord__partner',
+ ).prefetch_related(
+ 'reviews',
+ 'variants',
+ 'product_options',
+ 'product_class__options',
+ ).all()
def get_queryset(self):
q = self.get_search_query()
if q:
# Send signal to record the view of this product
self.search_signal.send(sender=self, query=q, user=self.request.user)
+ base_queryset = self.get_base_queryset()
return self.get_base_queryset().filter(title__icontains=q)
else:
- return self.get_base_queryset().select_related().all()
-
+ return self.get_base_queryset()
+
def get_context_data(self, **kwargs):
context = super(ProductListView, self).get_context_data(**kwargs)
q = self.get_search_query()
@@ -3,8 +3,8 @@
{% load basket_tags %}
<ul class="basket_item">
-{% if request.basket.lines.count %}
- {% for line in basket.lines.all %}
+{% if request.basket.num_lines %}
+ {% for line in basket.all_lines %}
<li>
<article>
<h4><a href="{{ line.product.get_absolute_url }}">{{ line.description }}</a></h4>
@@ -30,6 +30,11 @@
{% block content %}
<article class="product_page"><!-- Start of product page -->
<div class="row-fluid">
+ <div class="well">
+ <a href="{% url dashboard:catalogue-product product.id %}"><button class="btn btn-success" value='Edit this product'>Edit this product</button></a>
+ </div>
+ </div>
+ <div class="row-fluid">
<div class="span6">
{% include "catalogue/partials/gallery.html" %}
</div><!-- /span6 -->
@@ -72,7 +72,7 @@ <h4 class="span1">Quantity</h4>
<h4 class="span1">Price</h4>
</div>
</div>
- {% for line in basket.lines.all %}
+ {% for line in basket.all_lines %}
<div class="basket-items">
<div class="row-fluid">
<div class="span10">
@@ -23,16 +23,15 @@ class ProductImageNode(template.Node):
def __init__(self, product_var, context_key):
self.product_var = template.Variable(product_var)
self.context_key = context_key
-
+
def render(self, context):
try:
product = self.product_var.resolve(context)
except template.VariableDoesNotExist:
return ''
- images = product.images.all().order_by('display_order')
- if images.count():
- image = images[0]
- else:
+ try:
+ image = product.images.all().order_by('display_order')[0]
+ except IndexError:
image = {'thumbnail_url': settings.OSCAR_MISSING_IMAGE_URL}
context[self.context_key] = image
return ''

0 comments on commit cebeb5a

Please sign in to comment.