Skip to content
Snippets Groups Projects
Commit 2a2ea58f authored by Leonidas Poulopoulos's avatar Leonidas Poulopoulos
Browse files

User monkeypatching (longer username) now works for admin as well

parent ff8bc42e
No related branches found
No related tags found
No related merge requests found
...@@ -10,6 +10,8 @@ from flowspy.flowspec.forms import * ...@@ -10,6 +10,8 @@ from flowspy.flowspec.forms import *
import datetime import datetime
from django.conf import settings from django.conf import settings
from flowspy.monkey_patch.forms import UserCreationForm, UserChangeForm
class RouteAdmin(admin.ModelAdmin): class RouteAdmin(admin.ModelAdmin):
form = RouteForm form = RouteForm
actions = ['deactivate'] actions = ['deactivate']
...@@ -49,6 +51,8 @@ class UserProfileInline(admin.StackedInline): ...@@ -49,6 +51,8 @@ class UserProfileInline(admin.StackedInline):
model = UserProfile model = UserProfile
class UserProfileAdmin(UserAdmin): class UserProfileAdmin(UserAdmin):
add_form = UserCreationForm
form = UserChangeForm
actions = ['deactivate', 'activate'] actions = ['deactivate', 'activate']
list_display = ('username', 'email', 'first_name' , 'last_name', 'is_staff', 'is_active', 'is_superuser', 'get_userprofile_peer') list_display = ('username', 'email', 'first_name' , 'last_name', 'is_staff', 'is_active', 'is_superuser', 'get_userprofile_peer')
inlines = [UserProfileInline] inlines = [UserProfileInline]
......
from django.conf import settings
# This patch was retrieved from: https://github.com/GoodCloud/django-longer-username
def MAX_USERNAME_LENGTH():
if hasattr(settings,"MAX_USERNAME_LENGTH"):
return settings.MAX_USERNAME_LENGTH
else:
return 255
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from flowspy.monkey_patch.forms import UserCreationForm, UserChangeForm
class LongerUserNameUserAdmin(UserAdmin):
add_form = UserCreationForm
form = UserChangeForm
admin.site.unregister(User)
admin.site.register(User, LongerUserNameUserAdmin)
\ No newline at end of file
from django.utils.translation import ugettext as _
from django.core.validators import MaxLengthValidator
from django.contrib.auth import forms as auth_forms
from django import forms
from flowspy.monkey_patch import MAX_USERNAME_LENGTH
def update_username_field(field):
field.widget.attrs['maxlength'] = MAX_USERNAME_LENGTH()
field.max_length = MAX_USERNAME_LENGTH()
field.help_text = _("Required, %s characters or fewer. Only letters, numbers, and characters such as @.+_- are allowed." % MAX_USERNAME_LENGTH())
# we need to find the MaxLengthValidator and change its
# limit_value otherwise the auth forms will fail validation
for v in field.validators:
if isinstance(v, MaxLengthValidator):
v.limit_value = MAX_USERNAME_LENGTH()
class UserCreationForm(auth_forms.UserCreationForm):
def __init__(self, *args, **kwargs):
super(UserCreationForm, self).__init__(*args, **kwargs)
update_username_field(self.fields['username'])
class UserChangeForm(auth_forms.UserChangeForm):
def __init__(self, *args, **kwargs):
super(UserChangeForm, self).__init__(*args, **kwargs)
update_username_field(self.fields['username'])
class AuthenticationForm(auth_forms.AuthenticationForm):
def __init__(self, *args, **kwargs):
super(AuthenticationForm, self).__init__(*args, **kwargs)
update_username_field(self.fields['username'])
from django.contrib.auth.models import User from django.utils.translation import ugettext as _
from django.db.models.signals import class_prepared
from django.core.validators import MaxLengthValidator from django.core.validators import MaxLengthValidator
from flowspy.monkey_patch import MAX_USERNAME_LENGTH
NEW_USERNAME_LENGTH = 255 def longer_username(sender, *args, **kwargs):
if sender.__name__ == "User" and sender.__module__ == "django.contrib.auth.models":
def monkey_patch_username(): sender._meta.get_field("username").max_length = MAX_USERNAME_LENGTH()
username = User._meta.get_field("username") # For Django 1.2 to work, the validator has to be declared apart from max_length
username.max_length = NEW_USERNAME_LENGTH sender._meta.get_field("username").validators = [MaxLengthValidator(MAX_USERNAME_LENGTH())]
for v in username.validators: sender._meta.get_field("username").help_text = _("Required, %s characters or fewer. Only letters, numbers, and @, ., +, -, or _ characters." % MAX_USERNAME_LENGTH())
if isinstance(v, MaxLengthValidator): class_prepared.connect(longer_username)
v.limit_value = NEW_USERNAME_LENGTH
monkey_patch_username()
# Create your views here.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment