简体   繁体   中英

Django / Userena Allow Signup if on Roster

I was wondering the best way to approach this. I am using Userena as a base for my project. I want to load a team roster on to the database or keep a text file of the roster and before the user signs up, the website should check if the user is on the roster or not. If not, then they will not be able to sign up.

In userena.forms is the SignupForm. I would extend one of the three clean methods that are being implemented in the form validation. These are clean_username, clean_email, and clean.

For example, below is the clean_email method. It already checks if the email is already in use. I would maintain a Roster table that contains the valid emails. As such you can add another layer of checking. I will put that below the first.

def clean_email(self):
    """ Validate that the e-mail address is unique. """
    if User.objects.filter(email__iexact=self.cleaned_data['email']):
        raise forms.ValidationError(_('This email is already in use. Please supply a different email.'))
    return self.cleaned_data['email']

Compared to checking if the email is used by another user. For Roster we will raise the error if it is not found in the Roster table.

def clean_email(self):
    """ Validate that the e-mail address is unique. """
    if User.objects.filter(email__iexact=self.cleaned_data['email']):
        raise forms.ValidationError(_('This email is already in use. Please supply a different email.'))
    if not Roster.objects.filter(email__iexact=self.cleaned_data['email']):
        raise forms.ValidationError(_('You are not able to signup as you are not part of the Roster.'))
    return self.cleaned_data['email']

Note: Be sure to import your Roster model to wherever you are adding the check.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM