Skip to content

Commit

Permalink
fix(ill): conditional filter
Browse files Browse the repository at this point in the history
The "remove_archived" parameter is used to records older than 6 months
when the status is different from "Pending".

* Closes rero#3527.

Co-Authored-by: Bertrand Zuchuat <[email protected]>
  • Loading branch information
Garfield-fr committed Mar 27, 2024
1 parent 97f4820 commit 34ee5a3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
17 changes: 13 additions & 4 deletions rero_ils/query.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
59 changes: 48 additions & 11 deletions tests/api/ill_requests/test_ill_requests_rest.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 34ee5a3

Please sign in to comment.