0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Move emailuser model into a separate wagtail.tests.emailuser app

This commit is contained in:
Matt Westcott 2020-08-13 22:32:55 +01:00 committed by Matt Westcott
parent 30c1a4132c
commit 00bcf5474d
9 changed files with 110 additions and 57 deletions

View File

@ -64,7 +64,7 @@ class TestUserPasswordReset(TestCase, WagtailTestUtils):
self.assertEqual(len(mail.outbox), 1)
self.assertIn("Your username (in case you've forgotten): siteeditor", mail.outbox[0].body)
@override_settings(AUTH_USER_MODEL='customuser.EmailUser')
@override_settings(AUTH_USER_MODEL='emailuser.EmailUser')
def test_password_reset_no_username_when_email_is_username(self):
# When the user model is using email as the username, the password reset email
# should not contain "Your username (in case you've forgotten)..."

View File

@ -0,0 +1,16 @@
# Generated by Django 3.1 on 2020-08-13 21:28
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('customuser', '0002_added_file_field'),
]
operations = [
migrations.DeleteModel(
name='EmailUser',
),
]

View File

@ -1,5 +1,4 @@
from django.contrib.auth.models import (
AbstractBaseUser, BaseUserManager, Group, Permission, PermissionsMixin)
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.db import models
from wagtail.admin.edit_handlers import FieldPanel
@ -58,56 +57,3 @@ class CustomUser(AbstractBaseUser, PermissionsMixin):
FieldPanel('first_name'),
FieldPanel('last_name'),
]
class EmailUserManager(BaseUserManager):
def _create_user(self, email, password,
is_staff, is_superuser, **extra_fields):
"""
Creates and saves a User with the given email and password.
"""
email = self.normalize_email(email)
user = self.model(email=email, is_staff=is_staff, is_active=True,
is_superuser=is_superuser, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
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)
class EmailUser(AbstractBaseUser):
# Cant inherit from PermissionsMixin because of clashes with
# groups/user_permissions related_names.
email = models.EmailField(max_length=255, unique=True)
is_staff = models.BooleanField(default=True)
is_active = models.BooleanField(default=True)
first_name = models.CharField(max_length=50, blank=True)
last_name = models.CharField(max_length=50, blank=True)
is_superuser = models.BooleanField(default=False)
groups = models.ManyToManyField(Group, related_name='+', blank=True)
user_permissions = models.ManyToManyField(Permission, related_name='+', blank=True)
USERNAME_FIELD = 'email'
objects = EmailUserManager()
def get_full_name(self):
return self.first_name + ' ' + self.last_name
def get_short_name(self):
return self.first_name
methods = ['get_group_permissions', 'get_all_permissions', 'has_perm',
'has_perms', 'has_module_perms']
for method in methods:
func = getattr(PermissionsMixin, method)
setattr(EmailUser, method, func)

View File

View File

@ -0,0 +1,34 @@
# Generated by Django 3.1 on 2020-08-13 21:28
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
]
operations = [
migrations.CreateModel(
name='EmailUser',
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')),
('email', models.EmailField(max_length=255, unique=True)),
('is_staff', models.BooleanField(default=True)),
('is_active', models.BooleanField(default=True)),
('first_name', models.CharField(blank=True, max_length=50)),
('last_name', models.CharField(blank=True, max_length=50)),
('is_superuser', models.BooleanField(default=False)),
('groups', models.ManyToManyField(blank=True, related_name='_emailuser_groups_+', to='auth.Group')),
('user_permissions', models.ManyToManyField(blank=True, related_name='_emailuser_user_permissions_+', to='auth.Permission')),
],
options={
'abstract': False,
},
),
]

View File

@ -0,0 +1,56 @@
from django.contrib.auth.models import (
AbstractBaseUser, BaseUserManager, Group, Permission, PermissionsMixin)
from django.db import models
class EmailUserManager(BaseUserManager):
def _create_user(self, email, password,
is_staff, is_superuser, **extra_fields):
"""
Creates and saves a User with the given email and password.
"""
email = self.normalize_email(email)
user = self.model(email=email, is_staff=is_staff, is_active=True,
is_superuser=is_superuser, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
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)
class EmailUser(AbstractBaseUser):
# Cant inherit from PermissionsMixin because of clashes with
# groups/user_permissions related_names.
email = models.EmailField(max_length=255, unique=True)
is_staff = models.BooleanField(default=True)
is_active = models.BooleanField(default=True)
first_name = models.CharField(max_length=50, blank=True)
last_name = models.CharField(max_length=50, blank=True)
is_superuser = models.BooleanField(default=False)
groups = models.ManyToManyField(Group, related_name='+', blank=True)
user_permissions = models.ManyToManyField(Permission, related_name='+', blank=True)
USERNAME_FIELD = 'email'
objects = EmailUserManager()
def get_full_name(self):
return self.first_name + ' ' + self.last_name
def get_short_name(self):
return self.first_name
methods = ['get_group_permissions', 'get_all_permissions', 'has_perm',
'has_perms', 'has_module_perms']
for method in methods:
func = getattr(PermissionsMixin, method)
setattr(EmailUser, method, func)

View File

@ -106,6 +106,7 @@ INSTALLED_APPS = (
'wagtail.tests.testapp',
'wagtail.tests.demosite',
'wagtail.tests.customuser',
'wagtail.tests.emailuser',
'wagtail.tests.snippets',
'wagtail.tests.routablepage',
'wagtail.tests.search',

View File

@ -898,7 +898,7 @@
},
{
"pk": 5,
"model": "customuser.emailuser",
"model": "emailuser.emailuser",
"fields": {
"first_name": "",
"last_name": "",