6

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) 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) 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.

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!

Gravity Grave
  • 2,802
  • 1
  • 27
  • 39
  • You haven't really described what _specific programming_ problem you have encountered while trying to produce these results. – takendarkk Jul 16 '14 at 21:51
  • Sorry -- 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. 2) How to trigger the code to change the user active flag to true once the e-mail verification is complete. – Gravity Grave Jul 16 '14 at 21:57

4 Answers4

13

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).

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()

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.

Community
  • 1
  • 1
Gravity Grave
  • 2,802
  • 1
  • 27
  • 39
  • Hey this is what I am trying to do using similar code for setting user as active. However, in my case, the user status is not being set as active. It is basically not changing. Is that common or am I doing something wrong? Let me know if you specifically need the code. – Divij Sehgal Apr 05 '17 at 20:11
  • @DivijSehgal I'd recommend you post your code in another question and link it here. I'll see if I can answer it there. – Gravity Grave Apr 07 '17 at 03:44
  • Lame question it was, I had wrongly setup a pre_save signal to ensure I was always saving a user as inactive. Shifted that to business logic now. Thanks, though :) – Divij Sehgal Apr 08 '17 at 05:27
1

ACCOUNT_EMAIL_VERIFICATION (=”optional”) Determines the e-mail verification method during signup – choose one of “mandatory”, “optional”, or “none”. When set to “mandatory” the user is blocked from logging in until the email address is verified. Choose “optional” or “none” to allow logins with an unverified e-mail address. In case of “optional”, the e-mail verification mail is still sent, whereas in case of “none” no e-mail verification mails are sent.

John
  • 359
  • 1
  • 3
  • 9
  • 1
    I have it set to mandatory, and that part is working fine. Unfortunately this doesn't seem to have a bearing on the 'active' property in the user entry in the database. What you're saying would apply if I were using the allauth login, but I'm actually bypassing that for my site and am using the admin panel to login, which doesn't seem to care if the address is verified or not. The admin panel _does_ care if the 'active' property is set to false though, which is why I want to set it to true when the address is verified (and have it default to false when the user is initially created). – Gravity Grave Jul 17 '14 at 02:07
0

I know this is an old post but I came across this thread in my own searches. In reply to your email_confirmed problem, use email=kwargs['email_address'].email in your EmailAddress instance call.

@receiver(email_confirmed)
def email_confirmed_(request, *args, **kwargs):
    user = request.user
    new_email_address = EmailAddress.objects.get(
        email=kwargs['email_address'].email)
    user = User.objects.get(
        email=new_email_address.user)
    user.is_active = True
    user.save()
Baz
  • 306
  • 1
  • 5
  • 12
-1

In general, for email validation, Django has its own table. You can check e.g. mysql> Select * From account_emailconfirmation;

BTW, if emails are not sent to your mailbox: I struggled a lot and found out that this configuration was missing: DEFAULT_FROM_EMAIL = 'test.noreply@gmail.com'. Otherwise, its webmaster@... which was not working for me with mod_wsgi.

  • This does not answer the question of how to set users as active when their email address is verified. – He3lixxx Aug 30 '23 at 00:18