Skip to content

Commit

Permalink
Check out: don't save addresses until all forms are valid.
Browse files Browse the repository at this point in the history
  • Loading branch information
diefenbach committed Mar 2, 2012
1 parent ab52148 commit 7ff11d2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 16 additions & 6 deletions lfs/checkout/views.py
Expand Up @@ -222,7 +222,22 @@ def one_page_checkout(request, checkout_form=OnePageCheckoutForm,
form.errors = {}
form.errors["confirm_toc"] = _(u"Please confirm our terms and conditions")

if form.is_valid() and toc:
# Validate invoice address
prefix="invoice"
country_iso = request.POST.get(prefix + "-country", shop.default_country.code)
form_class = form_factory(country_iso)
invoice_form = form_class(request.POST, prefix=prefix)

# Validate shipping address
valid_shipping_form = True
if not request.POST.get("no_shipping"):
prefix="shipping"
country_iso = request.POST.get(prefix + "-country", shop.default_country.code)
form_class = form_factory(country_iso)
shipping_form = form_class(request.POST, prefix=prefix)
valid_shipping_form = shipping_form.is_valid()

if form.is_valid() and invoice_form.is_valid() and valid_shipping_form and toc:
# save invoice details
customer.selected_invoice_address.firstname = request.POST.get("invoice_firstname")
customer.selected_invoice_address.lastname = request.POST.get("invoice_lastname")
Expand Down Expand Up @@ -283,9 +298,6 @@ def one_page_checkout(request, checkout_form=OnePageCheckoutForm,
customer.selected_invoice_address.email = request.POST.get("invoice_email")
customer.selected_invoice_address.company_name = request.POST.get("invoice_company_name")

# Create or update invoice address
save_address(request, customer, INVOICE_PREFIX)

# If the shipping address differs from invoice firstname we create
# or update the shipping address.
if not form.data.get("no_shipping"):
Expand All @@ -297,8 +309,6 @@ def one_page_checkout(request, checkout_form=OnePageCheckoutForm,
customer.selected_shipping_address.company_name = request.POST.get("shipping_company_name")
customer.save()

save_address(request, customer, SHIPPING_PREFIX)

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

Expand Down
2 changes: 1 addition & 1 deletion lfs/customer/views.py
Expand Up @@ -220,7 +220,7 @@ def addresses(request, template_name="lfs/customer/addresses.html"):
form_class = form_factory(country_iso)
invoice_form = form_class(request.POST, prefix=prefix)

# Validate invoice address
# Validate shipping address
prefix="shipping"
country_iso = request.POST.get(prefix + "-country", shop.default_country.code)
form_class = form_factory(country_iso)
Expand Down

0 comments on commit 7ff11d2

Please sign in to comment.