Skip to content

Commit

Permalink
Merge pull request #114 from dimagi/ce/username
Browse files Browse the repository at this point in the history
remove username field
  • Loading branch information
calellowitz authored Sep 26, 2023
2 parents d092413 + 56dbac6 commit 8016185
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
23 changes: 14 additions & 9 deletions commcare_connect/users/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
1 change: 0 additions & 1 deletion commcare_connect/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion commcare_connect/users/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_add(self, admin_client):
assert User.objects.filter(email="[email protected]").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
4 changes: 2 additions & 2 deletions commcare_connect/users/tests/test_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ def test_createsuperuser_command():
out = StringIO()
command_result = call_command(
"createsuperuser",
"--email",
"--username",
"[email protected]",
interactive=False,
stdout=out,
)

assert command_result is None
assert out.getvalue() == "Superuser created successfully.\n"
user = User.objects.get(email="[email protected]")
user = User.objects.get(username="[email protected]")
assert not user.has_usable_password()

0 comments on commit 8016185

Please sign in to comment.