From b2fad16a720216b9bcdc5577e735be4d52627fe6 Mon Sep 17 00:00:00 2001 From: Aly Ibrahim Date: Fri, 12 Nov 2021 17:26:55 -0500 Subject: [PATCH 1/2] add the ability to filter oganization membership by the division the people represent --- openstates/data/models/people_orgs.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/openstates/data/models/people_orgs.py b/openstates/data/models/people_orgs.py index b5825d51f..e8bfcc811 100644 --- a/openstates/data/models/people_orgs.py +++ b/openstates/data/models/people_orgs.py @@ -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: @@ -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): From e5c6563e551eaf7c6b3bfeaf6c2bae3d10462635 Mon Sep 17 00:00:00 2001 From: Aly Ibrahim Date: Tue, 7 Dec 2021 06:53:01 -0500 Subject: [PATCH 2/2] add test --- openstates/data/tests/test_models.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/openstates/data/tests/test_models.py b/openstates/data/tests/test_models.py index 77d79d114..28381b672 100644 --- a/openstates/data/tests/test_models.py +++ b/openstates/data/tests/test_models.py @@ -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")