From 455551195bc5273f25fcc2034eb6784202f7d923 Mon Sep 17 00:00:00 2001 From: Bertrand Zuchuat Date: Fri, 7 Feb 2020 06:33:57 +0100 Subject: [PATCH] items: can request api for an item * Removes duplicate fixtures function. * Adds sort on pickup_name location Co-Authored-by: Bertrand Zuchuat Co-Authored-by: Aly Badr --- rero_ils/config.py | 4 ++ rero_ils/modules/items/api_views.py | 46 ++++++++++++++++++- rero_ils/modules/items/utils.py | 71 ----------------------------- 3 files changed, 48 insertions(+), 73 deletions(-) delete mode 100644 rero_ils/modules/items/utils.py diff --git a/rero_ils/config.py b/rero_ils/config.py index dbb4ef8fc6..bef6749d23 100644 --- a/rero_ils/config.py +++ b/rero_ils/config.py @@ -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') diff --git a/rero_ils/modules/items/api_views.py b/rero_ils/modules/items/api_views.py index aab1badd14..89441108a5 100644 --- a/rero_ils/modules/items/api_views.py +++ b/rero_ils/modules/items/api_views.py @@ -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 @@ -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.') diff --git a/rero_ils/modules/items/utils.py b/rero_ils/modules/items/utils.py deleted file mode 100644 index 59cb27a2d0..0000000000 --- a/rero_ils/modules/items/utils.py +++ /dev/null @@ -1,71 +0,0 @@ -# -*- coding: utf-8 -*- -# -# RERO ILS -# Copyright (C) 2019 RERO -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, version 3 of the License. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . - -"""Item utils.""" - -from flask import jsonify -from flask_babelex import gettext as _ - -from .api import Item, ItemStatus -from ..libraries.api import Library -from ..loans.api import Loan -from ..loans.utils import can_be_requested -from ..patrons.api import Patron - - -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.'))