Skip to content

Commit

Permalink
rerodoc: redirection to the correct views
Browse files Browse the repository at this point in the history
* Adds redirections for files.
* Adds redirections the dedicated or shared views.

Co-Authored-by: Johnny Mariéthoz <[email protected]>
  • Loading branch information
jma committed Jan 26, 2022
1 parent 2422b85 commit 632667c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
27 changes: 25 additions & 2 deletions sonar/theme/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -92,7 +93,8 @@ def error():


@blueprint.route('/rerodoc/<pid>')
def rerodoc_redirection(pid):
@blueprint.route('/rerodoc/<pid>/files/<filename>')
def rerodoc_redirection(pid, filename=None):
"""Redirection to document with identifier from RERODOC.
:param pid: PID from RERODOC.
Expand All @@ -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))


Expand Down
36 changes: 34 additions & 2 deletions tests/ui/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 632667c

Please sign in to comment.