-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathforms.py
More file actions
143 lines (118 loc) · 5.75 KB
/
Copy pathforms.py
File metadata and controls
143 lines (118 loc) · 5.75 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from allauth.account.forms import SignupForm, ChangePasswordForm as BaseChangePasswordForm
from allauth.utils import set_form_field_order
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from wagtail.users.forms import UserEditForm as WagtailUserEditForm, \
UserCreationForm as WagtailUserCreationForm
from user_notifications.models import UserNotificationTemplate
from user_notifications.tasks import send_app_notifications
from .fields import IogtPasswordField
from .models import User
from notifications.signals import notify
class AccountSignupForm(SignupForm):
display_name = forms.CharField(
label=_("Display name"),
widget=forms.TextInput(
attrs={"placeholder": _("Choose a display name that will be shown publicly if you post to the IoGT site, for example next to comments you post"),}
),
required=False,
)
date_of_birth = forms.DateField(
required=True,
widget=forms.DateInput(attrs={"type": "date"})
)
gender = forms.ChoiceField(
choices=[("male", "Male"), ("female", "Female"), ("other", "Other")],
required=True,
)
location = forms.CharField(
required=False,
max_length=255
)
terms_accepted = forms.BooleanField(label=_('I accept the Terms and Conditions.'))
field_order = [
"username",
"display_name",
"date_of_birth",
"gender",
"location",
"password1",
"password2",
"terms_accepted",
]
def __init__(self, *args, **kwargs):
super(AccountSignupForm, self).__init__(*args, **kwargs)
self.fields.pop('email')
self.fields["password1"] = IogtPasswordField(label=_("Choose a 4-digit PIN or a longer password that you will use to login to IoGT"), autocomplete="new-password")
if 'password2' in self.fields:
self.fields["password2"] = IogtPasswordField(label=_("Repeat your 4-digital PIN or longer password"), autocomplete="new-password")
self.fields["username"].widget = forms.TextInput(attrs={
"placeholder": _("Choose a username that you will use to login to IoGT")
})
self.fields["location"].widget = forms.TextInput(attrs={
"placeholder": _("Enter a location")
})
if hasattr(self, "field_order"):
set_form_field_order(self, self.field_order)
def save(self, request):
user = super().save(request)
send_app_notifications.delay(user.id, notification_type='signup')
user.date_of_birth = self.cleaned_data["date_of_birth"]
user.gender = self.cleaned_data["gender"]
user.location = self.cleaned_data["location"]
user.save()
return user
def clean_username(self):
username = self.cleaned_data.get('username')
if User.objects.filter(username__iexact=username):
raise ValidationError(_('Username not available.'))
return username
def clean_displayname(self):
display_name = self.cleaned_data.get('display_name')
if User.objects.filter(display_name__iexact=display_name):
raise ValidationError(_('Display name not available.'))
return display_name
class ChangePasswordForm(BaseChangePasswordForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["oldpassword"] = IogtPasswordField(label=_("Old 4-digit PIN"), autocomplete='old-password')
self.fields["password1"] = IogtPasswordField(label=_("New 4-digit PIN"), autocomplete='old-password')
self.fields["password2"] = IogtPasswordField(label=_("Confirm new 4-digit PIN"), autocomplete='old-password')
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = User
fields = ('username', 'display_name',)
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = User
fields = ('username', 'display_name', 'groups', 'user_permissions')
class WagtailAdminUserCreateForm(WagtailUserCreationForm):
email = forms.EmailField(required=False, label='Email')
display_name = forms.CharField(required=False, label='Display Name')
first_name = forms.CharField(required=False, label='First Name')
last_name = forms.CharField(required=False, label='Last Name')
terms_accepted = forms.BooleanField(label=_('I accept the Terms and Conditions.'))
def clean_username(self):
username = self.cleaned_data['username']
if User.objects.filter(username__iexact=username):
raise ValidationError(_('A user with that username already exists.'))
return username
def clean_displayname(self):
display_name = self.cleaned_data.get('display_name')
if User.objects.filter(display_name__iexact=display_name):
raise ValidationError(_('Display name not available.'))
return display_name
class Meta(WagtailUserCreationForm.Meta):
model = User
fields = WagtailUserCreationForm.Meta.fields | {'first_name', 'last_name', 'username', 'display_name', 'terms_accepted', 'groups'}
class WagtailAdminUserEditForm(WagtailUserEditForm):
email = forms.EmailField(required=False, label='Email')
display_name = forms.CharField(required=False, label='Display Name')
first_name = forms.CharField(required=False, label='First Name')
last_name = forms.CharField(required=False, label='Last Name')
terms_accepted = forms.BooleanField(label=_('I accept the Terms and Conditions.'))
class Meta(WagtailUserEditForm.Meta):
model = User
fields = WagtailUserEditForm.Meta.fields | {'first_name', 'last_name', 'username', 'display_name', 'terms_accepted', 'groups'}