Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/views/content.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2021 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 | ||||
import difflib | import difflib | ||||
from distutils.util import strtobool | from distutils.util import strtobool | ||||
import sentry_sdk | import sentry_sdk | ||||
from django.http import HttpResponse, JsonResponse | from django.http import HttpResponse, JsonResponse | ||||
from django.shortcuts import render | from django.shortcuts import render | ||||
from swh.model.hashutil import hash_to_hex | from swh.model.hashutil import hash_to_hex | ||||
from swh.model.identifiers import CONTENT, DIRECTORY, RELEASE, REVISION, SNAPSHOT | from swh.model.identifiers 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 ( | from swh.web.browse.utils import ( | ||||
content_display_max_size, | content_display_max_size, | ||||
gen_link, | gen_link, | ||||
prepare_content_for_display, | prepare_content_for_display, | ||||
request_content, | request_content, | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 179 Lines • ▼ Show 20 Lines | if origin_url is not None or snapshot_id is not None: | ||||
try: | try: | ||||
snapshot_context = get_snapshot_context( | snapshot_context = get_snapshot_context( | ||||
origin_url=origin_url, | origin_url=origin_url, | ||||
snapshot_id=snapshot_id, | snapshot_id=snapshot_id, | ||||
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, | ||||
browse_context=CONTENT, | browse_context="content", | ||||
) | ) | ||||
except NotFoundExc as e: | except NotFoundExc as e: | ||||
if str(e).startswith("Origin"): | if str(e).startswith("Origin"): | ||||
raw_cnt_url = reverse( | raw_cnt_url = reverse( | ||||
"browse-content", url_args={"query_string": query_string} | "browse-content", url_args={"query_string": query_string} | ||||
) | ) | ||||
error_message = ( | error_message = ( | ||||
"The Software Heritage archive has a content " | "The Software Heritage archive has a content " | ||||
▲ Show 20 Lines • Show All 80 Lines • ▼ Show 20 Lines | def content_display(request, query_string): | ||||
content_raw_url = reverse( | content_raw_url = reverse( | ||||
"browse-content-raw", | "browse-content-raw", | ||||
url_args={"query_string": query_string}, | url_args={"query_string": query_string}, | ||||
query_params=query_params, | query_params=query_params, | ||||
) | ) | ||||
content_metadata = ContentMetadata( | content_metadata = ContentMetadata( | ||||
object_type=CONTENT, | object_type=ObjectType.CONTENT, | ||||
object_id=content_checksums.get("sha1_git"), | object_id=content_checksums.get("sha1_git"), | ||||
sha1=content_checksums.get("sha1"), | sha1=content_checksums.get("sha1"), | ||||
sha1_git=content_checksums.get("sha1_git"), | sha1_git=content_checksums.get("sha1_git"), | ||||
sha256=content_checksums.get("sha256"), | sha256=content_checksums.get("sha256"), | ||||
blake2s256=content_checksums.get("blake2s256"), | blake2s256=content_checksums.get("blake2s256"), | ||||
content_url=content_url, | content_url=content_url, | ||||
mimetype=content_data.get("mimetype"), | mimetype=content_data.get("mimetype"), | ||||
encoding=content_data.get("encoding"), | encoding=content_data.get("encoding"), | ||||
size=content_data.get("length", 0), | size=content_data.get("length", 0), | ||||
language=content_data.get("language"), | language=content_data.get("language"), | ||||
root_directory=root_dir, | root_directory=root_dir, | ||||
path=f"/{path}" if path else None, | path=f"/{path}" if path else None, | ||||
filename=filename or "", | filename=filename or "", | ||||
directory=directory_id, | directory=directory_id, | ||||
revision=None, | revision=None, | ||||
release=None, | release=None, | ||||
snapshot=None, | snapshot=None, | ||||
origin_url=origin_url, | origin_url=origin_url, | ||||
) | ) | ||||
swh_objects = [ | swh_objects = [ | ||||
SWHObjectInfo(object_type=CONTENT, object_id=content_checksums.get("sha1_git")) | SWHObjectInfo( | ||||
object_type=ObjectType.CONTENT, object_id=content_checksums.get("sha1_git") | |||||
) | |||||
] | ] | ||||
if directory_id: | if directory_id: | ||||
swh_objects.append(SWHObjectInfo(object_type=DIRECTORY, object_id=directory_id)) | swh_objects.append( | ||||
SWHObjectInfo(object_type=ObjectType.DIRECTORY, object_id=directory_id) | |||||
) | |||||
if snapshot_context: | if snapshot_context: | ||||
swh_objects.append( | swh_objects.append( | ||||
SWHObjectInfo( | SWHObjectInfo( | ||||
object_type=REVISION, object_id=snapshot_context["revision_id"] | object_type=ObjectType.REVISION, | ||||
object_id=snapshot_context["revision_id"], | |||||
) | ) | ||||
) | ) | ||||
swh_objects.append( | swh_objects.append( | ||||
SWHObjectInfo( | SWHObjectInfo( | ||||
object_type=SNAPSHOT, object_id=snapshot_context["snapshot_id"] | object_type=ObjectType.SNAPSHOT, | ||||
object_id=snapshot_context["snapshot_id"], | |||||
) | ) | ||||
) | ) | ||||
if snapshot_context["release_id"]: | if snapshot_context["release_id"]: | ||||
swh_objects.append( | swh_objects.append( | ||||
SWHObjectInfo( | SWHObjectInfo( | ||||
object_type=RELEASE, object_id=snapshot_context["release_id"] | object_type=ObjectType.RELEASE, | ||||
object_id=snapshot_context["release_id"], | |||||
) | ) | ||||
) | ) | ||||
swhids_info = get_swhids_info( | swhids_info = get_swhids_info( | ||||
swh_objects, snapshot_context, extra_context=content_metadata, | swh_objects, snapshot_context, extra_context=content_metadata, | ||||
) | ) | ||||
heading = "Content - %s" % content_checksums.get("sha1_git") | heading = "Content - %s" % content_checksums.get("sha1_git") | ||||
Show All 36 Lines |