def create_account(request): """ Create an account """ problems = [] if request.method == 'POST': # Form is submitted form = CreateUser(request.POST) if form.is_valid(): # Get form-data username = form.cleaned_data['username'] first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] email = form.cleaned_data['email'] password = form.cleaned_data['password'] address = form.cleaned_data['address'] phone_number = form.cleaned_data['phone_number'] # Do extra validation: # Check if username is available: users = User.objects.filter(username=username) if not len(users) == 0: # The username already exists problems.append("Username already taken") else: # We could do more checks here, for instance "is e-mail unique". But we won't... # Create the User object: user = User.objects.create_user(username, email, password) user.first_name = first_name user.last_name = last_name user.save() # Create the Customer object: cust = Customer(address=address, phone_number=phone_number, user=user) cust.save() # The user-account is now created. # Log in and redirect to welcome.html: # Authenticate (this needs to be done before login()) u_auth = authenticate(username=username, password=password) # Make sure that authentication was successful, if we can't authenticate the newly created # user, something is very wrong.. assert type(u_auth) is not None # Log on the user: login(request, u_auth) # Send user to the welcome page: return redirect('/account/welcome') else: # Start a new, blank, form form = CreateUser() # Either form will be a newly created one, or an old one with information attached request_context = RequestContext(request, { 'form': form, 'problems': problems, }) return render(request, "create_account.html", request_context)
def CompleteUserInfo(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/') if request.method == "POST": if request.user.is_complete == True: form = CustomerForm(request.POST, request.FILES, instance=request.user.customer) else: form = CustomerForm(request.POST, request.FILES) if form.is_valid(): if 'submit' in request.POST: temp = Customer() temp = form.save(commit=False) request.user.is_complete = True temp.user = request.user temp.save() request.user.save() return HttpResponseRedirect('/') else: if request.user.is_complete == True: form = CustomerForm(instance=request.user.customer) else: form = CustomerForm() context = { 'form': form, 'user': request.user, } return render(request, 'shopping/user_info.html', context)
def create_missing_customer(request): """ This is called if the current user does not have a Customer-object associated to it """ if request.method == 'POST': form = CreateCustomer(request.POST) if form.is_valid(): address = form.cleaned_data['address'] phone_number = form.cleaned_data['phone_number'] user = request.user cust = Customer(address=address, phone_number=phone_number, user=user) cust.save() return redirect('/account/thankyou') else: form = CreateCustomer() request_context = RequestContext(request, {'form': form}) return render(request, "create_customer.html", request_context)