forms.py
11.6 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# -*- coding: utf-8 -*-
from importlib import import_module
from django import forms
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import (ReadOnlyPasswordHashField,
SetPasswordForm, PasswordChangeForm)
from django.core.urlresolvers import reverse
from django.utils.functional import lazy
from django.utils.translation import ugettext_lazy as _
from django.utils.safestring import mark_safe
from .signals import user_created
from .utils.validators import validate_social_account
from .utils import mailman
User = get_user_model()
SOCIAL_NETWORK_ENABLED = getattr(settings, 'SOCIAL_NETWORK_ENABLED')
class SocialAccountField(forms.Field):
def __init__(self, *args, **kwargs):
self.url = kwargs.pop('url', None)
super(SocialAccountField, self).__init__(*args, **kwargs)
def validate(self, value):
super(SocialAccountField, self).validate(value)
if value and not validate_social_account(value, self.url):
raise forms.ValidationError(_('Social account does not exist'),
code='social-account-doesnot-exist')
class UserForm(forms.ModelForm):
username = forms.CharField(
# Forces username to be lowercase always
widget=forms.TextInput(attrs={'style': 'text-transform: lowercase;'}),
)
required = ('first_name', 'last_name', 'username')
class Meta:
fields = ('first_name', 'last_name', 'username')
model = User
def __init__(self, *args, **kwargs):
super(UserForm, self).__init__(*args, **kwargs)
for field_name, field in self.fields.items():
# Adds form-control class to all form fields
field.widget.attrs.update({'class': 'form-control'})
# Set UserForm.required fields as required
if field_name in UserForm.required:
field.required = True
def clean_username(self):
username = self.cleaned_data["username"].strip()
if not username:
raise forms.ValidationError(_('This field cannot be blank.'))
return username
def clean_first_name(self):
first_name = self.cleaned_data["first_name"].strip()
if not first_name:
raise forms.ValidationError(_('This field cannot be blank.'))
return first_name
def clean_last_name(self):
last_name = self.cleaned_data["last_name"].strip()
if not last_name:
raise forms.ValidationError(_('This field cannot be blank.'))
return last_name
def clean_twitter(self):
twitter = self.cleaned_data["twitter"]
if twitter is not None and twitter.isspace():
return self.initial.get("twitter")
return twitter
def clean_facebook(self):
facebook = self.cleaned_data["facebook"]
if facebook is not None and facebook.isspace():
return self.initial.get("facebook")
return facebook
def clean_webpage(self):
webpage = self.cleaned_data["webpage"].strip()
return webpage
def clean_role(self):
role = self.cleaned_data["role"].strip()
return role
def clean_institution(self):
institution = self.cleaned_data["institution"].strip()
return institution
def clean_bio(self):
bio = self.cleaned_data["bio"].strip()
return bio
def clean_github(self):
github = self.cleaned_data["github"].strip()
return github
class UserUpdateForm(UserForm):
bio = forms.CharField(
widget=forms.Textarea(attrs={'rows': '6', 'maxlength': '200'}),
max_length=200,
label=_(u'Bio'),
help_text=_(u'Write something about you in 200 characters or less.'),
required=False,
)
def __init__(self, *args, **kwargs):
super(UserUpdateForm, self).__init__(*args, **kwargs)
self.fields.pop('username')
class Meta:
model = User
fields = ('first_name', 'last_name',
'institution', 'role')
if SOCIAL_NETWORK_ENABLED:
fields += ('twitter', 'facebook', 'google_talk', 'github')
fields += ('webpage', 'bio')
if SOCIAL_NETWORK_ENABLED:
twitter = SocialAccountField(url='https://twitter.com/',
required=False)
facebook = SocialAccountField(url='https://graph.facebook.com/',
required=False)
def get_lists_choices():
lists_names = []
for mlist in mailman.all_lists():
name = mlist.get('listname')
desc = mlist.get('description')
formatted_desc = u'{} ({})'.format(name, desc)
lists_names.append((name, formatted_desc))
return lists_names
# XXX: This field is no longer required when using django 1.8
class MultipleChoiceFieldLazy(forms.MultipleChoiceField):
def _set_choices(self, value):
self._choices = self.widget.choices = value
def _get_choices(self):
return list(self._choices)
choices = property(_get_choices, _set_choices)
class ListsForm(forms.Form):
lists = MultipleChoiceFieldLazy(label=_(u'Mailing lists'),
required=False,
widget=forms.CheckboxSelectMultiple,
choices=lazy(get_lists_choices, list)())
class ColabSetPasswordFormMixin(object):
def apply_custom_validators(self, password):
for app in settings.COLAB_APPS.values():
if 'password_validators' in app:
for validator_path in app.get('password_validators'):
module_path, func_name = validator_path.rsplit('.', 1)
module = import_module(module_path)
validator_func = getattr(module, func_name, None)
if validator_func:
validator_func(password)
return password
def clean_new_password2(self):
try:
password = super(ColabSetPasswordFormMixin, self).clean_new_password2()
except AttributeError:
password = self.cleaned_data['new_password2']
self.apply_custom_validators(password)
return password
def clean_password2(self):
try:
password = super(ColabSetPasswordFormMixin, self).clean_password2()
except AttributeError:
password = self.cleaned_data['password2']
self.apply_custom_validators(password)
return password
class UserCreationForm(UserForm, ColabSetPasswordFormMixin):
"""
A form that creates a user, with no privileges, from the given username and
password.
"""
error_messages = {
'duplicate_email': _("Email already used. Is it you? "
" Please <a href='%(url)s'>login</a>"),
'duplicate_username': _("A user with that username already exists."),
'password_mismatch': _("The two password fields didn't match."),
}
username = forms.RegexField(label=_("Username"), max_length=30,
regex=r'^[\w]+$',
help_text=_(("Required. 30 characters or fewer"
". Letter and digits.")),
error_messages={
'invalid': _(("This value may contain only"
" letters and numbers."))})
password1 = forms.CharField(label=_("Password"),
widget=forms.PasswordInput)
password2 = forms.CharField(label=_("Password confirmation"),
widget=forms.PasswordInput,
help_text=_(("Enter the same password as above"
", for verification.")))
def __init__(self, *args, **kwargs):
super(UserCreationForm, self).__init__(*args, **kwargs)
self.fields['email'].required = True
class Meta:
model = User
fields = ("username", "first_name", "last_name", "email")
def clean_email(self):
email = self.cleaned_data.get('email')
username = self.cleaned_data.get('username')
user_qs = User.objects.filter(email=email).exclude(username=username)
if email and user_qs.exists():
msg = self.error_messages.get('duplicate_email') % {
'url': reverse('login')
}
raise forms.ValidationError(mark_safe(msg))
return email
def clean_username(self):
# Since User.username is unique, this check is redundant,
# but it sets a nicer error message than the ORM. See #13147.
username = self.cleaned_data["username"].strip()
try:
User._default_manager.get(username=username)
except User.DoesNotExist:
return username
raise forms.ValidationError(
self.error_messages['duplicate_username'],
code='duplicate_username',
)
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError(
self.error_messages['password_mismatch'],
code='password_mismatch',
)
super(UserCreationForm, self).clean_password2()
return password2
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
password = self.cleaned_data["password1"]
user.set_password(password)
if commit:
user.save()
user_created.send(user.__class__, user=user, password=password)
return user
class UserChangeForm(forms.ModelForm):
username = forms.RegexField(
label=_("Username"), max_length=30, regex=r'^[\w]+$',
help_text=_("Required. 30 characters or fewer. Letters and digits."),
error_messages={
'invalid': _("This value may contain only letters and numbers.")})
# TODO: remove this hardcoded URL from here
password = ReadOnlyPasswordHashField(label=_("Password"),
help_text=_("Raw passwords are not"
" stored, so there is no"
" way to see "
"this user's password, "
"but you can change the "
"password "
"using <a "
"href=\"password/\">this"
" form</a>."))
class Meta:
model = User
fields = '__all__'
def __init__(self, *args, **kwargs):
super(UserChangeForm, self).__init__(*args, **kwargs)
f = self.fields.get('user_permissions', None)
if f is not None:
f.queryset = f.queryset.select_related('content_type')
# Set email as required field
self.fields['email'].required = True
def clean_password(self):
# Regardless of what the user provides, return the initial value.
# This is done here, rather than on the field, because the
# field does not have access to the initial value
return self.initial["password"]
class ColabSetPasswordForm(ColabSetPasswordFormMixin, SetPasswordForm):
pass
class ColabPasswordChangeForm(ColabSetPasswordFormMixin, PasswordChangeForm):
pass