-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50517f1
commit d16080c
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from django.contrib.auth.models import AbstractUser, BaseUserManager | ||
from django.db import models | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
|
||
class UserManager(BaseUserManager): | ||
""" | ||
Define a model manager for User model with no username field. | ||
Copied and modified from django.contrib.auth.models.UserManager | ||
""" | ||
|
||
use_in_migrations = True | ||
|
||
def _create_user(self, username, email, password, **extra_fields): | ||
""" | ||
Create and save a user with the given username, email, and password. | ||
""" | ||
if not username: | ||
raise ValueError('The given username must be set') | ||
email = self.normalize_email(email) | ||
username = self.model.normalize_username(username) | ||
user = self.model(username=username, email=email, **extra_fields) | ||
user.set_password(password) | ||
user.save(using=self._db) | ||
return user | ||
|
||
|
||
def create_user(self, username, email=None, password=None, **extra_fields): | ||
extra_fields.setdefault('is_staff', False) | ||
extra_fields.setdefault('is_superuser', False) | ||
return self._create_user(username, email, password, **extra_fields) | ||
|
||
|
||
def create_superuser(self, username, email, password, **extra_fields): | ||
extra_fields.setdefault('is_staff', True) | ||
extra_fields.setdefault('is_superuser', True) | ||
|
||
if extra_fields.get('is_staff') is not True: | ||
raise ValueError('Superuser must have is_staff=True.') | ||
if extra_fields.get('is_superuser') is not True: | ||
raise ValueError('Superuser must have is_superuser=True.') | ||
|
||
return self._create_user(username, email, password, **extra_fields) | ||
|
||
|
||
|
||
class User(AbstractUser): | ||
objects = UserManager() | ||
|
||
username = None | ||
email = models.EmailField(_('email address'), unique=True) | ||
|
||
USERNAME_FIELD = 'email' | ||
REQUIRED_FIELDS = [] | ||
|
||
|
||
# from django.contrib import admin | ||
# from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin | ||
# | ||
# | ||
# @admin.register(User) | ||
# class UserAdmin(DjangoUserAdmin): | ||
# """Define admin model for custom User model with no username field.""" | ||
# | ||
# fieldsets = ( | ||
# (None, {'fields': ('email', 'password')}), | ||
# (_('Personal info'), {'fields': ('first_name', 'last_name')}), | ||
# (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser', | ||
# 'groups', 'user_permissions')}), | ||
# (_('Important dates'), {'fields': ('last_login', 'date_joined')}), | ||
# ) | ||
# add_fieldsets = ( | ||
# (None, { | ||
# 'classes': ('wide',), | ||
# 'fields': ('email', 'password1', 'password2'), | ||
# }), | ||
# ) | ||
# list_display = ('email', 'first_name', 'last_name', 'is_staff') | ||
# search_fields = ('email', 'first_name', 'last_name') | ||
# ordering = ('email',) |