Skip to content

Commit

Permalink
persons: filter persons view
Browse files Browse the repository at this point in the history
* Adds `organisations` to persons ES index.

Co-Authored-by: Peter Weber <[email protected]>
  • Loading branch information
rerowep and rerowep committed Dec 16, 2019
1 parent 3c7d0d2 commit 733a5c3
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
2 changes: 2 additions & 0 deletions rero_ils/modules/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from .locations.listener import enrich_location_data
from .notifications.listener import enrich_notification_data
from .patrons.listener import enrich_patron_data
from .persons.listener import enrich_persons_data
from .persons.receivers import publish_api_harvested_records
from ..filter import format_date_filter, jsondumps, text_to_id, to_pretty_json

Expand Down Expand Up @@ -86,6 +87,7 @@ def register_signals(self):
"""Register signals."""
before_record_index.connect(enrich_loan_data)
before_record_index.connect(enrich_document_data)
before_record_index.connect(enrich_persons_data)
before_record_index.connect(enrich_item_data)
before_record_index.connect(enrich_patron_data)
before_record_index.connect(enrich_location_data)
Expand Down
17 changes: 17 additions & 0 deletions rero_ils/modules/persons/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

from .models import PersonIdentifier
from ..api import IlsRecord
from ..documents.api import DocumentsSearch
from ..fetchers import id_fetcher
from ..holdings.api import Holding
from ..minters import id_minter
from ..providers import Provider
from ...utils import unique_list
Expand Down Expand Up @@ -161,3 +163,18 @@ def _get_author_for_document(self):
if variant_person:
author['variant_name'] = unique_list(variant_person)
return author

@property
def organisation_pids(self):
"""Get organisations pids."""
organisations = []
results = DocumentsSearch().filter(
'term', authors__pid=self.pid
).execute()
for result in results:
hold_pids = Holding.get_holdings_pid_by_document_pid(result['pid'])
for hold_pid in hold_pids:
organisations.append(
Holding.get_record_by_pid(hold_pid).organisation_pid
)
return organisations
33 changes: 33 additions & 0 deletions rero_ils/modules/persons/listener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- 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 Persons."""

from .api import PersonsSearch


def enrich_persons_data(sender, json=None, record=None, index=None,
doc_type=None, **dummy_kwargs):
"""Signal sent before a record is indexed.
:param json: The dumped record dictionary which can be modified.
:param record: The record being indexed.
:param index: The index in which the record will be indexed.
:param doc_type: The doc_type for the record.
"""
if index == '-'.join([PersonsSearch.Meta.index, doc_type]):
json['organisations'] = record.organisation_pids
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@
}
}
},
"organisations": {
"type": "keyword"
},
"_created": {
"type": "date"
},
Expand Down
2 changes: 2 additions & 0 deletions tests/api/test_persons_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,7 @@ def test_persons_get(client, person):
res = client.get(list_url)
assert res.status_code == 200
data = get_json(res)
person = person.replace_refs()
person['organisations'] = person.organisation_pids

assert data['hits']['hits'][0]['metadata'] == person.replace_refs()
5 changes: 3 additions & 2 deletions tests/ui/persons/test_persons_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

from __future__ import absolute_import, print_function

from rero_ils.modules.persons.api import Person, PersonsSearch, \
person_id_fetcher
from rero_ils.modules.persons.api import Person, person_id_fetcher


def test_person_create(db, person_data_tmp):
Expand Down Expand Up @@ -49,3 +48,5 @@ def test_person_create(db, person_data_tmp):
)
pers = Person.get_record_by_pid('2')
assert pers.get('viaf_pid') == '1234'

assert pers.organisation_pids == []

0 comments on commit 733a5c3

Please sign in to comment.