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
|
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()
|