def registration_view(request): context = {"user_form": UserCreationForm, "customer_form": CustomerForm, "address_form": AddressForm, "errors": []} if request.POST: valid = True user_form = UserCreationForm(request.POST) customer_form = CustomerForm(request.POST) address_form = AddressForm(request.POST) if not user_form.is_valid(): valid = False context["errors"].append("Invalid username or password.") if not customer_form.is_valid(): valid = False context["errors"].append("Invalid name, email or telephone.") if not address_form.is_valid(): valid = False context["errors"].append("Invalid address.") if valid: user = user_form.save() address = address_form.save() customer = customer_form.save(commit=False) customer.address = address customer.user = user customer.save() return redirect("login") return render_to_response("registration/create_user.html", context, context_instance=RequestContext(request))
def restaurant_creation_view(request): context = {"restaurant_form": RestaurantForm, "address_form": AddressForm, "errors": []} if request.POST: valid = True restaurant_form = RestaurantForm(request.POST) address_form = AddressForm(request.POST) if not restaurant_form.is_valid(): valid = False context["errors"].append("Invalid Name or Telephone.") if not address_form.is_valid(): valid = False context["errors"].append("Invalid address.") if valid: address = address_form.save() restaurant = restaurant_form.save(commit=False) restaurant.address = address restaurant.save() redirect('restaurant_list') return render_to_response('restaurant_creation.html', context=context, context_instance=RequestContext(request))
def checkout_address(request): form = AddressForm(request.POST) if form.is_valid(): check_address = form.save(commit=False) guest_email_id = request.session.get('guest_email_id') user = request.user if user.is_authenticated: billing, billing_created = Billing.objects.get_or_create( user=user, email=user.email) elif guest_email_id is not None: guest_email_obj = GuestEmail.objects.get(id=guest_email_id) billing, billing_created = Billing.objects.get_or_create( email=guest_email_obj.email) else: pass if billing is not None: check_address.user = user check_address.billing = billing check_address.save() else: print(error) return redirect('home') return redirect('checkout_success')
def checkout(request): cart_obj, new_obj = Cart.objects.new_or_get(request) order_obj = None if new_obj or cart_obj.products.count() == 0: return redirect('home') user = request.user billing = None login_form = LoginForm() guest_form = GuestForm() address_form = AddressForm() guest_email_id = request.session.get('guest_email_id') if user.is_authenticated: billing, billing_created = Billing.objects.get_or_create( user=user, email=user.email) elif guest_email_id is not None: guest_email_obj = GuestEmail.objects.get(id=guest_email_id) billing, billing_created = Billing.objects.get_or_create( email=guest_email_obj.email) else: pass address_query = None if billing is not None: address_query = Address.objects.filter(billing=billing) order_query = Order.objects.filter(billing=billing, cart=cart_obj, active=True) if order_query.count() == 1: order_obj = order_query.first() else: old_order_query = Order.objects.exclude(billing=billing).filter( cart=cart_obj) if old_order_query.exists(): old_order_query.update(active=False) order_obj = Order.objects.create(billing=billing, cart=cart_obj) context = { 'order': order_obj, 'billing': billing, 'guest_form': guest_form, 'login_form': login_form, 'address_form': address_form, 'address_query': address_query, } return render(request, 'carts/checkout.html', context)