Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

documents: implement record availability #489

Merged
merged 1 commit into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions rero_ils/modules/documents/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ class Document(IlsRecord):
fetcher = document_id_fetcher
provider = DocumentProvider

@property
def available(self):
"""Get availability for document."""
from ..holdings.api import Holding
for holding_pid in Holding.get_holdings_pid_by_document_pid(self.pid):
holding = Holding.get_record_by_pid(holding_pid)
if holding.available:
return True
return False

@property
def harvested(self):
"""Is this record harvested from an external service."""
Expand Down
11 changes: 11 additions & 0 deletions rero_ils/modules/documents/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ def decorated_view(*args, **kwargs):
return decorated_view


@api_blueprint.route('/availabilty/<document_pid>', methods=['GET'])
def document_availability(document_pid):
"""HTTP GET request for document availability."""
document = Document.get_record_by_pid(document_pid)
if not document:
abort(404)
return jsonify({
'availability': document.available
})


@api_blueprint.route('/cover/<isbn>')
def cover(isbn):
"""Documenet cover service."""
Expand Down
5 changes: 1 addition & 4 deletions rero_ils/modules/holdings/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@
from invenio_search import current_search
from invenio_search.api import RecordsSearch

from rero_ils.modules.circ_policies.models import CircPolicyIdentifier

from .models import HoldingIdentifier
from ..api import IlsRecord, IlsRecordError, IlsRecordIndexer
from ..documents.api import Document, DocumentsSearch
from ..api import IlsRecord, IlsRecordIndexer
from ..fetchers import id_fetcher
from ..items.api import Item, ItemsSearch
from ..locations.api import Location
Expand Down
88 changes: 80 additions & 8 deletions tests/api/test_availability.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
from rero_ils.modules.loans.api import LoanAction


def test_item_holding_availability(
client, holding_lib_martigny, item_lib_martigny, item2_lib_martigny,
def test_item_holding_document_availability(
client, document,
holding_lib_martigny,
item_lib_martigny, item2_lib_martigny,
librarian_martigny_no_email, librarian_saxon_no_email,
patron_martigny_no_email, loc_public_saxon, circulation_policies):
"""Test item availability."""

patron_martigny_no_email, patron2_martigny_no_email,
loc_public_saxon, circulation_policies):
"""Test item, holding and document availability."""
assert item_availablity_status(
client, item_lib_martigny.pid, librarian_martigny_no_email.user)
assert item_lib_martigny.available
Expand All @@ -42,6 +44,9 @@ def test_item_holding_availability(
assert holding_availablity_status(
client, holding_lib_martigny.pid, librarian_martigny_no_email.user)
assert holding_lib_martigny.get_holding_loan_conditions() == 'standard'
assert document.available
assert document_availablity_status(
client, document.pid, librarian_martigny_no_email.user)

# login as patron
with mock.patch(
Expand Down Expand Up @@ -79,7 +84,9 @@ def test_item_holding_availability(
assert holding_availablity_status(
client, holding_lib_martigny.pid, librarian_martigny_no_email.user)
assert holding_lib_martigny.get_holding_loan_conditions() == 'standard'

assert document.available
assert document_availablity_status(
client, document.pid, librarian_martigny_no_email.user)
# validate request
res = client.post(
url_for('api_item.validate_request'),
Expand All @@ -103,7 +110,9 @@ def test_item_holding_availability(
assert holding_availablity_status(
client, holding_lib_martigny.pid, librarian_martigny_no_email.user)
assert holding_lib_martigny.get_holding_loan_conditions() == 'standard'

assert document.available
assert document_availablity_status(
client, document.pid, librarian_martigny_no_email.user)
login_user_via_session(client, librarian_saxon_no_email.user)
# receive
res = client.post(
Expand All @@ -128,7 +137,9 @@ def test_item_holding_availability(
assert holding_availablity_status(
client, holding_lib_martigny.pid, librarian_saxon_no_email.user)
assert holding_lib_martigny.get_holding_loan_conditions() == 'standard'

assert document.available
assert document_availablity_status(
client, document.pid, librarian_martigny_no_email.user)
# checkout
res = client.post(
url_for('api_item.checkout'),
Expand All @@ -151,6 +162,9 @@ def test_item_holding_availability(
assert holding_availablity_status(
client, holding_lib_martigny.pid, librarian_saxon_no_email.user)
assert holding_lib_martigny.get_holding_loan_conditions() == 'standard'
assert document.available
assert document_availablity_status(
client, document.pid, librarian_martigny_no_email.user)

class current_i18n:
class locale:
Expand All @@ -162,6 +176,50 @@ class locale:
end_date = item.get_item_end_date()
assert not_available_reasons(item) == 'due until ' + end_date

"""
request second item with another patron and test document and holding
availability
"""

# login as patron
with mock.patch(
'rero_ils.modules.patrons.api.current_patron',
patron_martigny_no_email
):
login_user_via_session(client, patron2_martigny_no_email.user)
assert holding_lib_martigny.get_holding_loan_conditions() \
== 'short 15 days'
# request second item
login_user_via_session(client, librarian_martigny_no_email.user)

res = client.post(
url_for('api_item.librarian_request'),
data=json.dumps(
dict(
item_pid=item2_lib_martigny.pid,
pickup_location_pid=loc_public_saxon.pid,
patron_pid=patron2_martigny_no_email.pid
)
),
content_type='application/json',
)
assert res.status_code == 200
data = get_json(res)
actions = data.get('action_applied')
loan_pid = actions[LoanAction.REQUEST].get('pid')
assert not item2_lib_martigny.available
assert not item_availablity_status(
client, item2_lib_martigny.pid, librarian_martigny_no_email.user)
assert not_available_reasons(item2_lib_martigny) == '(1 requests)'
holding = Holding.get_record_by_pid(holding_lib_martigny.pid)
assert not holding.available
assert not holding_availablity_status(
client, holding_lib_martigny.pid, librarian_martigny_no_email.user)
assert holding_lib_martigny.get_holding_loan_conditions() == 'standard'
assert not document.available
assert not document_availablity_status(
client, document.pid, librarian_martigny_no_email.user)


def item_availablity_status(client, pid, user):
"""Returns item availability."""
Expand Down Expand Up @@ -189,3 +247,17 @@ def holding_availablity_status(client, pid, user):
assert res.status_code == 200
data = get_json(res)
return data.get('availability')


def document_availablity_status(client, pid, user):
"""Returns document availability."""
login_user_via_session(client, user)
res = client.get(
url_for(
'api_documents.document_availability',
document_pid=pid,
)
)
assert res.status_code == 200
data = get_json(res)
return data.get('availability')