Exemplo n.º 1
0
def changed_shipping_country(request):
    """
    Refreshes the shipping address form, after the shipping country has been
    changed.
    """
    customer = lfs.customer.utils.get_or_create_customer(request)
    address = customer.selected_shipping_address
    country_iso = request.POST.get("shipping-country")
    if address:
        address.country = Country.objects.get(code=country_iso.lower())
        address.save()

    am = AddressManagement(address, "shipping")
    result = simplejson.dumps({"shipping_address": am.render(request, country_iso)})

    return HttpResponse(result)
Exemplo n.º 2
0
def changed_shipping_country(request):
    """
    Refreshes the shipping address form, after the shipping country has been
    changed.
    """
    customer = lfs.customer.utils.get_or_create_customer(request)
    address = customer.selected_shipping_address
    country_iso = request.POST.get("shipping-country")
    if address:
        address.country = Country.objects.get(code=country_iso.lower())
        address.save()
        customer.sync_selected_to_default_shipping_address()

    am = AddressManagement(customer, address, "shipping")
    result = json.dumps({
        "shipping_address": am.render(request, country_iso),
    })

    return HttpResponse(result, content_type='application/json')
Exemplo n.º 3
0
def changed_invoice_country(request):
    """
    Refreshes the invoice address form, after the invoice country has been
    changed.
    """
    customer = lfs.customer.utils.get_or_create_customer(request)
    address = customer.selected_invoice_address
    country_iso = request.POST.get("invoice-country")
    if address and country_iso:
        address.country = Country.objects.get(code=country_iso.lower())
        address.save()
        customer.sync_selected_to_default_invoice_address()

    am = AddressManagement(customer, address, "invoice")
    result = simplejson.dumps({
        "invoice_address": am.render(request, country_iso),
    })

    return HttpResponse(result, mimetype='application/json')
Exemplo n.º 4
0
    def test_change_address_page(self):
        """
        Tests that we can see a shipping and an invoice address
        """
         # login as our customer
        logged_in = self.client.login(username=self.username, password=self.password)
        self.assertEqual(logged_in, True)

        iam = AddressManagement(self.customer, self.address2, "invoice")
        sam = AddressManagement(self.customer, self.address1, "shipping")

        iam_data = iam.get_address_as_dict()
        sam_data = sam.get_address_as_dict()
        data = {"invoice-firstname": "newname",
                "invoice-lastname": self.address2.lastname,
                "invoice-phone": self.address2.phone,
                "invoice-email": self.address2.email,

                "shipping-firstname": self.address1.firstname,
                "shipping-lastname": self.address1.lastname,
                "shipping-phone": self.address1.phone,
                "shipping-email": self.address1.email,
                }
        for key, value in iam_data.items():
            data['invoice-%s' % key] = value

        for key, value in sam_data.items():
            data['shipping-%s' % key] = value

        data['invoice-country'] = 'AT'

        response = self.client.post(reverse('lfs_my_addresses'), data=data, follow=True)
        self.assertEqual(response.status_code, 200)
        iam2 = Address.objects.get(pk=self.address2.pk)
        self.assertEqual(iam2.firstname, "newname")
        self.assertEqual(iam2.country.code.upper(), "AT")
