Skip to content

Commit

Permalink
items: can request api for an item
Browse files Browse the repository at this point in the history
* Removes duplicate fixtures function.
* Adds sort on pickup_name location

Co-Authored-by: Bertrand Zuchuat <[email protected]>
Co-Authored-by: Aly Badr <[email protected]>
  • Loading branch information
Garfield-fr and Aly Badr committed Mar 5, 2020
1 parent db1e4cb commit 4555511
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 73 deletions.
4 changes: 4 additions & 0 deletions rero_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,10 @@ def _(x):
fields=['name'], title='Location name',
default_order='asc'
)
RECORDS_REST_SORT_OPTIONS['locations']['pickup_name'] = dict(
fields=['pickup_name'], title='Pickup Location name',
default_order='asc'
)
RECORDS_REST_DEFAULT_SORT['locations'] = dict(
query='bestmatch', noquery='name')

Expand Down
46 changes: 44 additions & 2 deletions rero_ils/modules/items/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
from invenio_circulation.errors import CirculationException
from werkzeug.exceptions import NotFound

from .api import Item
from .utils import is_librarian_can_request_item_for_patron
from .api import Item, ItemStatus
from ..circ_policies.api import CircPolicy
from ..libraries.api import Library
from ..loans.api import Loan
from ..loans.utils import can_be_requested
from ..patrons.api import Patron
from ...permissions import librarian_permission

Expand Down Expand Up @@ -337,3 +337,45 @@ def can_request(item_pid, library_pid, patron_barcode):
"""
return is_librarian_can_request_item_for_patron(
item_pid, library_pid, patron_barcode)


def jsonify_response(response=False, reason=None):
"""Jsonify api response."""
return jsonify({
'can_request': response,
'reason': reason
})


def is_librarian_can_request_item_for_patron(
item_pid, library_pid, patron_barcode):
"""Check if a librarian can request an item for a patron.
required_parameters: item_pid, library_pid, patron_barcode
"""
item = Item.get_record_by_pid(item_pid)
if not item:
return jsonify_response(reason='Item not found.')
patron = Patron.get_patron_by_barcode(patron_barcode)
if not patron:
return jsonify_response(reason='Patron not found.')
library = Library.get_record_by_pid(library_pid)
if not library:
return jsonify_response(reason='Library not found.')
# Create a loan
loan = Loan({
'patron_pid': patron.pid, 'item_pid': item.pid,
'library_pid': library_pid})
if not can_be_requested(loan):
return jsonify_response(
reason='Circulation policies do not allow request on this item.')
if item.status != ItemStatus.MISSING:
loaned_to_patron = item.is_loaned_to_patron(patron_barcode)
if loaned_to_patron:
return jsonify_response(
reason='Item is already checked-out or requested by patron.')
return jsonify_response(
response=True, reason='Request is possible.')
else:
return jsonify_response(
reason='Item status does not allow requests.')
71 changes: 0 additions & 71 deletions rero_ils/modules/items/utils.py

This file was deleted.

0 comments on commit 4555511

Please sign in to comment.