add longer fields to custom user

This commit is contained in:
j 2020-05-30 12:19:02 +02:00
parent 38ce57bdae
commit 373d433516
4 changed files with 64 additions and 7 deletions

View file

@ -13,10 +13,17 @@ config.init()
NEW_LENGTH = { NEW_LENGTH = {
'username': 255, 'username': 255,
'email': 255, 'email': 254,
'password': 255, 'password': 255,
} }
def monkey_patch_groupname():
f = Group._meta.get_field('name')
f.max_length = 255
for v in f.validators:
if isinstance(v, MaxLengthValidator):
v.limit_value = 255
def monkey_patch_username(): def monkey_patch_username():
for field in NEW_LENGTH: for field in NEW_LENGTH:
f = User._meta.get_field(field) f = User._meta.get_field(field)
@ -24,11 +31,22 @@ def monkey_patch_username():
for v in f.validators: for v in f.validators:
if isinstance(v, MaxLengthValidator): if isinstance(v, MaxLengthValidator):
v.limit_value = NEW_LENGTH[field] v.limit_value = NEW_LENGTH[field]
monkey_patch_groupname()
def apply_patch():
from django.db import connection, transaction
cursor = connection.cursor()
table = connection.introspection.get_table_description(cursor, Group._meta.db_table)
sql = []
for row in table:
if row.name == 'name' and row.internal_size != 255:
sql.append('ALTER TABLE "%s" ALTER "%s" TYPE varchar(%d)' % (
Group._meta.db_table, row.name, 255)
)
for q in sql:
cursor.execute(q)
if sql:
transaction.commit()
f = Group._meta.get_field('name')
f.max_length = 255
for v in f.validators:
if isinstance(v, MaxLengthValidator):
v.limit_value = 255
monkey_patch_username() monkey_patch_username()

View file

@ -30,6 +30,8 @@ class Command(BaseCommand):
print(sql) print(sql)
cursor.execute(sql) cursor.execute(sql)
app.monkey_patch.apply_patch()
if settings.DB_GIN_TRGM: if settings.DB_GIN_TRGM:
import entity.models import entity.models
import document.models import document.models

View file

@ -0,0 +1,24 @@
# Generated by Django 3.0.6 on 2020-05-30 10:10
import django.contrib.auth.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('system', '0002_rename_user_table'),
]
operations = [
migrations.AlterField(
model_name='user',
name='email',
field=models.EmailField(blank=True, max_length=254, verbose_name='email address'),
),
migrations.AlterField(
model_name='user',
name='username',
field=models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 255 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=255, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username'),
),
]

View file

@ -1,6 +1,19 @@
from django.db import models from django.db import models
from django.contrib.auth.models import AbstractUser from django.contrib.auth.models import AbstractUser
from django.utils.translation import ugettext_lazy as _
class User(AbstractUser): class User(AbstractUser):
pass
password = models.CharField(_('password'), max_length=255)
username = models.CharField(
_('username'),
max_length=255,
unique=True,
help_text=_('Required. 255 characters or fewer. Letters, digits and @/./+/-/_ only.'),
validators=[AbstractUser.username_validator],
error_messages={
'unique': _("A user with that username already exists."),
},
)
last_name = models.CharField(_('last name'), max_length=150, blank=True)