Exemplo n.º 5
0
def one_page_checkout(request, template_name="lfs/checkout/one_page_checkout.html"):
    """
    One page checkout form.
    """
    cart = lfs.cart.utils.get_cart(request)
    if cart is None:
        return HttpResponseRedirect(reverse('lfs_cart'))

    shop = lfs.core.utils.get_default_shop(request)
    if request.user.is_anonymous() and shop.checkout_type == CHECKOUT_TYPE_AUTH:
        return HttpResponseRedirect(reverse("lfs_checkout_login"))

    customer = lfs.customer.utils.get_or_create_customer(request)
    invoice_address = customer.selected_invoice_address
    shipping_address = customer.selected_shipping_address
    bank_account = customer.selected_bank_account
    credit_card = customer.selected_credit_card

    if request.method == "POST":
        checkout_form = OnePageCheckoutForm(data=request.POST)
        iam = AddressManagement(customer, invoice_address, "invoice", request.POST)
        sam = AddressManagement(customer, shipping_address, "shipping", request.POST)
        bank_account_form = BankAccountForm(instance=bank_account, data=request.POST)
        credit_card_form = CreditCardForm(instance=credit_card, data=request.POST)

        if shop.confirm_toc and ("confirm_toc" not in request.POST):
            toc = False
            if checkout_form.errors is None:
                checkout_form.errors = {}
            checkout_form.errors["confirm_toc"] = _(u"Please confirm our terms and conditions")
        else:
            toc = True

        if checkout_form.is_valid() and bank_account_form.is_valid() and iam.is_valid() and sam.is_valid() and toc:
            # Save addresses
            iam.save()

            # If there the shipping address is not given, the invoice address
            # is copied.
            if request.POST.get("no_shipping", "") == "":
                sam.save()
            else:
                if customer.selected_shipping_address:
                    customer.selected_shipping_address.delete()
                shipping_address = deepcopy(customer.selected_invoice_address)
                shipping_address.id = None
                shipping_address.save()
                customer.selected_shipping_address = shipping_address

            # Save payment method
            customer.selected_payment_method_id = request.POST.get("payment_method")

            # Save bank account
            if customer.selected_payment_method_id and \
               int(customer.selected_payment_method_id) == lfs.payment.settings.PM_BANK:
                customer.selected_bank_account = bank_account_form.save()

            # Save credit card
            if customer.selected_payment_method_id and \
               int(customer.selected_payment_method_id) == lfs.payment.settings.PM_CREDIT_CARD:
                customer.selected_credit_card = credit_card_form.save()

            customer.save()

            # process the payment method
            result = lfs.payment.utils.process_payment(request)

            if result["accepted"]:
                return HttpResponseRedirect(result.get("next_url", reverse("lfs_thank_you")))
            else:
                if "message" in result:
                    checkout_form._errors[result.get("message_location")] = result.get("message")

    else:
        checkout_form = OnePageCheckoutForm()
        iam = AddressManagement(customer, invoice_address, "invoice")
        sam = AddressManagement(customer, shipping_address, "shipping")
        bank_account_form = BankAccountForm(instance=bank_account)
        credit_card_form = CreditCardForm(instance=credit_card)

    # Payment
    try:
        selected_payment_method_id = request.POST.get("payment_method")
        selected_payment_method = PaymentMethod.objects.get(pk=selected_payment_method_id)
    except PaymentMethod.DoesNotExist:
        selected_payment_method = lfs.payment.utils.get_selected_payment_method(request)

    valid_payment_methods = lfs.payment.utils.get_valid_payment_methods(request)
    display_bank_account = any([pm.type == lfs.payment.settings.PM_BANK for pm in valid_payment_methods])
    display_credit_card = any([pm.type == lfs.payment.settings.PM_CREDIT_CARD for pm in valid_payment_methods])

    return render_to_response(template_name, RequestContext(request, {
        "checkout_form": checkout_form,
        "bank_account_form": bank_account_form,
        "credit_card_form": credit_card_form,
        "invoice_address_inline": iam.render(request),
        "shipping_address_inline": sam.render(request),
        "shipping_inline": shipping_inline(request),
        "payment_inline": payment_inline(request, bank_account_form),
        "selected_payment_method": selected_payment_method,
        "display_bank_account": display_bank_account,
        "display_credit_card": display_credit_card,
        "voucher_number": lfs.voucher.utils.get_current_voucher_number(request),
        "cart_inline": cart_inline(request),
        "settings": settings,
    }))
