Revision 2a2ea58f monkey_patch/models.py
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) |
Also available in: Unified diff