Skip to content

Commit

Permalink
Merge pull request #160 from openstates/update-committee-names
Browse files Browse the repository at this point in the history
to-database should update names
  • Loading branch information
alexobaseki authored Feb 14, 2025
2 parents 5907f4c + 4a47845 commit e2de709
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 6.20.15 - Feb 14, 2025

* Committee yaml-to-database cli should be able to update committee name

## 6.20.14 - Jan 27, 2025

* Allow duplicate items to be imported in import via new runtime flag --allow_duplicates
Expand Down
25 changes: 18 additions & 7 deletions openstates/cli/committees.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ def _parent_lookup(jurisdiction_id: str, chamber: str, parent: str) -> str:
).id


def committee_to_db(com: Committee) -> tuple[bool, bool]:
def committee_to_db(com: Committee) -> tuple[bool, bool, bool]:
from openstates.data.models import Organization, Membership

updated = False
name_changed = False

db_com, created = Organization.objects.get_or_create(
id=com.id,
Expand All @@ -120,6 +121,11 @@ def committee_to_db(com: Committee) -> tuple[bool, bool]:
defaults=dict(name=com.name, extras=com.extras),
)

if db_com.name != com.name:
updated = True
name_changed = True
db_com.name = com.name

if db_com.extras != com.extras:
updated = True
db_com.extras = com.extras
Expand Down Expand Up @@ -156,7 +162,7 @@ def committee_to_db(com: Committee) -> tuple[bool, bool]:
# don't set updated to true in return if created
if created:
updated = False
return created, updated
return created, updated, name_changed


def merge_lists(orig: list, new: list, key_attr: str) -> list:
Expand Down Expand Up @@ -192,9 +198,9 @@ def __init__(
# allow overriding directory explicitly, useful for testing
self.directory = directory if directory else get_data_path(abbr) / "committees"
# chamber -> name -> Committee
self.coms_by_parent_and_name: defaultdict[
str, dict[str, Committee]
] = defaultdict(dict)
self.coms_by_parent_and_name: defaultdict[str, dict[str, Committee]] = (
defaultdict(dict)
)
self.errors = []
# person matcher will be prepared if/when needed
self.person_matcher: typing.Optional[PersonMatcher] = None
Expand Down Expand Up @@ -406,6 +412,7 @@ def to_database(self, purge: bool) -> None:
ids = set()
created_count = 0
updated_count = 0
name_change_count = 0

jurisdiction_id = lookup(abbr=self.abbr).jurisdiction_id
existing_ids = set(
Expand All @@ -421,7 +428,7 @@ def to_database(self, purge: bool) -> None:
committees.items(), key=lambda c: c[1].parent or ""
):
ids.add(committee.id)
created, updated = committee_to_db(committee)
created, updated, name_changed = committee_to_db(committee)

if created:
click.secho(f"created committee {name}", fg="cyan", bold=True)
Expand All @@ -430,6 +437,9 @@ def to_database(self, purge: bool) -> None:
click.secho(f"updated committee {name}", fg="cyan")
updated_count += 1

if name_changed:
name_change_count += 1

missing_ids = existing_ids - ids

# ids that are missing need to be purged
Expand All @@ -447,7 +457,8 @@ def to_database(self, purge: bool) -> None:

click.secho(
f"processed {len(ids)} committees, {created_count} created, "
f"{updated_count} updated",
f"{updated_count} updated"
f" {name_change_count} name changed",
fg="green",
)

Expand Down
26 changes: 16 additions & 10 deletions openstates/utils/tests/test_to_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,20 @@ def test_committee_to_db_simple():
sources=[{"url": "https://example.com"}],
members=[Membership(name="someone", role="member")],
)
created, updated = committee_to_db(new_com)
assert created and not updated
created, updated, name_changed = committee_to_db(new_com)
assert created and not updated and not name_changed
org = Organization.objects.get(pk=com_id)
assert org.sources == new_com.sources

new_com.links = [{"url": "https://example.com"}]
created, updated = committee_to_db(new_com)
assert updated and not created
created, updated, name_changed = committee_to_db(new_com)
assert updated and not created and not name_changed
org = Organization.objects.get(pk=com_id)
assert org.links == new_com.links

new_com.name = "Education k-12"
created, updated, name_changed = committee_to_db(new_com)
assert updated and not created and name_changed
org = Organization.objects.get(pk=com_id)
assert org.links == new_com.links

Expand All @@ -346,7 +352,7 @@ def test_committee_to_db_memberships():
jurisdiction="ocd-jurisdiction/country:us/state:nc/government",
members=[Membership(name="Steve", role="chair")],
)
created, updated = committee_to_db(new_com)
created, updated, name_changed = committee_to_db(new_com)
org = Organization.objects.get(pk=com_id)
assert org.memberships.count() == 1
s_mem = org.memberships.get()
Expand All @@ -357,8 +363,8 @@ def test_committee_to_db_memberships():
wendy = DjangoPerson.objects.create(id=person_id, name="Wendy")
new_com.add_member("Wendy", role="chair")
new_com.members[-1].person_id = person_id
created, updated = committee_to_db(new_com)
assert updated and not created
created, updated, name_changed = committee_to_db(new_com)
assert updated and not created and not name_changed
org = Organization.objects.get(pk=com_id)
assert org.memberships.count() == 2
w_mem = org.memberships.filter(person_name="Wendy")[0]
Expand All @@ -377,7 +383,7 @@ def test_no_person_updates_with_committee(person):
jurisdiction="ocd-jurisdiction/country:us/state:nc/government",
members=[Membership(name="Steve", role="chair")],
)
created, updated = committee_to_db(parent_com)
created, updated, name_changed = committee_to_db(parent_com)

sub_com = Committee(
id="ocd-organization/00000000-1111-1111-1111-333333333333",
Expand All @@ -388,8 +394,8 @@ def test_no_person_updates_with_committee(person):
jurisdiction="ocd-jurisdiction/country:us/state:nc/government",
members=[Membership(name="Steve", role="chair", person_id=person.id)],
)
created, updated = committee_to_db(sub_com)
assert created and not updated
created, updated, name_changed = committee_to_db(sub_com)
assert created and not updated and not name_changed

created, updated = load_person(person)
assert not created and not updated
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "openstates"
version = "6.20.14"
version = "6.20.15"
description = "core infrastructure for the openstates project"
authors = ["James Turk <dev@jamesturk.net>"]
license = "MIT"
Expand Down

0 comments on commit e2de709

Please sign in to comment.