Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/views/directory.py
Show All 10 Lines | |||||
from django.shortcuts import redirect, render | from django.shortcuts import redirect, render | ||||
from django.template.defaultfilters import filesizeformat | from django.template.defaultfilters import filesizeformat | ||||
from swh.model.identifiers import DIRECTORY, RELEASE, REVISION, SNAPSHOT | from swh.model.identifiers import DIRECTORY, RELEASE, REVISION, SNAPSHOT | ||||
from swh.web.browse.browseurls import browse_route | from swh.web.browse.browseurls import browse_route | ||||
from swh.web.browse.snapshot_context import get_snapshot_context | from swh.web.browse.snapshot_context import get_snapshot_context | ||||
from swh.web.browse.utils import gen_link, get_directory_entries, get_readme_to_display | from swh.web.browse.utils import gen_link, get_directory_entries, get_readme_to_display | ||||
from swh.web.common import archive | from swh.web.common import archive | ||||
from swh.web.common.exc import NotFoundExc, handle_view_exception | from swh.web.common.exc import 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 gen_path_info, reverse, swh_object_icons | from swh.web.common.utils import gen_path_info, reverse, 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: | |||||
if path: | if path: | ||||
dir_info = archive.lookup_directory_with_path(sha1_git, path) | dir_info = archive.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") | origin_url = request.GET.get("origin_url") | ||||
if not origin_url: | if not origin_url: | ||||
origin_url = request.GET.get("origin") | origin_url = request.GET.get("origin") | ||||
snapshot_id = request.GET.get("snapshot") | snapshot_id = request.GET.get("snapshot") | ||||
snapshot_context = None | snapshot_context = None | ||||
if origin_url is not None or snapshot_id is not None: | if origin_url is not None or snapshot_id is not None: | ||||
try: | try: | ||||
snapshot_context = get_snapshot_context( | snapshot_context = get_snapshot_context( | ||||
snapshot_id=snapshot_id, | snapshot_id=snapshot_id, | ||||
origin_url=origin_url, | origin_url=origin_url, | ||||
branch_name=request.GET.get("branch"), | branch_name=request.GET.get("branch"), | ||||
release_name=request.GET.get("release"), | release_name=request.GET.get("release"), | ||||
revision_id=request.GET.get("revision"), | revision_id=request.GET.get("revision"), | ||||
path=path, | path=path, | ||||
) | ) | ||||
except NotFoundExc as e: | except NotFoundExc as e: | ||||
if str(e).startswith("Origin"): | if str(e).startswith("Origin"): | ||||
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)) | ||||
) | ) | ||||
raise NotFoundExc(error_message) | raise NotFoundExc(error_message) | ||||
else: | else: | ||||
raise e | raise e | ||||
except Exception as exc: | |||||
return handle_view_exception(request, exc) | |||||
path_info = gen_path_info(path) | path_info = gen_path_info(path) | ||||
query_params = snapshot_context["query_params"] if snapshot_context else {} | query_params = snapshot_context["query_params"] if snapshot_context else {} | ||||
breadcrumbs = [] | breadcrumbs = [] | ||||
breadcrumbs.append( | breadcrumbs.append( | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 199 Lines • Show Last 20 Lines |