Skip to content

Commit

Permalink
documents: clear controlled affiliations
Browse files Browse the repository at this point in the history
* Closes #807.
* Removes controlled affiliations because sometimes a controlled
  affiliation becomes uncontrolled.

Co-Authored-by: Johnny Mariéthoz <Johnny.Mariethoz@rero.ch>
Co-Authored-by:  Pascal Repond <pascal.repond@rero.ch>
  • Loading branch information
jma and PascalRepond committed Nov 24, 2022
1 parent 78edd42 commit 4ed6af9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
8 changes: 5 additions & 3 deletions sonar/modules/documents/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,11 @@ def guess_controlled_affiliations(cls, data):
affiliation_resolver = AffiliationResolver()
for contributor in data.get('contribution', []):
if contributor.get('affiliation'):
controlled_affiliations = affiliation_resolver.resolve(
contributor['affiliation'])
if controlled_affiliations:
# remove existing controlled affiliation
with contextlib.suppress(KeyError):
del contributor['controlledAffiliation']
if controlled_affiliations := affiliation_resolver.resolve(
contributor['affiliation']):
contributor[
'controlledAffiliation'] = controlled_affiliations

Expand Down
14 changes: 14 additions & 0 deletions tests/ui/documents/test_documents_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

"""Test documents API."""

from copy import deepcopy

from flask import url_for
from invenio_stats.tasks import aggregate_events, process_events

Expand Down Expand Up @@ -184,3 +186,15 @@ def test_stats(app, client, document_with_file, es, db, event_queues):
assert document_with_file.statistics['file-download']['test1.pdf'] == 1
# the thumbnail should not be in the statistics
assert not 'test1-pdf.jpg' in document_with_file.statistics['file-download']


def test_affiliations(document):
"""Test controlled affiliation."""
data = deepcopy(dict(document))
data['contribution'][0]['affiliation'] = 'foo'
document.update(data)
assert 'controlledAffiliation' not in document['contribution'][0]

data['contribution'][0]['affiliation'] = 'Uni of Geneva and HUG, Uni of Lausanne and CHUV'
document.update(data)
assert len(document['contribution'][0]['controlledAffiliation']) == 2

0 comments on commit 4ed6af9

Please sign in to comment.