1

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

我试图克服的问题是我们需要通知新的有效用户何时可以开始使用他们的帐户。显然,手动发送电子邮件很麻烦。

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

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

我意识到还有其他测试方法(检查cron 式工作last_login==Noneactive==True帐户),但我希望通知几乎是即时的。

4

3 回答 3

1

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

于 2010-12-15T15:03:48.763 回答
1

ok 5 years latter but this works for me with django 1.8 and python 2.7

The context is: the user create a new account then the admin receives an email to verify the user and chage active to True when the admin makes the change the user receives an email telling that now he can log in.

Sorry for my bad english.

#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

I use the signals in models.py file but you can use it in a signals.py file

#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)
于 2015-12-30T06:27:17.527 回答
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')

希望这可以帮助!

于 2010-12-15T21:07:30.857 回答