Skip to content

Commit

Permalink
Merge pull request #424 from dimagi/hy/org-memebership-flow-update
Browse files Browse the repository at this point in the history
Update Organization Member Invite Flow to Use Text Field
  • Loading branch information
hemant10yadav authored Nov 20, 2024
2 parents 7b63bc5 + f4a56ca commit 1bd704b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
28 changes: 21 additions & 7 deletions commcare_connect/organization/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from crispy_forms import helper, layout
from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import gettext

from commcare_connect.organization.models import Organization, UserOrganizationMembership
Expand All @@ -25,29 +26,42 @@ def __init__(self, *args, **kwargs):


class MembershipForm(forms.ModelForm):
email = forms.CharField(
max_length=254,
required=True,
label="",
widget=forms.TextInput(attrs={"placeholder": "Enter email address"}),
)

class Meta:
model = UserOrganizationMembership
fields = ("user", "role")
labels = {"user": "", "role": ""}
fields = ("role",)
labels = {"role": ""}

def __init__(self, *args, **kwargs):
self.organization = kwargs.pop("organization")
super().__init__(*args, **kwargs)

self.fields["user"].queryset = User.objects.filter(email__isnull=False).exclude(
memberships__organization=self.organization
)

self.helper = helper.FormHelper(self)
self.helper.layout = layout.Layout(
layout.Row(
layout.HTML("<h4>Add new member</h4>"),
layout.Field("user", wrapper_class="col-md-5"),
layout.Field("email", wrapper_class="col-md-5"),
layout.Field("role", wrapper_class="col-md-5"),
layout.Div(layout.Submit("submit", gettext("Submit")), css_class="col-md-2"),
),
)

def clean_email(self):
email = self.cleaned_data["email"]
user = User.objects.filter(email=email).exclude(memberships__organization=self.organization).first()

if not user:
raise ValidationError("User with this email does not exist or is already a member")

self.instance.user = user
return email


class AddCredentialForm(forms.Form):
credential = forms.CharField(widget=forms.Select)
Expand Down
Empty file.
54 changes: 54 additions & 0 deletions commcare_connect/organization/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import pytest
from django.test import Client
from django.urls import reverse

from commcare_connect.organization.models import Organization
from commcare_connect.users.tests.factories import UserFactory


class TestAddMembersView:
@pytest.fixture(autouse=True)
def setup(self, organization: Organization, client: Client):
self.url = reverse("organization:add_members", kwargs={"org_slug": organization.slug})
self.user = organization.memberships.filter(role="admin").first().user
self.client = client
client.force_login(self.user)

@pytest.mark.django_db
@pytest.mark.parametrize(
"email, role, expected_status_code, create_user, expected_role, should_exist",
[
("[email protected]", "member", 302, True, "member", True),
("[email protected]", "admin", 302, True, "admin", True),
("[email protected]", "member", 302, False, None, False),
("[email protected]", "admin", 302, True, "member", True),
],
)
def test_add_member(
self,
email,
role,
expected_status_code,
create_user,
expected_role,
should_exist,
organization,
):
if create_user:
user = UserFactory(email=email)

if email == "[email protected]":
organization.members.add(user, through_defaults={"role": expected_role})

data = {"email": email, "role": role}
response = self.client.post(self.url, data)

membership_filter = {"user__email": email}

assert response.status_code == expected_status_code
membership_exists = organization.memberships.filter(**membership_filter).exists()
assert membership_exists == should_exist

if should_exist and expected_role:
membership = organization.memberships.get(**membership_filter)
assert membership.role == expected_role

0 comments on commit 1bd704b

Please sign in to comment.