Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/snapshot_context.py
# Copyright (C) 2018-2022 The Software Heritage developers | # Copyright (C) 2018-2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
# Utility module for browsing the archive in a snapshot context. | # Utility module for browsing the archive in a snapshot context. | ||||
from collections import defaultdict | from collections import defaultdict | ||||
from typing import Any, Dict, List, Optional, Tuple | from typing import Any, Dict, List, Optional, Tuple | ||||
from django.http import HttpRequest, HttpResponse | |||||
from django.shortcuts import render | from django.shortcuts import render | ||||
from django.utils.html import escape | from django.utils.html import escape | ||||
from swh.model.hashutil import hash_to_bytes | from swh.model.hashutil import hash_to_bytes | ||||
from swh.model.model import Snapshot | from swh.model.model import Snapshot | ||||
from swh.model.swhids import CoreSWHID, ObjectType | from swh.model.swhids import CoreSWHID, ObjectType | ||||
from swh.web.browse.utils import ( | from swh.web.browse.utils import ( | ||||
format_log_entries, | format_log_entries, | ||||
Show All 24 Lines | from swh.web.common.utils import ( | ||||
reverse, | reverse, | ||||
swh_object_icons, | swh_object_icons, | ||||
) | ) | ||||
from swh.web.config import get_config | from swh.web.config import get_config | ||||
_empty_snapshot_id = Snapshot(branches={}).id.hex() | _empty_snapshot_id = Snapshot(branches={}).id.hex() | ||||
def _get_branch(branches, branch_name, snapshot_id): | def _get_branch( | ||||
branches: List[SnapshotBranchInfo], branch_name: str, snapshot_id: str | |||||
) -> Optional[SnapshotBranchInfo]: | |||||
""" | """ | ||||
Utility function to get a specific branch from a snapshot. | Utility function to get a specific branch from a snapshot. | ||||
Returns None if the branch cannot be found. | Returns None if the branch cannot be found. | ||||
""" | """ | ||||
filtered_branches = [b for b in branches if b["name"] == branch_name] | filtered_branches = [b for b in branches if b["name"] == branch_name] | ||||
if filtered_branches: | if filtered_branches: | ||||
return filtered_branches[0] | return filtered_branches[0] | ||||
else: | else: | ||||
# case where a large branches list has been truncated | # case where a large branches list has been truncated | ||||
snp = archive.lookup_snapshot( | snp = archive.lookup_snapshot( | ||||
snapshot_id, | snapshot_id, | ||||
branches_from=branch_name, | branches_from=branch_name, | ||||
branches_count=1, | branches_count=1, | ||||
target_types=["revision", "alias"], | target_types=["revision", "alias"], | ||||
# pull request branches must be browsable even if they are hidden | # pull request branches must be browsable even if they are hidden | ||||
# by default in branches list | # by default in branches list | ||||
branch_name_exclude_prefix=None, | branch_name_exclude_prefix=None, | ||||
) | ) | ||||
snp_branch, _, _ = process_snapshot_branches(snp) | snp_branch, _, _ = process_snapshot_branches(snp) | ||||
if snp_branch and snp_branch[0]["name"] == branch_name: | if snp_branch and snp_branch[0]["name"] == branch_name: | ||||
branches.append(snp_branch[0]) | branches.append(snp_branch[0]) | ||||
return snp_branch[0] | return snp_branch[0] | ||||
return None | |||||
def _get_release(releases, release_name, snapshot_id): | def _get_release( | ||||
releases: List[SnapshotReleaseInfo], release_name: Optional[str], snapshot_id: str | |||||
) -> Optional[SnapshotReleaseInfo]: | |||||
""" | """ | ||||
Utility function to get a specific release from a snapshot. | Utility function to get a specific release from a snapshot. | ||||
Returns None if the release cannot be found. | Returns None if the release cannot be found. | ||||
""" | """ | ||||
filtered_releases = [r for r in releases if r["name"] == release_name] | filtered_releases = [r for r in releases if r["name"] == release_name] | ||||
if filtered_releases: | if filtered_releases: | ||||
return filtered_releases[0] | return filtered_releases[0] | ||||
else: | elif release_name: | ||||
# case where a large branches list has been truncated | # case where a large branches list has been truncated | ||||
try: | try: | ||||
# git origins have specific branches for releases | # git origins have specific branches for releases | ||||
snp = archive.lookup_snapshot( | snp = archive.lookup_snapshot( | ||||
snapshot_id, | snapshot_id, | ||||
branches_from=f"refs/tags/{release_name}", | branches_from=f"refs/tags/{release_name}", | ||||
branches_count=1, | branches_count=1, | ||||
target_types=["release"], | target_types=["release"], | ||||
) | ) | ||||
except NotFoundExc: | except NotFoundExc: | ||||
snp = archive.lookup_snapshot( | snp = archive.lookup_snapshot( | ||||
snapshot_id, | snapshot_id, | ||||
branches_from=release_name, | branches_from=release_name, | ||||
branches_count=1, | branches_count=1, | ||||
target_types=["release", "alias"], | target_types=["release", "alias"], | ||||
) | ) | ||||
_, snp_release, _ = process_snapshot_branches(snp) | _, snp_release, _ = process_snapshot_branches(snp) | ||||
if snp_release and snp_release[0]["name"] == release_name: | if snp_release and snp_release[0]["name"] == release_name: | ||||
releases.append(snp_release[0]) | releases.append(snp_release[0]) | ||||
return snp_release[0] | return snp_release[0] | ||||
return None | |||||
def _branch_not_found( | def _branch_not_found( | ||||
branch_type, branch, snapshot_id, snapshot_sizes, origin_info, timestamp, visit_id | branch_type: str, | ||||
): | branch: str, | ||||
snapshot_id: str, | |||||
snapshot_sizes: Dict[str, int], | |||||
origin_info: Optional[OriginInfo], | |||||
timestamp: Optional[str], | |||||
visit_id: Optional[int], | |||||
) -> None: | |||||
""" | """ | ||||
Utility function to raise an exception when a specified branch/release | Utility function to raise an exception when a specified branch/release | ||||
can not be found. | can not be found. | ||||
""" | """ | ||||
if branch_type == "branch": | if branch_type == "branch": | ||||
branch_type = "Branch" | branch_type = "Branch" | ||||
branch_type_plural = "branches" | branch_type_plural = "branches" | ||||
target_type = "revision" | target_type = "revision" | ||||
else: | else: | ||||
branch_type = "Release" | branch_type = "Release" | ||||
branch_type_plural = "releases" | branch_type_plural = "releases" | ||||
target_type = "release" | target_type = "release" | ||||
if snapshot_id and snapshot_sizes[target_type] == 0: | if snapshot_id and snapshot_sizes[target_type] == 0: | ||||
msg = "Snapshot with id %s has an empty list" " of %s!" % ( | msg = "Snapshot with id %s has an empty list" " of %s!" % ( | ||||
snapshot_id, | snapshot_id, | ||||
branch_type_plural, | branch_type_plural, | ||||
) | ) | ||||
elif snapshot_id: | elif snapshot_id: | ||||
msg = "%s %s for snapshot with id %s" " not found!" % ( | msg = "%s %s for snapshot with id %s" " not found!" % ( | ||||
branch_type, | branch_type, | ||||
branch, | branch, | ||||
snapshot_id, | snapshot_id, | ||||
) | ) | ||||
elif visit_id and snapshot_sizes[target_type] == 0: | elif visit_id and snapshot_sizes[target_type] == 0 and origin_info: | ||||
msg = ( | msg = ( | ||||
"Origin with url %s" | "Origin with url %s" | ||||
" for visit with id %s has an empty list" | " for visit with id %s has an empty list" | ||||
" of %s!" % (origin_info["url"], visit_id, branch_type_plural) | " of %s!" % (origin_info["url"], visit_id, branch_type_plural) | ||||
) | ) | ||||
elif visit_id: | elif visit_id and origin_info: | ||||
msg = ( | msg = ( | ||||
"%s %s associated to visit with" | "%s %s associated to visit with" | ||||
" id %s for origin with url %s" | " id %s for origin with url %s" | ||||
" not found!" % (branch_type, branch, visit_id, origin_info["url"]) | " not found!" % (branch_type, branch, visit_id, origin_info["url"]) | ||||
) | ) | ||||
elif snapshot_sizes[target_type] == 0: | elif snapshot_sizes[target_type] == 0 and origin_info and timestamp: | ||||
msg = ( | msg = ( | ||||
"Origin with url %s" | "Origin with url %s" | ||||
" for visit with timestamp %s has an empty list" | " for visit with timestamp %s has an empty list" | ||||
" of %s!" % (origin_info["url"], timestamp, branch_type_plural) | " of %s!" % (origin_info["url"], timestamp, branch_type_plural) | ||||
) | ) | ||||
else: | elif origin_info and timestamp: | ||||
msg = ( | msg = ( | ||||
"%s %s associated to visit with" | "%s %s associated to visit with" | ||||
" timestamp %s for origin with " | " timestamp %s for origin with " | ||||
"url %s not found!" % (branch_type, branch, timestamp, origin_info["url"]) | "url %s not found!" % (branch_type, branch, timestamp, origin_info["url"]) | ||||
) | ) | ||||
raise NotFoundExc(escape(msg)) | raise NotFoundExc(escape(msg)) | ||||
▲ Show 20 Lines • Show All 402 Lines • ▼ Show 20 Lines | elif snapshot_total_size: | ||||
# HEAD alias targets a revision | # HEAD alias targets a revision | ||||
head_rev = archive.lookup_revision(head["target"]) | head_rev = archive.lookup_revision(head["target"]) | ||||
branch_name = "HEAD" | branch_name = "HEAD" | ||||
revision_id = head_rev["id"] | revision_id = head_rev["id"] | ||||
root_directory = head_rev["directory"] | root_directory = head_rev["directory"] | ||||
else: | else: | ||||
# HEAD alias targets a release | # HEAD alias targets a release | ||||
release_name = archive.lookup_release(head["target"])["name"] | release_name = archive.lookup_release(head["target"])["name"] | ||||
head_rel = _get_release(releases, release_name, snapshot_id) | head_rel = _get_release(releases, release_name, snapshot_id) | ||||
vlorentz: why can't it be None? | |||||
Done Inline ActionsI should throw an exception here if the release is not found like it is done some lines above. anlambert: I should throw an exception here if the release is not found like it is done some lines above. | |||||
if head_rel["target_type"] == "revision": | if head_rel is None: | ||||
_branch_not_found( | |||||
"release", | |||||
str(release_name), | |||||
snapshot_id, | |||||
snapshot_sizes, | |||||
origin_info, | |||||
timestamp, | |||||
visit_id, | |||||
) | |||||
elif head_rel["target_type"] == "revision": | |||||
revision = archive.lookup_revision(head_rel["target"]) | revision = archive.lookup_revision(head_rel["target"]) | ||||
root_directory = revision["directory"] | root_directory = revision["directory"] | ||||
revision_id = head_rel["target"] | revision_id = head_rel["target"] | ||||
elif head_rel["target_type"] == "directory": | elif head_rel["target_type"] == "directory": | ||||
root_directory = head_rel["target"] | root_directory = head_rel["target"] | ||||
if head_rel is not None: | |||||
release_id = head_rel["id"] | release_id = head_rel["id"] | ||||
elif branches: | elif branches: | ||||
# fallback to browse first branch otherwise | # fallback to browse first branch otherwise | ||||
branch = branches[0] | branch = branches[0] | ||||
branch_name = branch["name"] | branch_name = branch["name"] | ||||
revision_id = branch["revision"] | revision_id = branch["revision"] | ||||
root_directory = branch["directory"] | root_directory = branch["directory"] | ||||
elif releases: | elif releases: | ||||
# fallback to browse last release otherwise | # fallback to browse last release otherwise | ||||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | snapshot_context = SnapshotContext( | ||||
snapshot_id=snapshot_id, | snapshot_id=snapshot_id, | ||||
snapshot_sizes=snapshot_sizes, | snapshot_sizes=snapshot_sizes, | ||||
snapshot_swhid=swh_snp_id, | snapshot_swhid=swh_snp_id, | ||||
url_args=url_args, | url_args=url_args, | ||||
visit_info=visit_info, | visit_info=visit_info, | ||||
) | ) | ||||
if revision_info: | if revision_info: | ||||
revision_info["revision_url"] = gen_revision_url(revision_id, snapshot_context) | revision_info["revision_url"] = gen_revision_url( | ||||
revision_info["id"], snapshot_context | |||||
) | |||||
return snapshot_context | return snapshot_context | ||||
def _build_breadcrumbs(snapshot_context: SnapshotContext, path: str): | def _build_breadcrumbs( | ||||
snapshot_context: SnapshotContext, path: Optional[str] | |||||
) -> List[Dict[str, str]]: | |||||
origin_info = snapshot_context["origin_info"] | origin_info = snapshot_context["origin_info"] | ||||
url_args = snapshot_context["url_args"] | url_args = snapshot_context["url_args"] | ||||
query_params = dict(snapshot_context["query_params"]) | query_params = dict(snapshot_context["query_params"]) | ||||
root_directory = snapshot_context["root_directory"] | root_directory = snapshot_context["root_directory"] | ||||
path_info = gen_path_info(path) | path_info = gen_path_info(path) | ||||
if origin_info: | if origin_info: | ||||
Show All 20 Lines | for pi in path_info: | ||||
"url": reverse( | "url": reverse( | ||||
browse_view_name, url_args=url_args, query_params=query_params | browse_view_name, url_args=url_args, query_params=query_params | ||||
), | ), | ||||
} | } | ||||
) | ) | ||||
return breadcrumbs | return breadcrumbs | ||||
def _check_origin_url(snapshot_id, origin_url): | def _check_origin_url(snapshot_id: Optional[str], origin_url: Optional[str]) -> None: | ||||
if snapshot_id is None and origin_url is None: | if snapshot_id is None and origin_url is None: | ||||
raise BadInputExc("An origin URL must be provided as query parameter.") | raise BadInputExc("An origin URL must be provided as query parameter.") | ||||
def browse_snapshot_directory( | def browse_snapshot_directory( | ||||
request, snapshot_id=None, origin_url=None, timestamp=None, path=None | request: HttpRequest, | ||||
): | snapshot_id: Optional[str] = None, | ||||
origin_url: Optional[str] = None, | |||||
timestamp: Optional[str] = None, | |||||
path: Optional[str] = None, | |||||
) -> HttpResponse: | |||||
""" | """ | ||||
Django view implementation for browsing a directory in a snapshot context. | Django view implementation for browsing a directory in a snapshot context. | ||||
""" | """ | ||||
_check_origin_url(snapshot_id, origin_url) | _check_origin_url(snapshot_id, origin_url) | ||||
visit_id = int(request.GET.get("visit_id", 0)) | visit_id = int(request.GET.get("visit_id", 0)) | ||||
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, | ||||
timestamp=timestamp, | timestamp=timestamp, | ||||
visit_id=visit_id or None, | visit_id=visit_id or None, | ||||
path=path, | path=path, | ||||
browse_context="directory", | browse_context="directory", | ||||
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"), | ||||
) | ) | ||||
root_directory = snapshot_context["root_directory"] | root_directory = snapshot_context["root_directory"] | ||||
sha1_git = root_directory | sha1_git = root_directory | ||||
error_info = { | error_info: Dict[str, Any] = { | ||||
"status_code": 200, | "status_code": 200, | ||||
"description": None, | "description": None, | ||||
} | } | ||||
if root_directory and path: | if root_directory and path: | ||||
try: | try: | ||||
dir_info = archive.lookup_directory_with_path(root_directory, path) | dir_info = archive.lookup_directory_with_path(root_directory, path) | ||||
sha1_git = dir_info["target"] | sha1_git = dir_info["target"] | ||||
except NotFoundExc as e: | except NotFoundExc as e: | ||||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | ) -> HttpResponse: | ||||
nb_dirs = None | nb_dirs = None | ||||
dir_path = None | dir_path = None | ||||
if root_directory: | if root_directory: | ||||
nb_files = len(files) | nb_files = len(files) | ||||
nb_dirs = len(dirs) | nb_dirs = len(dirs) | ||||
dir_path = "/" + path | dir_path = "/" + path | ||||
swh_objects = [] | swh_objects = [] | ||||
vault_cooking = { | vault_cooking: Dict[str, Any] = { | ||||
"directory_context": False, | "directory_context": False, | ||||
"directory_swhid": None, | "directory_swhid": None, | ||||
"revision_context": False, | "revision_context": False, | ||||
"revision_swhid": None, | "revision_swhid": None, | ||||
} | } | ||||
revision_found = False | revision_found = False | ||||
if revision_id is not None: | if revision_id is not None: | ||||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Lines | return render( | ||||
}, | }, | ||||
status=error_info["status_code"], | status=error_info["status_code"], | ||||
) | ) | ||||
PER_PAGE = 100 | PER_PAGE = 100 | ||||
def browse_snapshot_log(request, snapshot_id=None, origin_url=None, timestamp=None): | def browse_snapshot_log( | ||||
request: HttpRequest, | |||||
snapshot_id: Optional[str] = None, | |||||
origin_url: Optional[str] = None, | |||||
timestamp: Optional[str] = None, | |||||
) -> HttpResponse: | |||||
""" | """ | ||||
Django view implementation for browsing a revision history in a | Django view implementation for browsing a revision history in a | ||||
snapshot context. | snapshot context. | ||||
""" | """ | ||||
_check_origin_url(snapshot_id, origin_url) | _check_origin_url(snapshot_id, origin_url) | ||||
visit_id = int(request.GET.get("visit_id", 0)) | visit_id = int(request.GET.get("visit_id", 0)) | ||||
snapshot_context = get_snapshot_context( | snapshot_context = get_snapshot_context( | ||||
Show All 29 Lines | if len(rev_log) < offset + per_page: | ||||
revision_id, | revision_id, | ||||
max_revs=offset + per_page + 1, | max_revs=offset + per_page + 1, | ||||
state=revs_walker_state, | state=revs_walker_state, | ||||
) | ) | ||||
rev_log += [rev["id"] for rev in revs_walker] | rev_log += [rev["id"] for rev in revs_walker] | ||||
revs_walker_state = revs_walker.export_state() | revs_walker_state = revs_walker.export_state() | ||||
revs = rev_log[offset : offset + per_page] | revs = rev_log[offset : offset + per_page] | ||||
revision_log = archive.lookup_revision_multiple(revs) | revision_log = archive.lookup_revision_multiple(revs) | ||||
Not Done Inline Actionswhy can't it be None? vlorentz: why can't it be None? | |||||
Done Inline ActionsI do not recall why i added that assert but it is not needed, will remove. anlambert: I do not recall why i added that assert but it is not needed, will remove. | |||||
request.session[session_key] = { | request.session[session_key] = { | ||||
"rev_log": rev_log, | "rev_log": rev_log, | ||||
"revs_walker_state": revs_walker_state, | "revs_walker_state": revs_walker_state, | ||||
} | } | ||||
origin_info = snapshot_context["origin_info"] | origin_info = snapshot_context["origin_info"] | ||||
visit_info = snapshot_context["visit_info"] | visit_info = snapshot_context["visit_info"] | ||||
url_args = snapshot_context["url_args"] | url_args = snapshot_context["url_args"] | ||||
query_params = snapshot_context["query_params"] | query_params = snapshot_context["query_params"] | ||||
snapshot_id = snapshot_context["snapshot_id"] | snapshot_id = snapshot_context["snapshot_id"] | ||||
query_params["per_page"] = per_page | query_params["per_page"] = str(per_page) | ||||
revs_ordering = request.GET.get("revs_ordering", "") | revs_ordering = request.GET.get("revs_ordering", "") | ||||
query_params["revs_ordering"] = revs_ordering or None | if revs_ordering: | ||||
query_params["revs_ordering"] = revs_ordering | |||||
if origin_info: | if origin_info: | ||||
browse_view_name = "browse-origin-log" | browse_view_name = "browse-origin-log" | ||||
else: | else: | ||||
browse_view_name = "browse-snapshot-log" | browse_view_name = "browse-snapshot-log" | ||||
prev_log_url = None | prev_log_url = None | ||||
if len(rev_log) > offset + per_page: | if len(rev_log) > offset + per_page: | ||||
query_params["offset"] = offset + per_page | query_params["offset"] = str(offset + per_page) | ||||
prev_log_url = reverse( | prev_log_url = reverse( | ||||
browse_view_name, url_args=url_args, query_params=query_params | browse_view_name, url_args=url_args, query_params=query_params | ||||
) | ) | ||||
next_log_url = None | next_log_url = None | ||||
if offset != 0: | if offset != 0: | ||||
query_params["offset"] = offset - per_page | query_params["offset"] = str(offset - per_page) | ||||
next_log_url = reverse( | next_log_url = reverse( | ||||
browse_view_name, url_args=url_args, query_params=query_params | browse_view_name, url_args=url_args, query_params=query_params | ||||
) | ) | ||||
revision_log_data = format_log_entries(revision_log, per_page, snapshot_context) | revision_log_data = format_log_entries(revision_log, per_page, snapshot_context) | ||||
browse_rev_link = gen_revision_link(revision_id) | browse_rev_link = gen_revision_link(revision_id) | ||||
browse_log_link = gen_revision_log_link(revision_id) | browse_log_link = gen_revision_log_link(revision_id) | ||||
browse_snp_link = gen_snapshot_link(snapshot_id) | browse_snp_link = gen_snapshot_link(snapshot_id) | ||||
revision_metadata = { | revision_metadata = { | ||||
"context-independent revision": browse_rev_link, | "context-independent revision": browse_rev_link, | ||||
"context-independent revision history": browse_log_link, | "context-independent revision history": browse_log_link, | ||||
"context-independent snapshot": browse_snp_link, | "context-independent snapshot": browse_snp_link, | ||||
"snapshot": snapshot_id, | "snapshot": snapshot_id, | ||||
} | } | ||||
if origin_info: | if origin_info and visit_info: | ||||
revision_metadata["origin url"] = origin_info["url"] | revision_metadata["origin url"] = origin_info["url"] | ||||
revision_metadata["origin visit date"] = format_utc_iso_date(visit_info["date"]) | revision_metadata["origin visit date"] = format_utc_iso_date(visit_info["date"]) | ||||
revision_metadata["origin visit type"] = visit_info["type"] | revision_metadata["origin visit type"] = visit_info["type"] | ||||
swh_objects = [ | swh_objects = [ | ||||
SWHObjectInfo(object_type=ObjectType.REVISION, object_id=revision_id), | SWHObjectInfo(object_type=ObjectType.REVISION, object_id=revision_id), | ||||
SWHObjectInfo(object_type=ObjectType.SNAPSHOT, object_id=snapshot_id), | SWHObjectInfo(object_type=ObjectType.SNAPSHOT, object_id=snapshot_id), | ||||
] | ] | ||||
Show All 31 Lines | return render( | ||||
"vault_cooking": None, | "vault_cooking": None, | ||||
"show_actions": True, | "show_actions": True, | ||||
"swhids_info": swhids_info, | "swhids_info": swhids_info, | ||||
}, | }, | ||||
) | ) | ||||
def browse_snapshot_branches( | def browse_snapshot_branches( | ||||
request, snapshot_id=None, origin_url=None, timestamp=None, branch_name_include=None | request: HttpRequest, | ||||
): | snapshot_id: Optional[str] = None, | ||||
origin_url: Optional[str] = None, | |||||
timestamp: Optional[str] = None, | |||||
branch_name_include: Optional[str] = None, | |||||
) -> HttpResponse: | |||||
""" | """ | ||||
Django view implementation for browsing a list of branches in a snapshot | Django view implementation for browsing a list of branches in a snapshot | ||||
context. | context. | ||||
""" | """ | ||||
_check_origin_url(snapshot_id, origin_url) | _check_origin_url(snapshot_id, origin_url) | ||||
visit_id = int(request.GET.get("visit_id", 0)) | visit_id = int(request.GET.get("visit_id", 0)) | ||||
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, | ||||
timestamp=timestamp, | timestamp=timestamp, | ||||
visit_id=visit_id or None, | visit_id=visit_id or None, | ||||
) | ) | ||||
branches_bc = request.GET.get("branches_breadcrumbs", "") | branches_bc_str = request.GET.get("branches_breadcrumbs", "") | ||||
branches_bc = branches_bc.split(",") if branches_bc else [] | branches_bc = branches_bc_str.split(",") if branches_bc_str else [] | ||||
branches_from = branches_bc[-1] if branches_bc else "" | branches_from = branches_bc[-1] if branches_bc else "" | ||||
origin_info = snapshot_context["origin_info"] | origin_info = snapshot_context["origin_info"] | ||||
url_args = snapshot_context["url_args"] | url_args = snapshot_context["url_args"] | ||||
query_params = snapshot_context["query_params"] | query_params = snapshot_context["query_params"] | ||||
if origin_info: | if origin_info: | ||||
browse_view_name = "browse-origin-directory" | browse_view_name = "browse-origin-directory" | ||||
else: | else: | ||||
browse_view_name = "browse-snapshot-directory" | browse_view_name = "browse-snapshot-directory" | ||||
snapshot = archive.lookup_snapshot( | snapshot = archive.lookup_snapshot( | ||||
snapshot_context["snapshot_id"], | snapshot_context["snapshot_id"], | ||||
branches_from, | branches_from, | ||||
PER_PAGE + 1, | PER_PAGE + 1, | ||||
target_types=["revision", "alias"], | target_types=["revision", "alias"], | ||||
branch_name_include_substring=branch_name_include, | branch_name_include_substring=branch_name_include, | ||||
) | ) | ||||
displayed_branches = [] | displayed_branches: List[Dict[str, Any]] = [] | ||||
if snapshot: | if snapshot: | ||||
displayed_branches, _, _ = process_snapshot_branches(snapshot) | branches, _, _ = process_snapshot_branches(snapshot) | ||||
displayed_branches = [dict(branch) for branch in branches] | |||||
for branch in displayed_branches: | for branch in displayed_branches: | ||||
rev_query_params = {} | rev_query_params = {} | ||||
if origin_info: | if origin_info: | ||||
rev_query_params["origin_url"] = origin_info["url"] | rev_query_params["origin_url"] = origin_info["url"] | ||||
revision_url = reverse( | revision_url = reverse( | ||||
"browse-revision", | "browse-revision", | ||||
▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Lines | return render( | ||||
"next_branches_url": next_branches_url, | "next_branches_url": next_branches_url, | ||||
"snapshot_context": snapshot_context, | "snapshot_context": snapshot_context, | ||||
"search_string": branch_name_include or "", | "search_string": branch_name_include or "", | ||||
}, | }, | ||||
) | ) | ||||
def browse_snapshot_releases( | def browse_snapshot_releases( | ||||
request, | request: HttpRequest, | ||||
snapshot_id=None, | snapshot_id: Optional[str] = None, | ||||
origin_url=None, | origin_url: Optional[str] = None, | ||||
timestamp=None, | timestamp: Optional[str] = None, | ||||
release_name_include=None, | release_name_include: Optional[str] = None, | ||||
): | ): | ||||
""" | """ | ||||
Django view implementation for browsing a list of releases in a snapshot | Django view implementation for browsing a list of releases in a snapshot | ||||
context. | context. | ||||
""" | """ | ||||
_check_origin_url(snapshot_id, origin_url) | _check_origin_url(snapshot_id, origin_url) | ||||
visit_id = int(request.GET.get("visit_id", 0)) | visit_id = int(request.GET.get("visit_id", 0)) | ||||
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, | ||||
timestamp=timestamp, | timestamp=timestamp, | ||||
visit_id=visit_id or None, | visit_id=visit_id or None, | ||||
) | ) | ||||
rel_bc = request.GET.get("releases_breadcrumbs", "") | rel_bc_str = request.GET.get("releases_breadcrumbs", "") | ||||
rel_bc = rel_bc.split(",") if rel_bc else [] | rel_bc = rel_bc_str.split(",") if rel_bc_str else [] | ||||
rel_from = rel_bc[-1] if rel_bc else "" | rel_from = rel_bc[-1] if rel_bc else "" | ||||
origin_info = snapshot_context["origin_info"] | origin_info = snapshot_context["origin_info"] | ||||
url_args = snapshot_context["url_args"] | url_args = snapshot_context["url_args"] | ||||
query_params = snapshot_context["query_params"] | query_params = snapshot_context["query_params"] | ||||
snapshot = archive.lookup_snapshot( | snapshot = archive.lookup_snapshot( | ||||
snapshot_context["snapshot_id"], | snapshot_context["snapshot_id"], | ||||
rel_from, | rel_from, | ||||
PER_PAGE + 1, | PER_PAGE + 1, | ||||
target_types=["release", "alias"], | target_types=["release", "alias"], | ||||
branch_name_include_substring=release_name_include, | branch_name_include_substring=release_name_include, | ||||
) | ) | ||||
displayed_releases = [] | displayed_releases: List[Dict[str, Any]] = [] | ||||
if snapshot: | if snapshot: | ||||
_, displayed_releases, _ = process_snapshot_branches(snapshot) | _, releases, _ = process_snapshot_branches(snapshot) | ||||
displayed_releases = [dict(release) for release in releases] | |||||
for release in displayed_releases: | for release in displayed_releases: | ||||
query_params_tgt = {"snapshot": snapshot_id, "release": release["name"]} | query_params_tgt = {"snapshot": snapshot_id, "release": release["name"]} | ||||
if origin_info: | if origin_info: | ||||
query_params_tgt["origin_url"] = origin_info["url"] | query_params_tgt["origin_url"] = origin_info["url"] | ||||
release_url = reverse( | release_url = reverse( | ||||
"browse-release", | "browse-release", | ||||
▲ Show 20 Lines • Show All 97 Lines • Show Last 20 Lines |
why can't it be None?