def get(self, request, *args, **kwargs): user_hash = kwargs['user_hash'] user = UserHasher.reverse_hash(user_hash) if user is not None: if user.is_active: request.session['user_pk'] = user.pk context = { "password_reset_form": ResetForm(auto_id=True) } context.update(csrf(request)) return render( request, 'account/forgot-password-reset.html', context ) else: messages.add_message( request, messages.ERROR, 'Account not activated!') return HttpResponse( 'Account not activated!', status_code=403, reason_phrase='You are not allowed to view this\ content because your account is not activated!' ) else: raise Http404("User does not exist")
def post(self, request, *args, **kwargs): try: # get the email inputted email_inputted = request.POST.get("email") # query the database if that email exists user = User.objects.get(email=email_inputted) # generate a recovery hash for that user user_hash = UserHasher.gen_hash(user) user_hash_url = request.build_absolute_uri( reverse('reset_password', kwargs={'user_hash': user_hash})) hash_email_context = RequestContext( request, {'user_hash_url': user_hash_url}) # compose the email email_compose = SendGrid.compose( sender='Codango <{}>'.format(CODANGO_EMAIL), recipient=user.email, subject='Codango: Password Recovery', text=loader.get_template( 'account/forgot-password-email.txt' ).render(hash_email_context), html=loader.get_template( 'account/forgot-password-email.html' ).render(hash_email_context), ) # send email email_response = SendGrid.send(email_compose) # inform the user if mail sent was successful context = { "email_status": email_response } return render( request, 'account/forgot-password-status.html', context ) except ObjectDoesNotExist: messages.add_message( request, messages.ERROR, 'The email specified does not belong to any valid user.') return render(request, 'account/forgot-password.html')