Skip to content

Commit

Permalink
circulation: allow checkout from never open library
Browse files Browse the repository at this point in the history
- Closes rero#2419.

Co-Authored-by: Pascal Repond <pascal.repond@rero.ch>
  • Loading branch information
PascalRepond committed Jun 15, 2023
1 parent eeca0e2 commit da2fca3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
14 changes: 10 additions & 4 deletions rero_ils/modules/items/api/circulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from ...errors import NoCirculationAction
from ...item_types.api import ItemType
from ...libraries.api import Library
from ...libraries.exceptions import LibraryNeverOpen
from ...loans.api import Loan, get_last_transaction_loc_for_item, \
get_request_by_item_pid_by_patron_pid
from ...loans.models import LoanAction, LoanState
Expand Down Expand Up @@ -420,10 +421,15 @@ def checkout(self, current_loan, **kwargs):
transaction_library_pid = self.library_pid
library = Library.get_record_by_pid(transaction_library_pid)
if not library.is_open(action_params['end_date'], True):
new_end_date = library.next_open(action_params['end_date'])
new_end_date = new_end_date.astimezone()\
.replace(microsecond=0).isoformat()
action_params['end_date'] = new_end_date
try:
new_end_date = library.next_open(action_params['end_date'])
new_end_date = new_end_date.astimezone()\
.replace(microsecond=0).isoformat()
action_params['end_date'] = new_end_date
except LibraryNeverOpen:
# If library has no open dates, keep the default due date
# to avoid circulation errors
pass
# Call invenio_circulation for 'checkout' trigger
loan = current_circulation.circulation.trigger(
current_loan,
Expand Down
2 changes: 1 addition & 1 deletion rero_ils/modules/libraries/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def is_open(self, date=None, day_only=False):
# is into this periods (depending of `day_only` method argument).
day_name = date.strftime("%A").lower()
regular_rule = [
rule for rule in self['opening_hours']
rule for rule in self.get('opening_hours', [])
if rule['day'] == day_name
]
if regular_rule:
Expand Down
34 changes: 34 additions & 0 deletions tests/api/circulation/test_actions_views_checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import ciso8601
from invenio_accounts.testutils import login_user_via_session
from invenio_db import db
from utils import postdata

from rero_ils.modules.items.models import ItemStatus
Expand Down Expand Up @@ -155,3 +156,36 @@ def test_checkout(
transaction_end_date = ciso8601.parse_datetime(transaction_end_date)
next_open_date = lib_martigny.next_open(next_saturday)
assert next_open_date.date() == transaction_end_date.date()

# Test checkout from library with no opening hours
res, _ = postdata(
client,
'api_item.checkin',
dict(
item_pid=item.pid,
transaction_library_pid=lib_martigny.pid,
transaction_user_pid=librarian_martigny.pid
)
)
assert res.status_code == 200
assert item.status == ItemStatus.ON_SHELF

del lib_martigny['opening_hours']
lib_martigny.update(lib_martigny, commit=True)

params = dict(
item_pid=item.pid,
patron_pid=patron_martigny.pid,
transaction_user_pid=librarian_martigny.pid,
transaction_location_pid=loc_public_martigny.pid
)

# test is done WITHOUT loan PID
res, _ = postdata(
client,
'api_item.checkout',
params
)

assert res.status_code == 200
db.session.rollback()

0 comments on commit da2fca3

Please sign in to comment.