first comit
This commit is contained in:
@@ -0,0 +1 @@
|
||||
__version__ = "1.4.0"
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,32 @@
|
||||
"""Integrate DjangoUseEmailAsUsername with admin module."""
|
||||
|
||||
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
|
||||
class BaseUserAdmin(DjangoUserAdmin):
|
||||
"""Define admin model for custom User model with no email 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",)
|
||||
@@ -0,0 +1,8 @@
|
||||
"""Declare app configuration for Django Use Email as Username."""
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class DjangoUseEmailAsUsernameConfig(AppConfig):
|
||||
"""Define app configuration class."""
|
||||
|
||||
name = "django_use_email_as_username"
|
||||
Binary file not shown.
@@ -0,0 +1,6 @@
|
||||
from django.contrib import admin
|
||||
from django_use_email_as_username.admin import BaseUserAdmin
|
||||
|
||||
from .models import User
|
||||
|
||||
admin.site.register(User, BaseUserAdmin)
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class {{ camel_case_app_name }}Config(AppConfig):
|
||||
name = '{{ app_name }}'
|
||||
verbose_name = 'Custom User Management'
|
||||
@@ -0,0 +1,5 @@
|
||||
from django_use_email_as_username.models import BaseUser, BaseUserManager
|
||||
|
||||
|
||||
class User(BaseUser):
|
||||
objects = BaseUserManager()
|
||||
@@ -0,0 +1,39 @@
|
||||
"""Define create_emailuser_app command."""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.management import call_command
|
||||
from django.core.management.templates import BaseCommand
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
"""Define create_emailuser_app command."""
|
||||
|
||||
requires_system_checks = []
|
||||
help = (
|
||||
"Creates a Django app with a custom User model that subclasses the"
|
||||
" User model declared by Django Use Email as Username."
|
||||
)
|
||||
|
||||
def add_arguments(self, parser):
|
||||
"""Define arguments for create_emailuser_app command."""
|
||||
parser.add_argument(
|
||||
"name",
|
||||
nargs="?",
|
||||
default="custom_user",
|
||||
help="Optional name of the application or project. [custom_user]",
|
||||
)
|
||||
parser.add_argument(
|
||||
"directory", nargs="?", help="Optional destination directory"
|
||||
)
|
||||
|
||||
def handle(self, name, **options):
|
||||
"""Call "startapp" to generate app with custom user model."""
|
||||
options["template"] = (
|
||||
os.path.dirname(os.path.abspath(__file__)) + "/app_template"
|
||||
)
|
||||
try:
|
||||
del options["skip_checks"]
|
||||
except KeyError:
|
||||
pass
|
||||
call_command("startapp", name, **options)
|
||||
@@ -0,0 +1,55 @@
|
||||
"""Declare models for DjangoUseEmailAsUsername app."""
|
||||
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.contrib.auth.models import BaseUserManager as DjangoBaseUserManager
|
||||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
|
||||
class BaseUserManager(DjangoBaseUserManager):
|
||||
"""Define a model manager for User model with no username field."""
|
||||
|
||||
use_in_migrations = True
|
||||
|
||||
def _create_user(self, email, password, **extra_fields):
|
||||
"""Create and save a User with the given email and password."""
|
||||
if not email:
|
||||
raise ValueError("The given email must be set")
|
||||
email = self.normalize_email(email)
|
||||
user = self.model(email=email, **extra_fields)
|
||||
user.set_password(password)
|
||||
user.save(using=self._db)
|
||||
return user
|
||||
|
||||
def create_user(self, email, password=None, **extra_fields):
|
||||
"""Create and save a regular User with the given email and password."""
|
||||
extra_fields.setdefault("is_staff", False)
|
||||
extra_fields.setdefault("is_superuser", False)
|
||||
return self._create_user(email, password, **extra_fields)
|
||||
|
||||
def create_superuser(self, email, password, **extra_fields):
|
||||
"""Create and save a SuperUser with the given email and password."""
|
||||
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(email, password, **extra_fields)
|
||||
|
||||
|
||||
class BaseUser(AbstractUser):
|
||||
"""User model."""
|
||||
|
||||
username = None
|
||||
email = models.EmailField(_("email address"), unique=True)
|
||||
|
||||
USERNAME_FIELD = "email"
|
||||
REQUIRED_FIELDS = []
|
||||
|
||||
class Meta:
|
||||
"""Define Meta class for BaseUser model."""
|
||||
|
||||
abstract = True
|
||||
Reference in New Issue
Block a user