Skip to content

Fixed #10 - Use citext to make usernames case insensitive #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions warehouse/accounts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@
class Migration(SchemaMigration):

def forwards(self, orm):
# CREATE the citext extension
db.execute("CREATE EXTENSION IF NOT EXISTS citext")

# Adding model 'User'
db.create_table('accounts_user', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('password', self.gf('django.db.models.fields.CharField')(max_length=128)),
('last_login', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now)),
('is_superuser', self.gf('django.db.models.fields.BooleanField')(default=False)),
('username', self.gf('django.db.models.fields.CharField')(unique=True, max_length=50)),
('username', self.gf('warehouse.utils.db_fields.CaseInsensitiveCharField')(unique=True, max_length=50)),
('name', self.gf('django.db.models.fields.CharField')(blank=True, max_length=100)),
('is_staff', self.gf('django.db.models.fields.BooleanField')(default=False)),
('is_active', self.gf('django.db.models.fields.BooleanField')(default=True)),
('date_joined', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now)),
))

# Adding constraint to model field 'User.username'
db.execute("ALTER TABLE accounts_user ADD CONSTRAINT accounts_user_username_length CHECK (length(username) <= 50)")

# Send signal to show we've created the User model
db.send_create_signal('accounts', ['User'])

# Adding M2M table for field groups on 'User'
Expand Down Expand Up @@ -110,4 +118,4 @@ def backwards(self, orm):
}
}

complete_apps = ['accounts']
complete_apps = ['accounts']
3 changes: 2 additions & 1 deletion warehouse/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from warehouse import accounts
from warehouse.accounts import adapters
from warehouse.utils.db_fields import CaseInsensitiveCharField


class UserManager(BaseUserManager):
Expand Down Expand Up @@ -52,7 +53,7 @@ class User(AbstractBaseUser, PermissionsMixin):

USERNAME_FIELD = "username"

username = models.CharField(_("username"),
username = CaseInsensitiveCharField(_("username"),
max_length=50,
unique=True,
help_text=accounts.VALID_USERNAME_DESC,
Expand Down
15 changes: 15 additions & 0 deletions warehouse/utils/db_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.db.models import fields
from south.modelsinspector import add_introspection_rules


class CaseInsensitiveCharField(fields.CharField):
# NOTE: You MUST manually add a CHECK CONSTRAINT to the database for
# max_length to be respected in the DB.

def db_type(self, connection):
return "citext"


add_introspection_rules([],
["^warehouse\.utils\.db_fields\.CaseInsensitiveCharField"],
)