def edit_address_view(request, pk): address = get_object_or_404(Address, pk=pk, user=request.user) if request.method == 'POST': form = AddressForm(request.POST, instance=address) if form.is_valid(): contact_name = form.cleaned_data['contact_name'] country_name = form.cleaned_data['country_name'] city_name = form.cleaned_data['city_name'] state_name = form.cleaned_data['state_name'] street_address_line_1 = form.cleaned_data['street_address_line_1'] street_address_line_2 = form.cleaned_data['street_address_line_2'] zipcode = form.cleaned_data['zipcode'] phone_number = form.cleaned_data['phone_number'] country_code_phone_number = form.cleaned_data['country_code_phone_number'] address.contact_name = contact_name address.country_name = country_name address.city_name = city_name address.state_name = state_name address.street_address_line_1 = street_address_line_1 address.street_address_line_2 = street_address_line_2 address.zipcode = zipcode address.phone_number = phone_number address.country_code_phone_number = country_code_phone_number address.last_updated_datetime = datetime.datetime.now() address.save() return HttpResponseRedirect(reverse('accounts:address')) else: form = AddressForm(instance=address) return render(request, "accounts/edit_address.html", {'form': form})
def save(request, address_id=None): user=request.user if request.method =='POST': form=AddressForm(request.POST) if form.is_valid(): if form.cleaned_data['address_id']: address=Address.objects.get(pk=form.cleaned_data['address_id']) else: address=Address() address.name=form.cleaned_data['name'] address.address=form.cleaned_data['address'] address.postal_code=form.cleaned_data['postal_code'] address.user=user address.save() return HttpResponseRedirect('/address/') else: if address_id: address=Address.objects.get(pk=address_id) if address.user==request.user: form=AddressForm(initial={'address_id': address.id, 'name': address.name, 'address': address.address, 'postal_code': address.postal_code}) else: form=AddressForm() return render_to_response('address_form.html', { 'form': form}, context_instance=RequestContext(request))
def test_address_form_fails_without_some_field(self): form = AddressForm({ 'house_number_name': '8 house', 'street': '11 test street', 'town': 'test town', }) self.assertFalse(form.is_valid())
def add_address_company(request): if request.method == 'POST': form = AddressForm(request.POST) if form.is_valid(): endereco = form.save() return redirect(request.user.get_absolute_url() ) else: form = AddressForm(initial={'empregador': request.user.empregador}) return render_to_response('accounts/add_address_company.html', {'form': form}, RequestContext(request))
def test_address_form(self): form = AddressForm({ 'house_number_name': '8 house', 'street': '11 test street', 'town': 'test town', 'postcode': 'sss', }) self.assertTrue(form.is_valid()) # get user user = User.objects.get(pk=16) # assign user to address address = form.save(commit=False) address.user = user # save address address.save() # check if saved address is user's address now self.assertTrue(address, user.address_set.all())
def address_create_view(request): form = AddressForm(request.POST or None) if form.is_valid(): print(request.POST) instance = form.save(commit=False) billing_profile, billing_profile_created = BillingProfile.objects.new_or_get(request=request) cart_obj = Cart.objects.new_or_get(request=request) if billing_profile is not None: order_obj, order_obj_created = Order.objects.new_or_get(cart_obj=cart_obj, billing_profile=billing_profile) payment_method = request.POST.get("payment_method", None) if payment_method: order_obj.payment_method = payment_method order_obj.save() instance.billing_profile = billing_profile instance.save() request.session['billing_address_id'] = instance.id else: messages.error(request, "Might have some problem....!") return redirect("cart:checkout") return redirect("cart:checkout")
def edit_customer(request, profile_id): if profile_id is not None: user_profile = get_object_or_404(UserProfile, pk=profile_id) user = user_profile.user try: address = Address.objects.get(user=user) except Address.DoesNotExist: address = None if request.POST: userprofile_form = UserProfileForm(request.POST, ) if userprofile_form.is_valid(): user_profile.title = userprofile_form.cleaned_data['title'] user_profile.date_of_birth = userprofile_form.cleaned_data[ 'date_of_birth'] user_profile.phone = userprofile_form.cleaned_data['phone'] user_profile.save() address_form = AddressForm(request.POST) if address_form.is_valid(): if not address: address = Address() address.user = user address.line_1 = address_form.cleaned_data['line_1'] address.line_2 = address_form.cleaned_data['line_2'] address.city = address_form.cleaned_data['city'] address.county = address_form.cleaned_data['county'] address.postcode = address_form.cleaned_data['postcode'] address.save() return render_to_response( 'admin/accounts/edit_customer.html', { 'userprofile_form': UserProfileForm(instance=user_profile), 'address_form': AddressForm(instance=address), 'user_profile': user_profile, }, RequestContext(request))
def edit_customer(request, profile_id): if profile_id is not None: user_profile = get_object_or_404(UserProfile, pk=profile_id) user = user_profile.user try: address = Address.objects.get(user=user) except Address.DoesNotExist: address = None if request.POST: userprofile_form = UserProfileForm(request.POST,) if userprofile_form.is_valid(): user_profile.title = userprofile_form.cleaned_data['title'] user_profile.date_of_birth = userprofile_form.cleaned_data['date_of_birth'] user_profile.phone = userprofile_form.cleaned_data['phone'] user_profile.save() address_form = AddressForm(request.POST) if address_form.is_valid(): if not address: address = Address() address.user = user address.line_1 = address_form.cleaned_data['line_1'] address.line_2 = address_form.cleaned_data['line_2'] address.city = address_form.cleaned_data['city'] address.county = address_form.cleaned_data['county'] address.postcode = address_form.cleaned_data['postcode'] address.save() return render_to_response('admin/accounts/edit_customer.html', { 'userprofile_form': UserProfileForm(instance=user_profile), 'address_form': AddressForm(instance=address), 'user_profile' : user_profile, }, RequestContext(request) )
def new_address(request): # if this is a POST request we need to process the form data if request.method == 'POST': # create a form instance and populate it with data from the request: form = AddressForm(request.POST) # check whether it's valid: if form.is_valid(): # process the data in form.cleaned_data as required # ... # redirect to a new URL: contact_name = form.cleaned_data['contact_name'] country_name = form.cleaned_data['country_name'] city_name = form.cleaned_data['city_name'] state_name = form.cleaned_data['state_name'] street_address_line_1 = form.cleaned_data['street_address_line_1'] street_address_line_2 = form.cleaned_data['street_address_line_2'] zipcode = form.cleaned_data['zipcode'] phone_number = form.cleaned_data['phone_number'] country_code_phone_number = form.cleaned_data['country_code_phone_number'] Address.objects.create(user=request.user, contact_name=contact_name, country_name=country_name, city_name=city_name,state_name=state_name, street_address_line_1=street_address_line_1, street_address_line_2=street_address_line_2, zipcode=zipcode, phone_number=phone_number, country_code_phone_number=country_code_phone_number, ) return HttpResponseRedirect(reverse("accounts:address")) # if a GET (or any other method) we'll create a blank form else: form = AddressForm() return render(request, "accounts/new_address.html", {'form': form})
def delete_address(request, pk): address = Address.objects.get(pk=pk) form = AddressForm(instance=address) for _, field in form.fields.items(): field.widget.attrs['disabled'] = True if request.method == 'GET': context = { 'form': form, 'address': address, } return render(request, 'accounts/delete_address.html', context) else: address.delete() return redirect('user addresses')
def checkout_home(request): order_obj = None if request.user.is_authenticated: cart_obj = Cart.objects.new_or_get(request) if cart_obj.products.count() == 0: messages.info(request, "Cart is empty now!") return redirect("cart:home") address_form = AddressForm() billing_profile, billing_profile_created = BillingProfile.objects.new_or_get( request=request) if billing_profile is not None: order_obj, order_obj_created = Order.objects.new_or_get( cart_obj=cart_obj, billing_profile=billing_profile) billing_address_id = request.session.get('billing_address_id') if billing_address_id: order_obj.billing_address = Address.objects.get( id=billing_address_id) del request.session['billing_address_id'] order_obj.save() if request.method == 'POST': if order_obj.check_done(): if order_obj.payment_method == 'Cash On Delivery': request.session['cart_items_count'] = 0 del request.session["cart_id"] return redirect("cart:success") else: order_obj.mark_paid() print( "Credit or debit selected. go to the online payment page." ) return redirect("cart:payment_page") else: messages.warning(request, "Please login, you cannot access this page.") return redirect("account:login") context = { 'address_form': address_form, 'cart_obj': cart_obj, 'order_obj': order_obj, 'billing_profile': billing_profile } return render(request, "carts/checkout.html", context=context)
def create_address(request): if request.method == 'GET': form = AddressForm() context = { 'form': form, } return render(request, 'accounts/create_address.html', context) else: form = AddressForm(request.POST) if form.is_valid(): address = form.save(commit=False) address.profile_id = request.user.profile.id address.is_default = False address.save() return redirect('user addresses') context = { 'form': form, } return render(request, 'accounts/create_address.html', context)
def edit_address(request, pk): address = Address.objects.get(pk=pk) if request.method == 'GET': form = AddressForm(instance=address) context = { 'form': form, 'address': address, } return render(request, 'accounts/edit_address.html', context) else: form = AddressForm(request.POST, instance=address) if form.is_valid(): address = form.save(commit=False) address.profile = request.user.profile address.is_default = False address.save() return redirect('user addresses') context = { 'form': form, 'address': address, } return render(request, 'accounts/edit_address.html', context)
def checkout_home(request): stripe_session_id = request.session.get('stripe_session_id') cart_obj, cart_created = Cart.objects.new_or_get(request) order_obj = None if cart_created or cart_obj.products.count() == 0: return redirect("cart:home") login_form = UserLoginForm() address_form = AddressForm() billing_address_id = request.session.get("billing_address_id", None) shipping_address_id = request.session.get("shipping_address_id", None) # billing address save with order billing_profile, billing_guest_profile_created = BillingProfile.objects.new_or_get( request) address_qs = None if billing_profile is not None: address_qs = Address.objects.filter(billing_profile=billing_profile) order_obj, order_obj_created = Order.objects.new_or_get( billing_profile, cart_obj) request.session["order_obj_id"] = order_obj.id if shipping_address_id: order_obj.shipping_address = Address.objects.get( id=shipping_address_id) del request.session["shipping_address_id"] if billing_address_id: order_obj.billing_address = Address.objects.get( id=billing_address_id) del request.session["billing_address_id"] if billing_address_id or shipping_address_id: order_obj.save() # stripe integration if not stripe_session_id: try: order_obj_forStripe = Order.objects.get(cart=cart_obj) product_data_forStripe = [] host_uri = "{}://{}".format(request.scheme, request.get_host()) # Add each item into stripe list for item in order_obj_forStripe.cart.products.all(): product_data_forStripe.append({ 'name': item.title, 'description': item.description, 'images': [urljoin(host_uri, "media/" + str(item.image))], 'amount': int(item.price * 100), 'currency': 'usd', 'quantity': 1, }) # stripe checkout stripe.api_key = settings.STRIPE_SECRET_KEY stripeSession = stripe.checkout.Session.create( payment_method_types=['card'], line_items=product_data_forStripe, success_url=urljoin( host_uri, reverse("cart:success") + '?session_id={CHECKOUT_SESSION_ID}'), cancel_url=urljoin(host_uri, 'checkout/cancel'), ) stripe_session_id = stripeSession.get('id') request.session['stripe_session_id'] = stripe_session_id except Order.DoesNotExist: # no need stripe if no order pass context = { "object": order_obj, "billing_profile": billing_profile, "login_form": login_form, "address_form": address_form, "address_qs": address_qs, "stripe_pub_key": settings.STRIPE_PUBLISHABLE_KEY, "stripe_session_id": stripe_session_id, } return render(request, "cart/checkout.html", context)