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 Dec 21, 2021
1 parent 556b3b9 commit 0b5b595
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
3 changes: 2 additions & 1 deletion sonar/modules/documents/dojson/rerodoc/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
from sonar.modules.collections.api import Record as CollectionRecord
from sonar.modules.documents.dojson.rerodoc.overdo import Overdo
from sonar.modules.organisations.api import OrganisationRecord
from sonar.modules.subdivisions.api import RecordSearch, Record as SubdivisionRecord
from sonar.modules.subdivisions.api import Record as SubdivisionRecord
from sonar.modules.subdivisions.api import RecordSearch
from sonar.modules.utils import remove_trailing_punctuation

overdo = Overdo()
Expand Down
21 changes: 20 additions & 1 deletion 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 @@ -102,6 +104,23 @@ def rerodoc_redirection(pid):
pid = PersistentIdentifier.get('rerod', pid)
except Exception:
abort(404)
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', [])
if len(orgs) == 1:
org = orgs.pop()
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))

return redirect(
url_for('invenio_records_ui.doc',
Expand Down
35 changes: 33 additions & 2 deletions tests/ui/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,46 @@ 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, document, organisation):
"""Test redirection with RERODOC identifier."""
# 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/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 0b5b595

Please sign in to comment.