Skip to content

Commit

Permalink
Merge pull request #33 from dimagi/sk/create-org
Browse files Browse the repository at this point in the history
create organization for user on signup
  • Loading branch information
snopoke committed Jul 26, 2023
2 parents 3acd29a + 4258d6c commit 215713b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions commcare_connect/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@
<a class="nav-link {% active_link "about" %}" href="{% url 'about' %}">About</a>
</li>
{% if request.user.is_authenticated %}
{% if request.org %}
<li class="nav-item">
<a class="nav-link {% active_link "list || create || edit" namespace='opportunity' %}" href="{% url 'opportunity:list' request.org.slug %}">{% translate "Opportunities" %}</a>
</li>
{% endif %}
<li class="nav-item">
<a class="nav-link {% active_link "users:detail" %}" href="{% url 'users:detail' request.user.pk %}">{% translate "My Profile" %}</a>
</li>
Expand Down
4 changes: 4 additions & 0 deletions commcare_connect/users/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.urls import reverse
Expand Down Expand Up @@ -39,6 +40,9 @@ def get_absolute_url(self) -> str:
class Organization(BaseModel):
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique=True)
members = models.ManyToManyField(
settings.AUTH_USER_MODEL, related_name="organizations", through="UserOrganizationMembership"
)

def save(self, *args, **kwargs):
if not self.id:
Expand Down
17 changes: 17 additions & 0 deletions commcare_connect/users/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from allauth.account.signals import user_signed_up
from django.dispatch import receiver

from commcare_connect.users.models import Organization, User, UserOrganizationMembership


@receiver(user_signed_up)
def create_org_for_user(request, user, **kwargs):
if not user.members.exists():
_create_default_org_for_user(user)


def _create_default_org_for_user(user: User):
organization = Organization.objects.create(name=user.email.split("@")[0])
organization.members.add(user, through_defaults={"role": UserOrganizationMembership.Role.ADMIN})
organization.save()
return organization

0 comments on commit 215713b

Please sign in to comment.