Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

documents: fix reindexing documents #3034

Merged
merged 1 commit into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions rero_ils/modules/contributions/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,17 @@ class Contribution(IlsRecord):
@classmethod
def get_contribution(cls, ref_type, ref_pid):
"""Get contribution."""
contribution = None
if ref_type == 'mef':
contribution = cls.get_record_by_pid(ref_pid)
return cls.get_record_by_pid(ref_pid)
if ref_type == 'viaf':
query = ContributionsSearch() \
.filter('term', viaf_pid=ref_pid)
else:
if ref_type == 'viaf':
result = ContributionsSearch().filter(
'term', viaf_pid=ref_pid
).source('pid').scan()
else:
result = ContributionsSearch().filter(
{'term': {f'{ref_type}.pid': ref_pid}}
).source('pid').scan()
with contextlib.suppress(StopIteration):
pid = next(result).pid
contribution = cls.get_record_by_pid(pid)
return contribution
query = ContributionsSearch() \
.filter({'term': {f'{ref_type}.pid': ref_pid}})
with contextlib.suppress(StopIteration):
pid = next(query.source('pid').scan()).pid
return cls.get_record_by_pid(pid)

@classmethod
def get_record_by_ref(cls, ref):
Expand Down
23 changes: 12 additions & 11 deletions rero_ils/modules/documents/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,21 +301,22 @@ def get_document_pids_by_issn(cls, issn_number: str):
def replace_refs(self):
"""Replace $ref with real data."""
from ..contributions.api import Contribution
contributions = self.get('contribution', [])
for contribution in contributions:
for contribution in self.get('contribution', []):
if ref := contribution['agent'].get('$ref'):
agent, _ = Contribution.get_record_by_ref(ref)
if agent:
contribution['agent'] = agent
subjects = self.get('subjects', [])
for subject in subjects:
subject_ref = subject.get('$ref')
subject_type = subject.get('type')
if subject_ref and subject_type in \
[DocumentSubjectType.PERSON, DocumentSubjectType.ORGANISATION]:
data, _ = Contribution.get_record_by_ref(subject_ref)
del subject['$ref']
subject.update(data)
for subjects in ['subjects', 'subjects_imported']:
for subject in self.get(subjects, []):
subject_ref = subject.get('$ref')
subject_type = subject.get('type')
if subject_ref and subject_type in [
DocumentSubjectType.PERSON,
DocumentSubjectType.ORGANISATION
]:
data, _ = Contribution.get_record_by_ref(subject_ref)
del subject['$ref']
subject.update(data)

return super().replace_refs()

Expand Down