|
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(attrs={'placeholder': 'First Name', 'autofocus': 'on'}), |
|
54
|
|
|
'last_name': forms.TextInput(attrs={'placeholder': 'Last Name'}), |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
def signup(self, request, user): |
|
58
|
|
|
profile = user.profile |
|
59
|
|
|
profile.mobile = self.cleaned_data['mobile'] |
|
60
|
|
|
profile.save() |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
class UserProfileForm(forms.ModelForm): |
|
64
|
|
|
queryset = models.UserType.objects.exclude(slug__in=['admin', 'lead']) |
|
65
|
|
|
usertype = forms.ModelMultipleChoiceField( |
|
66
|
|
|
label="Usertype", queryset=queryset) |
|
67
|
|
|
|
|
68
|
|
|
def __init__(self, *args, **kwargs): |
|
69
|
|
|
super(UserProfileForm, self).__init__(*args, **kwargs) |
|
70
|
|
|
mandatory_field(self) |
|
71
|
|
|
|
|
72
|
|
|
class Meta: |
|
73
|
|
|
model = models.Profile |
|
74
|
|
|
exclude = ('user', 'slug') |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
class ContactUsForm(forms.Form): |
|
78
|
|
|
|
|
79
|
|
|
name = forms.CharField(label='Your name*', required=True) |
|
80
|
|
|
email = forms.EmailField(label='Your email*', required=True) |
|
81
|
|
|
feedback_type = forms.ChoiceField(label='Feedback on*', required=True, |
|
82
|
|
|
choices=ContactFeedbackType.CHOICES) |
|
83
|
|
|
comments = forms.CharField( |
|
84
|
|
|
label='Comments*', required=True, widget=forms.Textarea) |
|
85
|
|
|
contact_number = forms.CharField( |
|
86
|
|
|
label='Your contact number', required=False) |
|
87
|
|
|
captcha = MathCaptchaField() |
|
88
|
|
|
|
|
89
|
|
|
def clean_contact_number(self): |
|
90
|
|
|
contact_number = self.cleaned_data['contact_number'] |
|
91
|
|
|
error_message = [] |
|
92
|
|
|
if not contact_number.isdigit(): |
|
93
|
|
|
error_message.append( |
|
94
|
|
|
"Contact Number should only consist digits") |
|
95
|
|
|
if not len(contact_number) == 10: |
|
96
|
|
|
error_message.append( |
|
97
|
|
|
"Contact Number should be of 10 digits") |
|
98
|
|
|
|
|
99
|
|
|
if error_message: |
|
100
|
|
|
raise ValidationError(error_message) |
|
101
|
|
|
return contact_number |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
def mandatory_field(self): |
|
105
|
|
|
for v in filter(lambda x: x.required, self.fields.values()): |
|
106
|
|
|
v.label = str(v.label) + "*" |
|
107
|
|
|
|