diff --git a/rero_ils/query.py b/rero_ils/query.py index 007a192210..e1ebf577ed 100644 --- a/rero_ils/query.py +++ b/rero_ils/query.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # RERO ILS -# Copyright (C) 2019-2022 RERO +# Copyright (C) 2019-2024 RERO # Copyright (C) 2020 UCLOUVAIN # # This program is free software: you can redistribute it and/or modify @@ -30,6 +30,8 @@ from invenio_records_rest.errors import InvalidQueryRESTError from werkzeug.datastructures import ImmutableMultiDict, MultiDict +from rero_ils.modules.ill_requests.models import ILLRequestStatus + from .facets import default_facets_factory from .modules.items.models import TypeOfItem from .modules.organisations.api import Organisation @@ -283,16 +285,23 @@ def ill_request_search_factory(self, search, query_parser=None): 'terms', patron__pid=[ptrn.pid for ptrn in current_patrons]) + search = search.exclude(Q('term', to_anonymize=True)) + + if not request.args.get('remove_archived'): + return search, urlkwargs + months = current_app.config.get('RERO_ILS_ILL_HIDE_MONTHS', 6) date_delta = datetime.now(timezone.utc) - relativedelta(months=months) filters = Q( 'range', _created={'lte': 'now', 'gte': date_delta} ) - filters |= Q('term', status='pending') + filters |= Q('terms', status=[ + ILLRequestStatus.PENDING, + ILLRequestStatus.VALIDATED + ]) - return search.filter( - filters).exclude(Q('term', to_anonymize=True)), urlkwargs + return search.filter(filters), urlkwargs def circulation_search_factory(self, search, query_parser=None): diff --git a/tests/api/ill_requests/test_ill_requests_rest.py b/tests/api/ill_requests/test_ill_requests_rest.py index 95ce0dd04f..f3fb4aa880 100644 --- a/tests/api/ill_requests/test_ill_requests_rest.py +++ b/tests/api/ill_requests/test_ill_requests_rest.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # RERO ILS -# Copyright (C) 2019-2023 RERO +# Copyright (C) 2019-2024 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 @@ -262,7 +262,33 @@ def db_commit_reindex(record): # Initial status is pending list_url = url_for( 'invenio_records_rest.illr_list', - q='pid:'+ill_request_martigny['pid'] + q=f'pid:{ill_request_martigny["pid"]}' + ) + res = client.get(list_url) + result = res.json + assert result['hits']['total']['value'] == 1 + + # Closed record + ill_request_martigny = ILLRequest\ + .get_record_by_pid(ill_request_martigny.pid) + ill_request_martigny['status'] = ILLRequestStatus.CLOSED + ill_request_martigny.update( + ill_request_martigny, dbcommit=True, reindex=True) + + # Without filter (show record) + list_url = url_for( + 'invenio_records_rest.illr_list', + q=f'pid:{ill_request_martigny["pid"]}' + ) + res = client.get(list_url) + result = res.json + assert result['hits']['total']['value'] == 1 + + # With filter (hide record) + list_url = url_for( + 'invenio_records_rest.illr_list', + q=f'pid:{ill_request_martigny["pid"]}', + remove_archived='1' ) res = client.get(list_url) result = res.json @@ -273,35 +299,46 @@ def db_commit_reindex(record): ill_request_martigny.model.created = date_delta(7) db_commit_reindex(ill_request_martigny) + # Without filter (show record) list_url = url_for( 'invenio_records_rest.illr_list', - q='pid:'+ill_request_martigny['pid'] + q=f'pid:{ill_request_martigny["pid"]}' ) res = client.get(list_url) result = res.json assert result['hits']['total']['value'] == 1 - # closed 7 months + # With filter (show record) + list_url = url_for( + 'invenio_records_rest.illr_list', + q=f'pid:{ill_request_martigny["pid"]}', + remove_archived='1' + ) + res = client.get(list_url) + result = res.json + assert result['hits']['total']['value'] == 0 + + # Make record to pending status ill_request_martigny = ILLRequest\ .get_record_by_pid(ill_request_martigny.pid) - ill_request_martigny['status'] = ILLRequestStatus.CLOSED + ill_request_martigny['status'] = ILLRequestStatus.PENDING ill_request_martigny.update( ill_request_martigny, dbcommit=True, reindex=True) + # Without filter (show record) list_url = url_for( 'invenio_records_rest.illr_list', - q='pid:'+ill_request_martigny['pid'] + q=f'pid:{ill_request_martigny["pid"]}' ) res = client.get(list_url) result = res.json - assert result['hits']['total']['value'] == 0 - - # Change delta - app.config['RERO_ILS_ILL_HIDE_MONTHS'] = 8 + assert result['hits']['total']['value'] == 1 + # With filter (show record) list_url = url_for( 'invenio_records_rest.illr_list', - q='pid:'+ill_request_martigny['pid'] + q=f'pid:{ill_request_martigny["pid"]}', + remove_archived='1' ) res = client.get(list_url) result = res.json