├── core ├── __init__.py ├── __pycache__ │ ├── urls.cpython-38.pyc │ ├── wsgi.cpython-38.pyc │ ├── __init__.cpython-38.pyc │ └── settings.cpython-38.pyc ├── asgi.py ├── wsgi.py ├── urls.py └── settings.py ├── users ├── __init__.py ├── migrations │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-38.pyc │ │ └── 0001_initial.cpython-38.pyc │ └── 0001_initial.py ├── views.py ├── apps.py ├── __pycache__ │ ├── admin.cpython-38.pyc │ ├── models.cpython-38.pyc │ ├── tests.cpython-38.pyc │ └── __init__.cpython-38.pyc ├── admin.py ├── tests.py └── models.py ├── .gitattributes ├── db.sqlite3 ├── commands.txt └── manage.py /core/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /users/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /users/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /users/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | 3 | # Create your views here. 4 | -------------------------------------------------------------------------------- /users/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class UsersConfig(AppConfig): 5 | name = 'users' 6 | -------------------------------------------------------------------------------- /db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT-Django-Theory-Create-Custom-User-Models-Admin-Testing/HEAD/db.sqlite3 -------------------------------------------------------------------------------- /core/__pycache__/urls.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT-Django-Theory-Create-Custom-User-Models-Admin-Testing/HEAD/core/__pycache__/urls.cpython-38.pyc -------------------------------------------------------------------------------- /core/__pycache__/wsgi.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT-Django-Theory-Create-Custom-User-Models-Admin-Testing/HEAD/core/__pycache__/wsgi.cpython-38.pyc -------------------------------------------------------------------------------- /users/__pycache__/admin.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT-Django-Theory-Create-Custom-User-Models-Admin-Testing/HEAD/users/__pycache__/admin.cpython-38.pyc -------------------------------------------------------------------------------- /users/__pycache__/models.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT-Django-Theory-Create-Custom-User-Models-Admin-Testing/HEAD/users/__pycache__/models.cpython-38.pyc -------------------------------------------------------------------------------- /users/__pycache__/tests.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT-Django-Theory-Create-Custom-User-Models-Admin-Testing/HEAD/users/__pycache__/tests.cpython-38.pyc -------------------------------------------------------------------------------- /core/__pycache__/__init__.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT-Django-Theory-Create-Custom-User-Models-Admin-Testing/HEAD/core/__pycache__/__init__.cpython-38.pyc -------------------------------------------------------------------------------- /core/__pycache__/settings.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT-Django-Theory-Create-Custom-User-Models-Admin-Testing/HEAD/core/__pycache__/settings.cpython-38.pyc -------------------------------------------------------------------------------- /users/__pycache__/__init__.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT-Django-Theory-Create-Custom-User-Models-Admin-Testing/HEAD/users/__pycache__/__init__.cpython-38.pyc -------------------------------------------------------------------------------- /users/migrations/__pycache__/__init__.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT-Django-Theory-Create-Custom-User-Models-Admin-Testing/HEAD/users/migrations/__pycache__/__init__.cpython-38.pyc -------------------------------------------------------------------------------- /users/migrations/__pycache__/0001_initial.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/veryacademy/YT-Django-Theory-Create-Custom-User-Models-Admin-Testing/HEAD/users/migrations/__pycache__/0001_initial.cpython-38.pyc -------------------------------------------------------------------------------- /commands.txt: -------------------------------------------------------------------------------- 1 | py -m venv venv 2 | venv\Scripts\activate 3 | pip install django 4 | django-admin startproject core . 5 | py manage.py startapp users 6 | py manage.py makemigrations --dry-run (optional --verbosity 1,2,3) 7 | pip install coverage 8 | coverage run --omit='*/venv/*' manage.py test 9 | coverage html -------------------------------------------------------------------------------- /core/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for core project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /core/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for core project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /core/urls.py: -------------------------------------------------------------------------------- 1 | """core URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/3.1/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | 19 | urlpatterns = [ 20 | path('admin/', admin.site.urls), 21 | ] 22 | -------------------------------------------------------------------------------- /users/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from .models import NewUser 3 | from django.contrib.auth.admin import UserAdmin 4 | from django.forms import TextInput, Textarea 5 | 6 | 7 | class UserAdminConfig(UserAdmin): 8 | model = NewUser 9 | search_fields = ('email', 'user_name', 'first_name',) 10 | list_filter = ('email', 'user_name', 'first_name', 'is_active', 'is_staff') 11 | ordering = ('-start_date',) 12 | list_display = ('email', 'user_name', 'first_name', 13 | 'is_active', 'is_staff') 14 | fieldsets = ( 15 | (None, {'fields': ('email', 'user_name', 'first_name',)}), 16 | ('Permissions', {'fields': ('is_staff', 'is_active')}), 17 | ('Personal', {'fields': ('about',)}), 18 | ) 19 | formfield_overrides = { 20 | NewUser.about: {'widget': Textarea(attrs={'rows': 10, 'cols': 40})}, 21 | } 22 | add_fieldsets = ( 23 | (None, { 24 | 'classes': ('wide',), 25 | 'fields': ('email', 'user_name', 'first_name', 'password1', 'password2', 'is_active', 'is_staff')} 26 | ), 27 | ) 28 | 29 | 30 | admin.site.register(NewUser, UserAdminConfig) 31 | -------------------------------------------------------------------------------- /users/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | from django.contrib.auth import get_user_model 3 | 4 | 5 | class UserAccountTests(TestCase): 6 | 7 | def test_new_superuser(self): 8 | db = get_user_model() 9 | super_user = db.objects.create_superuser( 10 | 'testuser@super.com', 'username', 'firstname', 'password') 11 | self.assertEqual(super_user.email, 'testuser@super.com') 12 | self.assertEqual(super_user.user_name, 'username') 13 | self.assertEqual(super_user.first_name, 'firstname') 14 | self.assertTrue(super_user.is_superuser) 15 | self.assertTrue(super_user.is_staff) 16 | self.assertTrue(super_user.is_active) 17 | self.assertEqual(str(super_user), "username") 18 | 19 | with self.assertRaises(ValueError): 20 | db.objects.create_superuser( 21 | email='testuser@super.com', user_name='username1', first_name='first_name', password='password', is_superuser=False) 22 | 23 | with self.assertRaises(ValueError): 24 | db.objects.create_superuser( 25 | email='testuser@super.com', user_name='username1', first_name='first_name', password='password', is_staff=False) 26 | 27 | with self.assertRaises(ValueError): 28 | db.objects.create_superuser( 29 | email='', user_name='username1', first_name='first_name', password='password', is_superuser=True) 30 | 31 | def test_new_user(self): 32 | db = get_user_model() 33 | user = db.objects.create_user( 34 | 'testuser@user.com', 'username', 'firstname', 'password') 35 | self.assertEqual(user.email, 'testuser@user.com') 36 | self.assertEqual(user.user_name, 'username') 37 | self.assertEqual(user.first_name, 'firstname') 38 | self.assertFalse(user.is_superuser) 39 | self.assertFalse(user.is_staff) 40 | self.assertFalse(user.is_active) 41 | 42 | with self.assertRaises(ValueError): 43 | db.objects.create_user( 44 | email='', user_name='a', first_name='first_name', password='password') 45 | -------------------------------------------------------------------------------- /users/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | from django.utils import timezone 3 | from django.utils.translation import gettext_lazy as _ 4 | from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager 5 | 6 | 7 | class CustomAccountManager(BaseUserManager): 8 | 9 | def create_superuser(self, email, user_name, first_name, password, **other_fields): 10 | 11 | other_fields.setdefault('is_staff', True) 12 | other_fields.setdefault('is_superuser', True) 13 | other_fields.setdefault('is_active', True) 14 | 15 | if other_fields.get('is_staff') is not True: 16 | raise ValueError( 17 | 'Superuser must be assigned to is_staff=True.') 18 | if other_fields.get('is_superuser') is not True: 19 | raise ValueError( 20 | 'Superuser must be assigned to is_superuser=True.') 21 | 22 | return self.create_user(email, user_name, first_name, password, **other_fields) 23 | 24 | def create_user(self, email, user_name, first_name, password, **other_fields): 25 | 26 | if not email: 27 | raise ValueError(_('You must provide an email address')) 28 | 29 | email = self.normalize_email(email) 30 | user = self.model(email=email, user_name=user_name, 31 | first_name=first_name, **other_fields) 32 | user.set_password(password) 33 | user.save() 34 | return user 35 | 36 | 37 | class NewUser(AbstractBaseUser, PermissionsMixin): 38 | 39 | email = models.EmailField(_('email address'), unique=True) 40 | user_name = models.CharField(max_length=150, unique=True) 41 | first_name = models.CharField(max_length=150, blank=True) 42 | start_date = models.DateTimeField(default=timezone.now) 43 | about = models.TextField(_( 44 | 'about'), max_length=500, blank=True) 45 | is_staff = models.BooleanField(default=False) 46 | is_active = models.BooleanField(default=False) 47 | 48 | objects = CustomAccountManager() 49 | 50 | USERNAME_FIELD = 'email' 51 | REQUIRED_FIELDS = ['user_name', 'first_name'] 52 | 53 | def __str__(self): 54 | return self.user_name 55 | -------------------------------------------------------------------------------- /users/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.1.1 on 2020-09-07 14:29 2 | 3 | from django.db import migrations, models 4 | import django.utils.timezone 5 | 6 | 7 | class Migration(migrations.Migration): 8 | 9 | initial = True 10 | 11 | dependencies = [ 12 | ('auth', '0012_alter_user_first_name_max_length'), 13 | ] 14 | 15 | operations = [ 16 | migrations.CreateModel( 17 | name='NewUser', 18 | fields=[ 19 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 20 | ('password', models.CharField(max_length=128, verbose_name='password')), 21 | ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), 22 | ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), 23 | ('email', models.EmailField(max_length=254, unique=True, verbose_name='email address')), 24 | ('user_name', models.CharField(max_length=150, unique=True)), 25 | ('first_name', models.CharField(blank=True, max_length=150)), 26 | ('start_date', models.DateTimeField(default=django.utils.timezone.now)), 27 | ('about', models.TextField(blank=True, max_length=500, verbose_name='about')), 28 | ('is_staff', models.BooleanField(default=False)), 29 | ('is_active', models.BooleanField(default=False)), 30 | ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')), 31 | ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')), 32 | ], 33 | options={ 34 | 'abstract': False, 35 | }, 36 | ), 37 | ] 38 | -------------------------------------------------------------------------------- /core/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for core project. 3 | 4 | Generated by 'django-admin startproject' using Django 3.1.1. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/3.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = '3gjm^_scxasa&ewe64tb$ass7t)z*a45aycsqnwo3j!+^=xq&j' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'users', 41 | ] 42 | 43 | MIDDLEWARE = [ 44 | 'django.middleware.security.SecurityMiddleware', 45 | 'django.contrib.sessions.middleware.SessionMiddleware', 46 | 'django.middleware.common.CommonMiddleware', 47 | 'django.middleware.csrf.CsrfViewMiddleware', 48 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 49 | 'django.contrib.messages.middleware.MessageMiddleware', 50 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 51 | ] 52 | 53 | ROOT_URLCONF = 'core.urls' 54 | 55 | TEMPLATES = [ 56 | { 57 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 58 | 'DIRS': [], 59 | 'APP_DIRS': True, 60 | 'OPTIONS': { 61 | 'context_processors': [ 62 | 'django.template.context_processors.debug', 63 | 'django.template.context_processors.request', 64 | 'django.contrib.auth.context_processors.auth', 65 | 'django.contrib.messages.context_processors.messages', 66 | ], 67 | }, 68 | }, 69 | ] 70 | 71 | WSGI_APPLICATION = 'core.wsgi.application' 72 | 73 | 74 | # Database 75 | # https://docs.djangoproject.com/en/3.1/ref/settings/#databases 76 | 77 | DATABASES = { 78 | 'default': { 79 | 'ENGINE': 'django.db.backends.sqlite3', 80 | 'NAME': BASE_DIR / 'db.sqlite3', 81 | } 82 | } 83 | 84 | 85 | # Password validation 86 | # https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators 87 | 88 | AUTH_PASSWORD_VALIDATORS = [ 89 | { 90 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 91 | }, 92 | { 93 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 94 | }, 95 | { 96 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 97 | }, 98 | { 99 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 100 | }, 101 | ] 102 | 103 | 104 | # Internationalization 105 | # https://docs.djangoproject.com/en/3.1/topics/i18n/ 106 | 107 | LANGUAGE_CODE = 'en-us' 108 | 109 | TIME_ZONE = 'UTC' 110 | 111 | USE_I18N = True 112 | 113 | USE_L10N = True 114 | 115 | USE_TZ = True 116 | 117 | 118 | # Static files (CSS, JavaScript, Images) 119 | # https://docs.djangoproject.com/en/3.1/howto/static-files/ 120 | 121 | STATIC_URL = '/static/' 122 | AUTH_USER_MODEL = 'users.NewUser' 123 | --------------------------------------------------------------------------------