【问题标题】:Send confirmation email when changing the email in Django在 Django 中更改电子邮件时发送确认电子邮件
【发布时间】:2012-04-04 11:59:51
【问题描述】:

我目前正在使用django-registration,它运行良好(有一些技巧)。当用户注册时,他必须检查他/她的邮件并点击激活链接。没关系,但是……

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

是否有应用程序、sn-p 或其他可以节省我自己编写的时间

【问题讨论】:

  • 如果这些都不起作用,我的意思是花时间调整两个应用程序以使其协同工作还不如编写一个小视图来读取哈希验证码,并添加一个名为“正在验证”的状态字段" 在用户模型类中。在验证完成之前,用户被锁定。

标签: django registration django-registration


【解决方案1】:

我最近遇到了同样的问题。而且我不喜欢为此拥有另一个应用程序/插件的想法。

您可以通过收听User 模特的单曲(pre_save, post_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 的电子邮件发生更改时,该用户将被停用,并且将向该用户发送一封激活电子邮件。

【讨论】:

  • 对于post_save,您不需要传递请求之类的吗?
  • 其中一个缺陷可能是,如果用户错误地将他们的电子邮件地址更新为不正确的地址,然后没有收到电子邮件,他们将被锁定?
  • 不喜欢让用户帐户处于非活动状态
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-07
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 2011-10-18
  • 1970-01-01
  • 2021-08-09
相关资源
最近更新 更多