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

Events to Bills: Add -1 Day to Match Query #141

Merged
merged 7 commits into from
Sep 16, 2024
25 changes: 23 additions & 2 deletions openstates/importers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import logging
import typing
from datetime import datetime, timedelta
from django.db.models import Q, Model
from django.db.models.signals import post_save
from .. import settings
Expand Down Expand Up @@ -168,8 +169,12 @@ def resolve_bill(self, bill_id: str, *, date: str) -> typing.Optional[_ID]:
if bill_transform_func:
bill_id = bill_transform_func(bill_id)

# move the start_date up a bit in case the event is on the last day of a session to compare with end_date
date = datetime.fromisoformat(date)
new_date = date + timedelta(days=-1)
NewAgeAirbender marked this conversation as resolved.
Show resolved Hide resolved

objects = Bill.objects.filter(
Q(legislative_session__end_date__gte=date)
Q(legislative_session__end_date__gte=new_date)
| Q(legislative_session__end_date=""),
legislative_session__start_date__lte=date,
legislative_session__jurisdiction_id=self.jurisdiction_id,
Expand All @@ -180,7 +185,23 @@ def resolve_bill(self, bill_id: str, *, date: str) -> typing.Optional[_ID]:
if len(ids) == 1:
return ids.pop()
elif len(ids) == 0:
NewAgeAirbender marked this conversation as resolved.
Show resolved Hide resolved
self.error(f"could not resolve bill id {bill_id} {date}, no matches")
self.warning(f"could not resolve {bill_id} {new_date}, no matches so will retry with a week earlier")
week_date = date + timedelta(days=-7)
NewAgeAirbender marked this conversation as resolved.
Show resolved Hide resolved
objects = Bill.objects.filter(
Q(legislative_session__end_date__gte=week_date)
| Q(legislative_session__end_date=""),
legislative_session__start_date__lte=date,
legislative_session__jurisdiction_id=self.jurisdiction_id,
identifier=bill_id,
)
ids = {each.id for each in objects}
if len(ids) == 1:
self.info(f"resolved bill id {bill_id} with tweaked start_date {week_date}")
return ids.pop()
else:
self.error(
f"could not resolve bill with tweaked date {bill_id} {new_date}, {len(ids)} matches"
)
else:
self.error(
f"could not resolve bill id {bill_id} {date}, {len(ids)} matches"
Expand Down
2 changes: 1 addition & 1 deletion openstates/importers/tests/test_event_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def create_other_jurisdiction():
def ge():
event = ScrapeEvent(
name="America's Birthday",
start_date="2014-07-04T05:00Z",
start_date="2014-07-04T05:00:00+00:00",
location_name="America",
all_day=True,
)
Expand Down
Loading