Revision 2a2ea58f
b/flowspec/admin.py | ||
---|---|---|
10 | 10 |
import datetime |
11 | 11 |
from django.conf import settings |
12 | 12 |
|
13 |
from flowspy.monkey_patch.forms import UserCreationForm, UserChangeForm |
|
14 |
|
|
13 | 15 |
class RouteAdmin(admin.ModelAdmin): |
14 | 16 |
form = RouteForm |
15 | 17 |
actions = ['deactivate'] |
... | ... | |
49 | 51 |
model = UserProfile |
50 | 52 |
|
51 | 53 |
class UserProfileAdmin(UserAdmin): |
54 |
add_form = UserCreationForm |
|
55 |
form = UserChangeForm |
|
52 | 56 |
actions = ['deactivate', 'activate'] |
53 | 57 |
list_display = ('username', 'email', 'first_name' , 'last_name', 'is_staff', 'is_active', 'is_superuser', 'get_userprofile_peer') |
54 | 58 |
inlines = [UserProfileInline] |
b/monkey_patch/__init__.py | ||
---|---|---|
1 |
from django.conf import settings |
|
2 |
# This patch was retrieved from: https://github.com/GoodCloud/django-longer-username |
|
3 |
def MAX_USERNAME_LENGTH(): |
|
4 |
if hasattr(settings,"MAX_USERNAME_LENGTH"): |
|
5 |
return settings.MAX_USERNAME_LENGTH |
|
6 |
else: |
|
7 |
return 255 |
b/monkey_patch/admin.py | ||
---|---|---|
1 |
from django.contrib import admin |
|
2 |
from django.contrib.auth.admin import UserAdmin |
|
3 |
from django.contrib.auth.models import User |
|
4 |
|
|
5 |
from flowspy.monkey_patch.forms import UserCreationForm, UserChangeForm |
|
6 |
|
|
7 |
class LongerUserNameUserAdmin(UserAdmin): |
|
8 |
add_form = UserCreationForm |
|
9 |
form = UserChangeForm |
|
10 |
|
|
11 |
admin.site.unregister(User) |
|
12 |
admin.site.register(User, LongerUserNameUserAdmin) |
b/monkey_patch/forms.py | ||
---|---|---|
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']) |
b/monkey_patch/models.py | ||
---|---|---|
1 |
from django.contrib.auth.models import User |
|
1 |
from django.utils.translation import ugettext as _ |
|
2 |
from django.db.models.signals import class_prepared |
|
2 | 3 |
from django.core.validators import MaxLengthValidator |
4 |
from flowspy.monkey_patch import MAX_USERNAME_LENGTH |
|
3 | 5 |
|
4 |
NEW_USERNAME_LENGTH = 255 |
|
5 |
|
|
6 |
def monkey_patch_username(): |
|
7 |
username = User._meta.get_field("username") |
|
8 |
username.max_length = NEW_USERNAME_LENGTH |
|
9 |
for v in username.validators: |
|
10 |
if isinstance(v, MaxLengthValidator): |
|
11 |
v.limit_value = NEW_USERNAME_LENGTH |
|
12 |
|
|
13 |
monkey_patch_username() |
|
6 |
def longer_username(sender, *args, **kwargs): |
|
7 |
if sender.__name__ == "User" and sender.__module__ == "django.contrib.auth.models": |
|
8 |
sender._meta.get_field("username").max_length = MAX_USERNAME_LENGTH() |
|
9 |
# For Django 1.2 to work, the validator has to be declared apart from max_length |
|
10 |
sender._meta.get_field("username").validators = [MaxLengthValidator(MAX_USERNAME_LENGTH())] |
|
11 |
sender._meta.get_field("username").help_text = _("Required, %s characters or fewer. Only letters, numbers, and @, ., +, -, or _ characters." % MAX_USERNAME_LENGTH()) |
|
12 |
class_prepared.connect(longer_username) |
b/monkey_patch/views.py | ||
---|---|---|
1 |
# Create your views here. |
Also available in: Unified diff