如果Django用户的活动状态已更改,请向其发送电子邮件

时间:2010-12-15 14:59:15

标签: django django-models django-users

我已经建立了一个网站,需要某些部分的成员资格。这是一个俱乐部网站,所以要成为网站的会员,你必须成为现实生活中的一员。计划是让俱乐部的某些人检查新成员(我可能让系统在用户注册时向他们发送电子邮件),然后管理员检查Django中用户记录下的active复选框管理并保存用户。

我想要解决的问题是,我们需要新的有效用户,以便他们何时可以开始使用他们的帐户。显然,手动发送电子邮件很麻烦。

是否有办法挂钩save()逻辑,检查记录的active状态是否已更改,如果已激活,请向该用户发送一封电子邮件,告诉他们现在可以登录?

我处于所有电子邮件逻辑之上,我只需要知道将它放在哪里。

我意识到还有其他测试方法(在cron风格的作业上检查last_login==None帐户上的active==True),但我希望通知能够接近即时。

3 个答案:

答案 0 :(得分:1)

是的,您需要使用django signals,特别是post_save()。正如您可能猜到的那样,在模型保存之后调用它,然后您可以实现所需的任何保存后功能(即,写入数据库)。

答案 1 :(得分:1)

确定5年后,但这适用于django 1.8和python 2.7

上下文是:用户创建一个新帐户,然后当管理员进行更改时,管理员会收到一封电子邮件,以验证用户和chage是否为True,用户会收到一封电子邮件,告诉他现在可以登录。

抱歉我的英语不好。

#forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User

#A register form that save field is_active as False
class RegisterForm(UserCreationForm):
    email = forms.EmailField(label=_("E-mail"))
    first_name = forms.CharField(label=_("First Name"))
    last_name = forms.CharField(label=_("Last Name"))
    is_active = forms.BooleanField(required=False, initial=False, label=_("Activo"), widget=forms.HiddenInput())

    class Meta:
        model = User
        fields = ('username','first_name','last_name','email','password1','password2','is_active')

        def save(self, commit=True):
            user = super(UserCreationForm, self).save(commit=False)
            user.first_name = self.cleaned_data['first_name']
            user.last_name = self.cleaned_data['last_name']
            user.email = self.cleaned_data['email']
            user.is_active = self.cleaned_data['is_active']
            if commit:
                user.save()
            return user

我使用models.py文件中的信号,但您可以在signals.py文件中使用它

#models.py
from django.contrib.auth.models import User
from django.db.models import signals
from django.db import models
from django.dispatch import receiver
from django.db.models.signals import pre_save, post_save
from django.conf import settings
from django.core.mail import send_mail

#signal used for is_active=False to is_active=True
@receiver(pre_save, sender=User, dispatch_uid='active')
def active(sender, instance, **kwargs):
    if instance.is_active and User.objects.filter(pk=instance.pk, is_active=False).exists():
        subject = 'Active account'
        mesagge = '%s your account is now active' %(instance.username)
        from_email = settings.EMAIL_HOST_USER
        send_mail(subject, mesagge, from_email, [instance.email], fail_silently=False)

#signal to send an email to the admin when a user creates a new account
@receiver(post_save, sender=User, dispatch_uid='register')
def register(sender, instance, **kwargs):
    if kwargs.get('created', False):
        subject = 'Verificatión of the %s account' %(instance.username)
        mesagge = 'here goes your message to the admin'
        from_email = settings.EMAIL_HOST_USER
        send_mail(subject, mesagge, from_email, [from_email], fail_silently=False)

答案 2 :(得分:0)

这尚未经过测试,但这是我所做的类似的修改版本:

from django.contrib.auth.models import User
from django.db.models import signals
from django.conf import settings
from django.core.mail import send_mail

def pre_user_save(sender, instance, *args, **kwargs):
    if instance.active != User.objects.get(id=instance.id).active:
        send_mail(
            subject='Active changed: %s -> %s' % (instance.username, instance.active),
            message='Guess who changed active status??',
            from_email=settings.SERVER_EMAIL,
            recipient_list=[p[1] for p in settings.MANAGERS],
        )
signals.pre_save.connect(pre_user_save, sender=User, dispatch_uid='pre_user_save')

希望这有帮助!