Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Improve checkout storage, remove groups
Michał Ociepka8992c94ac594
diff --git a/saleor/checkout/__init__.py b/saleor/checkout/__init__.pyindex 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 defaultdictfrom satchless.process import ProcessManager@@ -11,14 +11,14 @@ from ..order.models import OrderSTORAGE_SESSION_KEY = 'checkout_storage'-class CheckoutStorage(dict):+class CheckoutStorage(defaultdict):modified = Falsedef __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 = DigitalDeliveryStepelse: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)@propertydef anonymous_user_email(self):- return self.storage['anonymous_user_email']+ return self.storage['billing']['anonymous_user_email']@anonymous_user_email.setterdef anonymous_user_email(self, email):- self.storage['anonymous_user_email'] = email+ self.storage['billing']['anonymous_user_email'] = email@anonymous_user_email.deleterdef anonymous_user_email(self, email):- self.storage['anonymous_user_email'] = ''+ self.storage['billing']['anonymous_user_email'] = ''@propertydef billing_address(self):- return self.storage['billing_address']+ return self.storage['billing']['address']@billing_address.setterdef billing_address(self, address):- self.storage['billing_address'] = address+ self.storage['billing']['address'] = address@billing_address.deleterdef 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] = groupdef 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.htmlindex 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 %}