def cart_show(request): cart = Cart().state(request) object_list = [] for el in cart.get('object_list'): product = get_object_or_404(models.Product, pk=el.get('pk')) object_list.append( dict(el, image=product.get_thumbnail_64(), total=el.get('price')*el.get('quantity')) ) cart['object_list'] = object_list formset = forms.CartFormSet(request.POST or None, initial=object_list) if formset.is_valid(): for form in formset: form.save(request) return redirect('shop:cart_show') context = { 'breadcrumb': [ {'url': reverse('shop:home'), 'title': _(u'Home')}, {'url': reverse('shop:cart_show'), 'title': _(u'Shopping Cart')}, ], 'cart': cart, 'formset': formset, } return direct_to_template(request, 'shop/cart_list.html', context)
def cart_show(request): cart = Cart().state(request) object_list = [] for el in cart.get('object_list'): product = get_object_or_404(models.Product, pk=el.get('pk')) object_list.append( dict(el, image=product.get_thumbnail_64(), total=el.get('price') * el.get('quantity'))) cart['object_list'] = object_list formset = forms.CartFormSet(request.POST or None, initial=object_list) if formset.is_valid(): for form in formset: form.save(request) return redirect('shop:cart_show') context = { 'breadcrumb': [ { 'url': reverse('shop:home'), 'title': _(u'Home') }, { 'url': reverse('shop:cart_show'), 'title': _(u'Shopping Cart') }, ], 'cart': cart, 'formset': formset, } return direct_to_template(request, 'shop/cart_list.html', context)
def save(self, request): interface = Cart() cart = interface.state(request) totalprice = cart.get('price', 0.00) order = models.Order(**dict(self.cleaned_data, totalprice=totalprice)) order.save() for item in cart.get('object_list'): product = get_object_or_404(models.Product, pk=item.get('pk')) details = models.OrderDetail(order=order, product=product, quantity=item.get('quantity'), price=item.get('price')) details.save() interface.reset(request) mail_from = u'"%s" <%s>' % (settings.EMAIL_ORDER_FROM_NAME, settings.EMAIL_ORDER_FROM_ADDRESS) mail_to = u'"%s" <%s>' % (settings.EMAIL_ORDER_MANAGER_NAME, settings.EMAIL_ORDER_MANAGER_ADDRESS) subject = u'%s: %s' % (settings.EMAIL_ORDER_SUBJECT % order.pk, totalprice) context = { 'manager': settings.EMAIL_ORDER_MANAGER_NAME, 'customer': self.cleaned_data.get('name'), 'phone': self.cleaned_data.get('phone'), 'object_list': cart.get('object_list'), 'price': totalprice, 'ship_to': self.cleaned_data.get('ship_to'), 'comment': self.cleaned_data.get('comment'), } body = self.prepare_body('shop/order_notify.html', context) email = EmailMessage(subject, body, mail_from, [mail_to]) email.content_subtype = 'html' email.send(fail_silently=True)
def save(self, request): pk = self.cleaned_data.get('pk') quantity = self.cleaned_data.get('quantity') delete = self.cleaned_data.get('DELETE') if delete: Cart().drop(request, int(pk)) else: product = get_object_or_404(models.Product, pk=pk) Cart().change(request, product, quantity)
def checkout(request): # empty cart causes redirection to home page cart = Cart().state(request) if len(cart.get('object_list')) == 0: return redirect('shop:home') # form processing form = forms.Checkout(request.POST or None) if form.is_valid(): form.save(request) return redirect('shop:status') context = { 'form': form, } return direct_to_template(request, 'shop/checkout.html', context)
def save(self, request): pk = self.cleaned_data.get('product_id') return Cart().drop(request, int(pk))
def save(self, request): pk = self.cleaned_data.get('product_id') quantity = self.cleaned_data.get('quantity') product = get_object_or_404(models.Product, pk=pk) return Cart().add(request, product, quantity)
def cart_tag(context): return Cart().state(context['request'])