def forwards(self, orm): # Changing field 'User.username' db.alter_column('auth_user', 'username', models.CharField(max_length=MAX_USERNAME_LENGTH())) # Increase length of email field to match db.alter_column('auth_user', 'email', models.CharField(max_length=MAX_USERNAME_LENGTH())) # Add an index to make email field unique db.create_index('auth_user', ['email'], unique=True)
def longer_username(sender, *args, **kwargs): if sender.__name__ == "User" and sender.__module__ == "django.contrib.auth.models": sender._meta.get_field("username").max_length = MAX_USERNAME_LENGTH() sender._meta.get_field("username").help_text = _( "Required, %s characters or fewer. Only letters, numbers, and @, ., +, -, or _ characters." % MAX_USERNAME_LENGTH()) sender._meta.get_field("email").max_length = MAX_USERNAME_LENGTH() sender._meta.get_field("email").help_text = _( "Required, %s characters or fewer. Only letters, numbers, and @, ., +, -, or _ characters." % 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()
def patch_user_model(model): field = model._meta.get_field("username") field.max_length = MAX_USERNAME_LENGTH() field.help_text = _("Required, %s characters or fewer. Only letters, " "numbers, and @, ., +, -, or _ " "characters." % MAX_USERNAME_LENGTH()) # patch model field validator because validator doesn't change if we change # max_length for v in field.validators: if isinstance(v, MaxLengthValidator): v.limit_value = MAX_USERNAME_LENGTH()
class Migration(migrations.Migration): initial = True dependencies = [] operations = [ migrations.AlterField( model_name='auth_user', name='username', field=models.CharField(max_length=MAX_USERNAME_LENGTH()), ) ]
def __init__(self, db_model=User, db_field='username', must_exist=False, skip_clean=False, label=_('Choose a screen name'), widget_attrs=None, **kw): self.must_exist = must_exist self.skip_clean = skip_clean self.db_model = db_model self.db_field = db_field self.user_instance = None error_messages = { 'required': _('user name is required'), 'taken': _('sorry, this name is taken, please choose another'), 'forbidden': _('sorry, this name is not allowed, please choose another'), 'missing': _('sorry, there is no user with this name'), 'multiple-taken': _('sorry, we have a serious error - user name is taken by several users' ), 'invalid': _('user name can only consist of letters, empty space and underscore' ), 'meaningless': _('please use at least some alphabetic characters in the user name' ), 'noemail': _('symbol "@" is not allowed') } if 'error_messages' in kw: error_messages.update(kw['error_messages']) del kw['error_messages'] if widget_attrs: widget_attrs.update(login_form_widget_attrs) else: widget_attrs = login_form_widget_attrs max_length = MAX_USERNAME_LENGTH() super(UserNameField, self).__init__( max_length=max_length, widget=forms.TextInput(attrs=login_form_widget_attrs), label=label, error_messages=error_messages, **kw)
def forwards(self, orm): # Changing field 'User.username' db.alter_column('auth_user', 'username', models.CharField(max_length=MAX_USERNAME_LENGTH()))
"characters." % MAX_USERNAME_LENGTH()) # patch model field validator because validator doesn't change if we change # max_length for v in field.validators: if isinstance(v, MaxLengthValidator): v.limit_value = MAX_USERNAME_LENGTH() from django.contrib.auth.models import User # https://github.com/GoodCloud/django-longer-username/issues/1 # django 1.3.X loads User model before class_prepared signal is connected # so we patch model after it's prepared # check if User model is patched if User._meta.get_field("username").max_length != MAX_USERNAME_LENGTH(): patch_user_model(User) ########NEW FILE######## __FILENAME__ = tests from django.contrib.auth.models import User from django.test import TestCase class LongerUsernameTests(TestCase): """ Unit tests for longerusername app """ def setUp(self): """ creates a user with a terribly long username """ long_username = ''.join([str(i) for i in range(100)])
def update_field_length(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())