Frage

I'm using django-1.4.3 and the superuser can create users and assign password to them. Here I'm using django-email-as-username to enable users to login with their email as username. When the superuser adds a new user, the newly added user should be notified through email with his username and password.

I'm able to send email after user creation using post_save signal. But I couldn't get the password as it will be encrypted and stored. I want to email the user, the raw password. How can I achieve this?

War es hilfreich?

Lösung

I have achieved it using the code below:

from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver

@receiver(post_save, sender = User)
def my_callback(sender, **kwargs):
    import inspect
    records =[]
    for frame_record in inspect.stack():
        records.append(frame_record[3])
        if frame_record[3]=='get_response':
            request = frame_record[0].f_locals['request']
            email = request.POST.get('email')
            password1 =  request.POST.get('password1')
            password2 = request.POST.get('password2')
            if email != None and password1 != None and password2 != None and password1 == password2:
                html_content ="Hi,<br> Your username: %s <br> Password: %s"
                from_email   = settings.DEFAULT_FROM_EMAIL
                message      = EmailMessage('Welcome', html_content %(email, password1), from_email, [email])
                message.content_subtype = "html"  # Main content is now text/html
                message.send()
            break
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top