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 master 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 25, 2024
1 parent fba5bd8 commit d178662
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
10 changes: 7 additions & 3 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 @@ -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(
Expand All @@ -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):
Expand Down
53 changes: 45 additions & 8 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 @@ -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']
Expand All @@ -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
Expand Down

0 comments on commit d178662

Please sign in to comment.