-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathforms.py
122 lines (96 loc) · 4.37 KB
/
forms.py
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
from allauth.account.forms import SignupForm
from allauth.socialaccount.forms import SignupForm as SocialSignupForm
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.password_validation import validate_password
from django.utils.translation import ugettext_lazy as _
from country_codes import codes
from .models import Member
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = Member
fields = ('username', 'email')
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = Member
fields = UserChangeForm.Meta.fields
class CustomSignupForm(SignupForm):
phone_number = forms.IntegerField(required=True)
first_name = forms.CharField(required=True)
last_name = forms.CharField(required=True)
email = forms.EmailField(widget=forms.TextInput(
attrs={'type': 'email',
'placeholder': _('E-mail address')}))
password1 = forms.CharField(widget=forms.PasswordInput())
password2 = forms.CharField(widget=forms.PasswordInput())
country_code = forms.ChoiceField(
choices=[[i['dialling_code'], '{} ({})'.format(i['country_name'], i['dialling_code'])] for i in
codes])
MIN_LENGTH = 4
field_order = ['first_name', 'last_name', 'username', 'email', 'phone_number', 'country_code', 'password1',
'password2']
class Meta:
model = Member
fields = ['first_name', 'last_name', 'username', 'email', 'country_code', 'phone_number', 'password1',
'password2']
def clean_password1(self):
password = self.data.get('password1')
validate_password(password)
if password != self.data.get('password2'):
raise forms.ValidationError(_("Passwords do not match"))
return password
def clean_phone_number(self):
phone_number = self.data.get('phone_number')
if Member.objects.filter(phone_number=phone_number).exists():
raise forms.ValidationError(
_("Another user with this phone number already exists"))
return phone_number
def clean_username(self):
username = self.data.get('username')
if Member.objects.filter(username=username).exists():
raise forms.ValidationError(
_("Another user with this username already exists"))
return username
def save(self, *args, **kwargs):
user = super().save(*args, **kwargs)
user.set_password(self.cleaned_data['password1'])
print('Saving user with country_code', user.country_code)
user.save()
return user
class SendPhoneVerificationForm(forms.Form):
phone_number = forms.IntegerField(required=True)
country_code = forms.ChoiceField(
choices=[[i['dialling_code'], '{} ({})'.format(i['country_name'], i['dialling_code'])] for i in
codes])
via = forms.ChoiceField(choices=[['sms', 'sms'], ['call', 'call']])
def clean_phone_number(self):
phone_number = self.data.get('phone_number')
initial_phone = self.initial.get('phone_number')
if int(initial_phone) != int(phone_number):
if Member.objects.filter(phone_number=phone_number).exists():
raise forms.ValidationError(
_("Another user with this phone number already exists"))
return phone_number
class Meta:
fields = ['phone_number', 'country_code', 'via', ]
class PhoneVerificationForm(forms.Form):
one_time_password = forms.CharField()
class Meta:
fields = ['one_time_password']
class CustomSocialSignupForm(SocialSignupForm):
phone_number = forms.IntegerField(required=True)
country_code = forms.ChoiceField(
choices=[[i['dialling_code'], '{} ({})'.format(i['country_name'], i['dialling_code'])] for i in
codes])
MIN_LENGTH = 4
def clean_phone_number(self):
phone_number = self.data.get('phone_number')
if Member.objects.filter(phone_number=phone_number).exists():
raise forms.ValidationError(
_("Another user with this phone number already exists"))
return phone_number
def save(self, *args, **kwargs):
user = super().save(*args, **kwargs)
print('Saving user with country_code', user.country_code)
user.save()
return user