diff --git a/sonar/theme/views.py b/sonar/theme/views.py index d3136728..8965cd8b 100644 --- a/sonar/theme/views.py +++ b/sonar/theme/views.py @@ -42,6 +42,7 @@ from sonar.modules.collections.permissions import \ RecordPermission as CollectionPermission from sonar.modules.deposits.permissions import DepositPermission +from sonar.modules.documents.api import DocumentRecord from sonar.modules.documents.permissions import DocumentPermission from sonar.modules.organisations.permissions import OrganisationPermission from sonar.modules.permissions import can_access_manage_view @@ -92,7 +93,8 @@ def error(): @blueprint.route('/rerodoc/') -def rerodoc_redirection(pid): +@blueprint.route('/rerodoc//files/') +def rerodoc_redirection(pid, filename=None): """Redirection to document with identifier from RERODOC. :param pid: PID from RERODOC. @@ -103,9 +105,30 @@ def rerodoc_redirection(pid): except Exception: abort(404) + # Files URLs does not contains view + if filename: + return redirect( + url_for('invenio_records_ui.doc_files', + pid_value=pid.get_redirect().pid_value, + filename=filename)) + doc_pid = pid.get_redirect().pid_value + doc = DocumentRecord.get_record_by_pid(doc_pid) + if doc: + doc = doc.replace_refs() + orgs = doc.get('organisation', []) + # In case of multiple organisations we redirect to the global view + if len(orgs) == 1: + org = orgs.pop() + # Only for dedicated or shared + if org.get('isDedicated') or org.get('isShared'): + return redirect( + url_for('invenio_records_ui.doc', + view=org.get('code'), + pid_value=pid.get_redirect().pid_value)) + global_view = current_app.config.get('SONAR_APP_DEFAULT_ORGANISATION') return redirect( url_for('invenio_records_ui.doc', - view='global', + view=global_view, pid_value=pid.get_redirect().pid_value)) diff --git a/tests/ui/test_views.py b/tests/ui/test_views.py index 61f25f41..3f10ccae 100644 --- a/tests/ui/test_views.py +++ b/tests/ui/test_views.py @@ -260,15 +260,47 @@ def test_record_image_url(): assert record_image_url(record, 'test2.jpg') == '/api/files/1234/test2.jpg' -def test_rerodoc_redirection(client, document): +def test_rerodoc_redirection(client, app, document, organisation): """Test redirection with RERODOC identifier.""" + global_view = app.config.get('SONAR_APP_DEFAULT_ORGANISATION') + # Does not exists res = client.get(url_for('sonar.rerodoc_redirection', pid='NOT-EXISTING')) assert res.status_code == 404 + # Files + res = client.get(url_for('sonar.rerodoc_redirection', pid='111111', filename='test.pdf')) + assert res.status_code == 302 + assert res.location.find( + f'/documents/{document["pid"]}/files/test.pdf') != -1 + + def changeOrg(key, value): + organisation[key] = value + organisation.commit() + organisation.dbcommit() + # Note: this is not needed as all is done from the db + # organisation.reindex() + + # No dedicated + changeOrg('isShared', False) + res = client.get(url_for('sonar.rerodoc_redirection', pid='111111')) + assert res.status_code == 302 + assert res.location.find( + f'/{global_view}/documents/{document["pid"]}') != -1 + + # Dedicated + changeOrg('isDedicated', True) + res = client.get(url_for('sonar.rerodoc_redirection', pid='111111')) + assert res.status_code == 302 + assert res.location.find( + f'/{organisation["code"]}/documents/{document["pid"]}') != -1 + + # Shared + changeOrg('isDedicated', False) + changeOrg('isShared', True) res = client.get(url_for('sonar.rerodoc_redirection', pid='111111')) assert res.status_code == 302 assert res.location.find( - '/global/documents/{pid}'.format(pid=document['pid'])) != -1 + f'/{organisation["code"]}/documents/{document["pid"]}') != -1 def test_format_date(app):