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-2020 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 | ||||
import json | import json | ||||
from distutils.util import strtobool | from distutils.util import strtobool | ||||
from django.http import HttpResponse | from django.http import HttpResponse | ||||
from django.shortcuts import render | from django.shortcuts import render | ||||
from django.template.defaultfilters import filesizeformat | from django.template.defaultfilters import filesizeformat | ||||
import sentry_sdk | import sentry_sdk | ||||
from swh.model.hashutil import hash_to_hex | from swh.model.hashutil import hash_to_hex | ||||
from swh.model.identifiers import CONTENT | |||||
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 ( | ||||
request_content, | request_content, | ||||
prepare_content_for_display, | prepare_content_for_display, | ||||
content_display_max_size, | content_display_max_size, | ||||
get_swh_persistent_ids, | get_swh_persistent_ids, | ||||
gen_link, | gen_link, | ||||
gen_directory_link, | gen_directory_link, | ||||
) | ) | ||||
from swh.web.common import query, service, highlightjs | from swh.web.common import query, service, highlightjs | ||||
from swh.web.common.exc import NotFoundExc, handle_view_exception | from swh.web.common.exc import NotFoundExc, handle_view_exception | ||||
from swh.web.common.typing import ContentMetadata | |||||
from swh.web.common.utils import reverse, gen_path_info, swh_object_icons | from swh.web.common.utils import reverse, gen_path_info, swh_object_icons | ||||
@browse_route( | @browse_route( | ||||
r"content/(?P<query_string>[0-9a-z_:]*[0-9a-f]+.)/raw/", | r"content/(?P<query_string>[0-9a-z_:]*[0-9a-f]+.)/raw/", | ||||
view_name="browse-content-raw", | view_name="browse-content-raw", | ||||
checksum_args=["query_string"], | checksum_args=["query_string"], | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 248 Lines • ▼ Show 20 Lines | def content_display(request, query_string): | ||||
elif root_dir != path: | elif root_dir != path: | ||||
directory_id = root_dir | directory_id = root_dir | ||||
if directory_id: | if directory_id: | ||||
directory_url = gen_directory_link(directory_id) | directory_url = gen_directory_link(directory_id) | ||||
query_params = {"filename": filename} | query_params = {"filename": filename} | ||||
content_checksums = content_data["checksums"] | |||||
content_url = reverse( | |||||
"browse-content", | |||||
url_args={"query_string": f'sha1_git:{content_checksums["sha1_git"]}'}, | |||||
) | |||||
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 = { | content_metadata = ContentMetadata( | ||||
"sha1": content_data["checksums"]["sha1"], | object_type=CONTENT, | ||||
"sha1_git": content_data["checksums"]["sha1_git"], | sha1=content_checksums["sha1"], | ||||
"sha256": content_data["checksums"]["sha256"], | sha1_git=content_checksums["sha1_git"], | ||||
"blake2s256": content_data["checksums"]["blake2s256"], | sha256=content_checksums["sha256"], | ||||
"mimetype": content_data["mimetype"], | blake2s256=content_checksums["blake2s256"], | ||||
"encoding": content_data["encoding"], | content_url=content_url, | ||||
"size": filesizeformat(content_data["length"]), | mimetype=content_data["mimetype"], | ||||
"language": content_data["language"], | encoding=content_data["encoding"], | ||||
"licenses": content_data["licenses"], | size=filesizeformat(content_data["length"]), | ||||
"filename": filename, | language=content_data["language"], | ||||
"directory": directory_id, | licenses=content_data["licenses"], | ||||
"context-independent directory": directory_url, | path=path, | ||||
} | filename=filename, | ||||
directory=directory_id, | |||||
if filename: | directory_url=directory_url, | ||||
content_metadata["filename"] = filename | revision=None, | ||||
release=None, | |||||
snapshot=None, | |||||
origin_url=origin_url, | |||||
) | |||||
sha1_git = content_data["checksums"]["sha1_git"] | swh_ids = get_swh_persistent_ids( | ||||
swh_ids = get_swh_persistent_ids([{"type": "content", "id": sha1_git}]) | [{"type": "content", "id": content_checksums["sha1_git"]}] | ||||
) | |||||
heading = "Content - %s" % sha1_git | heading = "Content - %s" % content_checksums["sha1_git"] | ||||
if breadcrumbs: | if breadcrumbs: | ||||
content_path = "/".join([bc["name"] for bc in breadcrumbs]) | content_path = "/".join([bc["name"] for bc in breadcrumbs]) | ||||
heading += " - %s" % content_path | heading += " - %s" % content_path | ||||
return render( | return render( | ||||
request, | request, | ||||
"browse/content.html", | "browse/content.html", | ||||
{ | { | ||||
Show All 26 Lines |