From 373d43351631c27d93a668314c0a7a0818063cf3 Mon Sep 17 00:00:00 2001 From: j Date: Sat, 30 May 2020 12:19:02 +0200 Subject: [PATCH] add longer fields to custom user --- pandora/app/monkey_patch.py | 30 +++++++++++++++---- .../item/management/commands/sqlfindindex.py | 2 ++ .../system/migrations/0003_field_length.py | 24 +++++++++++++++ pandora/system/models.py | 15 +++++++++- 4 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 pandora/system/migrations/0003_field_length.py diff --git a/pandora/app/monkey_patch.py b/pandora/app/monkey_patch.py index 84b39001..f4579d56 100644 --- a/pandora/app/monkey_patch.py +++ b/pandora/app/monkey_patch.py @@ -13,10 +13,17 @@ config.init() NEW_LENGTH = { 'username': 255, - 'email': 255, + 'email': 254, '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(): for field in NEW_LENGTH: f = User._meta.get_field(field) @@ -24,11 +31,22 @@ def monkey_patch_username(): for v in f.validators: if isinstance(v, MaxLengthValidator): 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() diff --git a/pandora/item/management/commands/sqlfindindex.py b/pandora/item/management/commands/sqlfindindex.py index dfdf029e..81c39488 100644 --- a/pandora/item/management/commands/sqlfindindex.py +++ b/pandora/item/management/commands/sqlfindindex.py @@ -30,6 +30,8 @@ class Command(BaseCommand): print(sql) cursor.execute(sql) + app.monkey_patch.apply_patch() + if settings.DB_GIN_TRGM: import entity.models import document.models diff --git a/pandora/system/migrations/0003_field_length.py b/pandora/system/migrations/0003_field_length.py new file mode 100644 index 00000000..2381ebe7 --- /dev/null +++ b/pandora/system/migrations/0003_field_length.py @@ -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'), + ), + ] diff --git a/pandora/system/models.py b/pandora/system/models.py index 696b770a..2f59135c 100644 --- a/pandora/system/models.py +++ b/pandora/system/models.py @@ -1,6 +1,19 @@ from django.db import models from django.contrib.auth.models import AbstractUser +from django.utils.translation import ugettext_lazy as _ 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)