Skip to content

Commit

Permalink
Add parameter to stop refresh of loans feed. (#1899)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen authored Jun 12, 2024
1 parent 3b27a67 commit f34a428
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/palace/manager/api/controller/loan.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from flask import Response, redirect
from flask_babel import lazy_gettext as _
from lxml import etree
from pydantic import parse_obj_as
from werkzeug import Response as wkResponse

from palace.manager.api.circulation_exceptions import (
Expand Down Expand Up @@ -49,6 +50,14 @@ def sync(self) -> Response:
"""
patron: Patron = flask.request.patron # type: ignore[attr-defined]

try:
# Parse the refresh query parameter as a boolean.
refresh = parse_obj_as(bool, flask.request.args.get("refresh", "true"))
except ValueError:
# If we can't parse the refresh query parameter, default to True.
self.log.exception(f"Could not parse refresh query parameter.")
refresh = True

# Save some time if we don't believe the patron's loans or holds have
# changed since the last time the client requested this feed.
response = self.handle_conditional_request(patron.last_loan_activity_sync)
Expand All @@ -64,7 +73,7 @@ def sync(self) -> Response:

# First synchronize our local list of loans and holds with all
# third-party loan providers.
if patron.authorization_identifier:
if patron.authorization_identifier and refresh:
header = self.authorization_header()
credential = self.manager.auth.get_credential_from_header(header)
try:
Expand Down
30 changes: 30 additions & 0 deletions tests/manager/api/controller/test_loan.py
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,36 @@ def handle_conditional_request(last_modified=None):
# patron.last_loan_activity_sync was updated.
assert patron.last_loan_activity_sync > new_sync_time

@pytest.mark.parametrize(
"refresh,expected_sync_call_count",
[
["true", 1],
["abc", 1],
["t", 1],
["1", 1],
[None, 1],
["false", 0],
["f", 0],
["0", 0],
],
)
def test_loans_refresh(
self,
loan_fixture: LoanFixture,
refresh: str | None,
expected_sync_call_count: int,
):
url = f"loans/?refresh={refresh}" if refresh is not None else "/"
with (
loan_fixture.request_context_with_library(
url, headers=dict(Authorization=loan_fixture.valid_auth)
),
patch.object(loan_fixture.manager.d_circulation, "sync_bookshelf") as sync,
):
loan_fixture.manager.loans.authenticated_patron_from_request()
loan_fixture.manager.loans.sync()
assert sync.call_count == expected_sync_call_count

@pytest.mark.parametrize(
"target_loan_duration, "
"db_loan_duration, "
Expand Down

0 comments on commit f34a428

Please sign in to comment.