def register(request): # Cannot register if logged in already if request.user.is_authenticated(): return HttpResponseRedirect(reverse('login')) # The registration form form = None # Form has been submitted if request.method == 'POST': form = RegisterForm(request.POST) # Validate the registration form if form.is_valid(): user = User.create_user(form.cleaned_data['username'], form.cleaned_data['password1']) user.first_name = form.cleaned_data['first_name'] user.last_name = form.cleaned_data['last_name'] user.is_active = False user.save() profile = UserProfile.objects.create( phone_number=form.cleaned_data['phone'], user=user) stub = RegistrationStub.objects.create(user=user) # Send confirmation email hostname = _hostname(protocol="") activate_uri = reverse('activate') activate_link = '{}{}?key={}'.format(hostname, activate_uri, stub.activationCode) email_subject = "Welcome to Obietaxi!" email_to = [form.cleaned_data['username']] msg_body = "Welcome to Obietaxi! Your account has already been created with this email address, now all you need to do is confirm your accout by clicking on the link below. If there is no link, you should copy & paste the address into your browser's address bar and navigate there.\n\n{}".format( activate_link) send_email(email_to=email_to, email_subject=email_subject, email_body=msg_body) messages.add_message( request, messages.SUCCESS, "Your account has been created. Check your email for a confirmation link to complete the registration process." ) return HttpResponseRedirect(reverse('login')) # Form needs to be rendered else: form = RegisterForm() # Render the form (possibly with errors if form did not validate) return render_to_response('register.html', locals(), context_instance=RequestContext(request))
def register( request ): # Cannot register if logged in already if request.user.is_authenticated(): return HttpResponseRedirect( reverse('login') ) # The registration form form = None # Form has been submitted if request.method == 'POST': form = RegisterForm( request.POST ) # Validate the registration form if form.is_valid(): user = User.create_user( form.cleaned_data['username'], form.cleaned_data['password1'] ) user.first_name = form.cleaned_data['first_name'] user.last_name = form.cleaned_data['last_name'] user.is_active = False user.save() profile = UserProfile.objects.create( phone_number=form.cleaned_data['phone'], user=user ) stub = RegistrationStub.objects.create( user=user ) # Send confirmation email hostname = _hostname( protocol="" ) activate_uri = reverse( 'activate' ) activate_link = '{}{}?key={}'.format( hostname, activate_uri, stub.activationCode ) email_subject = "Welcome to Obietaxi!" email_to = [form.cleaned_data['username']] msg_body = "Welcome to Obietaxi! Your account has already been created with this email address, now all you need to do is confirm your accout by clicking on the link below. If there is no link, you should copy & paste the address into your browser's address bar and navigate there.\n\n{}".format( activate_link ) send_email( email_to=email_to, email_subject=email_subject, email_body=msg_body ) messages.add_message( request, messages.SUCCESS, "Your account has been created. Check your email for a confirmation link to complete the registration process." ) return HttpResponseRedirect( reverse('login') ) # Form needs to be rendered else: form = RegisterForm() # Render the form (possibly with errors if form did not validate) return render_to_response( 'register.html', locals(), context_instance=RequestContext(request) )
def forgot_password(request): ''' if the user forgot their password renders ForgotPasswordForm, or processes it if a POST request ''' if request.method == 'POST': form = ForgotPasswordForm(request.POST) if form.is_valid(): data = form.cleaned_data # Does the user in the email field even exist? try: user = User.objects.get(username=data['username']) profile = UserProfile.objects.get(user=user) except User.DoesNotExist: return HttpResponseRedirect(reverse('main_page')) # Ok, they do. Send them an email reset_string = random_string() profile.password_reset_stub = reset_string profile.save() reset_link = '%s%s?rid=%s&uid=%s' % (_hostname(), reverse('reset_password'), reset_string, str(profile.id)) email_body = render_message( 'mongologin/static/emails/forgot_password.txt', locals()) send_email(email_to=user.username, email_body=email_body, email_subject="Reset your password") messages.add_message( request, messages.SUCCESS, "An email has been sent to you with instructions on resetting your password." ) return HttpResponseRedirect(reverse('main_page')) else: form = ForgotPasswordForm() return render_to_response('forgot_password.html', locals(), context_instance=RequestContext(request))
def forgot_password( request ): ''' if the user forgot their password renders ForgotPasswordForm, or processes it if a POST request ''' if request.method == 'POST': form = ForgotPasswordForm(request.POST) if form.is_valid(): data = form.cleaned_data # Does the user in the email field even exist? try: user = User.objects.get(username=data['username']) profile = UserProfile.objects.get(user=user) except User.DoesNotExist: return HttpResponseRedirect( reverse('main_page') ) # Ok, they do. Send them an email reset_string = random_string() profile.password_reset_stub = reset_string profile.save() reset_link = '%s%s?rid=%s&uid=%s'%( _hostname(), reverse( 'reset_password' ), reset_string, str(profile.id) ) email_body = render_message( 'mongologin/static/emails/forgot_password.txt', locals() ) send_email( email_to=user.username, email_body=email_body, email_subject="Reset your password" ) messages.add_message( request, messages.SUCCESS, "An email has been sent to you with instructions on resetting your password." ) return HttpResponseRedirect( reverse('main_page') ) else: form = ForgotPasswordForm() return render_to_response( 'forgot_password.html', locals(), context_instance=RequestContext(request) )