Exemplo n.º 6
0
def addresses(request, template_name="lfs/customer/addresses.html"):
    """
    Provides a form to edit addresses in my account.
    """
    customer = lfs.customer.utils.get_or_create_customer(request)

    if request.method == "POST":
        iam = AddressManagement(customer, customer.default_invoice_address,
                                "invoice", request.POST)
        sam = AddressManagement(customer, customer.default_shipping_address,
                                "shipping", request.POST)

        if iam.is_valid() and sam.is_valid():
            iam.save()
            sam.save()

            customer.sync_default_to_selected_addresses(force=True)

            return lfs.core.utils.MessageHttpResponseRedirect(
                redirect_to=reverse("lfs_my_addresses"),
                msg=_(u"Your addresses have been saved."),
            )
        else:
            msg = _(u"An error has occured.")
    else:
        msg = None
        iam = AddressManagement(customer, customer.default_invoice_address,
                                "invoice")
        sam = AddressManagement(customer, customer.default_shipping_address,
                                "shipping")

    return lfs.core.utils.render_to_message_response(
        request,
        template_name,
        {
            "shipping_address_inline": sam.render(request),
            "invoice_address_inline": iam.render(request),
            "current": "addresses"
        },
        msg=msg,
    )
Exemplo n.º 7
0
def one_page_checkout(request,
                      template_name="lfs/checkout/one_page_checkout.html"):
    """
    One page checkout form.
    """
    OnePageCheckoutForm = lfs.core.utils.import_symbol(ONE_PAGE_CHECKOUT_FORM)

    cart = lfs.cart.utils.get_cart(request)
    if cart is None:
        return HttpResponseRedirect(reverse('lfs_cart'))

    initial_address = {}
    shop = lfs.core.utils.get_default_shop(request)
    if request.user.is_anonymous():
        if shop.checkout_type == CHECKOUT_TYPE_AUTH:
            return HttpResponseRedirect(reverse("lfs_checkout_login"))
    else:
        initial_address['email'] = request.user.email

    customer = lfs.customer.utils.get_or_create_customer(request)

    invoice_address = customer.selected_invoice_address
    shipping_address = customer.selected_shipping_address
    bank_account = customer.selected_bank_account
    credit_card = customer.selected_credit_card
    dinheiro_troco = customer.selected_dinheiro_troco

    if request.method == "POST":
        checkout_form = OnePageCheckoutForm(data=request.POST)
        iam = AddressManagement(customer,
                                invoice_address,
                                "invoice",
                                request.POST,
                                initial=initial_address)
        sam = AddressManagement(customer,
                                shipping_address,
                                "shipping",
                                request.POST,
                                initial=initial_address)
        bank_account_form = BankAccountForm(instance=bank_account,
                                            data=request.POST)
        credit_card_form = CreditCardForm(instance=credit_card,
                                          data=request.POST)
        dinheiro_troco_form = DinheiroTrocoForm(instance=dinheiro_troco,
                                                data=request.POST)

        if shop.confirm_toc and ("confirm_toc" not in request.POST):
            toc = False
            if checkout_form.errors is None:
                checkout_form._errors = {}
            checkout_form.errors["confirm_toc"] = _(
                u"Please confirm our terms and conditions")
        else:
            toc = True

        if checkout_form.is_valid() and bank_account_form.is_valid(
        ) and dinheiro_troco_form.is_valid() and iam.is_valid(
        ) and sam.is_valid() and toc:
            if CHECKOUT_NOT_REQUIRED_ADDRESS == 'shipping':
                iam.save()
                if request.POST.get("no_shipping", "") == "":
                    # If the shipping address is given then save it.
                    sam.save()
                else:
                    # If the shipping address is not given, the invoice address is copied.
                    if customer.selected_invoice_address:
                        if customer.selected_shipping_address:
                            # it might be possible that shipping and invoice addresses are same object
                            if customer.selected_shipping_address.pk != customer.selected_invoice_address.pk:
                                customer.selected_shipping_address.delete()
                        shipping_address = deepcopy(
                            customer.selected_invoice_address)
                        shipping_address.id = None
                        shipping_address.pk = None
                        shipping_address.save()
                        customer.selected_shipping_address = shipping_address
            else:
                sam.save()
                if request.POST.get("no_invoice", "") == "":
                    iam.save()
                else:
                    if customer.selected_shipping_address:
                        if customer.selected_invoice_address:
                            # it might be possible that shipping and invoice addresses are same object
                            if customer.selected_invoice_address.pk != customer.selected_shipping_address.pk:
                                customer.selected_invoice_address.delete()
                        invoice_address = deepcopy(
                            customer.selected_shipping_address)
                        invoice_address.id = None
                        invoice_address.pk = None
                        invoice_address.save()
                        customer.selected_invoice_address = invoice_address
            customer.sync_selected_to_default_addresses()

            # Save payment method
            customer.selected_payment_method_id = request.POST.get(
                "payment_method")

            # Save bank account
            forma_pagamento = ""
            if customer.selected_payment_method_id and \
               int(customer.selected_payment_method_id) == lfs.payment.settings.PM_BANK:
                customer.selected_bank_account = bank_account_form.save()
                forma_pagamento = "Deposito em\n\nSUPERMERCADO SANTA RITA LTDA ME\nBANCO - CAIXA ECONOMICA\nAGENCIA: 0739\nCONTA CORRENTE: 1278-8\nOPERACAO: 003\n\nOU\n\nSUPERMERCADO SANTA RITA LTDA ME\nBANCO - BANCO DO BRASIL\nAGENCIA: 07757\nCONTA CORRENTE: 13589-5\n\n"

            # Save credit card
            if customer.selected_payment_method_id and \
               int(customer.selected_payment_method_id) == lfs.payment.settings.PM_CREDIT_CARD:
                customer.selected_credit_card = credit_card_form.save()
                forma_pagamento = credit_card_form.cleaned_data
                forma_pagamento = "cartao {}".format(forma_pagamento["type"])

            if customer.selected_payment_method_id and \
               int(customer.selected_payment_method_id) == lfs.payment.settings.PM_DINHEIRO_TROCO:
                customer.selected_dinheiro_troco = dinheiro_troco_form.save()
                forma_pagamento = dinheiro_troco_form.cleaned_data
                forma_pagamento = "dinheiro e troco para {}".format(
                    forma_pagamento["Troco"].encode('utf-8'))

            # msg whatsapp
            selected_payment_method = lfs.payment.utils.get_selected_payment_method(
                request)
            selected_shipping_method = lfs.shipping.utils.get_selected_shipping_method(
                request)
            payment_costs = lfs.payment.utils.get_payment_costs(
                request, selected_payment_method)
            shipping_costs = lfs.shipping.utils.get_shipping_costs(
                request, selected_shipping_method)
            cart_price = cart.get_price_gross(request) + shipping_costs[
                "price_gross"] + payment_costs["price"]
            cart_items = []
            if cart:
                for cart_item in cart.get_items():
                    product = cart_item.product
                    quantity = product.get_clean_quantity(cart_item.amount)
                    cart_items.append({
                        "obj":
                        cart_item,
                        "quantity":
                        quantity,
                        "product":
                        product,
                        "product_price_net":
                        cart_item.get_price_net(request),
                        "product_price_gross":
                        cart_item.get_price_gross(request),
                        "product_tax":
                        cart_item.get_tax(request),
                    })

                cliente = customer.get_selected_shipping_address()

                termos_replace = [" ", "-", "_", "\\", "/", ")", "(", ",", "."]
                telefone = cliente.phone
                for termo in termos_replace:
                    telefone = telefone.replace(termo, "")
                telefone = telefone[-8:]
                telefone = "5579" + telefone.encode('utf-8')
                nome = cliente.firstname.encode('utf-8')
                total = cart_price
                msgw = 'Oi {}! Aqui é do Compre Sem Fila, venho lhe informar que o pedido no valor de {} com pagamento atraves de {} foi recebido com sucesso.\nObrigado!'.format(
                    nome, total,
                    forma_pagamento).decode("utf-8").encode("utf-8")
                comando = '{}/bin/python {}/yowsup/yowsup-cli demos -c {}/yowsup/config -s "{}" "{}"'.format(
                    sys.exec_prefix, sys.exec_prefix, sys.exec_prefix,
                    telefone, msgw)
                comando1 = '{}/bin/python {}/yowsup/yowsup-cli demos -c {}/yowsup/config -s "{}" "{}"'.format(
                    sys.exec_prefix, sys.exec_prefix, sys.exec_prefix,
                    "557999438361", msgw)
                comando2 = '{}/bin/python {}/yowsup/yowsup-cli demos -c {}/yowsup/config -s "{}" "{}"'.format(
                    sys.exec_prefix, sys.exec_prefix, sys.exec_prefix,
                    "557999654384", msgw)
                os.system(comando)
                os.system(comando1)
                os.system(comando2)

                # process the payment method
                result = lfs.payment.utils.process_payment(request)

                if result["accepted"]:
                    return HttpResponseRedirect(
                        result.get("next_url", reverse("lfs_thank_you")))
                else:
                    if "message" in result:
                        checkout_form._errors[result.get(
                            "message_location")] = result.get("message")

    else:
        checkout_form = OnePageCheckoutForm()
        iam = AddressManagement(customer,
                                invoice_address,
                                "invoice",
                                initial=initial_address)
        sam = AddressManagement(customer,
                                shipping_address,
                                "shipping",
                                initial=initial_address)
        bank_account_form = BankAccountForm(instance=bank_account)
        credit_card_form = CreditCardForm(instance=credit_card)
        dinheiro_troco_form = DinheiroTrocoForm(instance=dinheiro_troco)

    # Payment
    try:
        selected_payment_method_id = request.POST.get("payment_method")
        selected_payment_method = PaymentMethod.objects.get(
            pk=selected_payment_method_id)
    except PaymentMethod.DoesNotExist:
        selected_payment_method = lfs.payment.utils.get_selected_payment_method(
            request)

    valid_payment_methods = lfs.payment.utils.get_valid_payment_methods(
        request)
    display_bank_account = any([
        pm.type == lfs.payment.settings.PM_BANK for pm in valid_payment_methods
    ])
    display_credit_card = any([
        pm.type == lfs.payment.settings.PM_CREDIT_CARD
        for pm in valid_payment_methods
    ])
    display_dinheiro_troco = any([
        pm.type == lfs.payment.settings.PM_DINHEIRO_TROCO
        for pm in valid_payment_methods
    ])

    return render_to_response(
        template_name,
        RequestContext(
            request, {
                "checkout_form":
                checkout_form,
                "bank_account_form":
                bank_account_form,
                "credit_card_form":
                credit_card_form,
                "dinheiro_troco_form":
                dinheiro_troco_form,
                "invoice_address_inline":
                iam.render(request),
                "shipping_address_inline":
                sam.render(request),
                "shipping_inline":
                shipping_inline(request),
                "payment_inline":
                payment_inline(request, bank_account_form),
                "selected_payment_method":
                selected_payment_method,
                "display_bank_account":
                display_bank_account,
                "display_credit_card":
                display_credit_card,
                "display_dinheiro_troco":
                display_dinheiro_troco,
                "voucher_number":
                lfs.voucher.utils.get_current_voucher_number(request),
                "cart_inline":
                cart_inline(request),
                "settings":
                settings,
            }))
