Skip to content

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

Improve checkout storage, remove groups

Michał Ociepka8992c94ac594
diff --git a/saleor/checkout/__init__.py b/saleor/checkout/__init__.py
index 01b0a9dc4c..e560af3551 100644
--- a/saleor/checkout/__init__.py
+++ b/saleor/checkout/__init__.py
@@ -1,4 +1,4 @@
-from collections import defaultdict, namedtuple
+from collections import defaultdict
from satchless.process import ProcessManager
@@ -11,14 +11,14 @@ from ..order.models import Order
STORAGE_SESSION_KEY = 'checkout_storage'
-class CheckoutStorage(dict):
+class CheckoutStorage(defaultdict):
modified = False
def __init__(self, *args, **kwargs):
- super(CheckoutStorage, self).__init__(*args, **kwargs)
- self.update({'anonymous_user_email': '', 'billing_address': None,
- 'groups': defaultdict(dict), 'summary': False})
+ super(CheckoutStorage, self).__init__(dict, * args, **kwargs)
+ if not 'billing' in self:
+ self['billing'] = {'address': None, 'anonymous_user_email': None}
class Checkout(ProcessManager):
@@ -30,64 +30,68 @@ class Checkout(ProcessManager):
def __init__(self, request):
self.request = request
+ self.groups = []
+ self.steps = []
+ self.items = []
try:
- self.storage = request.session[STORAGE_SESSION_KEY]
+ self.storage = CheckoutStorage(
+ request.session[STORAGE_SESSION_KEY])
except KeyError:
self.storage = CheckoutStorage()
- self.generate_groups(request.cart)
+ self.generate_steps(request.cart)
- def generate_groups(self, cart):
+ def generate_steps(self, cart):
self.items = CartPartitioner(cart)
- self.groups = []
- self.billing = BillingAddressStep(self, self.request)
- self.steps = [self.billing]
+ self.billing = BillingAddressStep(
+ self, self.request, self.storage['billing'])
+ self.steps.append(self.billing)
for index, delivery_group in enumerate(self.items):
if isinstance(delivery_group, DigitalGroup):
step_class = DigitalDeliveryStep
else:
step_class = ShippingStep
- step = step_class(self, self.request,
+ storage_key = 'shipping_%s' % (index,)
+ step = step_class(self, self.request, self.storage[storage_key],
delivery_group, index)
- group = step.get_delivery_group()
- self.groups.append(group)
self.steps.append(step)
- self.steps.append(SummaryStep(self, self.request))
+ summary_step = SummaryStep(self, self.request, self.storage['summary'])
+ self.steps.append(summary_step)
@property
def anonymous_user_email(self):
- return self.storage['anonymous_user_email']
+ return self.storage['billing']['anonymous_user_email']
@anonymous_user_email.setter
def anonymous_user_email(self, email):
- self.storage['anonymous_user_email'] = email
+ self.storage['billing']['anonymous_user_email'] = email
@anonymous_user_email.deleter
def anonymous_user_email(self, email):
- self.storage['anonymous_user_email'] = ''
+ self.storage['billing']['anonymous_user_email'] = ''
@property
def billing_address(self):
- return self.storage['billing_address']
+ return self.storage['billing']['address']
@billing_address.setter
def billing_address(self, address):
- self.storage['billing_address'] = address
+ self.storage['billing']['address'] = address
@billing_address.deleter
def billing_address(self, address):
- self.storage['billing_address'] = None
+ self.storage['billing']['address'] = None
- def get_group(self, name):
- return self.storage['groups'][name]
+ def get_step_data(self, name):
+ return self.storage[name]
- def set_group(self, name, group):
- self['groups'][name] = group
+ def set_step_data(self, name, group):
+ self[name] = group
def get_total(self, **kwargs):
return self.items.get_total(**kwargs)
def save(self):
- self.request.session[STORAGE_SESSION_KEY] = self.storage
+ self.request.session[STORAGE_SESSION_KEY] = dict(self.storage)
def clear_storage(self):
del self.request.session[STORAGE_SESSION_KEY]
diff --git a/saleor/checkout/templates/checkout/summary.html b/saleor/checkout/templates/checkout/summary.html
index 2d03fc72a3..c3d6ac66bc 100644
--- a/saleor/checkout/templates/checkout/summary.html
+++ b/saleor/checkout/templates/checkout/summary.html
@@ -35,56 +35,58 @@
</div>
</div>
- {% for group in checkout.groups %}
- <div class="panel panel-default">
- <div class="panel-heading">
- {% blocktrans with number=forloop.counter %}Delivery group #{{ number }}{% endblocktrans %}
- </div>
- <div class="panel-body">
- <table class="table table-striped">
- <thead>
- <tr>
- <th class="col-sm-4">{% trans "Name" %}</th>
- <th class="col-sm-2">{% trans "Price" %}</th>
- <th class="col-sm-2">{% trans "Subtotal" %}</th>
- </tr>
- </thead>
- <tfoot>
- <tr>
- <th class="col-sm-4"></th>
- <th class="col-sm-2">{% trans "Total" %}</th>
- <th class="col-sm-2">{% gross group.get_total_with_delivery %}</th>
- </tr>
- </tfoot>
- <tbody>
- {% for item in group %}
+ {% for step in checkout %}
+ {% if step.group %}
+ <div class="panel panel-default">
+ <div class="panel-heading">
+ {% blocktrans with number=forloop.counter %}Delivery group #{{ number }}{% endblocktrans %}
+ </div>
+ <div class="panel-body">
+ <table class="table table-striped">
+ <thead>
<tr>
- <td class="col-sm-4">{{ item.product }}{% if item.quantity != 1 %} <em>×{{ item.quantity }}</em>{% endif %}</td>
- <td class="col-sm-2">{% gross item.get_price_per_item %}</td>
- <td class="col-sm-2">{% gross item.get_total %}</td>
+ <th class="col-sm-4">{% trans "Name" %}</th>
+ <th class="col-sm-2">{% trans "Price" %}</th>
+ <th class="col-sm-2">{% trans "Subtotal" %}</th>
</tr>
- {% endfor %}
- {% if group.get_delivery_total.gross %}
+ </thead>
+ <tfoot>
<tr>
- <td class="col-sm-4">
- {% trans "Shipping" %} ({{ group }})
- </td>
- <td class="col-sm-2">{% gross group.get_delivery_total %}</td>
- <td class="col-sm-2">{% gross group.get_delivery_total %}</td>
+ <th class="col-sm-4"></th>
+ <th class="col-sm-2">{% trans "Total" %}</th>
+ <th class="col-sm-2">{% gross step.group.get_total_with_delivery %}</th>
</tr>
- {% endif %}
- </tbody>
- </table>
- {% if group.email %}
- <p>{% trans "Will be sent to" %} {{ group.email }}</p>
- {% elif group.address %}
- <p>{% trans "Will be shipped to" %} {% include "userprofile/snippets/address-short.html" with address=group.address %}</p>
- {% else %}
- <p class="text-danger">{% trans "No address given yet." %}</p>
- {% endif %}
- <p><a href="{{ group.step.get_absolute_url }}">{% trans "Edit delivery details" %}</a></p>
+ </tfoot>
+ <tbody>
+ {% for item in step.group %}
+ <tr>
+ <td class="col-sm-4">{{ item.product }}{% if item.quantity != 1 %} <em>×{{ item.quantity }}</em>{% endif %}</td>
+ <td class="col-sm-2">{% gross item.get_price_per_item %}</td>
+ <td class="col-sm-2">{% gross item.get_total %}</td>
+ </tr>
+ {% endfor %}
+ {% if step.group.get_delivery_total.gross %}
+ <tr>
+ <td class="col-sm-4">
+ {% trans "Shipping" %} ({{ step.group }})
+ </td>
+ <td class="col-sm-2">{% gross step.group.get_delivery_total %}</td>
+ <td class="col-sm-2">{% gross step.group.get_delivery_total %}</td>
+ </tr>
+ {% endif %}
+ </tbody>
+ </table>
+ {% if step.group.email %}
+ <p>{% trans "Will be sent to" %} {{ step.group.email }}</p>
+ {% elif step.group.address %}
+ <p>{% trans "Will be shipped to" %} {% include "userprofile/snippets/address-short.html" with address=step.group.address %}</p>
+ {% else %}
+ <p class="text-danger">{% trans "No address given yet." %}</p>
+ {% endif %}
+ <p><a href="{{ step.group.step.get_absolute_url }}">{% trans "Edit delivery details" %}</a></p>
+ </div>
</div>
- </div>
+ {% endif %}
{% endfor %}
{% with total=checkout.get_total %}