Guest User

Untitled

a guest
Jul 4th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.52 KB | None | 0 0
  1. class RegistrationFormNameExclude(forms.Form):
  2.     """
  3.    Form for registering a new user account.
  4.    
  5.    Validates that the requested username is not already in use, and
  6.    requires the password to be entered twice to catch typos.
  7.    
  8.    Subclasses should feel free to add any additional validation they
  9.    need, but should avoid defining a ``save()`` method -- the actual
  10.    saving of collected user data is delegated to the active
  11.    registration backend.
  12.    
  13.    """
  14.     username = forms.RegexField(regex=r'^\w+$',
  15.                                 max_length=30,
  16.                                 widget=forms.TextInput(attrs=attrs_dict),
  17.                                 label=_("Username"),
  18.                                 error_messages={'invalid': _("This value must contain only letters, numbers and underscores.")})
  19.     email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
  20.                                                                maxlength=75)),
  21.                              label=_("E-mail"))
  22.     password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
  23.                                 label=_("Password"))
  24.     password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
  25.                                 label=_("Password (again)"))
  26.     whois = forms.CharField(label='', widget=forms.HiddenInput(), required=False)
  27.     tos = forms.BooleanField(widget=forms.CheckboxInput(),
  28.                              label=mark_safe(_(u'I have read and agree to the <a href="/register/tos/" title="Terms of Service" >Terms of Service</a>')))
  29.    
  30.     def clean_username(self):
  31.         """
  32.        Validate that the username is alphanumeric and is not already
  33.        in use. Check user name or permitted.
  34.        
  35.        """
  36.     if self.cleaned_data['username'] in settings.EXCLUDE_REGISTRATION_NAMES:
  37.             raise forms.ValidationError(_("This is not a valid username."))
  38.         try:
  39.             user = User.objects.get(username__iexact=self.cleaned_data['username'])
  40.         except User.DoesNotExist:
  41.             return self.cleaned_data['username']
  42.         raise forms.ValidationError(_("A user with that username already exists."))
  43.  
  44.     def clean_email(self):
  45.         """
  46.        Validate that the supplied email address is unique for the
  47.        site.
  48.        
  49.        """
  50.         if User.objects.filter(email__iexact=self.cleaned_data['email']):
  51.             raise forms.ValidationError(_("This email address is already in use. Please supply a different email address."))
  52.         return self.cleaned_data['email']
  53.  
  54.     def clean(self):
  55.         """
  56.        Verifiy that the values entered into the two password fields
  57.        match. Note that an error here will end up in
  58.        ``non_field_errors()`` because it doesn't apply to a single
  59.        field.
  60.        
  61.        """
  62.         if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
  63.             if self.cleaned_data['password1'] != self.cleaned_data['password2']:
  64.                 raise forms.ValidationError(_("The two password fields didn't match."))
  65.         return self.cleaned_data
  66.  
  67.     def clean_tos(self):
  68.         """
  69.        Validate that the user accepted the Terms of Service.
  70.        
  71.        """
  72.         if self.cleaned_data.get('tos', False):
  73.             return self.cleaned_data['tos']
  74.         raise forms.ValidationError(_(u'You must agree to the terms to register'))
  75.  
  76. class UserRegistrationForm(RegistrationFormNameExclude):
  77.  
  78.     def save(self, profile_callback=None):
  79.     """
  80.        Create the new ``User`` and ``RegistrationProfile``, and
  81.        returns the ``User``.
  82.        
  83.        This is essentially a light wrapper around
  84.        ``RegistrationProfile.objects.create_inactive_user()``,
  85.        feeding it the form data and a profile callback (see the
  86.        documentation on ``create_inactive_user()`` for details) if
  87.        supplied.
  88.        """
  89.         new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
  90.                                                                     password=self.cleaned_data['password1'],
  91.                                                                     email=self.cleaned_data['email'],
  92.                                                                     whois='user',
  93.                                                                     profile_callback=profile_callback)
  94.  
  95.         Profile.objects.get_or_create(user=new_user)
  96.         return new_user
Add Comment
Please, Sign In to add comment