Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RegistrationFormNameExclude(forms.Form):
- """
- Form for registering a new user account.
- Validates that the requested username is not already in use, and
- requires the password to be entered twice to catch typos.
- Subclasses should feel free to add any additional validation they
- need, but should avoid defining a ``save()`` method -- the actual
- saving of collected user data is delegated to the active
- registration backend.
- """
- username = forms.RegexField(regex=r'^\w+$',
- max_length=30,
- widget=forms.TextInput(attrs=attrs_dict),
- label=_("Username"),
- error_messages={'invalid': _("This value must contain only letters, numbers and underscores.")})
- email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
- maxlength=75)),
- label=_("E-mail"))
- password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
- label=_("Password"))
- password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
- label=_("Password (again)"))
- whois = forms.CharField(label='', widget=forms.HiddenInput(), required=False)
- tos = forms.BooleanField(widget=forms.CheckboxInput(),
- label=mark_safe(_(u'I have read and agree to the <a href="/register/tos/" title="Terms of Service" >Terms of Service</a>')))
- def clean_username(self):
- """
- Validate that the username is alphanumeric and is not already
- in use. Check user name or permitted.
- """
- if self.cleaned_data['username'] in settings.EXCLUDE_REGISTRATION_NAMES:
- raise forms.ValidationError(_("This is not a valid username."))
- try:
- user = User.objects.get(username__iexact=self.cleaned_data['username'])
- except User.DoesNotExist:
- return self.cleaned_data['username']
- raise forms.ValidationError(_("A user with that username already exists."))
- def clean_email(self):
- """
- Validate that the supplied email address is unique for the
- site.
- """
- if User.objects.filter(email__iexact=self.cleaned_data['email']):
- raise forms.ValidationError(_("This email address is already in use. Please supply a different email address."))
- return self.cleaned_data['email']
- def clean(self):
- """
- Verifiy that the values entered into the two password fields
- match. Note that an error here will end up in
- ``non_field_errors()`` because it doesn't apply to a single
- field.
- """
- if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
- if self.cleaned_data['password1'] != self.cleaned_data['password2']:
- raise forms.ValidationError(_("The two password fields didn't match."))
- return self.cleaned_data
- def clean_tos(self):
- """
- Validate that the user accepted the Terms of Service.
- """
- if self.cleaned_data.get('tos', False):
- return self.cleaned_data['tos']
- raise forms.ValidationError(_(u'You must agree to the terms to register'))
- class UserRegistrationForm(RegistrationFormNameExclude):
- def save(self, profile_callback=None):
- """
- Create the new ``User`` and ``RegistrationProfile``, and
- returns the ``User``.
- This is essentially a light wrapper around
- ``RegistrationProfile.objects.create_inactive_user()``,
- feeding it the form data and a profile callback (see the
- documentation on ``create_inactive_user()`` for details) if
- supplied.
- """
- new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
- password=self.cleaned_data['password1'],
- email=self.cleaned_data['email'],
- whois='user',
- profile_callback=profile_callback)
- Profile.objects.get_or_create(user=new_user)
- return new_user
Add Comment
Please, Sign In to add comment