-
Notifications
You must be signed in to change notification settings - Fork 7
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
Refactor LoanInfo and HoldInfo to be dataclasses (PP-1728) #2113
Open
jonathangreen
wants to merge
3
commits into
main
Choose a base branch
from
chore/refactor-loan-hold-info
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
33 changes: 33 additions & 0 deletions
33
alembic/versions/20241015_1938277e993f_remove_hold_external_identifier.py
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"""Remove Hold.external_identifier | ||
Revision ID: 1938277e993f | ||
Revises: 87901a6323d6 | ||
Create Date: 2024-10-15 19:47:55.697280+00:00 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "1938277e993f" | ||
down_revision = "87901a6323d6" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.drop_constraint("holds_external_identifier_key", "holds", type_="unique") | ||
op.drop_column("holds", "external_identifier") | ||
|
||
|
||
def downgrade() -> None: | ||
op.add_column( | ||
"holds", | ||
sa.Column( | ||
"external_identifier", sa.VARCHAR(), autoincrement=False, nullable=True | ||
), | ||
) | ||
op.create_unique_constraint( | ||
"holds_external_identifier_key", "holds", ["external_identifier"] | ||
) |
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 |
---|---|---|
|
@@ -45,7 +45,6 @@ | |
AlreadyCheckedOut, | ||
AlreadyOnHold, | ||
CannotFulfill, | ||
CannotHold, | ||
CannotLoan, | ||
CurrentlyAvailable, | ||
InvalidInputException, | ||
|
@@ -415,9 +414,7 @@ | |
patron_id = patron.authorization_identifier | ||
response = self._checkin(title_id, patron_id) | ||
try: | ||
CheckinResponseParser(licensepool.collection).process_first( | ||
response.content | ||
) | ||
CheckinResponseParser().process_first(response.content) | ||
except etree.XMLSyntaxError: | ||
raise RemoteInitiatedServerError(response.text, self.label()) | ||
|
||
|
@@ -454,12 +451,8 @@ | |
title_id, patron_id, self.internal_format(delivery_mechanism) | ||
) | ||
try: | ||
loan_info = CheckoutResponseParser(licensepool.collection).process_first( | ||
response.content | ||
) | ||
if loan_info is None: | ||
raise CannotLoan() | ||
return loan_info | ||
expiration_date = CheckoutResponseParser().process_first(response.content) | ||
return LoanInfo.from_license_pool(licensepool, end_date=expiration_date) | ||
except etree.XMLSyntaxError: | ||
raise RemoteInitiatedServerError(response.text, self.label()) | ||
|
||
|
@@ -525,17 +518,13 @@ | |
titleId=title_id, patronId=patron_id, email=hold_notification_email | ||
) | ||
response = self.request(url, params=params) | ||
hold_info = HoldResponseParser(licensepool.collection).process_first( | ||
response.content | ||
hold_position = HoldResponseParser().process_first(response.content) | ||
hold_info = HoldInfo.from_license_pool( | ||
licensepool, | ||
start_date=utc_now(), | ||
end_date=None, | ||
hold_position=hold_position, | ||
) | ||
if not hold_info: | ||
raise CannotHold() | ||
if not hold_info.identifier: | ||
# The Axis 360 API doesn't return the identifier of the | ||
# item that was placed on hold, so we have to fill it in | ||
# based on our own knowledge. | ||
hold_info.identifier_type = identifier.type | ||
hold_info.identifier = identifier.identifier | ||
return hold_info | ||
|
||
def release_hold(self, patron: Patron, pin: str, licensepool: LicensePool) -> None: | ||
|
@@ -546,9 +535,7 @@ | |
params = dict(titleId=title_id, patronId=patron_id) | ||
response = self.request(url, params=params) | ||
try: | ||
HoldReleaseResponseParser(licensepool.collection).process_first( | ||
response.content | ||
) | ||
HoldReleaseResponseParser().process_first(response.content) | ||
except NotOnHold: | ||
# Fine, it wasn't on hold and now it's still not on hold. | ||
pass | ||
|
@@ -1423,14 +1410,6 @@ | |
|
||
|
||
class XMLResponseParser(ResponseParser, Axis360Parser[T], ABC): | ||
def __init__(self, collection: Collection): | ||
"""Constructor. | ||
:param collection: A Collection, in case parsing this document | ||
results in the creation of LoanInfo or HoldInfo objects. | ||
""" | ||
self.collection = collection | ||
|
||
def raise_exception_on_error( | ||
self, | ||
e: _Element, | ||
|
@@ -1477,47 +1456,37 @@ | |
return True | ||
|
||
|
||
class CheckoutResponseParser(XMLResponseParser[LoanInfo]): | ||
class CheckoutResponseParser(XMLResponseParser[datetime.datetime | None]): | ||
@property | ||
def xpath_expression(self) -> str: | ||
return "//axis:checkoutResult" | ||
|
||
def process_one(self, e: _Element, namespaces: dict[str, str] | None) -> LoanInfo: | ||
def process_one( | ||
self, e: _Element, namespaces: dict[str, str] | None | ||
) -> datetime.datetime | None: | ||
"""Either turn the given document into a LoanInfo | ||
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. It looks like the doc needs to be updated |
||
object, or raise an appropriate exception. | ||
""" | ||
self.raise_exception_on_error(e, namespaces) | ||
|
||
# If we get to this point it's because the checkout succeeded. | ||
expiration_date = self._xpath1(e, "//axis:expirationDate", namespaces) | ||
fulfillment_url = self._xpath1(e, "//axis:url", namespaces) | ||
if fulfillment_url is not None: | ||
fulfillment_url = fulfillment_url.text | ||
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. This was parsed, but never used. |
||
|
||
if expiration_date is not None: | ||
expiration_date = expiration_date.text | ||
expiration_date = self._pd(expiration_date) | ||
|
||
loan_start = utc_now() | ||
loan = LoanInfo( | ||
collection=self.collection, | ||
data_source_name=DataSource.AXIS_360, | ||
identifier_type=self.id_type, | ||
identifier=None, | ||
start_date=loan_start, | ||
end_date=expiration_date, | ||
) | ||
return loan | ||
return expiration_date # type: ignore[no-any-return] | ||
|
||
|
||
class HoldResponseParser(XMLResponseParser[HoldInfo]): | ||
class HoldResponseParser(XMLResponseParser[int | None], LoggerMixin): | ||
@property | ||
def xpath_expression(self) -> str: | ||
return "//axis:addtoholdResult" | ||
|
||
def process_one(self, e: _Element, namespaces: dict[str, str] | None) -> HoldInfo: | ||
"""Either turn the given document into a HoldInfo | ||
object, or raise an appropriate exception. | ||
def process_one(self, e: _Element, namespaces: dict[str, str] | None) -> int | None: | ||
"""Either turn the given document into an int representing the hold position, | ||
or raise an appropriate exception. | ||
""" | ||
self.raise_exception_on_error(e, namespaces, {3109: AlreadyOnHold}) | ||
|
||
|
@@ -1529,22 +1498,10 @@ | |
try: | ||
queue_position = int(queue_position.text) | ||
except ValueError: | ||
print("Invalid queue position: %s" % queue_position) | ||
self.log.warning("Invalid queue position: %s" % queue_position) | ||
queue_position = None | ||
|
||
hold_start = utc_now() | ||
# NOTE: The caller needs to fill in Collection -- we have no idea | ||
# what collection this is. | ||
hold = HoldInfo( | ||
collection=self.collection, | ||
data_source_name=DataSource.AXIS_360, | ||
identifier_type=self.id_type, | ||
identifier=None, | ||
start_date=hold_start, | ||
end_date=None, | ||
hold_position=queue_position, | ||
) | ||
return hold | ||
return queue_position | ||
|
||
|
||
class HoldReleaseResponseParser(XMLResponseParser[Literal[True]]): | ||
|
@@ -1575,11 +1532,12 @@ | |
""" | ||
self.api = api | ||
self.internal_format = internal_format | ||
if api.collection is None: | ||
if api.collection_id is None: | ||
raise ValueError( | ||
"Cannot use an Axis360AvailabilityResponseParser without a Collection." | ||
"Cannot use an Axis360AvailabilityResponseParser without a collection_id." | ||
) | ||
super().__init__(api.collection) | ||
self.collection_id = api.collection_id | ||
super().__init__() | ||
|
||
@property | ||
def xpath_expression(self) -> str: | ||
|
@@ -1643,8 +1601,7 @@ | |
# We're out of luck -- we can't fulfill this loan. | ||
fulfillment = None | ||
info = LoanInfo( | ||
collection=self.collection, | ||
data_source_name=DataSource.AXIS_360, | ||
collection_id=self.collection_id, | ||
identifier_type=self.id_type, | ||
identifier=axis_identifier, | ||
start_date=start_date, | ||
|
@@ -1655,8 +1612,7 @@ | |
elif reserved: | ||
end_date = self._xpath1_date(availability, "axis:reservedEndDate", ns) | ||
info = HoldInfo( | ||
collection=self.collection, | ||
data_source_name=DataSource.AXIS_360, | ||
collection_id=self.collection_id, | ||
identifier_type=self.id_type, | ||
identifier=axis_identifier, | ||
start_date=None, | ||
|
@@ -1668,8 +1624,7 @@ | |
availability, "axis:holdsQueuePosition", ns | ||
) | ||
info = HoldInfo( | ||
collection=self.collection, | ||
data_source_name=DataSource.AXIS_360, | ||
collection_id=self.collection_id, | ||
identifier_type=self.id_type, | ||
identifier=axis_identifier, | ||
start_date=None, | ||
|
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.
The changes here make the XMLResponseParser subclasses return the parsed data directly, rather then a LoanInfo or HoldInfo object. Previously they returned a partial object, and the caller had to fill in the missing details, now the caller creates the whole LoanInfo or HoldInfo object.