From 1d26622dc0a0427c4881ec376404cf377b807f9e Mon Sep 17 00:00:00 2001 From: Cal Ellowitz Date: Sat, 16 Sep 2023 12:01:33 -0400 Subject: [PATCH] update manager and tests --- commcare_connect/users/managers.py | 19 ++++++++++--------- commcare_connect/users/tests/test_admin.py | 2 +- commcare_connect/users/tests/test_managers.py | 4 ++-- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/commcare_connect/users/managers.py b/commcare_connect/users/managers.py index 017ab14e..bfbffd29 100644 --- a/commcare_connect/users/managers.py +++ b/commcare_connect/users/managers.py @@ -5,24 +5,25 @@ 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 +32,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/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()