diff --git a/rero_ils/query.py b/rero_ils/query.py index 007a192210..5c8c966e33 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 @@ -283,6 +283,11 @@ 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( @@ -291,8 +296,7 @@ def ill_request_search_factory(self, search, query_parser=None): ) filters |= Q('term', status='pending') - 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..b61b55d74d 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 @@ -268,11 +268,38 @@ def db_commit_reindex(record): 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='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='pid:'+ill_request_martigny['pid'], + remove_archived='1' + ) + res = client.get(list_url) + result = res.json + assert result['hits']['total']['value'] == 1 + # Change created date initial_create = ill_request_martigny.model.created 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'] @@ -281,27 +308,37 @@ def db_commit_reindex(record): 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='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'] ) 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='pid:'+ill_request_martigny['pid'], + remove_archived='1' ) res = client.get(list_url) result = res.json