def dispatch(self, request, *args, **kwargs): # Cart not empty cart = get_cart(request) if cart.is_empty(): messages.error(request, _(u"You can not access this page when your cart is empty.")) return redirect("home") # Profile information not filled try: self.profile = Profile.objects.get(user=request.user) except ObjectDoesNotExist: messages.error(request, _(u"Profile information missing.")) return redirect("commerce_checkout_profile") # Shipping payment filled if SHIPPING_PAYMENT_IDENTIFIER not in request.session: messages.error(request, _(u"Shipping or payment information missing.")) return redirect("commerce_checkout_shipping_payment") initial = request.session.get(SHIPPING_PAYMENT_IDENTIFIER, None) self.shipping_payment_form = ShippingPaymentForm(initial, initial=initial, request=request) if not self.shipping_payment_form.is_valid(): raise Exception() request.breadcrumbs = ( {"name": _(u"Home"), "url": "/"}, {"name": _(u"Shopping cart"), "url": reverse("commerce_cart")}, {"name": _(u"Checkout"), "url": reverse("commerce_checkout_login_register")}, {"name": _(u"Order confirmation"), "url": reverse("commerce_checkout_confirm")}, ) return super(ConfirmView, self).dispatch(request, *args, **kwargs)
def get(self, request): cart = get_cart(request) return self.render_to_response( { "profile": self.profile, "cart": cart, "total": cart.total() + self.shipping_payment_form.price(), "shipping_payment_form": self.shipping_payment_form, } )
def dispatch(self, request, *args, **kwargs): cart = get_cart(request) if cart.is_empty(): messages.error(request, _(u"You can not access this page when your cart is empty.")) return redirect("home") request.breadcrumbs = ( {"name": _(u"Home"), "url": "/"}, {"name": _(u"Shopping cart"), "url": reverse("commerce_cart")}, {"name": _(u"Checkout"), "url": reverse("commerce_checkout_login_register")}, {"name": _(u"Profile information"), "url": reverse("commerce_checkout_profile")}, ) return super(ProfileView, self).dispatch(request, *args, **kwargs)
def get(self, request, pk): cart = get_cart(request) try: cart.add(self.product, int(request.GET.get('quantity', 1))) request.session[CART_IDENTIFIER] = cart messages.success( request, _(u'Product successfully added into cart.')) except ValueError: messages.error(request, _(u'Illegal value.')) try: return redirect(request.META['HTTP_REFERER']) except KeyError: return redirect('commerce_cart')
def post(self, request): cart = get_cart(request) # Create order order = self.shipping_payment_form.save(commit=False) order.user = request.user order.price_shipping = self.shipping_payment_form.price() order.total = cart.total() + self.shipping_payment_form.price() order.save() # Assign products to order for item in cart.items: Line.objects.create( order=order, product=item.product, quantity=item.quantity, total=item.price(), price=item.product.price, price_discount=item.product.price_discount, ) # Save profile information information_dict = self._prepare_information() information_dict["order_id"] = order.id information = Information(**information_dict) information.save() t = loader.get_template("commerce/mails/order-created.html") c = Context({"order": order, "host": COMMERCE_URL, "size": COMMERCE_THUMBNAIL_SIZE}) message = EmailMultiAlternatives(_(u"New order created"), "", COMMERCE_EMAIL_FROM, [order.user.email]) message.attach_alternative(t.render(c), "text/html") message.send() t = loader.get_template("commerce/mails/order-inform-admin.html") c = Context({"order": order, "host": COMMERCE_URL, "size": COMMERCE_THUMBNAIL_SIZE}) message = EmailMultiAlternatives(_(u"New order created"), "", COMMERCE_EMAIL_FROM, settings.EDITORS) message.attach_alternative(t.render(c), "text/html") message.send() # Flush sessions if SHIPPING_PAYMENT_IDENTIFIER in request.session: del request.session[SHIPPING_PAYMENT_IDENTIFIER] cart.clean() messages.success(request, _(u"Order was successful.")) return redirect("home")
def price(self): initial = self.initial.get('shipping', None) for shipping in COMMERCE_SHIPPINGS_PAYMENTS: if shipping[0] == initial: if type(shipping[1]) is tuple: cart = get_cart(self.request) price_cart = cart.total() price_current = None for sub_shipping in shipping[1]: if price_current is None: price_current = sub_shipping[2] elif sub_shipping[0] < price_cart <= sub_shipping[1]: price_current = sub_shipping[2] return price_current return shipping[1] return 0
def dispatch(self, request, *args, **kwargs): # Cart not empty cart = get_cart(request) if cart.is_empty(): messages.error(request, _(u"You can not access this page when your cart is empty.")) return redirect("home") # Profile information not filled try: Profile.objects.get(user=request.user) except ObjectDoesNotExist: messages.error(request, _(u"Profile information missing.")) return redirect("commerce_checkout_profile") request.breadcrumbs = ( {"name": _(u"Home"), "url": "/"}, {"name": _(u"Shopping cart"), "url": reverse("commerce_cart")}, {"name": _(u"Checkout"), "url": reverse("commerce_checkout_login_register")}, {"name": _(u"Shipping and payment"), "url": reverse("commerce_checkout_shipping_payment")}, ) return super(ShippingPaymentView, self).dispatch(request, *args, **kwargs)
def cart_block(context): request = context['request'] return { 'cart': get_cart(request) }
def get(self, request): return self.render_to_response({ 'cart': get_cart(request), })