1
|
|
|
from django import forms |
2
|
|
|
from django.contrib.auth import get_user_model |
3
|
|
|
from django.contrib.auth.forms import AuthenticationForm |
4
|
|
|
from django.core.exceptions import ValidationError |
5
|
|
|
from django.utils.translation import ugettext_lazy as _ |
6
|
|
|
|
7
|
|
|
from simplemathcaptcha.fields import MathCaptchaField |
8
|
|
|
|
9
|
|
|
from wye.base.constants import ContactFeedbackType |
10
|
|
|
|
11
|
|
|
from . import models |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class UserAuthenticationForm(AuthenticationForm): |
15
|
|
|
username = forms.CharField(label=_("Email"), max_length=100, required=True) |
16
|
|
|
password = forms.CharField(label=_("Password"), |
17
|
|
|
widget=forms.PasswordInput, |
18
|
|
|
required=True) |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
class SignupForm(forms.ModelForm): |
22
|
|
|
|
23
|
|
|
def __init__(self, *args, **kwargs): |
24
|
|
|
super(SignupForm, self).__init__(*args, **kwargs) |
25
|
|
|
mandatory_field(self) |
26
|
|
|
|
27
|
|
|
mobile = forms.CharField( |
28
|
|
|
label=_("Mobile"), |
29
|
|
|
max_length=10, |
30
|
|
|
widget=forms.TextInput( |
31
|
|
|
attrs={'placeholder': 'Mobile'} |
32
|
|
|
) |
33
|
|
|
) |
34
|
|
|
|
35
|
|
|
def clean_mobile(self): |
36
|
|
|
mobile = self.cleaned_data['mobile'] |
37
|
|
|
error_message = [] |
38
|
|
|
if not mobile.isdigit(): |
39
|
|
|
error_message.append( |
40
|
|
|
"Contact Number should only consist digits") |
41
|
|
|
if not len(mobile) == 10: |
42
|
|
|
error_message.append( |
43
|
|
|
"Contact Number should be of 10 digits") |
44
|
|
|
|
45
|
|
|
if error_message: |
46
|
|
|
raise ValidationError(error_message) |
47
|
|
|
return mobile |
48
|
|
|
|
49
|
|
|
class Meta: |
50
|
|
|
model = get_user_model() |
51
|
|
|
fields = ['first_name', 'last_name'] |
52
|
|
|
widgets = { |
53
|
|
|
'first_name': forms.TextInput( |
54
|
|
|
attrs={'placeholder': 'First Name', 'autofocus': 'on'}), |
55
|
|
|
'last_name': forms.TextInput( |
56
|
|
|
attrs={'placeholder': 'Last Name'}), |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
def signup(self, request, user): |
60
|
|
|
profile = user.profile |
61
|
|
|
profile.mobile = self.cleaned_data['mobile'] |
62
|
|
|
profile.save() |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
class UserProfileForm(forms.ModelForm): |
66
|
|
|
queryset = models.UserType.objects.exclude( |
67
|
|
|
slug__in=['admin', 'lead', 'coordinator']) |
68
|
|
|
usertype = forms.ModelMultipleChoiceField( |
69
|
|
|
label="Usertype", queryset=queryset) |
70
|
|
|
|
71
|
|
|
def __init__(self, *args, **kwargs): |
72
|
|
|
super(UserProfileForm, self).__init__(*args, **kwargs) |
73
|
|
|
mandatory_field(self) |
74
|
|
|
|
75
|
|
|
def clean_interested_states(self): |
76
|
|
|
if ('tutor' in [u.slug for u in self.cleaned_data['usertype']]): |
77
|
|
|
if not self.cleaned_data['interested_states']: |
78
|
|
|
raise ValidationError('States field is mandatory') |
79
|
|
|
return self.cleaned_data['interested_states'] |
80
|
|
|
|
81
|
|
|
def clean_github(self): |
82
|
|
|
if ('tutor' in [u.slug for u in self.cleaned_data['usertype']]): |
83
|
|
|
if not( |
84
|
|
|
('github' in self.cleaned_data and |
85
|
|
|
self.cleaned_data['github']) or |
86
|
|
|
('linkedin' in self.cleaned_data and |
87
|
|
|
self.cleaned_data['linkedin']) |
88
|
|
|
): |
89
|
|
|
raise ValidationError( |
90
|
|
|
'Github or LinkedIn field is mandatory') |
91
|
|
|
return self.cleaned_data['github'] |
92
|
|
|
|
93
|
|
|
def clean_linkedin(self): |
94
|
|
|
if ('tutor' in [u.slug for u in self.cleaned_data['usertype']]): |
95
|
|
|
if not ( |
96
|
|
|
('github' in self.cleaned_data and |
97
|
|
|
self.cleaned_data['github']) or |
98
|
|
|
('linkedin' in self.cleaned_data and |
99
|
|
|
self.cleaned_data['linkedin']) |
100
|
|
|
): |
101
|
|
|
raise ValidationError( |
102
|
|
|
'Github or LinkedIn field is mandatory') |
103
|
|
|
return self.cleaned_data['linkedin'] |
104
|
|
|
|
105
|
|
|
def clean_interested_level(self): |
106
|
|
|
if ('tutor' in [u.slug for u in self.cleaned_data['usertype']]): |
107
|
|
|
if not self.cleaned_data['interested_level']: |
108
|
|
|
raise ValidationError( |
109
|
|
|
'Interested workshop level field is mandatory') |
110
|
|
|
return self.cleaned_data['interested_level'] |
111
|
|
|
|
112
|
|
|
def is_valid(self): |
113
|
|
|
return super(UserProfileForm, self).is_valid() |
114
|
|
|
|
115
|
|
|
class Meta: |
116
|
|
|
model = models.Profile |
117
|
|
|
exclude = ('user', 'slug', 'interested_locations') |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
class ContactUsForm(forms.Form): |
121
|
|
|
|
122
|
|
|
name = forms.CharField(label='Your name*', required=True) |
123
|
|
|
email = forms.EmailField(label='Your email*', required=True) |
124
|
|
|
feedback_type = forms.ChoiceField(label='Feedback on*', required=True, |
125
|
|
|
choices=ContactFeedbackType.CHOICES) |
126
|
|
|
comments = forms.CharField( |
127
|
|
|
label='Comments*', required=True, widget=forms.Textarea) |
128
|
|
|
contact_number = forms.CharField( |
129
|
|
|
label='Your contact number', required=False) |
130
|
|
|
captcha = MathCaptchaField() |
131
|
|
|
|
132
|
|
|
def clean_contact_number(self): |
133
|
|
|
contact_number = self.cleaned_data['contact_number'] |
134
|
|
|
error_message = [] |
135
|
|
|
if not contact_number.isdigit(): |
136
|
|
|
error_message.append( |
137
|
|
|
"Contact Number should only consist digits") |
138
|
|
|
if not len(contact_number) == 10: |
139
|
|
|
error_message.append( |
140
|
|
|
"Contact Number should be of 10 digits") |
141
|
|
|
|
142
|
|
|
if error_message: |
143
|
|
|
raise ValidationError(error_message) |
144
|
|
|
return contact_number |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
def mandatory_field(self): |
148
|
|
|
for v in filter(lambda x: x.required, self.fields.values()): |
149
|
|
|
v.label = str(v.label) + "*" |
150
|
|
|
|