Skip to content

Commit

Permalink
Merge pull request #125 from openstates/rj_updates
Browse files Browse the repository at this point in the history
6.18.3: Add other_names to Match Committees on Event Importer
  • Loading branch information
NewAgeAirbender authored Mar 25, 2024
2 parents a13d798 + 2512a1f commit 902b36f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 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.18.3 - March 25, 2024

* Add other_names to committee matching for Event Imports

## 6.18.2

* Maine (ME): add a tribal representative district to the jurisdiction metadata
Expand Down
57 changes: 57 additions & 0 deletions openstates/importers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,3 +593,60 @@ def resolve_person(

# return the newly-cached object
return self.person_cache[cache_key]

def resolve_committee(
self, json_id: str, allow_no_match: bool = False
) -> typing.Optional[_ID]:
if not json_id:
return None

if json_id.startswith("~"):
# keep caches of all the pseudo-ids to avoid doing 1000s of lookups during import
if json_id not in self.pseudo_id_cache:
spec = get_pseudo_id(json_id)
# need to include other_names in matching
if list(spec.keys()) == ["name"]:
name = spec["name"]
spec = (
Q(name__iexact=name)
| Q(other_names__name__iexact=name)
)
else:
spec = Q(**spec)

# continue with logic from resolve_json_id
if isinstance(spec, Q):
objects = self.model_class.objects.filter(spec)
else:
objects = self.model_class.objects.filter(**spec)
ids = {each.id for each in objects}
if len(ids) == 1:
self.pseudo_id_cache[json_id] = ids.pop()
errmsg = None
elif not ids:
errmsg = "cannot resolve pseudo id to {}: {}".format(
self.model_class.__name__, json_id
)
else:
errmsg = "multiple objects returned for {} pseudo id {}: {}".format(
self.model_class.__name__, json_id, ids
)

# either raise or log error
if errmsg:
if not allow_no_match:
raise UnresolvedIdError(errmsg)
else:
self.error(errmsg)
self.pseudo_id_cache[json_id] = None

# return the cached object
return self.pseudo_id_cache[json_id]

# get the id that the duplicate points to, or use self
json_id = self.duplicates.get(json_id, json_id)

try:
return self.json_to_db_id[json_id]
except KeyError:
raise UnresolvedIdError("cannot resolve id: {}".format(json_id))
2 changes: 1 addition & 1 deletion openstates/importers/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def prepare_for_db(self, data: _JsonDict) -> _JsonDict:
if "person_id" in participant:
participant["person_id"] = self.resolve_person(participant["person_id"])
elif "organization_id" in participant:
participant["organization_id"] = self.org_importer.resolve_json_id(
participant["organization_id"] = self.resolve_committee(
participant["organization_id"], allow_no_match=True
)

Expand Down
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.18.2"
version = "6.18.3"
description = "core infrastructure for the openstates project"
authors = ["James Turk <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 902b36f

Please sign in to comment.