Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
9 changes: 8 additions & 1 deletion rero_ils/modules/stats/api/pricing.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(self, to_date=None):
fmt="YYYY-MM-DDT00:00:00"
)
_to = to_date.format(fmt="YYYY-MM-DDT23:59:59")
self.to_date = to_date
self.date_range = {"gte": _from, "lte": _to}

@classmethod
Expand Down Expand Up @@ -171,11 +172,17 @@ def number_of_active_patrons(self, library_pid):
:return: the number of matched active patrons
:rtype: integer
"""
# this should always count patrons that were active in the past year
_from = (self.to_date - relativedelta(months=12)).format(
fmt="YYYY-MM-DDT00:00:00"
)
_to = self.date_range.get("lte")
activity_period = {"gte": _from, "lte": _to}
op_logs_query = (
LoanOperationLogsSearch()
.get_logs_by_trigger(
triggers=[ItemCirculationAction.CHECKOUT, ItemCirculationAction.EXTEND],
date_range=self.date_range,
date_range=activity_period,
)
.filter("term", loan__item__library_pid=library_pid)
)
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/stats/test_stats_pricing.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"""Stats Pricing tests."""

import mock
from arrow import utcnow
from dateutil.relativedelta import relativedelta
from invenio_db import db

from rero_ils.modules.ill_requests.models import ILLRequestStatus
Expand Down Expand Up @@ -81,6 +83,19 @@ def test_stats_pricing_number_of_active_patrons(
assert stat_for_pricing.number_of_active_patrons("foo") == 0
assert stat_for_pricing.number_of_active_patrons(lib_martigny.pid) == 1

# make sure that the class's date_range has no influence on this indicator
default_date_range = stat_for_pricing.date_range
_to = utcnow() + relativedelta(months=3)
_from = utcnow() + relativedelta(days=1)
stat_for_pricing.date_range = {
"gte": _from.format(fmt="YYYY-MM-DDT00:00:00"),
"lte": _to.format(fmt="YYYY-MM-DDT00:00:00"),
}
assert stat_for_pricing.number_of_active_patrons(lib_martigny.pid) == 1

# revert to default
stat_for_pricing.date_range = default_date_range


def test_stats_pricing_number_of_order_lines(
stat_for_pricing, acq_order_line_fiction_martigny
Expand Down