distractedhamster
distractedhamster

Reputation: 777

Manual activation of new user accounts in Django

I created a working "Log in with facebook" setup with django-allauth. However, I want those new user accounts that allauth creates to not be activated automatically.

Basically I want to activate all new accounts from the Django admin pages.

What would be a best practice solution to this?

Thanks

Upvotes: 1

Views: 1150

Answers (3)

nowakasd
nowakasd

Reputation: 54

A strightforward solution to this problem would be to utilize pre_save signal on original User model.

@receiver(pre_save, sender=User)
def set_new_user_inactive(sender, instance, **kwargs):
    if instance._state.adding is True:
        instance.is_active = False

Hope that helps!

Upvotes: 0

Olivier Pons
Olivier Pons

Reputation: 15796

I'd suggest what follows because when I made cogofly I had this huge problem where some people (few but some of them do) click sometimes on "log in with google" and sometimes on "log in with facebook" and this is the same person! So be careful with that and this solution will help you to avoid such problem. The only thing to remember: there's only one primary key for all social networks: the email.

Like it's written in the documentation, I'd make a OneToOne link to the User model. From this I would suggest this:

  • precise the date of the first login
  • precise the date of the last login
  • precise if the account has been validated

Like this:

from django.contrib.auth.models import User

class Person(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    is_activated = models.BooleanField(default=False)

And make a social login model where you precise how the user logged in:

class SocialLogin(BaseModel):
    S_GOOGLE = 1
    S_FACEBOOK = 2

    TAB_S_LOGIN = {
        S_GOOGLE: _("Google"),
        S_FACEBOOK: _("Facebook"),
    }
    logged_in = models.IntegerField(
        choices=[(a, b) for a, b in list(TAB_S_LOGIN.items())],
        default=S_GOOGLE)
    date_first_login = models.DateTimeField(auto_now_add=True,
                                            verbose_name=_('First login'))
    date_last_login = models.DateTimeField(auto_now=True,
                                           verbose_name=_('Last login'))
    person = models.OneToOneField(Person, on_delete=models.CASCADE)

    def logged_in_desc(self):
        return SocialLogin.TAB_S_LOGIN[self.logged_in]

    def __str__(self):
        return '{} / {}'.format(
            self.logged_in_desc(),
            description if self.description is not None else '?')

Upvotes: 1

Jahongir Rahmonov
Jahongir Rahmonov

Reputation: 13753

How do you know that an account is activated or not? You need some kind of field in your User model.

Let's say that your field is called is_activated:

class User(BaseUser):
    is_activated = models.BooleanField(default=False)

and that's it. This field's default value will be False which means that a created user is not activated by default.

Then, you can add this field to the admin page and toggle it from there.

Hope it helps!

Upvotes: 1

Related Questions