Skip to content

Instantly share code, notes, and snippets.

@un33k
Created February 28, 2014 16:09
Show Gist options
  • Save un33k/9273782 to your computer and use it in GitHub Desktop.
Save un33k/9273782 to your computer and use it in GitHub Desktop.
Custom Django User with Case Insensitive Options
import uuid
from django.db import models
from django.utils import timezone
from django.core import validators
from django.utils.translation import ugettext_lazy as _
from django.core.validators import MinLengthValidator
from django.core.validators import MaxLengthValidator
from django.contrib.auth.models import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.models import BaseUserManager
from django.db.models.query import QuerySet
try:
from uuslug import slugify
except ImportError:
from django.template.defaultfilters import slugify
from .validators import *
from .utils import get_default_alias
from . import defaults
class CaseInsensitiveQuerySet(QuerySet):
"""
Custom QuerySet to treat queries on special field(s) as case insensitive.
Models fields that should be treated as case-insensitive should be place in
CASE_INSENSITIVE_FIELDS with in the model class.
Example: CASE_INSENSITIVE_FIELDS = ['email', 'username',]
"""
def _filter_or_exclude(self, mapper, *args, **kwargs):
for f in self.model.CASE_INSENSITIVE_FIELDS:
if f in kwargs:
kwargs[f + '__iexact'] = kwargs[f]
del kwargs[f]
return super(CaseInsensitiveQuerySet, self)._filter_or_exclude(mapper, *args, **kwargs)
class UserProfileManager(BaseUserManager):
"""
Custom User Manager Class.
USERNAME_FIELD is the email field.
"""
def _create_user(self, email, password, is_staff, is_superuser,
**extra_fields):
"""
Creates and saves a User with the given, email and password.
"""
if email:
user = self.model(email=self.normalize_email(email),
is_staff=is_staff, is_active=True,
is_superuser=is_superuser,
last_login=timezone.now(),
**extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
else:
raise ValueError(_('The given email must be set.'))
def create_user(self, email=None, password=None, **extra_fields):
return self._create_user(email, password, False, False, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
return self._create_user(email, password, True, True, **extra_fields)
def get_queryset(self):
return CaseInsensitiveQuerySet(self.model)
class UserProfile(AbstractBaseUser, PermissionsMixin):
"""
A custom user class.
The required fields are: email and password.
"""
VISIBILITY_PUBLIC = 1
VISIBILITY_MEMBERS = 2
VISIBILITY_PRIVATE = 3
VISIBILITY_OPTIONS = (
(VISIBILITY_PUBLIC, 'Public'),
(VISIBILITY_MEMBERS, 'Members only'),
(VISIBILITY_PRIVATE, 'Private'),
)
PASSWORD_MIN_LENGTH = defaults.PASSWORD_MIN_LENGTH
created_on = models.DateTimeField(_('Date Joined'), default=timezone.now())
updated_on = models.DateTimeField(_('Last Updated'), auto_now=True)
is_staff = models.BooleanField(
_('staff member'),
default=False,
help_text=_('Designates if the user can log into this admin site.'),
)
is_active = models.BooleanField(
_('active'),
default=True,
help_text=_('Inactive users cannot login.'),
)
email = models.EmailField(
_('email'),
db_index=True,
unique=True,
help_text=_('Your email address.')
)
alias = models.CharField(
_('alias'),
max_length=defaults.ALIAS_MAX_LENGHT,
unique=True,
default=get_default_alias,
validators=[MinLengthValidator(3), AlphaNumDashValidator,
MaxLengthValidator(defaults.ALIAS_MAX_LENGHT), ],
help_text=_('Choose alphanumeric and dashes. It cannot begin or end with a dash.'),
)
first_name = models.CharField(
_('first name'),
max_length=32,
blank=True,
help_text=_('Your first name, or nickname')
)
last_name = models.CharField(
_('last name'),
max_length=32,
blank=True,
help_text=_('Your last name, or family name')
)
slug = models.CharField(
_('slug'),
max_length=65,
blank=True,
editable=False,
)
bio = models.TextField(
_('your bio'),
blank=True,
validators=[MaxLengthValidator(1500)],
help_text=_('Your biographical information or background.')
)
visibility = models.IntegerField(
_('visibility'),
choices=VISIBILITY_OPTIONS,
default=VISIBILITY_PUBLIC,
help_text=_("Your account's visibility level.")
)
########### Add new fields above this line #############
objects = UserProfileManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
CASE_INSENSITIVE_FIELDS = ['email', 'alias', 'slug']
class Meta:
verbose_name = _('profile')
verbose_name_plural = _('profiles')
def get_absolute_url(self):
"""
Return public URL for user
"""
return "/m/{}/".format(self.alias)
def get_full_name(self):
"""
Returns name
"""
full_name = "{} {}".format(self.first_name, self.last_name)
return full_name.strip()
def get_short_name(self):
"""
Returns first name
"""
return self.first_name
def get_slug(self):
"""
Returns user slug.
"""
if not self.slug:
self.slug = slugify(self.get_full_name())
return self.slug
def email_user(self, subject, message, from_email=None, **kwargs):
"""
Sends an email to this Use
"""
send_mail(subject, message, from_email, [self.email], **kwargs)
def save(self, *args, **kwargs):
"""
Final housekeeping before save
"""
self.get_slug()
return super(UserProfile, self).save(*args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment