From e51f49b5a4b59a25b9a31eb9219a87286f161ee6 Mon Sep 17 00:00:00 2001 From: Penny Date: Sat, 9 Nov 2024 09:31:02 +0900 Subject: [PATCH] add superuser support --- .../migrations/0002_auto_20241108_1325.py | 23 +++++++++++++++++++ app/core/models.py | 10 +++++++- app/core/tests/test_models.py | 9 ++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 app/core/migrations/0002_auto_20241108_1325.py diff --git a/app/core/migrations/0002_auto_20241108_1325.py b/app/core/migrations/0002_auto_20241108_1325.py new file mode 100644 index 0000000..859a02f --- /dev/null +++ b/app/core/migrations/0002_auto_20241108_1325.py @@ -0,0 +1,23 @@ +# Generated by Django 3.2.25 on 2024-11-08 13:25 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='user', + name='is_staff', + field=models.BooleanField(default=False), + ), + migrations.AlterField( + model_name='user', + name='is_active', + field=models.BooleanField(default=True), + ), + ] diff --git a/app/core/models.py b/app/core/models.py index 8d56a93..a4eed56 100644 --- a/app/core/models.py +++ b/app/core/models.py @@ -21,13 +21,21 @@ def create_user(self, email, password=None, **extra_fields): return user + def create_superuser(self, email, password): + user = self.create_user(email, password) + user.is_staff = True + user.is_superuser = True + user.save(using=self._db) + + return user + class User(AbstractBaseUser, PermissionsMixin): """User in the system.""" email = models.EmailField(max_length=255, unique=True) name = models.CharField(max_length=255) is_active = models.BooleanField(default=True) - is_active = models.BooleanField(default=False) + is_staff = models.BooleanField(default=False) objects = UserManager() diff --git a/app/core/tests/test_models.py b/app/core/tests/test_models.py index 75d6ad1..1d6c2cd 100644 --- a/app/core/tests/test_models.py +++ b/app/core/tests/test_models.py @@ -29,3 +29,12 @@ def test_new_user_email_normalized(self): def test_new_user_without_email_raises_error(self): with self.assertRaises(ValueError): get_user_model().objects.create_user('', 'sample123') + + def test_create_superuser(self): + user = get_user_model().objects.create_superuser( + 'test@example.com', + 'test123' + ) + + self.assertTrue(user.is_superuser) + self.assertTrue(user.is_staff)