Ejemplo n.º 1
0
    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')