I have a custom auth app. I am trying to only allow emails to be lower case 
in the database.
I can create a user in the admin with Capitalized letters, even with 
email.lower() in the create_user method.

Another solution would be to specify to the authenticate that the email can 
be any case.

user = authenticate(email=email, password=password)

#models.py

class MyUserManager(BaseUserManager):
    def create_user(self, username, name, email, company, password, is_manager):
        #if not username:
        #    raise ValueError('User must have a username')

        user = self.model(username=username,
            email=MyUserManager.normalize_email(email.lower()),
            #email=email.lower(),
            name=name, company=company, is_manager=is_manager
        )

        user.set_password(password)
        user.save(using=self._db)

        #checks pending teams in the login view
        return user

    def create_superuser(self, password, username='', email=''):
        user = self.create_user(username=username,
            name='',
            email=email,
            company='not_a_companyNAMe',
            password=password,
            is_manager=False
        )
        user.is_admin = True
        user.is_superuser = True
        user.save(using=self._db)
        return user

class Users(AbstractBaseUser, PermissionsMixin):
    email           = models.EmailField(verbose_name=_('email address'), 
max_length=255, blank=True, unique=True)
    name            = models.CharField(verbose_name=_('Name'), max_length=50, 
blank=True)
    company         = models.CharField(verbose_name=_('Company'), 
max_length=255, blank=True)
    is_manager      = models.BooleanField(default=False, verbose_name=_('Check 
this box if you are a manager or need to create teams'))
    is_active       = models.BooleanField(default=True)
    is_admin        = models.BooleanField(default=False)
    is_customer     = models.BooleanField(default=False)
    datecreated     = models.DateField(auto_now=True)

    objects         = MyUserManager()
    if settings.LDAP:
        username        = models.CharField(verbose_name=_('Username'), 
max_length=50, unique=True)
        USERNAME_FIELD  = 'username'
    else:
        username        = models.CharField(verbose_name=_('Username'), 
max_length=50)
        USERNAME_FIELD  = 'email'

    REQUIRED_FIELDS = []

    class Meta:
        verbose_name_plural = "Users"


#admin.py

class UserCreationForm(forms.ModelForm):

    password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
    password2 = forms.CharField(label='Password confirmation', 
widget=forms.PasswordInput)
    name      = forms.CharField(label='Name', widget=forms.TextInput)
    company   = forms.CharField(label='Company', widget=forms.TextInput)
    is_manager= forms.BooleanField(label='Manager', required=False)

    class Meta:
        model = Users

    def clean_password2(self):
        # Check that the two password entries match
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("Passwords don't match")
        return password2

    def save(self, commit=True):
        # Save the provided password in hashed format
        user = super(UserCreationForm, self).save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        user.name = self.cleaned_data["name"]
        user.company = self.cleaned_data["company"]
        user.is_manager = self.cleaned_data['is_manager']
        if commit:
            user.save()
        return user

class UserChangeForm(forms.ModelForm):
    password = ReadOnlyPasswordHashField()

    class Meta:
        model = Users

    def clean_password(self):
        return self.initial["password"]

class MyUserAdmin(UserAdmin):
    form         = UserChangeForm
    add_form     = UserCreationForm

    readonly_fields = ('last_login',)

    list_display = ('name', 'email', 'is_manager', 'company',)
    list_editable = ('email', 'is_manager', 'company',)
    list_filter = ('is_admin',)
    fieldsets = (
        (None, {'fields': ('username', 'email', 'name', 'company', 
'password')}),
        ('Permissions', {'fields': ('is_manager', 'is_admin', 'groups', 
'user_permissions')}),
        ('Important dates', {'fields': ('last_login',)}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'name', 'company', 'password1', 'password2', 
'is_manager')}
        ),
    )
    search_fields = ('email', 'name', 'company', 'username',)
    ordering = ('email',)
    filter_horizontal = ()
admin.site.register(Users, MyUserAdmin)




-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to