root / monkey_patch / forms.py @ 5af3efa3
History | View | Annotate | Download (1.4 kB)
1 |
from django.utils.translation import ugettext as _ |
---|---|
2 |
from django.core.validators import MaxLengthValidator |
3 |
from django.contrib.auth import forms as auth_forms |
4 |
from django import forms |
5 |
|
6 |
from flowspy.monkey_patch import MAX_USERNAME_LENGTH |
7 |
|
8 |
def update_username_field(field): |
9 |
field.widget.attrs['maxlength'] = MAX_USERNAME_LENGTH()
|
10 |
field.max_length = MAX_USERNAME_LENGTH() |
11 |
field.help_text = _("Required, %s characters or fewer. Only letters, numbers, and characters such as @.+_- are allowed." % MAX_USERNAME_LENGTH())
|
12 |
|
13 |
# we need to find the MaxLengthValidator and change its
|
14 |
# limit_value otherwise the auth forms will fail validation
|
15 |
for v in field.validators: |
16 |
if isinstance(v, MaxLengthValidator): |
17 |
v.limit_value = MAX_USERNAME_LENGTH() |
18 |
|
19 |
class UserCreationForm(auth_forms.UserCreationForm): |
20 |
def __init__(self, *args, **kwargs): |
21 |
super(UserCreationForm, self).__init__(*args, **kwargs) |
22 |
update_username_field(self.fields['username']) |
23 |
|
24 |
class UserChangeForm(auth_forms.UserChangeForm): |
25 |
def __init__(self, *args, **kwargs): |
26 |
super(UserChangeForm, self).__init__(*args, **kwargs) |
27 |
update_username_field(self.fields['username']) |
28 |
|
29 |
class AuthenticationForm(auth_forms.AuthenticationForm): |
30 |
def __init__(self, *args, **kwargs): |
31 |
super(AuthenticationForm, self).__init__(*args, **kwargs) |
32 |
update_username_field(self.fields['username']) |