Exemplo n.º 8
0
def one_page_checkout(request, template_name="lfs/checkout/one_page_checkout.html"):
    """
    One page checkout form.
    """
    OnePageCheckoutForm = lfs.core.utils.import_symbol(ONE_PAGE_CHECKOUT_FORM)

    cart = lfs.cart.utils.get_cart(request)
    if cart is None:
        return HttpResponseRedirect(reverse('lfs_cart'))

    initial_address = {}
    shop = lfs.core.utils.get_default_shop(request)
    if request.user.is_anonymous():
        if shop.checkout_type == CHECKOUT_TYPE_AUTH:
            return HttpResponseRedirect(reverse("lfs_checkout_login"))
    else:
        initial_address['email'] = request.user.email

    customer = lfs.customer.utils.get_or_create_customer(request)

    invoice_address = customer.selected_invoice_address
    shipping_address = customer.selected_shipping_address
    bank_account = customer.selected_bank_account
    credit_card = customer.selected_credit_card

    if request.method == "POST":
        checkout_form = OnePageCheckoutForm(data=request.POST)
        iam = AddressManagement(customer, invoice_address, "invoice", request.POST, initial=initial_address)
        sam = AddressManagement(customer, shipping_address, "shipping", request.POST, initial=initial_address)
        bank_account_form = BankAccountForm(instance=bank_account, data=request.POST)
        credit_card_form = CreditCardForm(instance=credit_card, data=request.POST, prefix="credit_card")

        if shop.confirm_toc and ("confirm_toc" not in request.POST):
            toc = False
            if checkout_form.errors is None:
                checkout_form._errors = {}
            checkout_form.errors["confirm_toc"] = _(u"Please confirm our terms and conditions")
        else:
            toc = True

        # Prevent checkout if voucher is not valid (any more)
        voucher_number = request.POST.get("voucher")
        if voucher_number:
            try:
                voucher = Voucher.objects.get(number=voucher_number)
            except Voucher.DoesNotExist:
                is_valid_voucher = False
            else:
                is_valid_voucher = voucher.is_effective(request, cart)[0]
        else:
            is_valid_voucher = True

        if is_valid_voucher and checkout_form.is_valid() and bank_account_form.is_valid() and iam.is_valid() and sam.is_valid() and toc:
            if CHECKOUT_NOT_REQUIRED_ADDRESS == 'shipping':
                iam.save()
                if request.POST.get("no_shipping", "") == "":
                    # If the shipping address is given then save it.
                    sam.save()
                else:
                    # If the shipping address is not given, the invoice address is copied.
                    if customer.selected_invoice_address:
                        if customer.selected_shipping_address:
                            # it might be possible that shipping and invoice addresses are same object
                            if customer.selected_shipping_address.pk != customer.selected_invoice_address.pk:
                                customer.selected_shipping_address.delete()
                        shipping_address = deepcopy(customer.selected_invoice_address)
                        shipping_address.id = None
                        shipping_address.pk = None
                        shipping_address.save()
                        customer.selected_shipping_address = shipping_address
            else:
                sam.save()
                if request.POST.get("no_invoice", "") == "":
                    iam.save()
                else:
                    if customer.selected_shipping_address:
                        if customer.selected_invoice_address:
                            # it might be possible that shipping and invoice addresses are same object
                            if customer.selected_invoice_address.pk != customer.selected_shipping_address.pk:
                                customer.selected_invoice_address.delete()
                        invoice_address = deepcopy(customer.selected_shipping_address)
                        invoice_address.id = None
                        invoice_address.pk = None
                        invoice_address.save()
                        customer.selected_invoice_address = invoice_address
            customer.sync_selected_to_default_addresses()

            # Save payment method
            customer.selected_payment_method_id = request.POST.get("payment_method")

            # Save bank account
            if customer.selected_payment_method_id and \
               int(customer.selected_payment_method_id) == lfs.payment.settings.PM_BANK:
                customer.selected_bank_account = bank_account_form.save()

            # Save credit card
            if customer.selected_payment_method_id and \
               int(customer.selected_payment_method_id) == lfs.payment.settings.PM_CREDIT_CARD:
                customer.selected_credit_card = credit_card_form.save()

            customer.save()

            # process the payment method
            result = lfs.payment.utils.process_payment(request)

            if result["accepted"]:
                return HttpResponseRedirect(result.get("next_url", reverse("lfs_thank_you")))
            else:
                if "message" in result:
                    checkout_form._errors[result.get("message_location")] = result.get("message")

    else:
        checkout_form = OnePageCheckoutForm()
        iam = AddressManagement(customer, invoice_address, "invoice", initial=initial_address)
        sam = AddressManagement(customer, shipping_address, "shipping", initial=initial_address)
        bank_account_form = BankAccountForm(instance=bank_account)
        credit_card_form = CreditCardForm(instance=credit_card, prefix="credit_card")

    # Payment
    try:
        selected_payment_method_id = request.POST.get("payment_method")
        selected_payment_method = PaymentMethod.objects.get(pk=selected_payment_method_id)
    except PaymentMethod.DoesNotExist:
        selected_payment_method = lfs.payment.utils.get_selected_payment_method(request)

    valid_payment_methods = lfs.payment.utils.get_valid_payment_methods(request)
    display_bank_account = any([pm.type == lfs.payment.settings.PM_BANK for pm in valid_payment_methods])
    display_credit_card = any([pm.type == lfs.payment.settings.PM_CREDIT_CARD for pm in valid_payment_methods])

    return render(request, template_name, {
        "checkout_form": checkout_form,
        "bank_account_form": bank_account_form,
        "credit_card_form": credit_card_form,
        "invoice_address_inline": iam.render(request),
        "shipping_address_inline": sam.render(request),
        "shipping_inline": shipping_inline(request),
        "payment_inline": payment_inline(request, bank_account_form),
        "selected_payment_method": selected_payment_method,
        "display_bank_account": display_bank_account,
        "display_credit_card": display_credit_card,
        "voucher_number": lfs.voucher.utils.get_current_voucher_number(request),
        "cart_inline": cart_inline(request),
        "settings": settings,
    })
