2010年10月12日火曜日

djangoでnew userにpassword change mailを送る

サイトにアカウントを作成したら、ユーザーにパスワードを設定してもらえるようにメールを送りたい。
 1)emailを必須に(django/contrib/auth/models.py version 1.2.3)
@@ -196,7 +196,7 @@
     username = models.CharField(_('username'), max_length=30, unique=True, help_text=_("Required. 30 characters or fewer. Letters, numbers and @/./+/-/_ characters"))
     first_name = models.CharField(_('first name'), max_length=30, blank=True)
     last_name = models.CharField(_('last name'), max_length=30, blank=True)
-    email = models.EmailField(_('e-mail address'), blank=True)
+    email = models.EmailField(_('e-mail address'), blank=False)
     password = models.CharField(_('password'), max_length=128, help_text=_("Use '[algo]$[salt]$[hexdigest]' or use the <a href=\"password/\">change password form</a>."))
     is_staff = models.BooleanField(_('staff status'), default=False, help_text=_("Designates whether the user can log into this admin site."))
     is_active = models.BooleanField(_('active'), default=True, help_text=_("Designates whether this user should be treated as active. Unselect this instead of deleting accounts."))
2)admin pageに表示(django/contrib/auth/admin.py version 1.2.3)
@@ -34,7 +34,7 @@
     add_fieldsets = (
         (None, {
             'classes': ('wide',),
-            'fields': ('username', 'password1', 'password2')}
+            'fields': ('username', 'password1', 'password2', 'email')}
         ),
     )
     form = UserChangeForm
 3)User post save signal(profile/models.py)
from django.db.models.signals import post_save
from django.contrib.sites.models import Site
from django.core.mail import send_mail
from django.contrib.auth.tokens import default_token_generator
from django.template import Context, loader
from django.utils.http import int_to_base36

def sendEmailToNewUser(sender, **kwargs):
    if kwargs['created']:
        domain_override=None
        email_template_name='registration/add_member_email.html'
        use_https=False
        token_generator=default_token_generator
        '''
        Generates a one-use only link for resetting password and sends to the user
        from django.contrib.auth.forms.PasswordResetForm.save
        '''
        if not domain_override:
            current_site = Site.objects.get_current()
            site_name = current_site.name
            domain = current_site.domain
        else:
            site_name = domain = domain_override
        user = User.objects.get(id=kwargs['instance']._get_pk_val())
        t = loader.get_template(email_template_name)
        c = {
            'email': user.email,
            'domain': domain,
            'site_name': site_name,
            'uid': int_to_base36(user.id),
            'user': user,
            'token': token_generator.make_token(user),
            'protocol': use_https and 'https' or 'http',
        }
        send_mail(_("Set your account on %s as new user") % site_name,
            t.render(Context(c)),  None,  [user.email])

post_save.connect(sendEmailToNewUser, sender=User)
初期化時にはsuperuser作成のときにcrashするので、最後の行をコメントアウト

0 件のコメント:

コメントを投稿