Skip to content

Commit

Permalink
sip2: implement checkin
Browse files Browse the repository at this point in the history
Allows the patron to do a checkin on the selfcheck machine.

* Adds checkin handler.
* Adapts and write some tests.

Co-Authored-by: Lauren-D <[email protected]>
  • Loading branch information
lauren-d committed Dec 3, 2020
1 parent 9645fdd commit 64aedb7
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
1 change: 1 addition & 0 deletions rero_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2368,6 +2368,7 @@ def _(x):
),
circulation_handlers=dict(
checkout='rero_ils.modules.selfcheck.api:selfcheck_checkout',
checkin='rero_ils.modules.selfcheck.api:selfcheck_checkin',
)
)
)
Expand Down
50 changes: 49 additions & 1 deletion rero_ils/modules/selfcheck/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def item_information(patron_barcode, item_pid, **kwargs):
"""Get item information handler.
get item information according 'item_identifier' from selfcheck user.
:param barcode: barcode of the patron.
:param patron_barcode: barcode of the patron.
:param item_pid: item identifier.
:return: The SelfcheckItemInformation object.
"""
Expand Down Expand Up @@ -345,3 +345,51 @@ def selfcheck_checkout(user_pid, institution_id, patron_barcode,
# magnetic_media, desensitize

return checkout


def selfcheck_checkin(user_pid, institution_id, patron_barcode,
item_barcode, **kwargs):
"""SIP2 Handler to perform checkin.
perform checkin action received from the selfcheck.
:param user_pid: identifier of the staff user.
:param institution_id: library pid of the staff user.
:param patron_barcode: barcode of the patron.
:param item_barcode: item identifier.
:return: The SelfcheckCheckin object.
"""
if check_sip2_module():
from invenio_sip2.models import SelfcheckCheckin
library = Library.get_record_by_pid(institution_id)
item = Item.get_item_by_barcode(
barcode=item_barcode,
organisation_pid=library.organisation_pid
)
checkin = SelfcheckCheckin(
permanent_location=library.get('name')
)
if item:
document = Document.get_record_by_pid(item.document_pid)
checkin['title_id'] = title_format_text_head(
document.get('title')
)
staffer = Patron.get_record_by_pid(user_pid)
if staffer.is_librarian:
patron = Patron.get_patron_by_barcode(barcode=patron_barcode)
# do checkin
result, data = item.checkin(
patron_pid=patron.pid,
transaction_user_pid=staffer.pid,
transaction_library_pid=staffer.library_pid,
item_pid=item.pid,
)
if data[LoanAction.CHECKIN]:
language = kwargs.get('language', current_app.config
.get('BABEL_DEFAULT_LANGUAGE'))
with current_app.test_request_context() as ctx:
ctx.babel_locale = language
checkin['checkin'] = True
# TODO: When is possible, try to return fields:
# magnetic_media, resensitize

return checkin
45 changes: 43 additions & 2 deletions tests/api/selfcheck/test_selfcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
from rero_ils.modules.notifications.api import NotificationsSearch, \
number_of_reminders_sent
from rero_ils.modules.selfcheck.api import authorize_patron, enable_patron, \
item_information, patron_information, selfcheck_checkout, \
selfcheck_login, system_status, validate_patron_account
item_information, patron_information, selfcheck_checkin, \
selfcheck_checkout, selfcheck_login, system_status, \
validate_patron_account
from rero_ils.modules.selfcheck.utils import check_sip2_module

# skip tests if invenio-sip2 module is not installed
Expand Down Expand Up @@ -281,3 +282,43 @@ def test_selfcheck_checkout(client, sip2_librarian_martigny_no_email,
)
)
assert res.status_code == 200


def test_selfcheck_checkin(client, sip2_librarian_martigny_no_email,
librarian2_martigny_no_email, loc_public_martigny,
sip2_patron_martigny_no_email, item_lib_martigny,
document, circulation_policies):
"""Test selfcheck checkin."""
patron_barcode = sip2_patron_martigny_no_email \
.get('patron', {}).get('barcode')
item_barcode = item_lib_martigny.get('barcode')

# librarian checkout
login_user_via_session(client, librarian2_martigny_no_email.user)
res, data = postdata(client, 'api_item.checkout', dict(
item_pid=item_lib_martigny.pid,
patron_pid=sip2_patron_martigny_no_email.pid,
transaction_location_pid=loc_public_martigny.pid,
transaction_user_pid=librarian2_martigny_no_email.pid,
))
assert res.status_code == 200

# test selfcheck checkin with invalid item barcode
checkin = selfcheck_checkin(
user_pid=sip2_librarian_martigny_no_email.pid,
institution_id=sip2_librarian_martigny_no_email.library_pid,
patron_barcode=patron_barcode,
item_barcode='wrong_item_barcode',
)
assert checkin
assert not checkin.is_success

# selfcheck checkin
checkin = selfcheck_checkin(
user_pid=sip2_librarian_martigny_no_email.pid,
institution_id=sip2_librarian_martigny_no_email.library_pid,
patron_barcode=patron_barcode,
item_barcode=item_barcode,
)
assert checkin
assert checkin.is_success

0 comments on commit 64aedb7

Please sign in to comment.