reg/forms.py
changeset 6 057498d12450
parent 4 ededea9ad08b
child 7 af9ab5ad2786
--- a/reg/forms.py	Fri Apr 09 13:21:46 2010 +0530
+++ b/reg/forms.py	Fri Apr 09 15:35:40 2010 +0530
@@ -1,8 +1,12 @@
+import string
+
 from django.contrib.auth.models import User
 from django import forms
 
 from django.contrib.auth import authenticate
 
+from workshop.reg.models import Profile
+
 class LoginForm(forms.Form):
     """ a form to handle login.
     """
@@ -26,3 +30,39 @@
             raise forms.ValidationError("Incorrect e-mail or password")
         return email
 
+class RegisterForm(forms.ModelForm):
+    """ add the fields email and password
+    and then generate form using profile model.
+    """
+
+    email = forms.EmailField()
+    password = forms.CharField(widget=forms.PasswordInput, help_text="Choose a good password which 8 to 30 chars long")
+    confirm_password = forms.CharField(widget=forms.PasswordInput) 
+
+    first_name = forms.CharField(required=True)
+    last_name = forms.CharField()
+
+    class Meta:
+        model = Profile
+        fields = ['email', 'password', 'confirm_password', 'first_name', 'last_name', 'gender', 'profession', 'affiliated_to', 'interests']
+
+    def clean_email(self):
+        email = self.cleaned_data['email']
+        try:
+            User.objects.get(email__iexact=email)
+            raise forms.ValidationError("An account already exists with this email.\
+                                        Click on forgot password if you have forgotten your password")
+        except User.DoesNotExist:
+            return email
+
+    def clean_password(self):
+        password = self.cleaned_data['password']
+
+        if password.strip(string.ascii_letters+string.punctuation+string.digits):
+            raise forms.ValidationError("Only alphabets, digits and punctuation symbols are allowed in password")
+
+        if not 8 <= len(password) <= 30:
+            raise forms.ValidationError("Password must be atleast 8 characters and at most 30 characters")
+
+        if not password == self.data['confirm_password']:
+            raise forms.ValidationError("Passwords do not match")