def login(request): # Django standard form processing pattern: # User submitted form by POST if request.method == 'POST': print "login view posted" login_form = LoginForm(request.POST) if login_form.is_valid() and request.POST['submit'] == 'Login': # Use API to try to login # Write results to form if failed # Otherwise show home here print "login form was valid" username = login_form.cleaned_data['login_username'] password = login_form.cleaned_data['login_password'] try: print 'calling authenticate' authenticate(request, username, password) print 'authenticate returned' except api.LoginFailed as message: # assume that this always means bad username/password message = u'Please check your username and password' login_form._errors['login_username'] = ErrorList([message]) except TimeOutError: print "Timeout while authenticating" message = u'A timeout occurred. Please try again.' login_form._errors['login_username'] = ErrorList([message]) else: return HttpResponseRedirect(reverse('home')) else: print "login view getted" login_form = LoginForm() return render_to_response('frontpage/login.html', {'loginform': login_form} )
def login_register(request): # Django standard form processing pattern: # User submitted form by POST if request.method == 'POST': print "register view posted" register_form = RegisterForm(request.POST) if register_form.is_valid(): # Use API to try and register # If fails write errors to form # Otherwise login and show home here print "register form was valid" username = register_form.cleaned_data['username'] firstname = register_form.cleaned_data['firstname'] lastname = register_form.cleaned_data['lastname'] password = register_form.cleaned_data['password'] # Django has verified password email = register_form.cleaned_data['email'] # Django has verified that consent is checked print "Calling api people.create" try: api.people.create(username, password, email) except ValueError as error: register_form._errors['username'] = ErrorList(error.args) # And fall down to render_to_response else: try: authenticate(request, username, password) except api.LoginFailed: # This means that the user who was succesfully # registered could not authenticate. print "Successful register -> failed auth" return HttpResponseRedirect(reverse('internalerror')) except TimeOutError: print "Timeout while authenticating" return HttpResponseRedirect(reverse('internalerror')) return HttpResponseRedirect(reverse(welcome)) # User loaded page with form else: print "register view getted" register_form = RegisterForm() login_form = LoginForm() return render_to_response( 'frontpage/login-register.html', { 'loginform': login_form, 'registerform': register_form }, context_instance=RequestContext(request))