-
Notifications
You must be signed in to change notification settings - Fork 27
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
Update resolve_bill method #149
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
12f30a2
Update resolve_bill method
alexobaseki 62c99b1
Updates
alexobaseki 914c556
Add all_session_cache for event<>bill matching
alexobaseki cd8c5ca
Move function around
alexobaseki fa3452e
Bump version to 6.20.8
alexobaseki d9de13b
Return value for get_all_session()
alexobaseki ee73cf2
Add some comment
alexobaseki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
import json | ||
import logging | ||
import typing | ||
from datetime import datetime, timedelta | ||
from datetime import datetime | ||
from django.db.models import Q, Model | ||
from django.db.models.signals import post_save | ||
from .. import settings | ||
|
@@ -128,6 +128,7 @@ def __init__(self, jurisdiction_id: str, do_postimport=True) -> None: | |
self.pseudo_id_cache: typing.Dict[str, typing.Optional[_ID]] = {} | ||
self.person_cache: typing.Dict[_PersonCacheKey, typing.Optional[str]] = {} | ||
self.session_cache: typing.Dict[str, LegislativeSession] = {} | ||
self.all_sessions_cache: typing.List[LegislativeSession] = [] | ||
self.logger = logging.getLogger("openstates") | ||
self.info = self.logger.info | ||
self.debug = self.logger.debug | ||
|
@@ -139,6 +140,12 @@ def __init__(self, jurisdiction_id: str, do_postimport=True) -> None: | |
if settings.IMPORT_TRANSFORMERS.get(self._type): | ||
self.cached_transformers = settings.IMPORT_TRANSFORMERS[self._type] | ||
|
||
def get_all_sessions(self) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this method should return |
||
if not self.all_sessions_cache: | ||
self.all_sessions_cache = LegislativeSession.objects.filter( | ||
jurisdiction_id=self.jurisdiction_id | ||
).order_by("-start_date") | ||
|
||
def get_session(self, identifier: str) -> LegislativeSession: | ||
if identifier not in self.session_cache: | ||
self.session_cache[identifier] = LegislativeSession.objects.get( | ||
|
@@ -166,18 +173,27 @@ def resolve_bill(self, bill_id: str, *, date: str) -> typing.Optional[_ID]: | |
bill_transform_func = settings.IMPORT_TRANSFORMERS.get("bill", {}).get( | ||
"identifier", None | ||
) | ||
self.get_all_sessions() | ||
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) | ||
# Some steps here to first find the session that matches the incoming entity using the entity date | ||
# If a unique session is not found, then use the session with the latest "start_date" | ||
date = datetime.fromisoformat(date).strftime("%Y-%m-%d") | ||
session_ids = [ | ||
session.id | ||
for session in self.all_sessions_cache | ||
if session.start_date <= date | ||
and (session.end_date >= date or not session.end_date) | ||
] | ||
|
||
if len(session_ids) == 1: | ||
session_id = session_ids.pop() | ||
else: | ||
session_id = self.all_sessions_cache[0].id if self.all_sessions_cache else None | ||
|
||
objects = Bill.objects.filter( | ||
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, | ||
legislative_session__id=session_id, | ||
identifier=bill_id, | ||
) | ||
ids = {each.id for each in objects} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth a comment here explaining the difference between these two