Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to optionally filter organization membership by the division the people represent #43

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion openstates/data/models/people_orgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def __str__(self):


class PersonQuerySet(QuerySet):
def member_of(self, organization_name, current_only=True, post=None):
def member_of(self, organization_name, current_only=True, post=None, division_id=None):
if organization_name.startswith("ocd-organization/"):
org_filter = Q(memberships__organization_id=organization_name)
else:
Expand All @@ -132,6 +132,8 @@ def member_of(self, organization_name, current_only=True, post=None):
) & Q(memberships__end_date="") | Q(memberships__end_date__gte=today)
if post:
org_filter &= Q(memberships__post__label=post)
if division_id:
org_filter &= Q(memberships__post__division_id=division_id)
return qs.filter(org_filter).distinct()

def active(self):
Expand Down
28 changes: 28 additions & 0 deletions openstates/data/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,34 @@ def test_organization_membership():
assert len(Person.objects.member_of(o.id)) == 1


@pytest.mark.django_db
def test_organization_membership_multiple_divisions():
o = Organization.objects.create(name="state")
p1 = Person.objects.create(name="rep1")
p2 = Person.objects.create(name="rep2")
d1 = Division.objects.create(id="ocd-division/country:aa/place:locality1", name="locality1")
d2 = Division.objects.create(id="ocd-division/country:aa/place:locality2", name="locality2")

post1 = Post.objects.create(label="district rep", role="vip", organization=o, division=d1)
post2 = Post.objects.create(label="district rep", role="vip", organization=o, division=d2)

o.memberships.create(person=p1, post=post1)
o.memberships.create(person=p2, post=post2)

people = Person.objects.member_of(o.id, post="district rep").all()
assert len(people) == 2
assert p1 in people
assert p2 in people

people = Person.objects.member_of(o.id, post="district rep", division_id=d1.id).all()
assert len(people) == 1
assert p1 in people

people = Person.objects.member_of(o.id, post="district rep", division_id=d2.id).all()
assert len(people) == 1
assert p2 in people


@pytest.mark.django_db
def test_member_of_with_post():
o = Organization.objects.create(name="The Org")
Expand Down