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

loans: dumping related circulation policy #2589

Merged
merged 1 commit into from
Dec 20, 2021
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
18 changes: 16 additions & 2 deletions rero_ils/modules/loans/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
from flask import Blueprint, abort, jsonify
from flask_login import login_required

from rero_ils.modules.decorators import check_logged_as_librarian
from rero_ils.modules.items.api import Item
from rero_ils.modules.items.models import ItemCirculationAction
from rero_ils.modules.items.views.api_views import \
check_logged_user_authentication, jsonify_error
from rero_ils.modules.loans.api import Loan
from rero_ils.modules.loans.utils import sum_for_fees
from rero_ils.modules.loans.utils import get_circ_policy, sum_for_fees

api_blueprint = Blueprint(
'api_loan',
Expand All @@ -34,13 +35,26 @@
)


@api_blueprint.route('/<loan_pid>/circulation_policy', methods=['GET'])
@check_logged_as_librarian
def dump_loan_current_circulation_policy(loan_pid):
"""Search and dump the current circulation policy related to a loan."""
# NOTE : It's possible than the returned CIPO wasn't the same CIPO used
# for a circulation operation on this loan because data could
# changed (Patron.patron_type, Item.item_type, ...)
loan = Loan.get_record_by_pid(loan_pid)
if not loan:
abort(404, 'Loan not found')
return jsonify(get_circ_policy(loan))


@api_blueprint.route('/<loan_pid>/overdue/preview', methods=['GET'])
@login_required
def preview_loan_overdue(loan_pid):
"""HTTP GET request for overdue preview about a loan."""
loan = Loan.get_record_by_pid(loan_pid)
if not loan:
abort(404)
abort(404, 'Loan not found')
fees = loan.get_overdue_fees
fees = [(fee[0], fee[1].isoformat()) for fee in fees] # format date
return jsonify({
Expand Down
30 changes: 30 additions & 0 deletions tests/api/loans/test_loans_rest_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
"""Tests REST API loans."""

from flask import url_for
from invenio_accounts.testutils import login_user_via_session
from invenio_jsonschemas import current_jsonschemas
from utils import get_json, item_record_to_a_specific_loan_state, login_user

from rero_ils.modules.loans.models import LoanState
from rero_ils.modules.utils import get_schema_for_resource


def test_loan_can_extend(client, patron_martigny, item_lib_martigny,
Expand All @@ -46,3 +49,30 @@ def test_loan_can_extend(client, patron_martigny, item_lib_martigny,
'can': False,
'reasons': ['Circulation policies disallows the operation.']
}


def test_loan_circulation_policy(
client, patron_martigny, librarian_martigny,
item_on_loan_martigny_patron_and_loan_on_loan
):
"""Test dumping of circulation policy related to a loan."""
_, _, loan = item_on_loan_martigny_patron_and_loan_on_loan
base_url_for = 'api_loan.dump_loan_current_circulation_policy'
api_url = url_for(base_url_for, loan_pid=loan.pid)
dummy_url = url_for(base_url_for, loan_pid='dummy_pid')

# Patron user cannot access to this API
login_user_via_session(client, patron_martigny.user)
response = client.get(api_url)
assert response.status_code == 403

# Librarian user can access to this API
login_user_via_session(client, librarian_martigny.user)
response = client.get(api_url)
assert response.status_code == 200
data = get_json(response)
cipo_schema = get_schema_for_resource('cipo')
data['$schema'] = current_jsonschemas.path_to_url(cipo_schema)

response = client.get(dummy_url)
assert response.status_code == 404