def login(request, custom_message=None, force_form=False, initial_email=None): """ View for logging in. custom_message is a string to display at the top of the login form. force_form is used when you want to force display of the original form (regardless of whether it's a POST request). """ # If the user is already logged in, redirect to the dashboard. if not request.user.is_anonymous(): return http.HttpResponseRedirect('/accounts/dashboard/') if request.method == 'POST' and not force_form: form = forms.LoginForm(request, request.POST) if form.is_valid(): utils.login(request, form.user) if request.session.test_cookie_worked(): request.session.delete_test_cookie() # If the session contains a 'pending_login' variable, it will be a # tuple of (callback_name, data), where data is an unserialized # Python object and callback_name corresponds to a callback in # ebpub/accounts/callbacks.py. if 'pending_login' in request.session: try: callback, data = request.session['pending_login'] message = callbacks.do_callback(callback, form.user, data) except (TypeError, ValueError): message = None # We're done with the callbacks and don't want to risk them # happening again, so we delete the session value. del request.session['pending_login'] # Save the login message in the session so we can display it # for the user. if message: request.session['login_message'] = message next_url = request.session.pop('next_url', '/accounts/dashboard/') return http.HttpResponseRedirect(next_url) else: form = forms.LoginForm(request, initial={'email': initial_email}) request.session.set_test_cookie() if request.GET.get('next'): request.session['next_url'] = request.GET['next'] custom_message = request.session.pop('login_message', custom_message) context = { 'form': form, 'custom_message': custom_message, } return eb_render(request, 'accounts/login_form.html', context)
def login(request, custom_message=None, force_form=False, initial_email=None): # custom_message is a string to display at the top of the login form. # force_form is used when you want to force display of the original # form (regardless of whether it's a POST request). # If the user is already logged in, redirect to the dashboard. if request.user: return http.HttpResponseRedirect('/accounts/dashboard/') if request.method == 'POST' and not force_form: form = forms.LoginForm(request, request.POST) if form.is_valid(): utils.login(request, form.user) if request.session.test_cookie_worked(): request.session.delete_test_cookie() # If the session contains a 'pending_login' variable, it will be a # tuple of (callback_name, data), where data is an unserialized # Python object and callback_name corresponds to a callback in # ebpub/accounts/callbacks.py. if 'pending_login' in request.session: try: callback, data = request.session['pending_login'] message = callbacks.do_callback(callback, form.user, data) except (TypeError, ValueError): message = None # We're done with the callbacks and don't want to risk them # happening again, so we delete the session value. del request.session['pending_login'] # Save the login message in the session so we can display it # for the user. if message: request.session['login_message'] = message next_url = request.session.pop('next_url', '/accounts/dashboard/') return http.HttpResponseRedirect(next_url) else: form = forms.LoginForm(request, initial={'email': initial_email}) request.session.set_test_cookie() custom_message = request.session.pop('login_message', custom_message) return eb_render(request, 'accounts/login_form.html', { 'form': form, 'custom_message': custom_message })
def login(request, custom_message=None, force_form=False, initial_email=None, override_target=None): """ View for logging in. custom_message is a string to display at the top of the login form. force_form is used when you want to force display of the original form (regardless of whether it's a POST request). override_target can be used to set the form's target URL; otherwise it's self-POSTing. """ if not request.user.is_anonymous(): # If the user is already logged in, redirect to the dashboard. next_url = reverse(dashboard) return http.HttpResponseRedirect(next_url) if request.method == 'POST' and not force_form: next_url = (request.session.pop(REDIRECT_FIELD_NAME, None) or request.POST.get(REDIRECT_FIELD_NAME) or reverse(dashboard)) form = forms.LoginForm(request, request.POST) if form.is_valid(): utils.login(request, form.user_cache) if request.session.test_cookie_worked(): request.session.delete_test_cookie() # If the session contains a 'pending_login' variable, it will be a # tuple of (callback_name, data), where data is an unserialized # Python object and callback_name corresponds to a callback in # ebpub/accounts/callbacks.py. if 'pending_login' in request.session: try: callback, data = request.session['pending_login'] message = callbacks.do_callback(callback, form.user_cache, data) except (TypeError, ValueError): message = None # We're done with the callbacks and don't want to risk them # happening again, so we delete the session value. del request.session['pending_login'] # Save the login message in the session so we can display it # for the user. if message: request.session['login_message'] = message return http.HttpResponseRedirect(next_url) else: form = forms.LoginForm(request, initial={'email': initial_email}) # Rendering the form. request.session.set_test_cookie() if request.REQUEST.get(REDIRECT_FIELD_NAME): request.session[REDIRECT_FIELD_NAME] = request.REQUEST[REDIRECT_FIELD_NAME] custom_message = request.session.pop('login_message', custom_message) context = RequestContext(request, { 'form': form, 'custom_message': custom_message, 'target': override_target or reverse(login), }) return eb_render(request, 'accounts/login_form.html', context)
if request.method == 'POST': form = forms.PasswordRegistrationForm(request.POST) if form.is_valid(): u = User.objects.create_user( email=form.cleaned_data['e'], password=form.cleaned_data['password1'], main_metro=get_metro()['short_name'], ) u.save() utils.login(request, u) # Look for any PendingUserActions for this e-mail address and # execute the callbacks. for action in PendingUserAction.objects.filter(email=u.email): data = callbacks.unserialize(action.data) callbacks.do_callback(action.callback, u, data) action.delete() request.session['login_message'] = 'Your account was created! Thanks for signing up.' return http.HttpResponseRedirect(reverse(dashboard)) else: form = forms.PasswordRegistrationForm(initial={'e': email, 'h': email_hash}) return eb_render(request, 'accounts/register_form_2.html', {'form': form}) ################### # PASSWORD CHANGE # ################### def request_password_change(request): if request.method == 'POST': form = forms.PasswordResetRequestForm(request.POST)
form = forms.PasswordRegistrationForm(request.POST) if form.is_valid(): u = User.objects.create_user( email=form.cleaned_data['e'], password=form.cleaned_data['password1'], main_metro=get_metro()['short_name'], creation_date=datetime.datetime.now(), is_active=True, ) utils.login(request, u) # Look for any PendingUserActions for this e-mail address and # execute the callbacks. for action in PendingUserAction.objects.filter(email=u.email): data = callbacks.unserialize(action.data) callbacks.do_callback(action.callback, u, data) action.delete() request.session[ 'login_message'] = 'Your account was created! Thanks for signing up.' return http.HttpResponseRedirect('../dashboard/') else: form = forms.PasswordRegistrationForm(initial={ 'e': email, 'h': email_hash }) return eb_render(request, 'accounts/register_form_2.html', {'form': form}) ################### # PASSWORD CHANGE #
def login(request, custom_message=None, force_form=False, initial_email=None, override_target=None): """ View for logging in. custom_message is a string to display at the top of the login form. force_form is used when you want to force display of the original form (regardless of whether it's a POST request). override_target can be used to set the form's target URL; otherwise it's self-POSTing. """ if not request.user.is_anonymous(): # If the user is already logged in, redirect to the dashboard. next_url = reverse(dashboard) return http.HttpResponseRedirect(next_url) if request.method == 'POST' and not force_form: next_url = (request.session.pop(REDIRECT_FIELD_NAME, None) or request.POST.get(REDIRECT_FIELD_NAME) or reverse(dashboard)) form = forms.LoginForm(request, request.POST) if form.is_valid(): utils.login(request, form.user_cache) if request.session.test_cookie_worked(): request.session.delete_test_cookie() # If the session contains a 'pending_login' variable, it will be a # tuple of (callback_name, data), where data is an unserialized # Python object and callback_name corresponds to a callback in # ebpub/accounts/callbacks.py. if 'pending_login' in request.session: try: callback, data = request.session['pending_login'] message = callbacks.do_callback(callback, form.user_cache, data) except (TypeError, ValueError): message = None # We're done with the callbacks and don't want to risk them # happening again, so we delete the session value. del request.session['pending_login'] # Save the login message in the session so we can display it # for the user. if message: request.session['login_message'] = message return http.HttpResponseRedirect(next_url) else: form = forms.LoginForm(request, initial={'email': initial_email}) # Rendering the form. request.session.set_test_cookie() if request.REQUEST.get(REDIRECT_FIELD_NAME): request.session[REDIRECT_FIELD_NAME] = request.REQUEST[ REDIRECT_FIELD_NAME] custom_message = request.session.pop('login_message', custom_message) context = RequestContext( request, { 'form': form, 'custom_message': custom_message, 'target': override_target or reverse(login), }) return eb_render(request, 'accounts/login_form.html', context)