diff --git a/wagtail/admin/tests/test_password_reset.py b/wagtail/admin/tests/test_password_reset.py index 7dc752f5f3..e66602457a 100644 --- a/wagtail/admin/tests/test_password_reset.py +++ b/wagtail/admin/tests/test_password_reset.py @@ -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)..." diff --git a/wagtail/tests/customuser/migrations/0003_delete_emailuser.py b/wagtail/tests/customuser/migrations/0003_delete_emailuser.py new file mode 100644 index 0000000000..2a18a8cea5 --- /dev/null +++ b/wagtail/tests/customuser/migrations/0003_delete_emailuser.py @@ -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', + ), + ] diff --git a/wagtail/tests/customuser/models.py b/wagtail/tests/customuser/models.py index b5236e12fa..78869d30ac 100644 --- a/wagtail/tests/customuser/models.py +++ b/wagtail/tests/customuser/models.py @@ -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) diff --git a/wagtail/tests/emailuser/__init__.py b/wagtail/tests/emailuser/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/wagtail/tests/emailuser/migrations/0001_initial.py b/wagtail/tests/emailuser/migrations/0001_initial.py new file mode 100644 index 0000000000..8d8117d781 --- /dev/null +++ b/wagtail/tests/emailuser/migrations/0001_initial.py @@ -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, + }, + ), + ] diff --git a/wagtail/tests/emailuser/migrations/__init__.py b/wagtail/tests/emailuser/migrations/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/wagtail/tests/emailuser/models.py b/wagtail/tests/emailuser/models.py new file mode 100644 index 0000000000..9d2c2b7bc8 --- /dev/null +++ b/wagtail/tests/emailuser/models.py @@ -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) diff --git a/wagtail/tests/settings.py b/wagtail/tests/settings.py index 536acc87da..7ee330cb05 100644 --- a/wagtail/tests/settings.py +++ b/wagtail/tests/settings.py @@ -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', diff --git a/wagtail/tests/testapp/fixtures/test.json b/wagtail/tests/testapp/fixtures/test.json index f20d5e6f34..b3cdfb2e9f 100644 --- a/wagtail/tests/testapp/fixtures/test.json +++ b/wagtail/tests/testapp/fixtures/test.json @@ -898,7 +898,7 @@ }, { "pk": 5, - "model": "customuser.emailuser", + "model": "emailuser.emailuser", "fields": { "first_name": "", "last_name": "",