def login(request): if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] remember_me = form.cleaned_data['remember_me'] response = HttpResponseRedirect('/management') # delete autologin object and corresponding cookie of last user if exist -- safe forget_user(response=response, request=request) # after custom validation user is known to exist, and no need for password # since we check it in validator user = User.objects.get(email=email) ufps = user.user_forgetpassword_set.all() if ufps: ufps.delete() if remember_me: response = remember_user(user=user, response=response) # logged in user login_user(session=request.session, user=user) # check if the user came from somewhere and render return response # request.method is GET else: # An unbound form form = LoginForm() return render_form('login_form.html', form, '', request)
def activate(request, activation_code): try: ua = User_Activation.objects.get(activation_code=activation_code) except User_Activation.DoesNotExist: raise Http404 response = HttpResponseRedirect('/management') if not ua.activated_at: ua.activate() ua.save() user = ua.user # just in case he has a cookie forget_user(response=response, request=request) response = remember_user(user=user, response=response) # logged in user login_user(session=request.session, user=user) return response
def change_password(request, token): try: ufp = User_ForgetPassword.objects.get(token=token) except User_ForgetPassword.DoesNotExist: raise Http404 response = HttpResponseRedirect('/') import datetime now = datetime.datetime.now() delta = datetime.timedelta(seconds=2*60*60*24) expires = ufp.created_at + delta if now > expires: ufp.delete() return response if request.method == 'POST': form = ChangePasswordForm(request.POST) if form.is_valid(): password = form.cleaned_data['password'] user = ufp.user ufp.delete() user.password = password user.save() response = HttpResponseRedirect('/management') forget_user(response=response, request=request) response = remember_user(user=user, response=response) login_user(session=request.session, user=user) return response else: form = ChangePasswordForm() return render_to_response('changepassword_form.html', { 'form': form, 'token': token, } ,context_instance=RequestContext(request) )