Disable new users singup when using Django's allauth package

Sign up closed
Image: Sign up closed (License: CC-BY Marcelo Canina#"Public Domain")

Do not accept new users

Published: Tags Django , Uml

Overview

How to disable new users Sign Up while allowing old users to log in a website using Django’s allauth

Supposing you already have a custom user model in an users app, we create an adapter to disable new sign ups.

Django allauth comes with adapter methods that can be used to intervene in how User instances are created and populated with data1, in particular there is an is_open_for_signup method that can disable new sign ups.

is_open_for_signup(self, request): The default function returns True. You can override this method by returning False if you want to disable account signup.

1. Override method

We create an adapter.py file in users app extending the DefaultAccountAdapter. In users/adapter.py:

from allauth.account.adapter import DefaultAccountAdapter

class NoNewUsersAccountAdapter(DefaultAccountAdapter):
    """ 
    Adapter to disable allauth new signups
    Used at equilang/settings.py with key ACCOUNT_ADAPTER

    https://django-allauth.readthedocs.io/en/latest/advanced.html#custom-redirects """
    
    def is_open_for_signup(self, request):
        """
        Checks whether or not the site is open for signups.

        Next to simply returning True/False you can also intervene the
        regular flow by raising an ImmediateHttpResponse
        """
        return False

2. Update settings

Use the above adapter in our project settings file myapp/settings.py:

# Disable new users signup
# look at users/adapter.py
ACCOUNT_ADAPTER = 'users.adapter.NoNewUsersAccountAdapter'

Explanation

Now every time you try to visit the sign up page, allauth.account.SignupView will use the CloseableSignupMixin mixin which uses the template at allauth/templates/account/signup_closed.html.

To understand whats going on behind the scenes, this is the relevant code at the project:

class CloseableSignupMixin(object):
    template_name_signup_closed = (
        "account/signup_closed." + app_settings.TEMPLATE_EXTENSION)

    def dispatch(self, request, *args, **kwargs):
        try:
            if not self.is_open():
                return self.closed()
        except ImmediateHttpResponse as e:
            return e.response
        return super(CloseableSignupMixin, self).dispatch(request,
                                                          *args,
                                                          **kwargs)

    def is_open(self):
        return get_adapter(self.request).is_open_for_signup(self.request)

    def closed(self):
        response_kwargs = {
            "request": self.request,
            "template": self.template_name_signup_closed,
        }
        return self.response_class(**response_kwargs)


class SignupView(RedirectAuthenticatedUserMixin, CloseableSignupMixin,
                 AjaxCapableProcessFormViewMixin, FormView):
	// ...

References

Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus


Guide to disable the creation of new users in a site using Django allauth's while keeping the ability to log in preexisting users

Clutter-free software concepts.
Translations English Español

Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme

·