Skip to content

Revise bill relations filter #112

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

Merged
merged 2 commits into from
Jan 12, 2021
Merged
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
34 changes: 28 additions & 6 deletions legistar/bills.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,17 +414,39 @@ def _version_rank(self, version):

def relations(self, matter_id):
relations = self.endpoint('/matters/{0}/relations', matter_id)
if relations:
highest_flag = max(int(relation['MatterRelationFlag'])
for relation in relations)

relations = [relation for relation in relations
if relation['MatterRelationFlag'] == highest_flag]
if relations:
return self._filter_relations(relations)

return relations
else:
return []

def _filter_relations(self, relations):
'''
Sometimes, many versions of a bill are related. This method returns the
most recent version of each relation. Override this method to apply a
different filter or return the full array of relations.
'''
# Sort relations such that the latest version of each matter
# ID is returned first.
sorted_relations = sorted(
relations,
key=lambda x: (
x['MatterRelationMatterId'],
x['MatterRelationFlag']
),
reverse=True
)

seen_relations = set()

for relation in sorted_relations:
relation_id = relation['MatterRelationMatterId']

if relation_id not in seen_relations:
yield relation
seen_relations.add(relation_id)

def text(self, matter_id, latest_version_value=None):
'''Historically, we have determined the latest version of a bill
by finding the version with the highest value (either numerical or alphabetical).
Expand Down