diff options
Diffstat (limited to 'choice_seeker/allotter/forms.py')
-rw-r--r-- | choice_seeker/allotter/forms.py | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/choice_seeker/allotter/forms.py b/choice_seeker/allotter/forms.py new file mode 100644 index 0000000..65e4cf6 --- /dev/null +++ b/choice_seeker/allotter/forms.py @@ -0,0 +1,110 @@ + +from django import forms +from allotter.models import Profile +from django.forms.extras.widgets import SelectDateWidget + +from django.utils.encoding import * + +from django.contrib.auth import authenticate +from django.contrib.auth.models import User + +from string import digits + +BIRTH_YEAR_CHOICES = ('1986','1987','1988','1989','1990','1991') + + +class UserLoginForm(forms.Form): + + ##Registration Number as Username + username = forms.IntegerField(label="Registration Number", + help_text="As on your Examination ID Card") + + ##Application number as password + password = forms.CharField(label = "Application Number", + max_length=10, help_text="As on your Examination ID Card") + + dob = forms.DateField(label="Date of Birth", + widget=SelectDateWidget(years=BIRTH_YEAR_CHOICES), + initial=datetime.date.today) + + def clean_username(self): + u_name = self.cleaned_data["username"] + + if not u_name: + raise forms.ValidationError("Enter an username.") + + ##Verifies whether username contains only digits and is not + ##longer than 7, i.e Username == Registration Number. + if str(u_name).strip(digits) or len(str(u_name)) != 7: + msg = "Invalid Registration Number" + raise forms.ValidationError(msg) + + ##Verifying whether the user already exists in the database + ##Raising error otherwise + try: + User.objects.get(username__exact = u_name) + return u_name + except User.DoesNotExist: + raise forms.ValidationError("Entered Registration Number haven't appeared for JAM Exam.") + + def clean_password(self): + + pwd = self.cleaned_data['password'] + + ##Verifying the length of application number and whether it contains + ##only digits. + + if str(pwd).strip(digits) and len(pwd) != 5: + msg = "Not a valid Application Number" + raise forms.ValidationError(msg) + + return pwd + + def clean(self): + super(UserLoginForm, self).clean() + u_name, pwd = self.cleaned_data.get('username'), self.cleaned_data.get('password') + dob = self.cleaned_data['dob'] + try: + current_user = User.objects.get(username__exact = u_name) + profile = current_user.get_profile() + if profile.dob != dob: + raise forms.ValidationError("Date of Birth doesn't match.") + except User.DoesNotExist: + raise forms.ValidationError("Correct the following errors and try logging in again.") + + + ##Authentication part + user = authenticate(username = u_name, password = pwd) + if not user: + raise forms.ValidationError("Application Number or Registration Number doesn't match.") + return user + + +class UserDetailsForm(forms.Form): + + def __init__(self, user, *args, **kwargs): + self.user = user + super(UserDetailsForm, self).__init__(*args, **kwargs) + + email = forms.EmailField(label="Email Address", + help_text="Enter a valid email id if you have any.") + phone_number = forms.IntegerField(label="Phone number", + help_text="10 digit number with code") + + + def clean_phone_number(self): + pno = self.cleaned_data['phone_number'] + if str(pno).strip(digits) or len(str(pno)) != 10: + raise forms.ValidationError("Not a valid phone number") + return pno + + def save(self): + cleaned_data = self.cleaned_data + user_profile = self.user.get_profile() + + user_profile.secondary_email = self.cleaned_data['email'] + user_profile.phone_number = self.cleaned_data['phone_number'] + + user_profile.save() + + |