Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/views/directory.py
Show All 17 Lines | from swh.web.browse.utils import ( | ||||
get_directory_entries, | get_directory_entries, | ||||
get_readme_to_display, | get_readme_to_display, | ||||
gen_link, | gen_link, | ||||
) | ) | ||||
from swh.web.common import service | from swh.web.common import service | ||||
from swh.web.common.exc import handle_view_exception, NotFoundExc | from swh.web.common.exc import handle_view_exception, NotFoundExc | ||||
from swh.web.common.identifiers import get_swhids_info | from swh.web.common.identifiers import get_swhids_info | ||||
from swh.web.common.typing import DirectoryMetadata, SWHObjectInfo | from swh.web.common.typing import DirectoryMetadata, SWHObjectInfo | ||||
from swh.web.common.utils import reverse, gen_path_info | from swh.web.common.utils import reverse, gen_path_info, swh_object_icons | ||||
def _directory_browse(request, sha1_git, path=None): | def _directory_browse(request, sha1_git, path=None): | ||||
root_sha1_git = sha1_git | root_sha1_git = sha1_git | ||||
try: | try: | ||||
if path: | if path: | ||||
dir_info = service.lookup_directory_with_path(sha1_git, path) | dir_info = service.lookup_directory_with_path(sha1_git, path) | ||||
sha1_git = dir_info["target"] | sha1_git = dir_info["target"] | ||||
dirs, files = get_directory_entries(sha1_git) | dirs, files = get_directory_entries(sha1_git) | ||||
origin_url = request.GET.get("origin_url", None) | origin_url = request.GET.get("origin_url") | ||||
if not origin_url: | if not origin_url: | ||||
origin_url = request.GET.get("origin", None) | origin_url = request.GET.get("origin") | ||||
snapshot_id = request.GET.get("snapshot") | |||||
snapshot_context = None | snapshot_context = None | ||||
if origin_url: | if origin_url is not None or snapshot_id is not None: | ||||
try: | try: | ||||
snapshot_context = get_snapshot_context(origin_url=origin_url) | snapshot_context = get_snapshot_context( | ||||
except NotFoundExc: | snapshot_id=snapshot_id, | ||||
origin_url=origin_url, | |||||
branch_name=request.GET.get("branch"), | |||||
release_name=request.GET.get("release"), | |||||
revision_id=request.GET.get("revision"), | |||||
) | |||||
except NotFoundExc as e: | |||||
raw_dir_url = reverse( | raw_dir_url = reverse( | ||||
"browse-directory", url_args={"sha1_git": sha1_git} | "browse-directory", url_args={"sha1_git": sha1_git} | ||||
) | ) | ||||
error_message = ( | error_message = ( | ||||
"The Software Heritage archive has a directory " | "The Software Heritage archive has a directory " | ||||
"with the hash you provided but the origin " | "with the hash you provided but the origin " | ||||
"mentioned in your request appears broken: %s. " | "mentioned in your request appears broken: %s. " | ||||
"Please check the URL and try again.\n\n" | "Please check the URL and try again.\n\n" | ||||
"Nevertheless, you can still browse the directory " | "Nevertheless, you can still browse the directory " | ||||
"without origin information: %s" | "without origin information: %s" | ||||
% (gen_link(origin_url), gen_link(raw_dir_url)) | % (gen_link(origin_url), gen_link(raw_dir_url)) | ||||
) | ) | ||||
if str(e).startswith("Origin"): | |||||
raise NotFoundExc(error_message) | raise NotFoundExc(error_message) | ||||
if snapshot_context: | else: | ||||
snapshot_context["visit_info"] = None | raise e | ||||
except Exception as exc: | except Exception as exc: | ||||
return handle_view_exception(request, exc) | return handle_view_exception(request, exc) | ||||
path_info = gen_path_info(path) | path_info = gen_path_info(path) | ||||
query_params = {"origin_url": origin_url} | query_params = snapshot_context["query_params"] if snapshot_context else {} | ||||
breadcrumbs = [] | breadcrumbs = [] | ||||
breadcrumbs.append( | breadcrumbs.append( | ||||
{ | { | ||||
"name": root_sha1_git[:7], | "name": root_sha1_git[:7], | ||||
"url": reverse( | "url": reverse( | ||||
"browse-directory", | "browse-directory", | ||||
url_args={"sha1_git": root_sha1_git}, | url_args={"sha1_git": root_sha1_git}, | ||||
Show All 35 Lines | def _directory_browse(request, sha1_git, path=None): | ||||
for f in files: | for f in files: | ||||
query_string = "sha1_git:" + f["target"] | query_string = "sha1_git:" + f["target"] | ||||
f["url"] = reverse( | f["url"] = reverse( | ||||
"browse-content", | "browse-content", | ||||
url_args={"query_string": query_string}, | url_args={"query_string": query_string}, | ||||
query_params={ | query_params={ | ||||
"path": root_sha1_git + "/" + path + f["name"], | "path": root_sha1_git + "/" + path + f["name"], | ||||
"origin_url": origin_url, | **query_params, | ||||
}, | }, | ||||
) | ) | ||||
if f["length"] is not None: | if f["length"] is not None: | ||||
sum_file_sizes += f["length"] | sum_file_sizes += f["length"] | ||||
f["length"] = filesizeformat(f["length"]) | f["length"] = filesizeformat(f["length"]) | ||||
if f["name"].lower().startswith("readme"): | if f["name"].lower().startswith("readme"): | ||||
readmes[f["name"]] = f["checksums"]["sha1"] | readmes[f["name"]] = f["checksums"]["sha1"] | ||||
Show All 27 Lines | def _directory_browse(request, sha1_git, path=None): | ||||
swhids_info = get_swhids_info(swh_objects, snapshot_context, dir_metadata) | swhids_info = get_swhids_info(swh_objects, snapshot_context, dir_metadata) | ||||
heading = "Directory - %s" % sha1_git | heading = "Directory - %s" % sha1_git | ||||
if breadcrumbs: | if breadcrumbs: | ||||
dir_path = "/".join([bc["name"] for bc in breadcrumbs]) + "/" | dir_path = "/".join([bc["name"] for bc in breadcrumbs]) + "/" | ||||
heading += " - %s" % dir_path | heading += " - %s" % dir_path | ||||
top_right_link = None | |||||
if snapshot_context is not None and not snapshot_context["is_empty"]: | |||||
history_url = reverse( | |||||
"browse-revision-log", | |||||
url_args={"sha1_git": snapshot_context["revision_id"]}, | |||||
query_params=query_params, | |||||
) | |||||
top_right_link = { | |||||
"url": history_url, | |||||
"icon": swh_object_icons["revisions history"], | |||||
"text": "History", | |||||
} | |||||
return render( | return render( | ||||
request, | request, | ||||
"browse/directory.html", | "browse/directory.html", | ||||
{ | { | ||||
"heading": heading, | "heading": heading, | ||||
"swh_object_id": swhids_info[0]["swhid"], | "swh_object_id": swhids_info[0]["swhid"], | ||||
"swh_object_name": "Directory", | "swh_object_name": "Directory", | ||||
"swh_object_metadata": dir_metadata, | "swh_object_metadata": dir_metadata, | ||||
"dirs": dirs, | "dirs": dirs, | ||||
"files": files, | "files": files, | ||||
"breadcrumbs": breadcrumbs, | "breadcrumbs": breadcrumbs, | ||||
"top_right_link": None, | "top_right_link": top_right_link, | ||||
"readme_name": readme_name, | "readme_name": readme_name, | ||||
"readme_url": readme_url, | "readme_url": readme_url, | ||||
"readme_html": readme_html, | "readme_html": readme_html, | ||||
"snapshot_context": snapshot_context, | "snapshot_context": snapshot_context, | ||||
"vault_cooking": vault_cooking, | "vault_cooking": vault_cooking, | ||||
"show_actions_menu": True, | "show_actions_menu": True, | ||||
"swhids_info": swhids_info, | "swhids_info": swhids_info, | ||||
}, | }, | ||||
▲ Show 20 Lines • Show All 56 Lines • Show Last 20 Lines |