1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
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 crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
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, attrs={"class":"span1"}),
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
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_id = 'id-loginform'
self.helper.form_method = 'post'
self.helper.form_class = 'form-horizontal'
self.helper.form_action = " "
self.helper.add_input(Submit('submit', 'Submit'))
super(UserLoginForm, self).__init__(*args, **kwargs)
class UserDetailsForm(forms.Form):
def __init__(self, user, *args, **kwargs):
self.user = user
self.helper = FormHelper()
self.helper.form_id = 'id-detailsform'
self.helper.form_method = 'post'
self.helper.form_class = 'form-horizontal'
self.helper.form_action = "/allotter/details/"
self.helper.add_input(Submit('submit', 'Submit'))
super(UserDetailsForm, self).__init__(*args, **kwargs)
email = forms.EmailField(label="Email Address", widget=forms.TextInput(attrs={"placeholder":"john@example.com",}),
help_text="Enter a valid email id if you have any.")
phone_number = forms.IntegerField(label="Phone number", widget=forms.TextInput(attrs={"placeholder":"9876543210",}),
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()
|