Commits

Tino de Bruijn committed b18ad22

Converted the form to a ModelForm, and added a second password field

Comments (0)

Files changed (1)

user_creation/forms.py

 from django.utils.translation import ugettext_lazy as _
 from models import ActivationProfile
 
-class AccountCreationForm(forms.Form):
+class AccountCreationForm(forms.ModelForm):
     """
     A form that creates a user, with no privileges, from the given username and password.
     """
         help_text = _("Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores)."),
         error_message = _("This value must contain only letters, numbers and underscores."))
     email = forms.EmailField(label=_("Email address"))
-    password = forms.CharField(label=_("Password"), required=False,
-        help_text = _("Leave blank to create a user with a random password and an activation profile. \
-            Only use this if you want to set up an account with a known password"))
+    password1 = forms.CharField(label=_("Password"), 
+                                widget=forms.PasswordInput,
+                                required=False,
+                                help_text = _("Leave blank to create a user with "
+                                "a random password and an activation profile. Only "
+                                "use this if you want to set up an account with a "
+                                "known password"))
+    password2 = forms.CharField(label=_("Password confirmation"), 
+                                widget=forms.PasswordInput,
+                                required=False,
+                                help_text = _("Enter the same password as above, "
+                                "for verification."))
     email_user = forms.BooleanField(initial=True, label=_("Send the user a notification email"), required=False,
         help_text = _("If you generate a random password, this will be an email with an activation link. Otherwise it will contain the users login credentials"))
 
+    class Meta:
+        model = User
+        fields = ("username", "email", "email_user")
+        
     def clean_username(self):
         """ Validates that the username is alphanumeric and not already in use. """
         username = self.cleaned_data["username"]
             return email
         raise forms.ValidationError(_("A user with that email address already exists."))
         
-    def save(self):
+    def clean_password2(self):
+        password1 = self.cleaned_data.get("password1", "")
+        password2 = self.cleaned_data["password2"]
+        if password1 != password2:
+            raise forms.ValidationError(_("The two password fields didn't match."))
+        return password2
+        
+    def save(self, *args, **kwargs):
         """ Creates a normal new user and return that, or creates an inactive user with an activation profile. """ 
         send_email = self.cleaned_data["email_user"]
         if self.cleaned_data["password"] == '': # a activation profile needs to be created