Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add cart view, improve cart forms
Michal Ociepka45f37562f182
diff --git a/cart/forms.py b/cart/forms.pyindex 377c9769c5..2bac3e2c9d 100644--- a/cart/forms.py+++ b/cart/forms.py@@ -1,7 +1,8 @@+from . import InvalidQuantityExceptionfrom decimal import Decimalfrom django import forms+from django.forms.formsets import BaseFormSetfrom django.utils.translation import pgettext as _-from . import InvalidQuantityExceptionclass QuantityField(forms.DecimalField):@@ -15,18 +16,20 @@ class AddToCartForm(forms.Form):max_digits=10, decimal_places=4,initial=Decimal(1))- def __init__(self, cart, product, *args, **kwargs):+ def __init__(self, *args, **kwargs):+ self.cart = kwargs.pop('cart')+ self.product = kwargs.pop('product')+ self.cart_line = self.cart.get_line(self.product)+super(AddToCartForm, self).__init__(*args, **kwargs)- self.cart = cart- self.product = productdef clean_quantity(self):quantity = self.cleaned_data['quantity']- cart_line = self.cart.get_line(self.product)try:self.cart.check_quantity(self.product,- quantity + cart_line.quantity if cart_line else quantity)+ quantity + self.cart_line.quantity if+ self.cart_line else quantity)except InvalidQuantityException as e:raise forms.ValidationError(e)@@ -35,3 +38,45 @@ class AddToCartForm(forms.Form):def save(self):return self.cart.add_line(self.product,self.cleaned_data['quantity'])+++class ReplaceCartLineForm(AddToCartForm):++ def clean_quantity(self):+ quantity = self.cleaned_data['quantity']++ try:+ self.cart.check_quantity(self.product, quantity)+ except InvalidQuantityException as e:+ raise forms.ValidationError(e)++ return quantity++ def save(self):+ return self.cart.add_line(self.product,+ self.cleaned_data['quantity'], replace=True)+++class ReplaceCartLineFormSet(BaseFormSet):++ form = ReplaceCartLineForm+ extra = 0+ can_order = False,+ can_delete = False+ max_num = None++ def __init__(self, *args, **kwargs):+ self.cart = kwargs.pop('cart')+ kwargs['initial'] = [{'quantity':cart_line.get_quantity()} for+ cart_line in self.cart]++ super(ReplaceCartLineFormSet, self).__init__(*args, **kwargs)++ def _construct_form(self, i, **kwargs):+ kwargs['cart'] = self.cart+ kwargs['product'] = self.cart[i].product+ return super(ReplaceCartLineFormSet, self)._construct_form(i, **kwargs)++ def save(self):+ for form in self.forms:+ form.save()diff --git a/cart/templates/cart/index.html b/cart/templates/cart/index.htmlnew file mode 100644index 0000000000..cda87371bd--- /dev/null+++ b/cart/templates/cart/index.html@@ -0,0 +1,41 @@+{% extends "base.html" %}++{% load gross from prices %}++{% block content %}+{% if cart %}+<form action="." method="post">+ {% csrf_token %}+ {{ formset.management_form }}+ <table class="table table-bordered">+ <thead>+ <tr>+ <th>#</th><th>name</th><th colspan="2">quantity</th><th>price</th>+ </tr>+ </thead>+ <tbody>+ {% for form in formset %}+ <tr>+ <td>{{ forloop.counter }}</td>+ <td>{{ form.cart_line.product }}</td>+ <td colspan="2">+ {{ form.quantity }}+ </td>+ <td>{% gross form.cart_line.get_price_per_item %}</td>+ </tr>+ {% endfor %}+ </tbody>+ <tfoot>+ <tr>+ <td colspan="2"></td>+ <td><button class="btn" type="submit">Update</button></td>+ <td>Total:</td>+ <td>{% gross cart.get_total %}</td>+ </tr>+ </tfoot>+ </table>+</form>+{% else %}+<p>Your cart is empty</p>+{% endif %}+{% endblock content %}diff --git a/cart/urls.py b/cart/urls.pynew file mode 100644index 0000000000..07f2f9d3c9--- /dev/null+++ b/cart/urls.py@@ -0,0 +1,7 @@+from django.conf.urls import patterns, url+++urlpatterns = patterns('cart.views',+ url(r'^$', 'index', name='index'),+)+diff --git a/cart/views.py b/cart/views.pyindex 60f00ef0ef..0f85eafa97 100755--- a/cart/views.py+++ b/cart/views.py@@ -1 +1,16 @@-# Create your views here.+from . import get_cart_from_request+from .forms import ReplaceCartLineFormSet+from django.template.response import TemplateResponse++def index(request):+ cart = get_cart_from_request(request)++ formset = ReplaceCartLineFormSet(request.POST or None, cart=cart)++ if formset.is_valid():+ formset.save()++ return TemplateResponse(request, 'cart/index.html', {+ 'cart': cart,+ 'formset': formset+ })diff --git a/saleor/urls.py b/saleor/urls.pyindex 0ebba1c7d8..89d9e665dd 100644--- a/saleor/urls.py+++ b/saleor/urls.py@@ -9,4 +9,5 @@ urlpatterns = patterns('',url(r'^$', 'saleor.views.home', name='home'),url(r'^products/', include('product.urls', namespace='product')),url(r'^order/', include('order.urls', namespace='order')),+ url(r'^cart/', include('cart.urls', namespace='cart')),)