Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion accounts/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def save_user(
"""Create the account with its personal organization and admin membership."""
user = super().save_user(request, sociallogin, form)
Membership.objects.create(
org=Organization.objects.create(slug=user.username),
org=Organization.objects.create_personal(user),
user=user,
role=Membership.Role.ADMIN,
)
Expand Down
21 changes: 20 additions & 1 deletion accounts/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import secrets

from django.conf import settings
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.hashers import check_password, make_password
from django.core.validators import RegexValidator
from django.db import models
from django.db import IntegrityError, models, transaction
from django.urls import reverse
from django.utils import timezone
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _
from django_letter.fields import EmailHash
from django_letter.notifications import Notification
Expand All @@ -20,6 +22,21 @@
)


class OrganizationQuerySet(models.QuerySet):
def create_personal(self, user: AbstractBaseUser) -> Organization:
"""Create the personal organization for a user."""
slug = slugify(user.get_username())
counter = 0
while True:
try:
with transaction.atomic():
return self.create(
slug=slug if counter == 0 else f"{slug}-{counter}"
)
except IntegrityError:
counter += 1


class Organization(TimeStamped):
slug = models.SlugField(
_("slug"),
Expand Down Expand Up @@ -55,6 +72,8 @@ class Organization(TimeStamped):
),
)

objects = OrganizationQuerySet.as_manager()

def __str__(self):
return self.slug

Expand Down
4 changes: 2 additions & 2 deletions accounts/tests/test_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ def test_save_user__new_user(self):
request = RequestFactory().post("/social/github/login/callback/")
request.session = SessionStore()
sociallogin = SocialLogin(
user=User(username="newbie", email="n@example.com"),
user=User(username="New-Bie", email="n@example.com"),
account=SocialAccount(provider="github", uid="4242"),
email_addresses=[
EmailAddress(email="n@example.com", verified=True, primary=True)
],
)
RelaySocialAccountAdapter().save_user(request, sociallogin)
org = Organization.objects.get(slug="newbie")
org = Organization.objects.get(slug="new-bie")
assert Membership.objects.filter(
org=org, user=sociallogin.user, role=Membership.Role.ADMIN
).exists()
18 changes: 18 additions & 0 deletions accounts/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ def test_save__enables_billing_on_a_partial_free_organization_save(self):
assert org.billing_is_active is True


@pytest.mark.django_db
class TestOrganizationQuerySet:
def test_create_personal__slugifies_a_github_login(self):
user = User.objects.create_user(username="New-Bie")

org = Organization.objects.create_personal(user)

assert org.slug == "new-bie"

def test_create_personal__suffixes_a_taken_slug(self):
user = User.objects.create_user(username="New-Bie")
Organization.objects.create(slug="new-bie")

org = Organization.objects.create_personal(user)

assert org.slug == "new-bie-1"


@pytest.mark.django_db
class TestMembership:
def test_str__includes_user_org_and_role(self):
Expand Down
Loading