Skip to content

Commit

Permalink
persons: fixes removing persons records when document is suppressed
Browse files Browse the repository at this point in the history
* Fix indexer schema mapping for mef authorities
* Reforctoring of document listeners using MefPerson class
* Closes rero#601

Co-Authored-by: Renaud Michotte <renaud.michotte@gmail.com>
  • Loading branch information
zannkukai committed Nov 14, 2019
1 parent e746970 commit 53d25f5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
12 changes: 11 additions & 1 deletion rero_ils/modules/documents/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ def can_edit(self):
# TODO: Make this condition on data
return not self.harvested

@property
def mef_authors(self):
"""Return a generator containing all MEF authors linked to this doc"""
from ..mef_persons.api import MefPerson # preserve circular import
refs = [a['$ref'] for a in self.get('authors', []) if a.get('$ref')]
for ref in refs:
person = MefPerson.get_record_by_mef_uri(ref)
if person:
yield person

def get_number_of_items(self):
"""Get number of items for document."""
from ..items.api import ItemsSearch
Expand Down Expand Up @@ -138,6 +148,6 @@ def dumps(self, **kwargs):
provision_activity["_text"] = \
publication_statement_text(provision_activity)
series = dump.get('series')
for series_element in series:
for series_element in series or []:
series_element["_text"] = series_format_text(series_element)
return dump
36 changes: 20 additions & 16 deletions rero_ils/modules/documents/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,23 +176,25 @@ def mef_person_update_index(sender, *args, **kwargs):
def mef_person_delete(sender, *args, **kwargs):
"""Delete signal."""
record = kwargs['record']
if 'documents' in record.get('$schema', ''):
authors = record.get('authors', [])
for author in authors:
mef_url = author.get('$ref')
if mef_url:
mef_url = mef_url.replace(
'mef.rero.ch',
current_app.config['RERO_ILS_MEF_HOST']
)
request = requests_get(url=mef_url, params=dict(
if 'documents' not in record.get('$schema', ''):
return

for mef_author in record.mef_authors:
print(mef_author)
if len(mef_author.get_linked_documents()) == 1:
mef_
"""author_ref = author_ref.replace(
'mef.rero.ch',
current_app.config['RERO_ILS_MEF_HOST']
)
request = requests_get(url=author_ref, params=dict(
resolve=1,
sources=1
))
if request.status_code == requests_codes.ok:
data = request.json()
id = data['id']
data = data.get('metadata')
))
if request.status_code == requests_codes.ok:
data = request.json()
id = data['id']
data = data.get('metadata')
if data:
search = DocumentsSearch()
count = search.filter(
Expand All @@ -215,4 +217,6 @@ def mef_person_delete(sender, *args, **kwargs):
url=mef_url
)
)
raise Exception('unable to resolve')
raise Exception('unable to resolve')"""
raise Exception("DEBUG....")

5 changes: 5 additions & 0 deletions rero_ils/modules/indexer_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def record_to_index(record):
# put all document in the same index
if re.search(r'/documents/', schema):
schema = re.sub(r'-.*\.json', '.json', schema)
# authorities specific transformation
if re.search(r'/authorities/', schema):
schema = re.sub(r'/authorities/', '/persons/', schema)
schema = re.sub(r'mef-person', 'mef_person', schema)

index, doc_type = schema_to_index(schema, index_names=index_names)

if index and doc_type:
Expand Down
6 changes: 1 addition & 5 deletions rero_ils/modules/mef_persons/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,11 @@ def get_record_by_pid(cls, pid):
@classmethod
def get_documents_by_person_pid(cls, pid, org_pid=None):
search = DocumentsSearch()
search = search.filter(
'term',
authors__pid=pid
)
search = search.filter('term', authors__pid=pid)
if org_pid:
search = search.filter(
'term', holdings__organisation__organisation_pid=org_pid
)

for document in search.execute().hits.hits:
print('document : \n', document)

Expand Down

0 comments on commit 53d25f5

Please sign in to comment.