From 72c432dd1895233b867b72c53a91f559ff9494d9 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sat, 27 Jan 2018 21:06:40 +0100 Subject: [PATCH] Add the new user model --- khaganat/settings.py | 7 +++ neluser/__init__.py | 0 neluser/admin.py | 3 ++ neluser/apps.py | 5 ++ neluser/migrations/0001_initial.py | 39 +++++++++++++++ neluser/migrations/__init__.py | 0 neluser/models.py | 80 ++++++++++++++++++++++++++++++ neluser/tests.py | 3 ++ neluser/views.py | 3 ++ 9 files changed, 140 insertions(+) create mode 100644 neluser/__init__.py create mode 100644 neluser/admin.py create mode 100644 neluser/apps.py create mode 100644 neluser/migrations/0001_initial.py create mode 100644 neluser/migrations/__init__.py create mode 100644 neluser/models.py create mode 100644 neluser/tests.py create mode 100644 neluser/views.py diff --git a/khaganat/settings.py b/khaganat/settings.py index c7d31b5..41eb692 100644 --- a/khaganat/settings.py +++ b/khaganat/settings.py @@ -39,6 +39,7 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', 'khaganat', + 'neluser.apps.NeluserConfig', 'pages.apps.PagesConfig', 'navbar.apps.NavbarConfig', 'logs.apps.LogsConfig', @@ -87,6 +88,12 @@ DATABASES = { } +# User model +# https://docs.djangoproject.com/en/2.0/topics/auth/customizing/ + +AUTH_USER_MODEL = 'neluser.NelUser' + + # Password validation # https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators diff --git a/neluser/__init__.py b/neluser/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/neluser/admin.py b/neluser/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/neluser/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/neluser/apps.py b/neluser/apps.py new file mode 100644 index 0000000..6d32098 --- /dev/null +++ b/neluser/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class NeluserConfig(AppConfig): + name = 'neluser' diff --git a/neluser/migrations/0001_initial.py b/neluser/migrations/0001_initial.py new file mode 100644 index 0000000..adc940b --- /dev/null +++ b/neluser/migrations/0001_initial.py @@ -0,0 +1,39 @@ +# Generated by Django 2.0.1 on 2018-01-27 20:04 + +from django.db import migrations, models +import django.utils.timezone +import neluser.models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('auth', '0009_alter_user_last_name_max_length'), + ] + + operations = [ + migrations.CreateModel( + name='NelUser', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('password', models.CharField(max_length=128, verbose_name='password')), + ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), + ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), + ('email', models.EmailField(max_length=254, null=True, unique=True)), + ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), + ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), + ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), + ('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')), + ('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')), + ], + options={ + 'verbose_name': 'user', + 'verbose_name_plural': 'users', + }, + managers=[ + ('objects', neluser.models.NelUserManager()), + ], + ), + ] diff --git a/neluser/migrations/__init__.py b/neluser/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/neluser/models.py b/neluser/models.py new file mode 100644 index 0000000..9d71338 --- /dev/null +++ b/neluser/models.py @@ -0,0 +1,80 @@ +from django.contrib.auth.models import AbstractBaseUser,PermissionsMixin +from django.contrib.auth.models import BaseUserManager +from django.utils.translation import ugettext_lazy as _ +from django.core.mail import send_mail +from django.utils import timezone +from django.db import models + + +class NelUserManager(BaseUserManager): + use_in_migrations = True + + def _create_user(self, email, password, **extra_fields): + 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() + return user + + def create_user(self, email, password=None, **extra_fields): + 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): + extra_fields.setdefault('is_staff', True) + extra_fields.setdefault('is_superuser', True) + extra_fields.setdefault('is_active', 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 NelUser(AbstractBaseUser, PermissionsMixin): + email = models.EmailField(unique=True, null=True) + is_staff = models.BooleanField( + _('staff status'), + default=False, + help_text=_('Designates whether the user can log into this admin site.'), + ) + is_active = models.BooleanField( + _('active'), + default=True, + help_text=_( + 'Designates whether this user should be treated as active. ' + 'Unselect this instead of deleting accounts.' + ), + ) + date_joined = models.DateTimeField(_('date joined'), default=timezone.now) + + objects = NelUserManager() + + EMAIL_FIELD = 'email' + USERNAME_FIELD = 'email' + REQUIRED_FIELDS = [] + + class Meta: + verbose_name = _('user') + verbose_name_plural = _('users') + + def __str__(self): + return self.email.strip() + + def clean(self): + super().clean() + self.email = self.__class__.objects.normalize_email(self.email) + + def get_full_name(self): + return self.email.strip() + + def get_short_name(self): + return self.email.strip() + + def email_user(self, subject, message, from_email=None, **kwargs): + send_mail(subject, message, from_email, [self.email], **kwargs) diff --git a/neluser/tests.py b/neluser/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/neluser/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/neluser/views.py b/neluser/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/neluser/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here.