2012-03-19 124 views
2

我目前使用django-registration,它运行良好(有一些技巧)。当用户注册时,他必须检查他/她的邮件并点击激活链接。这很好,但...更改Django中的电子邮件时发送确认电子邮件

如果用户更改电子邮件会怎么样?我想给他/她发一封电子邮件,以确认他是电子邮件地址的所有者...

是否有一个应用程序,代码段或什么的这将节省我写它的时间我自己

+0

试试这些? http://stackoverflow.com/questions/2296846/django-apps-for-changing-user-email-with-verification – CppLearner 2012-03-20 00:05:32

+1

如果没有这项工作,我的意思是调整两个应用程序一起工作的时间可能花在编写一个读取哈希验证代码的小视图,并在用户模型类中添加一个名为“验证”的状态字段。在验证完成之前,用户被锁定。 – CppLearner 2012-03-20 00:07:16

回答

4

我最近面临同样的问题。而且我不喜欢为此提供另一个应用/插件的想法。

您可以做到这一点,听User模型单打(pre_savepost_save),并使用RegistrationProfile

signals.py:

from django.contrib.sites.models import Site, RequestSite 
from django.contrib.auth.models import User 
from django.db.models.signals import post_save, pre_save 
from django.dispatch import receiver 
from registration.models import RegistrationProfile 


# Check if email change 
@receiver(pre_save,sender=User) 
def pre_check_email(sender, instance, **kw): 
    if instance.id: 
     _old_email = instance._old_email = sender.objects.get(id=instance.id).email 
     if _old_email != instance.email: 
      instance.is_active = False 

@receiver(post_save,sender=User) 
def post_check_email(sender, instance, created, **kw): 
    if not created: 
     _old_email = getattr(instance, '_old_email', None) 
     if instance.email != _old_email: 
      # remove registration profile 
      try: 
       old_profile = RegistrationProfile.objects.get(user=instance) 
       old_profile.delete() 
      except: 
       pass 

      # create registration profile 
      new_profile = RegistrationProfile.objects.create_profile(instance) 

      # send activation email 
      if Site._meta.installed: 
       site = Site.objects.get_current() 
      else: 
       site = RequestSite(request) 
      new_profile.send_activation_email(site) 

因此,只要User的电子邮件是更改后,用户将被停用,激活电子邮件将发送给用户。

+0

对于'post_save',你是否需要传递请求或者其他的? – fpghost 2014-02-28 12:52:31

+0

这可能是一个缺陷,如果用户错误地更新他们的电子邮件地址到一个不正确的地址,然后没有收到电子邮件,他们将被锁定? – fpghost 2014-02-28 13:03:02

+0

不喜欢让用户帐户处于非活动状态的想法 – 2016-02-15 12:14:45