Skip to content

Commit

Permalink
feat: allow filtering of members list by email (#26450)
Browse files Browse the repository at this point in the history
  • Loading branch information
raquelmsmith authored Nov 27, 2024
1 parent 4ed8279 commit 63a2418
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
15 changes: 9 additions & 6 deletions posthog/api/organization_member.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import cast

from django.db.models import Model, Prefetch, QuerySet, F
from django.db.models import F, Model, Prefetch, QuerySet
from django.shortcuts import get_object_or_404
from django.views import View
from django_otp.plugins.otp_totp.models import TOTPDevice
Expand Down Expand Up @@ -124,14 +124,17 @@ def safely_get_queryset(self, queryset) -> QuerySet:
if self.action == "list":
params = self.request.GET.dict()

if "email" in params:
queryset = queryset.filter(user__email=params["email"])

if "updated_after" in params:
queryset = queryset.filter(updated_at__gt=params["updated_after"])

order = self.request.GET.get("order", None)
if order:
queryset = queryset.order_by(order)
else:
queryset = queryset.order_by("-joined_at")
order = self.request.GET.get("order", None)
if order:
queryset = queryset.order_by(order)
else:
queryset = queryset.order_by("-joined_at")

return queryset

Expand Down
14 changes: 14 additions & 0 deletions posthog/api/test/test_organization_members.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,17 @@ def test_add_owner_only_if_owner(self):
self.assertEqual(response.status_code, 403)
self.assertEqual(self.organization_membership.level, OrganizationMembership.Level.ADMIN)
self.assertEqual(membership.level, OrganizationMembership.Level.MEMBER)

def test_list_organization_members_filter_by_email(self):
# Create additional users
user1 = User.objects.create_and_join(self.organization, "[email protected]", None)
User.objects.create_and_join(self.organization, "[email protected]", None)

# Test filtering by email
response = self.client.get("/api/organizations/@current/members/[email protected]")
response_data = response.json()["results"]

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response_data), 1)
self.assertEqual(response_data[0]["user"]["email"], "[email protected]")
self.assertEqual(response_data[0]["user"]["uuid"], str(user1.uuid))

0 comments on commit 63a2418

Please sign in to comment.