Skip to content

Commit

Permalink
ill_request: add month filter
Browse files Browse the repository at this point in the history
* closes rero#3527.

Co-Authored-by: Bertrand Zuchuat <[email protected]>
  • Loading branch information
Garfield-fr committed Nov 21, 2023
1 parent 9154312 commit dbba614
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 5 deletions.
1 change: 1 addition & 0 deletions rero_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def _(x):
# ILL Request config
RERO_ILS_ILL_REQUEST_ON_GLOBAL_VIEW = True
RERO_ILS_ILL_DEFAULT_SOURCE = 'RERO +'
RERO_ILS_ILL_HIDE_MONTHS = -6

# Rate limiting
# =============
Expand Down
14 changes: 13 additions & 1 deletion rero_ils/modules/ill_requests/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@

"""API for manipulating ill_requests."""

from datetime import datetime
from functools import partial

from dateutil.relativedelta import *
from elasticsearch_dsl.query import Q
from flask import current_app
from flask_babelex import gettext as _

from rero_ils.modules.api import IlsRecord, IlsRecordsIndexer, IlsRecordsSearch
Expand Down Expand Up @@ -60,7 +64,15 @@ def get_ill_requests_total_for_patron(self, patron_pid):
:param patron_pid: the patron pid being searched.
:return: return total of ill requests.
"""
return self.filter('term', patron__pid=patron_pid).count()
months = current_app.config.get('RERO_ILS_ILL_HIDE_MONTHS', -6)
date_delta = datetime.now() + relativedelta(months = months)
filters = Q(
'range',
_created={'lte': 'now', 'gte': date_delta}
)
filters |= Q('term', status='pending')
filters &= Q('term', patron__pid=patron_pid)
return self.filter(filters).count()


class ILLRequest(IlsRecord):
Expand Down
15 changes: 12 additions & 3 deletions rero_ils/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
from __future__ import absolute_import, print_function

import re
from datetime import datetime

from dateutil.relativedelta import *
from elasticsearch_dsl.query import Q
from flask import current_app, request
from invenio_i18n.ext import current_i18n
Expand Down Expand Up @@ -280,10 +282,17 @@ def ill_request_search_factory(self, search, query_parser=None):
search = search.filter(
'terms',
patron__pid=[ptrn.pid for ptrn in current_patrons])
# exclude to_anonymize records
search = search.filter('bool', must_not=[Q('term', to_anonymize=True)])

return search, urlkwargs
months = current_app.config.get('RERO_ILS_ILL_HIDE_MONTHS', -6)
date_delta = datetime.now() + relativedelta(months = months)
filters = Q(
'range',
_created={'lte': 'now', 'gte': date_delta}
)
filters |= Q('term', status='pending')

return search.filter(
filters).exclude(Q('term', to_anonymize=True)), urlkwargs


def circulation_search_factory(self, search, query_parser=None):
Expand Down
68 changes: 67 additions & 1 deletion 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 RERO
# 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
Expand All @@ -19,13 +19,17 @@

import json
from copy import deepcopy
from datetime import datetime

import mock
from dateutil.relativedelta import *
from flask import url_for
from invenio_accounts.testutils import login_user_via_session
from utils import VerifyRecordPermissionPatch, get_json, postdata, \
to_relative_url

from rero_ils.modules.ill_requests.api import ILLRequest


def test_ill_requests_permissions(client, ill_request_martigny, json_header):
"""Test record retrieval."""
Expand Down Expand Up @@ -236,3 +240,65 @@ def test_ill_request_secure_api_delete(client, ill_request_martigny,
)
res = client.delete(record_url)
assert res.status_code == 403


def test_filtered_ill_requests_get_pending_months_filters(
client, app, db, librarian_martigny, ill_request_martigny):
"""Test ill_requests filter by pending and months."""

def date_delta(months):
"""Date delta."""
return datetime.now() + relativedelta(months=months)

def db_commit_reindex(record):
"""DB commit and reindex."""
db.session.merge(record.model)
db.session.commit()
record.reindex()

login_user_via_session(client, librarian_martigny.user)

# Initial status is pending
list_url = url_for('invenio_records_rest.illr_list')
res = client.get(list_url)
result = res.json
assert result['hits']['total']['value'] == 2

# Change created date
initial_create = ill_request_martigny.model.created
ill_request_martigny.model.created = date_delta(-7)
db_commit_reindex(ill_request_martigny)

list_url = url_for('invenio_records_rest.illr_list')
res = client.get(list_url)
result = res.json
assert result['hits']['total']['value'] == 2

# closed 7 months
ill_request_martigny = ILLRequest\
.get_record_by_pid(ill_request_martigny.pid)
ill_request_martigny['status'] = 'closed'
ill_request_martigny.update(
ill_request_martigny, dbcommit=True, reindex=True)

list_url = url_for('invenio_records_rest.illr_list')
res = client.get(list_url)
result = res.json
assert result['hits']['total']['value'] == 1

# Change delta
app.config['RERO_ILS_ILL_HIDE_MONTHS'] = -8

list_url = url_for('invenio_records_rest.illr_list')
res = client.get(list_url)
result = res.json
assert result['hits']['total']['value'] == 2

# Initial state
ill_request_martigny.model.created = initial_create
db_commit_reindex(ill_request_martigny)
ill_request_martigny = ILLRequest\
.get_record_by_pid(ill_request_martigny.pid)
ill_request_martigny['status'] = 'pending'
ill_request_martigny.update(
ill_request_martigny, dbcommit=True, reindex=True)

0 comments on commit dbba614

Please sign in to comment.