def get(self, request, **kwargs): form = AddressForm() if self.session_address_key in request.session: del request.session[self.session_address_key] return super(CheckoutAddressView, self).get(request, form=form, **kwargs)
def post(self, request): form = AddressForm(request.POST) address_id = request.POST['address_id'] if address_id: address_id = int(address_id) address = get_object_or_404(Address, id=address_id) # Binding address permenantly to authenticated user if address.customer is None and request.user.is_authenticated(): address.customer = request.user address.save() request.session[self.session_address_key] = address_id return HttpResponseRedirect(reverse(self.next_step)) if form.is_valid(): data = form.cleaned_data customer = request.user if request.user.is_authenticated( ) else None address = Address.objects.create( customer=customer, first_name=data['first_name'], last_name=data['last_name'], email=data['email'], address1=data['address1'], address2=data['address2'], phone_number=data['phone_number'], fax_number=data['fax_number'], zip_or_postal_code=data['zip_or_postal_code'], city=data['city'], country=data['country'], state=data['state'], company=data['company'], created_by=str(request.user), updated_by=str(request.user)) addresses = self.request.session.get('addresses', []) addresses.append(address.id) self.request.session['addresses'] = addresses request.session[self.session_address_key] = address.id return HttpResponseRedirect(reverse(self.next_step)) return super(CheckoutAddressView, self).get(request, form=form)
def post(self, request): form = AddressForm(request.POST) address_id = request.POST["address_id"] if address_id: address_id = int(address_id) address = get_object_or_404(Address, id=address_id) # Binding address permenantly to authenticated user if address.customer is None and request.user.is_authenticated(): address.customer = request.user address.save() request.session[self.session_address_key] = address_id return HttpResponseRedirect(reverse(self.next_step)) if form.is_valid(): data = form.cleaned_data customer = request.user if request.user.is_authenticated() else None address = Address.objects.create( customer=customer, first_name=data["first_name"], last_name=data["last_name"], email=data["email"], address1=data["address1"], address2=data["address2"], phone_number=data["phone_number"], fax_number=data["fax_number"], zip_or_postal_code=data["zip_or_postal_code"], city=data["city"], country=data["country"], state=data["state"], company=data["company"], created_by=str(request.user), updated_by=str(request.user), ) addresses = self.request.session.get("addresses", []) addresses.append(address.id) self.request.session["addresses"] = addresses request.session[self.session_address_key] = address.id return HttpResponseRedirect(reverse(self.next_step)) return super(CheckoutAddressView, self).get(request, form=form)