Skip to content

Commit

Permalink
loans: dumping related circulation policy
Browse files Browse the repository at this point in the history
Creates an API endpoint to allow dumping the circulation
policies informations related to a loan.

Co-Authored-by: Renaud Michotte <[email protected]>
  • Loading branch information
zannkukai committed Dec 9, 2021
1 parent 0404ce0 commit 5ecc121
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
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
31 changes: 31 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,13 @@
"""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.circ_policies.api import CircPolicy
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 +50,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

0 comments on commit 5ecc121

Please sign in to comment.