diff --git a/commcare_connect/users/managers.py b/commcare_connect/users/managers.py index 017ab14e..e41e621f 100644 --- a/commcare_connect/users/managers.py +++ b/commcare_connect/users/managers.py @@ -5,24 +5,29 @@ class UserManager(DjangoUserManager): """Custom manager for the User model.""" - def _create_user(self, email: str, password: str | None, **extra_fields): + def _create_user(self, username: str | None, email: str | None, password: str | None, **extra_fields): """ Create and save a user with the given email and password. """ - if not email: - raise ValueError("The given email must be set") - email = self.normalize_email(email) - user = self.model(email=email, **extra_fields) + if not username and not email: + raise ValueError("Username or email must be set") + if email: + email = self.normalize_email(email) + user = self.model(username=username, email=email, **extra_fields) user.password = make_password(password) user.save(using=self._db) return user - def create_user(self, email: str, password: str | None = None, **extra_fields): + def create_user( + self, username: str | None = None, email: str | None = None, password: str | None = None, **extra_fields + ): extra_fields.setdefault("is_staff", False) extra_fields.setdefault("is_superuser", False) - return self._create_user(email, password, **extra_fields) + return self._create_user(username, email, password, **extra_fields) - def create_superuser(self, email: str, password: str | None = None, **extra_fields): + def create_superuser( + self, username: str | None = None, email: str | None = None, password: str | None = None, **extra_fields + ): extra_fields.setdefault("is_staff", True) extra_fields.setdefault("is_superuser", True) @@ -31,4 +36,4 @@ def create_superuser(self, email: str, password: str | None = None, **extra_fiel 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) + return self._create_user(username, email, password, **extra_fields) diff --git a/commcare_connect/users/models.py b/commcare_connect/users/models.py index a15656a7..32fd1ea0 100644 --- a/commcare_connect/users/models.py +++ b/commcare_connect/users/models.py @@ -35,7 +35,6 @@ class User(AbstractUser): ) phone_number = models.CharField(max_length=15, null=True, blank=True) - USERNAME_FIELD = "email" REQUIRED_FIELDS = [] objects = UserManager() diff --git a/commcare_connect/users/tests/test_admin.py b/commcare_connect/users/tests/test_admin.py index 11f4e9ea..244781f2 100644 --- a/commcare_connect/users/tests/test_admin.py +++ b/commcare_connect/users/tests/test_admin.py @@ -31,7 +31,7 @@ def test_add(self, admin_client): assert User.objects.filter(email="new-admin@example.com").exists() def test_view_user(self, admin_client): - user = User.objects.get(email="admin@example.com") + user = User.objects.get(username="admin") url = reverse("admin:users_user_change", kwargs={"object_id": user.pk}) response = admin_client.get(url) assert response.status_code == 200 diff --git a/commcare_connect/users/tests/test_managers.py b/commcare_connect/users/tests/test_managers.py index f0d7ea22..b2b88e72 100644 --- a/commcare_connect/users/tests/test_managers.py +++ b/commcare_connect/users/tests/test_managers.py @@ -43,7 +43,7 @@ def test_createsuperuser_command(): out = StringIO() command_result = call_command( "createsuperuser", - "--email", + "--username", "henry@example.com", interactive=False, stdout=out, @@ -51,5 +51,5 @@ def test_createsuperuser_command(): assert command_result is None assert out.getvalue() == "Superuser created successfully.\n" - user = User.objects.get(email="henry@example.com") + user = User.objects.get(username="henry@example.com") assert not user.has_usable_password()