Commits

Kai Diefenbach committed e29a1e0

Bugfix customer addresses: issue #32; issue #4

Comments (0)

Files changed (2)

lfs/checkout/views.py

                     country_id = form.cleaned_data.get("invoice_country"),
                     phone = form.cleaned_data.get("invoice_phone"),
                     email = form.cleaned_data.get("invoice_email"),
+                    customer = customer,
                 )
                 customer.selected_invoice_address = invoice_address
             else:
                 selected_invoice_address.country_id = form.cleaned_data.get("invoice_country")
                 selected_invoice_address.phone = form.cleaned_data.get("invoice_phone")
                 selected_invoice_address.email = form.cleaned_data.get("invoice_email")
+                selected_invoice_address.customer = customer
                 selected_invoice_address.save()
 
             # If the shipping address differs from invoice firstname we create
                         country_id = form.cleaned_data.get("shipping_country"),
                         phone = form.cleaned_data.get("shipping_phone"),
                         email = form.cleaned_data.get("shipping_email"),
+                        customer = customer,
                     )
                     customer.selected_shipping_address = shipping_address
                 else:
                     selected_shipping_address.city = form.cleaned_data.get("shipping_city")
                     selected_shipping_address.country_id = form.cleaned_data.get("shipping_country")
                     selected_shipping_address.phone = form.cleaned_data.get("shipping_phone")
+                    selected_shipping_address.customer = customer
                     selected_shipping_address.save()
 
             # Payment method

lfs/customer/views.py

 def login(request, template_name="lfs/customer/login.html"):
     """Custom view to login or register/login a user.
 
-    The reason to use a custom login method are: 
+    The reason to use a custom login method are:
 
       * validate checkout type
       * integration of register and login form
     It uses Django's standard AuthenticationForm, though.
     """
     shop = lfs.core.utils.get_default_shop()
-    
+
     # If only anonymous checkout is allowed this view doesn't exists :)
     # if shop.checkout_type == CHECKOUT_TYPE_ANON:
     #     raise Http404()
 
     if request.POST.get("action") == "login":
         login_form = AuthenticationForm(data=request.POST)
-        
+
         if login_form.is_valid():
             redirect_to = request.POST.get("next")
             # Light security check -- make sure redirect_to isn't garbage.
             if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
                 redirect_to = reverse("lfs_shop_view")
-            
+
             from django.contrib.auth import login
             login(request, login_form.get_user())
 
             return lfs.core.utils.set_message_cookie(
                 redirect_to, msg = _(u"You have been logged in."))
-        
+
     elif request.POST.get("action") == "register":
         register_form = RegisterForm(data=request.POST)
         if register_form.is_valid():
-                    
+
             email = register_form.data.get("email")
             password = register_form.data.get("password_1")
 
             # Create user
             user = User.objects.create_user(
                 username=email, email=email, password=password)
-            
+
             # Create customer
             customer = customer_utils.get_or_create_customer(request)
             customer.user = user
-        
+
             # Notify
             lfs.core.signals.customer_added.send(user)
 
             from django.contrib.auth import authenticate
             user = authenticate(username=email, password=password)
 
-            from django.contrib.auth import login                
+            from django.contrib.auth import login
             login(request, user)
-        
+
             redirect_to = request.POST.get("next")
             if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
                 redirect_to = reverse("lfs_shop_view")
-            
+
             return lfs.core.utils.set_message_cookie(
                 redirect_to, msg = _(u"You have been registered and logged in."))
-    
+
     # Get next_url
     next_url = request.REQUEST.get("next")
     if next_url is None:
     if next_url is None:
         next_url =  reverse("lfs_shop_view")
 
-    # Get just the path of the url. See django.contrib.auth.views.login for more    
+    # Get just the path of the url. See django.contrib.auth.views.login for more
     next_url = urlparse(next_url)
     next_url = next_url[2]
-    
+
     try:
         login_form_errors = login_form.errors["__all__"]
     except KeyError:
         login_form_errors = None
-    
+
     return render_to_response(template_name, RequestContext(request, {
         "login_form" : login_form,
         "login_form_errors" : login_form_errors,
 
 def logout(request):
     """Custom method to logout a user.
-    
-    The reason to use a custom logout method is just to provide a login and a 
+
+    The reason to use a custom logout method is just to provide a login and a
     logoutmethod on one place.
     """
     from django.contrib.auth import logout
     logout(request)
-    
+
     return lfs.core.utils.set_message_cookie(reverse("lfs_shop_view"),
         msg = _(u"You have been logged out."))
-    
+
 @login_required
 def orders(request, template_name="lfs/customer/orders.html"):
     """Displays the orders of the current user
     """
     orders = Order.objects.filter(user=request.user)
-    
+
     return render_to_response(template_name, RequestContext(request, {
         "orders" : orders,
     }))
     """
     orders = Order.objects.filter(user=request.user)
     order = get_object_or_404(Order, pk=id, user=request.user)
-    
+
     return render_to_response(template_name, RequestContext(request, {
         "current_order" : order,
-        "orders" : orders,        
+        "orders" : orders,
     }))
 
 @login_required
     """Provides a form to edit addresses and bank account.
     """
     user = request.user
-    customer = lfs.customer.utils.get_customer(request)
-    
+    customer = lfs.customer.utils.get_or_create_customer(request)
+
     show_shipping_address = customer.selected_shipping_address and \
                             customer.selected_invoice_address.id != \
                             customer.selected_shipping_address.id
-    
-    if request.method == "POST":    
+
+    if request.method == "POST":
         shipping_form = AddressForm(prefix="shipping", data=request.POST,
             instance = customer.selected_shipping_address)
-        
+
         invoice_form = AddressForm(prefix="invoice", data=request.POST,
             instance = customer.selected_invoice_address)
-    
+
         if show_shipping_address:
             if shipping_form.is_valid() and invoice_form.is_valid():
-                shipping_form.save()
-                invoice_form.save()
+                shipping_address = shipping_form.save()
+                shipping_address.customer = customer
+                shipping_address.save()
+
+                invoice_address = invoice_form.save()
+                invoice_address.customer = customer
+                invoice_address.save()
+
+                customer.selected_invoice_address = invoice_address
+                customer.selected_shipping_address = invoice_address
+                customer.save()
+
                 return HttpResponseRedirect(reverse("lfs_my_addresses"))
         else:
             if invoice_form.is_valid():
-                invoice_form.save()
+                invoice_address = invoice_form.save()
+                invoice_address.customer = customer
+                invoice_address.save()
+
+                customer.selected_invoice_address = invoice_address
+                customer.save()
+
                 return HttpResponseRedirect(reverse("lfs_my_addresses"))
-    else:            
-        
+    else:
+
         shipping_form = AddressForm(prefix="shipping",
             instance=customer.selected_shipping_address)
-        
-        invoice_form = AddressForm(prefix="invoice", 
+
+        invoice_form = AddressForm(prefix="invoice",
             instance=customer.selected_invoice_address)
-        
+
     return render_to_response(template_name, RequestContext(request, {
         "show_shipping_address" : show_shipping_address,
         "shipping_address_form" : shipping_form,
             request.user.email = email_form.cleaned_data.get("email")
             request.user.save()
             return HttpResponseRedirect(reverse("lfs_my_email"))
-    else:        
+    else:
         email_form = EmailForm(initial={"email" : request.user.email})
 
     return render_to_response(template_name, RequestContext(request, {
             return HttpResponseRedirect(reverse("lfs_my_password"))
     else:
         form = PasswordChangeForm(request.user)
-        
+
     return render_to_response(template_name, RequestContext(request, {
         "form" : form
     }))
-    
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.