django-allauth:如何将用户设置为仅在电子邮件验证后生效 [英] django-allauth: How to set user to active only after e-mail verification

查看:480
本文介绍了django-allauth:如何将用户设置为仅在电子邮件验证后生效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django-allauth作为管理员后端创建用户帐户的一种方式。我想要发生的是:

I'm using django-allauth primarily as a way to create user accounts for the admin backend. What I would like to have happen is:

1)当用户通过注册程序发送验证电子邮件(我有这样工作到目前为止),并将用户设置为无效,员工,以及通过defult分配给他们的SurveyManager组。目前,用户创建的活动设置为true,员工设置为false,并且没有分配任何组。

1) When a user goes through the sign up procedure, send out the verification e-mail (I have this working so far) and set the user as inactive, staff, and with the "SurveyManager" group assigned to them by defult. Currently, the user is created with active set to true, staff set to false, and no groups assigned.

2)点击他们的电子邮件中的链接进行验证他们的地址,我希望用户被设置为活动,所以他们可以通过管理员后端登录。

2) After clicking the link in their e-mail to verify their address, I'd like the user to be set to active, so they can then log in through the admin backend.

我的具体问题是我不知道:1)如何或在哪里设置用户的活动,员工和组的默认值 - 我想像这将在models.py文件中完成,但我的理解是,用户模型包含在auth应用程序中;和2)如果电子邮件验证完成,如何触发代码将用户活动标志更改为true。

My specific problem is that I don't know: 1) how or where to set the defaults for active, staff, and group of the user -- I imagine this would be done in a models.py file, but it's my understanding that the user model is contained in the auth app; and 2) how to trigger the code to change the user active flag to true once the e-mail verification is complete.

提前感谢 - 如果这是不好的帖子,这是我的第一个!

Thanks in advance -- sorry if this is a poorly-created post, it's my first!

推荐答案

我似乎(主要)通过使用信号解决了我的问题。 这篇文章给了我这个想法(但不幸的是,不提供任何代码示例),而本网站给了我一些实际的具体例子来修改(在Django世界中我发现这是一个罕见的商品)。

I seemed to have (mostly) resolved my issue by using signals. This post gave me the idea (but unfortunately didn't provide any code examples), while this site gave me some actual concrete examples to modify (something I've found to be a rare commodity in the Django world).

我最终将以下代码放在我的页面的view.py文件中 - 我知道models.py是建议用于信号的,但所使用的模型实际上来自allauth包:从django.dispatch导入接收器
从django.contrib输入user_signed_up,email_confirmed


I ended up putting the following code in my page's view.py file -- I know models.py is recommended for signals, but the models being used in question are actually from the allauth package:

from allauth.account.signals import user_signed_up, email_confirmed
from django.dispatch import receiver
from django.contrib.auth.models import Group
from django.contrib.auth.models import User
from allauth.account.models import EmailAddress

@receiver(user_signed_up)
def user_signed_up_(request, user, **kwargs):

    user.is_active = False
    user.is_staff = True
    Group.objects.get(name='SurveyManager').user_set.add(user)

    user.save()

@receiver(email_confirmed)
def email_confirmed_(request, email_address, **kwargs):

    new_email_address = EmailAddress.objects.get(email=email_address)
    user = User.objects.get(new_email_address.user)
    user.is_active = True

    user.save()

唯一的事情是'电子邮件确认的信号处理 - 它声称EmailAddress匹配查询不存在,当它在数据库条目中清楚地匹配,但是我将继续发布在一个单独的问题

The only thing that isn't quite working yet is the email_confirmed signal processing -- it's claiming "EmailAddress matching query does not exist", when it clearly does match in the database entries, but I'll go ahead and post that in a separate question.

这篇关于django-allauth:如何将用户设置为仅在电子邮件验证后生效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