Exemplo n.º 9
0
def addresses(request, template_name="lfs/customer/addresses.html"):
    """
    Provides a form to edit addresses in my account.
    """
    customer = lfs.customer.utils.get_or_create_customer(request)

    if request.method == "POST":
        iam = AddressManagement(customer, customer.default_invoice_address, "invoice", request.POST)
        sam = AddressManagement(customer, customer.default_shipping_address, "shipping", request.POST)

        if iam.is_valid() and sam.is_valid():
            iam.save()
            sam.save()

            customer.sync_default_to_selected_addresses(force=True)

            return lfs.core.utils.MessageHttpResponseRedirect(
                redirect_to=reverse("lfs_my_addresses"), msg=_(u"Your addresses have been saved.")
            )
        else:
            msg = _(u"An error has occured.")
    else:
        msg = None
        iam = AddressManagement(customer, customer.default_invoice_address, "invoice")
        sam = AddressManagement(customer, customer.default_shipping_address, "shipping")

    return lfs.core.utils.render_to_message_response(
        template_name,
        RequestContext(
            request,
            {
                "shipping_address_inline": sam.render(request),
                "invoice_address_inline": iam.render(request),
                "current": "addresses",
            },
        ),
        msg=msg,
    )
Exemplo n.º 10
0
def one_page_checkout(request,
                      template_name="lfs/checkout/one_page_checkout.html"):
    """
    One page checkout form.
    """
    cart = lfs.cart.utils.get_cart(request)
    if cart is None:
        return HttpResponseRedirect(reverse('lfs_cart'))

    shop = lfs.core.utils.get_default_shop(request)
    if request.user.is_anonymous(
    ) and shop.checkout_type == CHECKOUT_TYPE_AUTH:
        return HttpResponseRedirect(reverse("lfs_checkout_login"))

    customer = lfs.customer.utils.get_or_create_customer(request)
    invoice_address = customer.selected_invoice_address
    shipping_address = customer.selected_shipping_address
    bank_account = customer.selected_bank_account
    credit_card = customer.selected_credit_card

    if request.method == "POST":
        checkout_form = OnePageCheckoutForm(data=request.POST)
        iam = AddressManagement(invoice_address, "invoice", request.POST)
        sam = AddressManagement(shipping_address, "shipping", request.POST)
        bank_account_form = BankAccountForm(instance=bank_account,
                                            data=request.POST)
        credit_card_form = CreditCardForm(instance=credit_card,
                                          data=request.POST)

        if shop.confirm_toc and ("confirm_toc" not in request.POST):
            toc = False
            if checkout_form.errors is None:
                checkout_form.errors = {}
            checkout_form.errors["confirm_toc"] = _(
                u"Please confirm our terms and conditions")
        else:
            toc = True

        if checkout_form.is_valid() and bank_account_form.is_valid(
        ) and iam.is_valid() and sam.is_valid() and toc:
            # Save addresses
            iam.save()

            # If there the shipping address is not given, the invoice address
            # is copied.
            if request.POST.get("no_shipping", "") == "":
                sam.save()
            else:
                shipping_address = deepcopy(customer.selected_invoice_address)
                shipping_address.id = None
                shipping_address.save()
                customer.selected_shipping_address = shipping_address

            # Save payment method
            customer.selected_payment_method_id = request.POST.get(
                "payment_method")

            # Save bank account
            if customer.selected_payment_method_id and \
               int(customer.selected_payment_method_id) == lfs.payment.settings.PM_BANK:
                customer.selected_bank_account = bank_account_form.save()

            # Save credit card
            if customer.selected_payment_method_id and \
               int(customer.selected_payment_method_id) == lfs.payment.settings.PM_CREDIT_CARD:
                customer.selected_credit_card = credit_card_form.save()

            customer.save()

            # process the payment method
            result = lfs.payment.utils.process_payment(request)

            if result["accepted"]:
                return HttpResponseRedirect(
                    result.get("next_url", reverse("lfs_thank_you")))
            else:
                if "message" in result:
                    form._errors[result.get("message_location")] = result.get(
                        "message")

    else:
        checkout_form = OnePageCheckoutForm()
        iam = AddressManagement(invoice_address, "invoice")
        sam = AddressManagement(shipping_address, "shipping")
        bank_account_form = BankAccountForm(instance=bank_account)
        credit_card_form = CreditCardForm(instance=credit_card)

    # Payment
    try:
        selected_payment_method_id = request.POST.get("payment_method")
        selected_payment_method = PaymentMethod.objects.get(
            pk=selected_payment_method_id)
    except PaymentMethod.DoesNotExist:
        selected_payment_method = lfs.payment.utils.get_selected_payment_method(
            request)

    valid_payment_methods = lfs.payment.utils.get_valid_payment_methods(
        request)
    display_bank_account = any([
        pm.type == lfs.payment.settings.PM_BANK for pm in valid_payment_methods
    ])
    display_credit_card = any([
        pm.type == lfs.payment.settings.PM_CREDIT_CARD
        for pm in valid_payment_methods
    ])

    return render_to_response(
        template_name,
        RequestContext(
            request, {
                "checkout_form":
                checkout_form,
                "bank_account_form":
                bank_account_form,
                "credit_card_form":
                credit_card_form,
                "invoice_address_inline":
                iam.render(request),
                "shipping_address_inline":
                sam.render(request),
                "shipping_inline":
                shipping_inline(request),
                "payment_inline":
                payment_inline(request, bank_account_form),
                "selected_payment_method":
                selected_payment_method,
                "display_bank_account":
                display_bank_account,
                "display_credit_card":
                display_credit_card,
                "voucher_number":
                lfs.voucher.utils.get_current_voucher_number(request),
                "cart_inline":
                cart_inline(request),
                "settings":
                settings,
            }))