Skip to content

Commit

Permalink
swh: added path to url
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandromumo committed Nov 28, 2024
1 parent cdf78d9 commit a1023d1
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions site/zenodo_rdm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import re
from urllib.parse import parse_qs, urlencode, urlsplit, urlunsplit
from zipfile import Path, is_zipfile

import idutils
from flask import current_app, url_for
Expand Down Expand Up @@ -139,13 +140,16 @@ def swh_link_render(record):
permissions = record.has_permissions_to(["manage"])
can_manage = permissions.get("can_manage", False)

def _get_success_text(deposit):
def _get_success_text(deposit, record):
"""Get the text to be displayed when the deposit was successful.
This function takes a `deposit` object as input and returns the text and link
to be displayed when the deposit was successful. The text is the identifier of
the deposit, and the link is a URL to the deposit's directory in the SWH UI.
Additionally, takes a `record` object as input to get the root path of the ZIP file
to be displayed in the SWH link.
Examples
--------
>>> deposit = Deposit(swhid="swh:1:dir:abc123")
Expand All @@ -156,10 +160,33 @@ def _get_success_text(deposit):
>>> _get_success_text(deposit)
('swh:1:origin:abc123', 'https://swh.example.com/browse/directory/abc123/')
>>> deposit = Deposit(swhid="swh:1:origin:abc123;origin=https://zenodo.org/record/xyz789&path=/")
>>> _get_success_text(deposit)
('swh:1:origin:abc123', 'https://swh.example.com/browse/directory/abc123&path=/root_directory/')
"""
swhid = deposit.swhid
base_url = current_app.config.get("SWH_UI_BASE_URL")
path = "/"
try:
# Displaying a specific 'path' only supports one ZIP file for now
if record._record.files.count == 1:
try:
frecord = next(iter(record._record.files.entries.values()))
with frecord.get_stream(mode="rb") as fp:
if is_zipfile(fp):
(top_level_path,) = Path(fp).iterdir()
path = top_level_path.name
# Update swhid with 'path' information, replace it if already present
swhid = (
swhid.replace("path=/", f"path={path}")
if "path=/" in swhid
else f"{swhid};path={path}"
)
except Exception:
pass # If there is an error, just ignore it and use the default path

# Generate the link and text to be displayed
swh_link = f"{base_url}/{swhid}"
swh_text = swhid.split(";")[0]
except Exception:
Expand Down Expand Up @@ -203,7 +230,7 @@ def _get_waiting_text(deposit):
subtitle, link = _get_waiting_text(d_res.deposit)

if d_res.deposit.status == SWHDepositStatus.SUCCESS:
subtitle, link = _get_success_text(d_res.deposit)
subtitle, link = _get_success_text(d_res.deposit, record)

if not subtitle:
return None
Expand Down

0 comments on commit a1023d1

Please sign in to comment.