Search code examples
pythondjangodjango-allauth

Django-allauth restrict registration to list of emails


I'm using Django-allauth, I have a list of emails and I want to restrict registration to this list. My idea was to check the signing up user email and, if not in the emails list, stop registration process and redirect. As suggested by Chetan Ganji I tried editing allauth.account.views.SignupView but it does not subscribe the form_valid method. How can i do that? Thank you for help

from allauth.account.views import SignupView

class AllauthCustomSignupView(SignupView):

    def form_valid(self, form):
        email = form.cleaned_data['email']
        auth_user_list =    [   'email_1',
                                'email_2',
                                ...
                            ]

        if not any(email in s for s in auth_user_list):
            return reverse('url')
        return super(MySignupView, self).form_valid(form)

Solution

  • You can do it by extending the DefaultAccountAdapter class. You have to figure out a way to store and fetch the restricted list on demand. You can then use the adapters and raise validation error in the registration from. Extend a DefaultAccountAdapter and override the clean_email method. Create an adapter.py in your project directory and extend the default adapter class.

    from allauth.account.adapter import DefaultAccountAdapter
    from django.forms import ValidationError
    
    class RestrictEmailAdapter(DefaultAccountAdapter):
    
        def clean_email(self,email):
            RestrictedList = ['Your restricted list goes here.']
            if email in RestrictedList
                raise ValidationError('You are restricted from registering. Please contact admin.')
            return email
    

    Finally, point the account adapter in settings.py to your extended class.

    ACCOUNT_ADAPTER = 'YourProject.adapter.RestrictEmailAdapter'