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