Reduce number of redundant SQL queries

This commit is contained in:
Raphael Michel
2017-10-18 09:53:57 +02:00
parent 64b67e5396
commit c6a7b52e34
3 changed files with 30 additions and 25 deletions

View File

@@ -91,14 +91,16 @@ class BaseCheckoutFlowStep:
@cached_property
def invoice_address(self):
iapk = self.cart_session.get('invoice_address')
if not iapk:
return InvoiceAddress()
try:
return InvoiceAddress.objects.get(pk=iapk, order__isnull=True)
except InvoiceAddress.DoesNotExist:
return InvoiceAddress()
if not hasattr(self.request, '_checkout_flow_invoice_address'):
iapk = self.cart_session.get('invoice_address')
if not iapk:
self.request._checkout_flow_invoice_address = InvoiceAddress()
else:
try:
self.request._checkout_flow_invoice_address = InvoiceAddress.objects.get(pk=iapk, order__isnull=True)
except InvoiceAddress.DoesNotExist:
self.request._checkout_flow_invoice_address = InvoiceAddress()
return self.request._checkout_flow_invoice_address
def get_checkout_flow(event):
@@ -160,7 +162,9 @@ class AddOnsStep(CartMixin, AsyncAction, TemplateFlowStep):
requires_valid_cart = False
def is_applicable(self, request):
return get_cart(request).filter(item__addons__isnull=False).exists()
if not hasattr(request, '_checkoutflow_addons_applicable'):
request._checkoutflow_addons_applicable = get_cart(request).filter(item__addons__isnull=False).exists()
return request._checkoutflow_addons_applicable
def is_completed(self, request, warn=False):
for cartpos in get_cart(request).filter(addon_to__isnull=True).prefetch_related(

View File

@@ -24,6 +24,19 @@ class CartMixin:
from pretix.presale.views.cart import cart_session
return cart_session(self.request)
@cached_property
def invoice_address(self):
if not hasattr(self.request, '_checkout_flow_invoice_address'):
iapk = self.cart_session.get('invoice_address')
if not iapk:
self.request._checkout_flow_invoice_address = InvoiceAddress()
else:
try:
self.request._checkout_flow_invoice_address = InvoiceAddress.objects.get(pk=iapk, order__isnull=True)
except InvoiceAddress.DoesNotExist:
self.request._checkout_flow_invoice_address = InvoiceAddress()
return self.request._checkout_flow_invoice_address
def get_cart(self, answers=False, queryset=None, order=None, downloads=False):
if queryset:
prefetch = []
@@ -107,15 +120,7 @@ class CartMixin:
if order:
fees = order.fees.all()
else:
iapk = self.cart_session.get('invoice_address')
ia = None
if iapk:
try:
ia = InvoiceAddress.objects.get(pk=iapk, order__isnull=True)
except InvoiceAddress.DoesNotExist:
pass
fees = get_fees(self.request.event, self.request, total, ia, self.cart_session.get('payment'))
fees = get_fees(self.request.event, self.request, total, self.invoice_address, self.cart_session.get('payment'))
total += sum([f.value for f in fees])
net_total += sum([f.net_value for f in fees])

View File

@@ -4,23 +4,19 @@ from django.shortcuts import redirect
from django.utils.translation import ugettext_lazy as _
from django.views.generic import View
from pretix.base.models import CartPosition
from pretix.base.services.cart import CartError
from pretix.base.signals import validate_cart
from pretix.multidomain.urlreverse import eventreverse
from pretix.presale.checkoutflow import get_checkout_flow
from pretix.presale.views.cart import get_or_create_cart_id
from pretix.presale.views import get_cart
class CheckoutView(View):
def dispatch(self, request, *args, **kwargs):
self.request = request
cart_pos = CartPosition.objects.filter(
cart_id=get_or_create_cart_id(request), event=self.request.event
)
if not cart_pos.exists() and "async_id" not in request.GET:
if not get_cart(request) and "async_id" not in request.GET:
messages.error(request, _("Your cart is empty"))
return redirect(eventreverse(self.request.event, 'presale:event.index'))
@@ -30,7 +26,7 @@ class CheckoutView(View):
cart_error = None
try:
validate_cart.send(sender=self.request.event, positions=cart_pos)
validate_cart.send(sender=self.request.event, positions=get_cart(request))
except CartError as e:
cart_error = e