Commits

mickt committed c0ef63c

refactor checkout so customer.selected_invoice_address and customer.selected_shipping_address are created at customer creation time

  • Participants
  • Parent commits d50aa24

Comments (0)

Files changed (2)

lfs/checkout/views.py

 
         if toc and form.is_valid():
             # save invoice details
-            if customer.selected_invoice_address is None:
-                customer.selected_invoice_address = Address.objects.create(customer=customer, country=shop.default_country)
-                customer.save()
             customer.selected_invoice_address.firstname = request.POST.get("invoice_firstname")
             customer.selected_invoice_address.lastname = request.POST.get("invoice_lastname")
             customer.selected_invoice_address.phone = request.POST.get("invoice_phone")
                 valid_shipping_address = True
                 if not form.cleaned_data.get("no_shipping"):
                     # save shipping details
-                    if customer.selected_shipping_address is None:
-                        customer.selected_shipping_address = Address.objects.create(customer=customer, country=shop.default_country)
-                        customer.save()
                     customer.selected_shipping_address.firstname = request.POST.get("shipping_firstname")
                     customer.selected_shipping_address.lastname = request.POST.get("shipping_lastname")
                     customer.selected_shipping_address.phone = request.POST.get("shipping_phone")
     else:
         # If there are addresses intialize the form.
         initial = {}
-        if customer.selected_invoice_address is None:
-            customer.selected_invoice_address = Address.objects.create(customer=customer, country=shop.default_country)
-            customer.save()
         invoice_address = customer.selected_invoice_address
         initial.update({
             "invoice_firstname" : invoice_address.firstname,
             "invoice_email" : invoice_address.email,
             "invoice_country" : invoice_address.country,
         })
-        if customer.selected_shipping_address is None:
-            customer.selected_shipping_address = Address.objects.create(customer=customer, country=shop.default_country)
-            customer.save()
         shipping_address = customer.selected_shipping_address
         initial.update({
             "shipping_firstname" : shipping_address.firstname,

lfs/customer/utils.py

 from django.core.exceptions import ObjectDoesNotExist
 
 # lfs imports
-from lfs.customer.models import Customer
+from lfs.customer.models import Customer, Address
+from lfs.core.utils import get_default_shop
 
 def get_or_create_customer(request):
     """Get or creates the customer object.
     customer = Customer(session = request.session.session_key)
     if request.user.is_authenticated():
         customer.user = request.user
-
+    shop = get_default_shop()
+    customer.selected_invoice_address = Address.objects.create(customer=customer, country=shop.default_country)
+    customer.selected_shipping_address = Address.objects.create(customer=customer, country=shop.default_country)
     customer.save()
     return customer