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)
class ConfirmView(View, TemplateResponseMixin):
    template_name = "commerce/checkout/confirm.html"

    @method_decorator(login_required)
    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 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 _prepare_information(self):
        profile_dict = model_to_dict(self.profile)
        del profile_dict["id"]
        del profile_dict["user"]
        del profile_dict["is_newsletter"]
        del profile_dict["created"]
        del profile_dict["modified"]
        return profile_dict