Example #1
0
    def form_valid(self, form):
        from prologin.email import send_email
        from django.utils.encoding import force_bytes
        from django.utils.http import urlsafe_base64_encode
        from django.contrib.auth.tokens import default_token_generator

        email = form.cleaned_data['email']
        active_users = auth.get_user_model().objects.filter(
            email__iexact=email, is_active=True)
        for user in active_users:
            # Make sure that no email is sent to a user that actually has
            # a password marked as unusable
            if not user.has_usable_password() and not user.legacy_md5_password:
                continue
            uid = urlsafe_base64_encode(force_bytes(user.pk))
            token = default_token_generator.make_token(user)
            url = absolute_site_url(
                self.request,
                reverse('users:password_reset_confirm',
                        kwargs=dict(uidb64=uid, token=token)))
            send_email("users/mails/pswd_reset", user.email, {
                'user': user,
                'url': url
            })

        messages.success(
            self.request,
            _("You should receive an email at %(email)s in a few moments. "
              "Please follow the instructions to complete the password reset.")
            % {'email': email})
        return super().form_valid(form)
Example #2
0
def send_activation_email(user, request=None):
    from django.test import RequestFactory
    from prologin.email import send_email
    request = request or RequestFactory().generic(
        'get', '/', secure=settings.SITE_BASE_URL.startswith('https'))
    activation = users.models.UserActivation.objects.register(user)
    url = absolute_site_url(request,
                            reverse('users:activate', args=[activation.slug]))
    send_email("users/mails/activation", user.email, {
        'user': user,
        'url': url
    })