reg/forms.py
changeset 18 7dae32a2439b
parent 17 125b6fc8f20b
child 22 737ec98cf6df
equal deleted inserted replaced
17:125b6fc8f20b 18:7dae32a2439b
    43     last_name = forms.CharField()
    43     last_name = forms.CharField()
    44 
    44 
    45     class Meta:
    45     class Meta:
    46         model = Profile
    46         model = Profile
    47         fields = ['email', 'password', 'confirm_password', 'first_name', 'last_name', 'gender', 'profession', 'affiliated_to', 'interests']
    47         fields = ['email', 'password', 'confirm_password', 'first_name', 'last_name', 'gender', 'profession', 'affiliated_to', 'interests']
       
    48 
       
    49     def clean_first_name(self):
       
    50         """ firstname should contain only alphabets.
       
    51         """
       
    52 
       
    53         first_name = self.cleaned_data['first_name']
       
    54         if first_name.strip(string.ascii_letters):
       
    55             raise forms.ValidationError("Name must contain only alphabets")
       
    56         return first_name
       
    57 
       
    58     def clean_last_name(self):
       
    59         """ only alphabets allowed.
       
    60         """
       
    61 
       
    62         last_name = self.cleaned_data['last_name']
       
    63         if last_name.strip(string.ascii_letters):
       
    64             raise forms.ValidationError("Name must contain only alphabets")
       
    65         return last_name
    48 
    66 
    49     def clean_email(self):
    67     def clean_email(self):
    50         """ check if a user exists with same email.
    68         """ check if a user exists with same email.
    51         """
    69         """
    52 
    70 
    95     def clean_stop_date(self):
   113     def clean_stop_date(self):
    96         """ see that stop_date is not less than start_date.
   114         """ see that stop_date is not less than start_date.
    97         """
   115         """
    98 
   116 
    99         stop_date = self.cleaned_data['stop_date']
   117         stop_date = self.cleaned_data['stop_date']
   100         try:
   118 
   101             start_date = datetime.strptime(self.data['start_date'], "%Y-%m-%d").date()
   119         start_date = self.clean_start_date()
   102         except ValueError:
       
   103             raise forms.ValidationError("Enter a valid date")
       
   104 
   120 
   105         if start_date > stop_date:
   121         if start_date > stop_date:
   106             raise forms.ValidationError('The event stops even before it starts. I am starting to wonder what kind of event is this.')
   122             raise forms.ValidationError('The event stops even before it starts. I am starting to wonder what kind of event is this.')
   107         return stop_date
   123         return stop_date
   108 
   124 
   155             raise forms.ValidationError("Password must be atleast 8 characters and at most 30 characters")
   171             raise forms.ValidationError("Password must be atleast 8 characters and at most 30 characters")
   156 
   172 
   157         if not new_password == self.data['confirm_password']:
   173         if not new_password == self.data['confirm_password']:
   158             raise forms.ValidationError("Passwords do not match")
   174             raise forms.ValidationError("Passwords do not match")
   159 
   175 
       
   176         return new_password
       
   177 
   160 class EditProfileForm(forms.Form):
   178 class EditProfileForm(forms.Form):
   161     """ form for editing the profile.
   179     """ form for editing the profile.
   162     """
   180     """
   163 
   181 
   164     first_name = forms.CharField(required=True)
   182     first_name = forms.CharField(required=True)
   166     gender = forms.ChoiceField(choices=GENDER_CHOICES)
   184     gender = forms.ChoiceField(choices=GENDER_CHOICES)
   167     profession = forms.ChoiceField(choices=PROFESSION_CHOICES)
   185     profession = forms.ChoiceField(choices=PROFESSION_CHOICES)
   168     affiliated_to = forms.CharField(max_length=100, required=True, label="College/Company")
   186     affiliated_to = forms.CharField(max_length=100, required=True, label="College/Company")
   169     interests = forms.CharField(max_length=100, label="Fields of interest")
   187     interests = forms.CharField(max_length=100, label="Fields of interest")
   170 
   188 
   171 
   189     def clean_first_name(self):
       
   190         """ firstname should contain only alphabets.
       
   191         """
       
   192 
       
   193         first_name = self.cleaned_data['first_name']
       
   194         if first_name.strip(string.ascii_letters):
       
   195             raise forms.ValidationError("Name must contain only alphabets")
       
   196         return first_name
       
   197 
       
   198     def clean_last_name(self):
       
   199         """ only alphabets allowed.
       
   200         """
       
   201 
       
   202         last_name = self.cleaned_data['last_name']
       
   203         if last_name.strip(string.ascii_letters):
       
   204             raise forms.ValidationError("Name must contain only alphabets")
       
   205         return last_name
       
   206 
       
   207