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

circulation: allow overriding exception #1494

Merged
merged 1 commit into from
Nov 26, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"type": "selectWithSort",
"options": [
{
"label": "Monochrome",
"label": "rdacc:1002",
"value": "rdacc:1002"
},
{
"label": "Polychrome",
"label": "rdacc:1003",
"value": "rdacc:1003"
}
],
Expand Down
2 changes: 2 additions & 0 deletions rero_ils/modules/items/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ def checkout(item, data):
transaction_location_pid,
transaction_user_pid
"""
data['override_blocking'] = flask_request.args.get(
'override_blocking', False)
return item.checkout(**data)


Expand Down
22 changes: 15 additions & 7 deletions rero_ils/modules/items/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ def wrapper(item, *args, **kwargs):
def check_operation_allowed(action):
"""Check if a specific action is allowed on an item.
Check if an 'override_blocking' parameter is present. If this param is
present and set to True (or corresponding True values [1, 'true', ...])
then no CIRCULATION_ACTIONS_VALIDATION should be tested.
Remove this parameter from the data arguments.
Check the CIRCULATION_ACTIONS_VALIDATION configuration file and execute
function corresponding to the action specified. All function are execute
until one return False (action denied) or all actions are successful.
Expand All @@ -80,13 +85,16 @@ def check_operation_allowed(action):
def inner_function(func):
@wraps(func)
def decorated_view(*args, **kwargs):
actions = current_app.config.get(
'CIRCULATION_ACTIONS_VALIDATION', {})
for func_name in actions.get(action, []):
func_callback = obj_or_import_string(func_name)
can, reasons = func_callback(args[0], **kwargs)
if not can:
raise CirculationException(description=reasons[0])
override_blocking = kwargs.pop('override_blocking', False)
override_blocking = bool(override_blocking)
if not override_blocking:
actions = current_app.config.get(
'CIRCULATION_ACTIONS_VALIDATION', {})
for func_name in actions.get(action, []):
func_callback = obj_or_import_string(func_name)
can, reasons = func_callback(args[0], **kwargs)
if not can:
raise CirculationException(description=reasons[0])
return func(*args, **kwargs)
return decorated_view
return inner_function
19 changes: 18 additions & 1 deletion tests/api/circulation/test_borrow_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,27 @@ def test_checkout_library_limit(
assert 'error' == data['messages'][0]['type']
assert 'Checkout denied' in data['messages'][0]['content']

# try a checkout with 'override_blocking' parameter.
# --> the restriction is no longer checked, the checkout will be success.
res, data = postdata(client, 'api_item.checkout', dict(
item_pid=item3.pid,
patron_pid=patron.pid,
transaction_location_pid=loc_public_martigny.pid,
transaction_user_pid=librarian_martigny_no_email.pid,
), url_data={'override_blocking': 'true'})
assert res.status_code == 200
loan3_pid = data.get('action_applied')[LoanAction.CHECKOUT].get('pid')

# reset fixtures
# --> checkin both loaned item
# --> checkin three loaned item
# --> reset patron_type to original value
# --> reset items to original values
res, data = postdata(client, 'api_item.checkin', dict(
item_pid=item3.pid,
pid=loan3_pid,
transaction_location_pid=loc_public_martigny.pid,
transaction_user_pid=librarian_martigny_no_email.pid,
))
res, data = postdata(client, 'api_item.checkin', dict(
item_pid=item2.pid,
pid=loan2_pid,
Expand Down