Skip to content

Commit

Permalink
patron: add blocked and expired filters
Browse files Browse the repository at this point in the history
* Closes #2715.

Co-Authored-by: Bertrand Zuchuat <[email protected]>
  • Loading branch information
Garfield-fr committed Dec 5, 2023
1 parent f0dbf3e commit d732ddb
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
5 changes: 4 additions & 1 deletion rero_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
from .modules.patrons.api import Patron
from .modules.patrons.models import CommunicationChannel
from .modules.patrons.permissions import PatronPermissionPolicy
from .modules.patrons.query import patron_expired
from .modules.selfcheck.permissions import seflcheck_permission_factory
from .modules.stats.api.api import Stat
from .modules.stats.permissions import StatisticsPermissionPolicy
Expand Down Expand Up @@ -2001,7 +2002,9 @@ def _(x):
filters={
_('roles'): and_term_filter('roles'),
_('city'): and_term_filter('facet_city'),
_('patron_type'): and_term_filter('patron.type.pid')
_('patron_type'): and_term_filter('patron.type.pid'),
_('blocked'): and_term_filter('patron.blocked'),
_('expired'): patron_expired()
},
),
acq_accounts=dict(
Expand Down
32 changes: 32 additions & 0 deletions rero_ils/modules/patrons/query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
#
# RERO ILS
# Copyright (C) 2019-2023 RERO
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Patron Query factories for REST API."""

from __future__ import absolute_import, print_function

from datetime import datetime

from elasticsearch_dsl import Q


def patron_expired():
"""Create a filter for the patron account is expired."""
def inner(values):
return Q('range', patron__expiration_date={'lte': datetime.now()}) \
if 'true' == values[0] else Q()
return inner
42 changes: 42 additions & 0 deletions tests/api/patrons/test_patrons_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,3 +569,45 @@ def test_patrons_search(client, librarian_martigny):
res = client.get(list_url)
hits = get_json(res)['hits']
assert hits['total']['value'] == 1


def test_patrons_expired(client, librarian_martigny, patron_martigny):
"""Test patron expired filter."""
login_user_via_session(client, librarian_martigny.user)
list_url = url_for('invenio_records_rest.ptrn_list', simple='1')
res = client.get(list_url)
hits = get_json(res)['hits']
assert hits['total']['value'] == 6

original_expiration_date = patron_martigny['patron']['expiration_date']
patron_martigny['patron']['barcode'] = ['4098124352']

new_expiration_date = datetime.now() - timedelta(days=10)
patron_martigny['patron']['expiration_date'] = new_expiration_date \
.strftime("%Y-%m-%d")
patron_martigny.update(patron_martigny, dbcommit=True, reindex=True)

list_url = url_for(
'invenio_records_rest.ptrn_list', expired='true', simple='1')
res = client.get(list_url)
hits = get_json(res)['hits']
assert hits['total']['value'] == 1

patron_martigny['patron']['expiration_date'] = original_expiration_date
patron_martigny.update(patron_martigny, dbcommit=True, reindex=True)


def test_patrons_blocked(client, librarian_martigny, patron_martigny,
patron3_martigny_blocked):
"""Test patron blocked filter."""
login_user_via_session(client, librarian_martigny.user)
list_url = url_for('invenio_records_rest.ptrn_list', simple='1')
res = client.get(list_url)
hits = get_json(res)['hits']
assert hits['total']['value'] == 6

list_url = url_for(
'invenio_records_rest.ptrn_list', blocked='true', simple='1')
res = client.get(list_url)
hits = get_json(res)['hits']
assert hits['total']['value'] == 1

0 comments on commit d732ddb

Please sign in to comment.