Code

Opened 8 years ago

Closed 8 years ago

#2442 closed enhancement (worksforme)

django.contrib.auth.forms.CreateUserForm

Reported by: django@… Owned by: adrian
Component: Contrib apps Version: master
Severity: normal Keywords: Auth User Form Manipulator Create
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

There are three Auth Forms for the User model (django.contrib.auth.forms):

  • AuthenticationForm - user login
  • PasswordChangeForm - obvious
  • PasswordResetForm - set password to a random value and e-mail it to the user

But there does not seem to be a CreateUserForm.
This is different from the AddManipulator (used by admin) in two ways:

  • CreateUserForm contains a password confirmation field
  • The default user AddManipulator should show the raw <hash>$<salt>$<check-sum> value.

There is a patch for changing the AddManipulator to accept a password, and not use the hash, but that still doesnt solve the password confirmation, and I perfer having a Form which is optional in use (like the login and password change forms).

(the encoding thing might need to be removed and later replaced with whatever the unicode solution will be)

class CreateUserForm(forms.Manipulator):
    """
    Base class for creating users. Extend this to get a form that accepts
    username/password creation.
    """
    def __init__(self, request=None, encoding='ASCII'):
        """
        If request is passed in, the manipulator will validate that cookies are
        enabled. Note that the request (a HttpRequest object) must have set a
        cookie with the key TEST_COOKIE_NAME and value TEST_COOKIE_VALUE before
        running this validator.
        
        Encoding is the string encoding to be used for the username and
        optional first and last names. This should match your Database.
        """
        self.request = request
        self.encoding = encoding
        self.fields = [
            forms.TextField(field_name="username", length=15, maxlength=30, is_required=True,
                validator_list=[self.isNotValidUser, self.hasCookiesEnabled]),
            forms.PasswordField(field_name="password", length=15, maxlength=30, is_required=True),
            forms.PasswordField(field_name="confirm", length=15, maxlength=30, is_required=True,
                                validator_list=[validators.AlwaysMatchesOtherField('password', "The two password fields didn't match.")]),
            forms.TextField(field_name="e-mail_address", length=15, maxlength=30, is_required=False,
                validator_list=[validators.isValidEmail]),
            forms.TextField(field_name="first_name", length=15, maxlength=30, is_required=False),
            forms.TextField(field_name="last_name", length=15, maxlength=30, is_required=False),
        ]

    def hasCookiesEnabled(self, field_data, all_data):
        if self.request and not self.request.session.test_cookie_worked():
            raise validators.ValidationError, _("Your Web browser doesn't appear to have cookies enabled. Cookies are required for logging in.")

    def isNotValidUser(self, field_data, all_data):
        try:
            User.objects.get(username=field_data)
        except User.DoesNotExist:
            return True
        raise validators.ValidationError, _("This username is already in use.")
    
    def save(self, new_data):
        "Saves the new user."
        user = User.objects.create_user(new_data['username'].encode(self.encoding), new_data['e-mail_address'], new_data['password'])
        if 'first_name' in new_data:
            user.first_name = new_data['first_name'].encode(self.encoding)
        if 'last_name' in new_data:
            user.last_name = new_data['last_name'].encode(self.encoding)
        user.save()
        

Attachments (0)

Change History (1)

comment:1 Changed 8 years ago by django@…

  • Resolution set to worksforme
  • Status changed from new to closed

I have no clue how I didnt see that there was already a create user form in auth.
Looking at the change history, this has been in there from the beginning.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.