Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/views/content.py
Show All 21 Lines | 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, | ||||
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.identifiers import get_swh_persistent_ids | from swh.web.common.identifiers import get_swhids_info | ||||
from swh.web.common.typing import ContentMetadata | from swh.web.common.typing import ContentMetadata, SWHObjectInfo | ||||
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 242 Lines • ▼ Show 20 Lines | def content_display(request, query_string): | ||||
if path and root_dir != path: | if path and root_dir != path: | ||||
try: | try: | ||||
dir_info = service.lookup_directory_with_path(root_dir, path) | dir_info = service.lookup_directory_with_path(root_dir, path) | ||||
directory_id = dir_info["target"] | directory_id = dir_info["target"] | ||||
except Exception as exc: | except Exception as exc: | ||||
return handle_view_exception(request, exc) | return handle_view_exception(request, exc) | ||||
elif root_dir != path: | elif root_dir != path: | ||||
directory_id = root_dir | directory_id = root_dir | ||||
else: | |||||
root_dir = None | |||||
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_checksums = content_data["checksums"] | ||||
content_url = reverse( | content_url = reverse( | ||||
"browse-content", | "browse-content", | ||||
url_args={"query_string": f'sha1_git:{content_checksums["sha1_git"]}'}, | 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 = ContentMetadata( | content_metadata = ContentMetadata( | ||||
object_type=CONTENT, | object_type=CONTENT, | ||||
object_id=content_checksums["sha1_git"], | |||||
sha1=content_checksums["sha1"], | sha1=content_checksums["sha1"], | ||||
sha1_git=content_checksums["sha1_git"], | sha1_git=content_checksums["sha1_git"], | ||||
sha256=content_checksums["sha256"], | sha256=content_checksums["sha256"], | ||||
blake2s256=content_checksums["blake2s256"], | blake2s256=content_checksums["blake2s256"], | ||||
content_url=content_url, | content_url=content_url, | ||||
mimetype=content_data["mimetype"], | mimetype=content_data["mimetype"], | ||||
encoding=content_data["encoding"], | encoding=content_data["encoding"], | ||||
size=filesizeformat(content_data["length"]), | size=filesizeformat(content_data["length"]), | ||||
language=content_data["language"], | language=content_data["language"], | ||||
licenses=content_data["licenses"], | licenses=content_data["licenses"], | ||||
path=path, | root_directory=root_dir, | ||||
filename=filename, | path=f"/{path}" if path else "", | ||||
filename=filename or "", | |||||
directory=directory_id, | directory=directory_id, | ||||
directory_url=directory_url, | directory_url=directory_url, | ||||
revision=None, | revision=None, | ||||
release=None, | release=None, | ||||
snapshot=None, | snapshot=None, | ||||
origin_url=origin_url, | origin_url=origin_url, | ||||
) | ) | ||||
swh_ids = get_swh_persistent_ids( | swhids_info = get_swhids_info( | ||||
[{"type": "content", "id": content_checksums["sha1_git"]}] | [SWHObjectInfo(object_type=CONTENT, object_id=content_checksums["sha1_git"])], | ||||
extra_context=content_metadata, | |||||
) | ) | ||||
heading = "Content - %s" % content_checksums["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", | ||||
{ | { | ||||
"heading": heading, | "heading": heading, | ||||
"swh_object_id": swh_ids[0]["swh_id"], | "swh_object_id": swhids_info[0]["swhid"], | ||||
"swh_object_name": "Content", | "swh_object_name": "Content", | ||||
"swh_object_metadata": content_metadata, | "swh_object_metadata": content_metadata, | ||||
"content": content, | "content": content, | ||||
"content_size": content_data["length"], | "content_size": content_data["length"], | ||||
"max_content_size": content_display_max_size, | "max_content_size": content_display_max_size, | ||||
"filename": filename, | "filename": filename, | ||||
"encoding": content_data["encoding"], | "encoding": content_data["encoding"], | ||||
"mimetype": mimetype, | "mimetype": mimetype, | ||||
"language": language, | "language": language, | ||||
"available_languages": available_languages, | "available_languages": available_languages, | ||||
"breadcrumbs": breadcrumbs, | "breadcrumbs": breadcrumbs, | ||||
"top_right_link": { | "top_right_link": { | ||||
"url": content_raw_url, | "url": content_raw_url, | ||||
"icon": swh_object_icons["content"], | "icon": swh_object_icons["content"], | ||||
"text": "Raw File", | "text": "Raw File", | ||||
}, | }, | ||||
"snapshot_context": snapshot_context, | "snapshot_context": snapshot_context, | ||||
"vault_cooking": None, | "vault_cooking": None, | ||||
"show_actions_menu": True, | "show_actions_menu": True, | ||||
"swh_ids": swh_ids, | "swhids_info": swhids_info, | ||||
"error_code": content_data["error_code"], | "error_code": content_data["error_code"], | ||||
"error_message": content_data["error_message"], | "error_message": content_data["error_message"], | ||||
"error_description": content_data["error_description"], | "error_description": content_data["error_description"], | ||||
}, | }, | ||||
status=content_data["error_code"], | status=content_data["error_code"], | ||||
) | ) |