Skip to content

Commit

Permalink
item: filter item search by barcode by organisation
Browse files Browse the repository at this point in the history
As item from multiple organisations can shared the same barcode, when a
search on barcode is done, we need to filter it by the current logged
user organisation pid.

Closes #1085

Co-Authored-by: Renaud Michotte <renaud.michotte@gmail.com>
  • Loading branch information
zannkukai committed Aug 14, 2020
1 parent bce5bf6 commit 5afe44a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
8 changes: 5 additions & 3 deletions rero_ils/modules/items/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from ...documents.api import Document, DocumentsSearch
from ...fetchers import id_fetcher
from ...minters import id_minter
from ...organisations.api import current_organisation
from ...providers import Provider

# provider
Expand Down Expand Up @@ -166,13 +167,14 @@ def get_item_record_for_ui(cls, **kwargs):
"""
from ...loans.api import Loan
item = None
item_pid = kwargs.get('item_pid', None)
item_pid = kwargs.get('item_pid')
item_barcode = kwargs.pop('item_barcode', None)
loan_pid = kwargs.get('pid', None)
loan_pid = kwargs.get('pid')
if item_pid:
item = Item.get_record_by_pid(item_pid)
elif item_barcode:
item = Item.get_item_by_barcode(item_barcode)
org_pid = kwargs.get('organisation_pid', current_organisation.pid)
item = Item.get_item_by_barcode(item_barcode, org_pid)
elif loan_pid:
item_pid = Loan.get_record_by_pid(loan_pid).item_pid
item = Item.get_record_by_pid(item_pid)
Expand Down
4 changes: 2 additions & 2 deletions rero_ils/modules/items/api/circulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,8 +1033,8 @@ def can_extend(cls, item, **kwargs):
item.item_type_pid
)
extension_count = loan.get('extension_count', 0)
if not (cipo.get('number_renewals') > 0 and
extension_count < cipo.get('number_renewals') and
if not (cipo.get('number_renewals', 0) > 0 and
extension_count < cipo.get('number_renewals', 0) and
extend_loan_data_is_valid(
loan.get('end_date'),
cipo.get('renewal_duration'),
Expand Down
19 changes: 15 additions & 4 deletions rero_ils/modules/items/api/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,22 @@ def get_items_pid_by_document_pid(cls, document_pid):
yield item_pid_to_object(item.pid)

@classmethod
def get_item_by_barcode(cls, barcode=None):
"""Get item by barcode."""
def get_item_by_barcode(cls, barcode, organisation_pid):
"""Get item by barcode.
:param barcode: the item barcode.
:param organisation_pid: the organisation pid. As barcode could be
shared between items from multiple
organisations we need to filter result by
organisation.pid
:return The item corresponding to the barcode if exists or None.
"""
from . import ItemsSearch
results = ItemsSearch().filter(
'term', barcode=barcode).source(includes='pid').scan()
results = ItemsSearch()\
.filter('term', barcode=barcode)\
.filter('term', organisation__pid=organisation_pid)\
.source(includes='pid')\
.scan()
try:
return cls.get_record_by_pid(next(results).pid)
except StopIteration:
Expand Down
5 changes: 3 additions & 2 deletions rero_ils/modules/items/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from ..errors import NoCirculationActionIsPermitted
from ..libraries.api import Library
from ..loans.api import Loan
from ..organisations.api import current_organisation
from ..patrons.api import Patron
from ...permissions import librarian_permission

Expand Down Expand Up @@ -142,7 +143,7 @@ def decorated_view(*args, **kwargs):
abort(403, "BLOCKED USER")
abort(403, str(error))
except NotFound as error:
raise(error)
raise error
except exceptions.RequestError as error:
# missing required parameters
return jsonify({'status': 'error: {error}'.format(
Expand Down Expand Up @@ -325,7 +326,7 @@ def loans(patron_pid):
@jsonify_error
def item(item_barcode):
"""HTTP GET request for requested loans for a library item and patron."""
item = Item.get_item_by_barcode(item_barcode)
item = Item.get_item_by_barcode(item_barcode, current_organisation.pid)
if not item:
abort(404)
loan = get_loan_for_item(item_pid_to_object(item.pid))
Expand Down

0 comments on commit 5afe44a

Please sign in to comment.