Skip to content

Commit

Permalink
patron: add circulation statistic API
Browse files Browse the repository at this point in the history
Adds an API to get patron circulation loan statistics. This API return
the number of loans relate to this patron by state.

Closes rero#1278

Co-Authored-by: Renaud Michotte <renaud.michotte@gmail.com>
  • Loading branch information
zannkukai committed Oct 19, 2020
1 parent b4780dc commit d493943
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
17 changes: 17 additions & 0 deletions rero_ils/modules/loans/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from operator import attrgetter

import ciso8601
from elasticsearch_dsl import A
from flask import current_app
from invenio_circulation.errors import MissingRequiredParameterError
from invenio_circulation.pidstore.fetchers import loan_pid_fetcher
Expand Down Expand Up @@ -472,6 +473,22 @@ def get_loans_by_item_pid_by_patron_pid(
return {}


def get_loans_stats_by_patron_pid(patron_pid):
"""Search loans for patron and aggregate result on loan state.
:param patron_pid: The patron pid
:return: a dict with loans state as key, number of loans as value
"""
agg = A('terms', field='state')
search = search_by_patron_item_or_document(patron_pid=patron_pid)
search.aggs.bucket('state', agg)
results = search.execute()
stats = {}
for result in results.aggregations.state.buckets:
stats[result.key] = result.doc_count
return stats


def get_loans_by_patron_pid(patron_pid, filter_states=[]):
"""Search all loans for patron to the given filter_states.
Expand Down
9 changes: 8 additions & 1 deletion rero_ils/modules/patrons/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from ..items.api import Item
from ..items.utils import item_pid_to_object
from ..libraries.api import Library
from ..loans.api import Loan, patron_profile
from ..loans.api import Loan, get_loans_stats_by_patron_pid, patron_profile
from ..locations.api import Location
from ..utils import get_base_url
from ...permissions import login_and_librarian
Expand Down Expand Up @@ -105,6 +105,13 @@ def number_of_patrons():
return jsonify(response)


@api_blueprint.route('/<patron_pid>/circulation_stats', methods=['GET'])
@check_permission
def patron_circulation_stats(patron_pid):
"""Get the circulation statistics about a patron."""
return jsonify(get_loans_stats_by_patron_pid(patron_pid))


blueprint = Blueprint(
'patrons',
__name__,
Expand Down
15 changes: 15 additions & 0 deletions tests/api/patrons/test_patrons_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,18 @@ def test_patrons_count(client, patron_sion_no_email,
res = client.get(url)
assert res.status_code == 200
assert get_json(res) == dict(hits=dict(total=1))


def test_patrons_circulation_stats(client, patron_sion_no_email,
librarian_martigny_no_email):
"""test patron circulation stats."""
url = url_for(
'api_patrons.patron_circulation_stats',
patron_pid=patron_sion_no_email.pid
)
res = client.get(url)
assert res.status_code == 401

login_user_via_session(client, librarian_martigny_no_email.user)
res = client.get(url)
assert res.status_code == 200

0 comments on commit d493943

Please sign in to comment.