Skip to content

Commit

Permalink
fees: add organisation search filter
Browse files Browse the repository at this point in the history
* Create new signal to enrich data before indexing.
* Add organisation pid in indexed fields.
* Closes rero#560 (search api return 0 records).

Co-Authored-by: Lauren-D <laurent.dubois@itld-solutions.be>
  • Loading branch information
lauren-d committed Oct 30, 2019
1 parent 0c3a26c commit 3d33731
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 2 additions & 0 deletions rero_ils/modules/ext.py
Expand Up @@ -30,6 +30,7 @@
from .documents.listener import enrich_document_data, mef_person_delete, \
mef_person_insert, mef_person_revert, mef_person_update
from .ebooks.receivers import publish_harvested_records
from .fees.listener import enrich_fee_data
from .holdings.listener import enrich_holding_data
from .items.listener import enrich_item_data
from .loans.listener import enrich_loan_data, listener_loan_state_changed
Expand Down Expand Up @@ -97,6 +98,7 @@ def register_signals(self):
before_record_index.connect(enrich_location_data)
before_record_index.connect(enrich_holding_data)
before_record_index.connect(enrich_notification_data)
before_record_index.connect(enrich_fee_data)

loan_state_changed.connect(listener_loan_state_changed, weak=False)

Expand Down
38 changes: 38 additions & 0 deletions rero_ils/modules/fees/listener.py
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
#
# RERO ILS
# Copyright (C) 2019 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
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Signals connector for Fee."""

from .api import Fee, FeesSearch


def enrich_fee_data(sender, json=None, record=None, index=None,
**dummy_kwargs):
"""Signal sent before a record is indexed.
:params json: The dumped record dictionary which can be modified.
:params record: The record being indexed.
:params index: The index in which the record will be indexed.
:params doc_type: The doc_type for the record.
"""
fee_index_name = FeesSearch.Meta.index
if index.startswith(fee_index_name):
fee = Fee.get_record_by_pid(record.get('pid'))
org_pid = fee.organisation_pid
json['organisation'] = {
'pid': org_pid
}
25 changes: 24 additions & 1 deletion tests/api/test_fees.py
Expand Up @@ -22,7 +22,7 @@
from flask import url_for
from invenio_accounts.testutils import login_user_via_session
from invenio_circulation.search.api import LoansSearch
from utils import flush_index, postdata
from utils import flush_index, get_json, postdata

from rero_ils.modules.loans.api import Loan, LoanAction, get_overdue_loans
from rero_ils.modules.notifications.api import NotificationsSearch
Expand Down Expand Up @@ -133,3 +133,26 @@ def test_create_fee_euro(client, librarian_martigny_no_email,
flush_index(LoansSearch.Meta.index)
fee = list(notification.fees)[0]
assert fee.get('currency') == org.get('default_currency')


def test_filtered_fees_get(
client,
librarian_martigny_no_email,
librarian_sion_no_email):
"""Test fee filter by organisation."""
# Martigny
login_user_via_session(client, librarian_martigny_no_email.user)
list_url = url_for('invenio_records_rest.fee_list')
res = client.get(list_url)
assert res.status_code == 200
data = get_json(res)
assert data['hits']['total'] == 2

# Sion
login_user_via_session(client, librarian_sion_no_email.user)
list_url = url_for('invenio_records_rest.fee_list')

res = client.get(list_url)
assert res.status_code == 200
data = get_json(res)
assert data['hits']['total'] == 0

0 comments on commit 3d33731

Please sign in to comment.