Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/views/directory.py
Show All 9 Lines | |||||
from django.http import HttpResponse | from django.http import HttpResponse | ||||
from django.shortcuts import redirect, render | from django.shortcuts import redirect, render | ||||
from swh.model.swhids import ObjectType | from swh.model.swhids import ObjectType | ||||
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, http_status_code_message | from swh.web.common.exc import BadInputExc, NotFoundExc, http_status_code_message | ||||
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, | |||||
redirect_to_new_route, | |||||
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 | ||||
error_info = {"status_code": 200, "description": None} | error_info = {"status_code": 200, "description": None} | ||||
if path: | if path: | ||||
try: | try: | ||||
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"] | ||||
except NotFoundExc as e: | except NotFoundExc as e: | ||||
error_info["status_code"] = 404 | error_info["status_code"] = 404 | ||||
error_info["description"] = f"NotFoundExc: {str(e)}" | error_info["description"] = f"NotFoundExc: {str(e)}" | ||||
sha1_git = None | sha1_git = None | ||||
dirs, files = [], [] | dirs, files = [], [] | ||||
if sha1_git is not None: | if sha1_git is not None: | ||||
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") or request.GET.get("snapshot_id") | ||||
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"), | ||||
▲ Show 20 Lines • Show All 175 Lines • ▼ Show 20 Lines | return render( | ||||
"error_code": error_info["status_code"], | "error_code": error_info["status_code"], | ||||
"error_message": http_status_code_message.get(error_info["status_code"]), | "error_message": http_status_code_message.get(error_info["status_code"]), | ||||
"error_description": error_info["description"], | "error_description": error_info["description"], | ||||
}, | }, | ||||
status=error_info["status_code"], | status=error_info["status_code"], | ||||
) | ) | ||||
def _get_directory_from_request(request): | |||||
if request.GET.get("sha1_git") is not None: | |||||
# This case happens when redirected from | |||||
# directory/(?P<sha1_git>[0-9a-f]+)/(?P<path>.+)/ | |||||
return request.GET.get("sha1_git") | |||||
snapshot = request.GET.get("snapshot") or request.GET.get("snapshot_id") | |||||
origin_url = request.GET.get("origin_url") | |||||
if snapshot is None and origin_url is None: | |||||
raise BadInputExc( | |||||
"The origin_url or snapshot query parameter must be provided." | |||||
) | |||||
snapshot_context = get_snapshot_context( | |||||
snapshot_id=snapshot, | |||||
origin_url=origin_url, | |||||
timestamp=request.GET.get("timestamp"), | |||||
visit_id=request.GET.get("visit_id"), | |||||
branch_name=request.GET.get("branch"), | |||||
release_name=request.GET.get("release"), | |||||
revision_id=request.GET.get("revision"), | |||||
) | |||||
path = request.GET.get("path") | |||||
if snapshot_context["root_directory"] is None: | |||||
raise NotFoundExc("The requested directory is not available") | |||||
# assert path | |||||
if path is not None: | |||||
archive.lookup_directory_with_path(snapshot_context["root_directory"], path) | |||||
return snapshot_context["root_directory"] | |||||
@browse_route( | @browse_route( | ||||
r"directory/(?P<sha1_git>[0-9a-f]+)/", | r"directory/(?P<sha1_git>[0-9a-f]+)/", | ||||
r"directory/", | |||||
view_name="browse-directory", | view_name="browse-directory", | ||||
checksum_args=["sha1_git"], | checksum_args=["sha1_git"], | ||||
) | ) | ||||
def directory_browse(request, sha1_git): | def directory_browse(request, sha1_git=None): | ||||
"""Django view for browsing the content of a directory identified | """Django view for browsing the content of a directory identified | ||||
by its sha1_git value. | by its sha1_git value. | ||||
The url that points to it is | The URLs that point to it are | ||||
:http:get:`/browse/directory/(sha1_git)/` | :http:get:`/browse/directory/(sha1_git)/` | ||||
:http:get:`/browse/directory/` | |||||
""" | """ | ||||
if sha1_git is None: | |||||
# this case happens when redirected from origin/directory or snapshot/directory | |||||
directory = _get_directory_from_request(request) | |||||
return redirect( | |||||
reverse( | |||||
"browse-directory", | |||||
url_args={"sha1_git": directory}, | |||||
query_params=request.GET, | |||||
), | |||||
) | |||||
return _directory_browse(request, sha1_git, request.GET.get("path")) | return _directory_browse(request, sha1_git, request.GET.get("path")) | ||||
@browse_route( | @browse_route( | ||||
r"directory/(?P<sha1_git>[0-9a-f]+)/(?P<path>.+)/", | r"directory/(?P<sha1_git>[0-9a-f]+)/(?P<path>.+)/", | ||||
view_name="browse-directory-legacy", | view_name="browse-directory-legacy", | ||||
checksum_args=["sha1_git"], | checksum_args=["sha1_git"], | ||||
) | ) | ||||
def directory_browse_legacy(request, sha1_git, path): | def directory_browse_legacy(request, sha1_git, path): | ||||
"""Django view for browsing the content of a directory identified | """ | ||||
This route is deprecated; use http:get:`/browse/directory` instead | |||||
Django view for browsing the content of a directory identified | |||||
by its sha1_git value. | by its sha1_git value. | ||||
The url that points to it is | The url that points to it is | ||||
:http:get:`/browse/directory/(sha1_git)/(path)/` | :http:get:`/browse/directory/(sha1_git)/(path)/` | ||||
""" | """ | ||||
return _directory_browse(request, sha1_git, path) | return redirect_to_new_route(request, "browse-directory") | ||||
@browse_route( | @browse_route( | ||||
r"directory/resolve/content-path/(?P<sha1_git>[0-9a-f]+)/", | r"directory/resolve/content-path/(?P<sha1_git>[0-9a-f]+)/", | ||||
view_name="browse-directory-resolve-content-path", | view_name="browse-directory-resolve-content-path", | ||||
checksum_args=["sha1_git"], | checksum_args=["sha1_git"], | ||||
) | ) | ||||
def _directory_resolve_content_path(request, sha1_git): | def _directory_resolve_content_path(request, sha1_git): | ||||
Show All 17 Lines |