Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/views/revision.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 hashlib | import hashlib | ||||
import json | import json | ||||
import textwrap | import textwrap | ||||
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 | ||||
from django.utils.safestring import mark_safe | from django.utils.safestring import mark_safe | ||||
from swh.model.identifiers import persistent_identifier, REVISION | from swh.model.identifiers import ( | ||||
persistent_identifier, | |||||
CONTENT, | |||||
DIRECTORY, | |||||
REVISION, | |||||
SNAPSHOT, | |||||
) | |||||
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 ( | ||||
gen_link, | gen_link, | ||||
gen_revision_link, | gen_revision_link, | ||||
gen_revision_url, | gen_revision_url, | ||||
get_revision_log_url, | get_revision_log_url, | ||||
get_directory_entries, | get_directory_entries, | ||||
gen_directory_link, | gen_directory_link, | ||||
request_content, | request_content, | ||||
prepare_content_for_display, | prepare_content_for_display, | ||||
content_display_max_size, | content_display_max_size, | ||||
gen_snapshot_link, | gen_snapshot_link, | ||||
get_readme_to_display, | get_readme_to_display, | ||||
format_log_entries, | format_log_entries, | ||||
gen_person_mail_link, | gen_person_mail_link, | ||||
) | ) | ||||
from swh.web.common import service | from swh.web.common import service | ||||
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 RevisionMetadata | from swh.web.common.typing import RevisionMetadata, SWHObjectInfo | ||||
from swh.web.common.utils import ( | from swh.web.common.utils import ( | ||||
reverse, | reverse, | ||||
format_utc_iso_date, | format_utc_iso_date, | ||||
gen_path_info, | gen_path_info, | ||||
swh_object_icons, | swh_object_icons, | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 231 Lines • ▼ Show 20 Lines | return render( | ||||
"revs_ordering": revs_ordering, | "revs_ordering": revs_ordering, | ||||
"next_log_url": next_log_url, | "next_log_url": next_log_url, | ||||
"prev_log_url": prev_log_url, | "prev_log_url": prev_log_url, | ||||
"breadcrumbs": None, | "breadcrumbs": None, | ||||
"top_right_link": None, | "top_right_link": None, | ||||
"snapshot_context": None, | "snapshot_context": None, | ||||
"vault_cooking": None, | "vault_cooking": None, | ||||
"show_actions_menu": True, | "show_actions_menu": True, | ||||
"swh_ids": None, | "swhids_info": None, | ||||
}, | }, | ||||
) | ) | ||||
@browse_route( | @browse_route( | ||||
r"revision/(?P<sha1_git>[0-9a-f]+)/", | r"revision/(?P<sha1_git>[0-9a-f]+)/", | ||||
view_name="browse-revision", | view_name="browse-revision", | ||||
checksum_args=["sha1_git"], | checksum_args=["sha1_git"], | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | try: | ||||
if dir_id: | if dir_id: | ||||
path = "" if path is None else (path + "/") | path = "" if path is None else (path + "/") | ||||
dirs, files = get_directory_entries(dir_id) | dirs, files = get_directory_entries(dir_id) | ||||
except Exception as exc: | except Exception as exc: | ||||
return handle_view_exception(request, exc) | return handle_view_exception(request, exc) | ||||
revision_metadata = RevisionMetadata( | revision_metadata = RevisionMetadata( | ||||
object_type=REVISION, | object_type=REVISION, | ||||
object_id=sha1_git, | |||||
revision=sha1_git, | revision=sha1_git, | ||||
revision_url=gen_revision_link(sha1_git), | revision_url=gen_revision_link(sha1_git), | ||||
author=revision["author"]["fullname"] if revision["author"] else "None", | author=revision["author"]["fullname"] if revision["author"] else "None", | ||||
author_url=gen_person_mail_link(revision["author"]) | author_url=gen_person_mail_link(revision["author"]) | ||||
if revision["author"] | if revision["author"] | ||||
else "None", | else "None", | ||||
committer=revision["committer"]["fullname"] | committer=revision["committer"]["fullname"] | ||||
if revision["committer"] | if revision["committer"] | ||||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | def revision_browse(request, sha1_git): | ||||
vault_cooking = { | vault_cooking = { | ||||
"directory_context": False, | "directory_context": False, | ||||
"directory_id": None, | "directory_id": None, | ||||
"revision_context": True, | "revision_context": True, | ||||
"revision_id": sha1_git, | "revision_id": sha1_git, | ||||
} | } | ||||
swh_objects = [{"type": "revision", "id": sha1_git}] | swh_objects = [SWHObjectInfo(object_type=REVISION, object_id=sha1_git)] | ||||
content = None | content = None | ||||
content_size = None | content_size = None | ||||
filename = None | filename = None | ||||
mimetype = None | mimetype = None | ||||
language = None | language = None | ||||
readme_name = None | readme_name = None | ||||
readme_url = None | readme_url = None | ||||
readme_html = None | readme_html = None | ||||
readmes = {} | readmes = {} | ||||
error_code = 200 | error_code = 200 | ||||
error_message = "" | error_message = "" | ||||
error_description = "" | error_description = "" | ||||
extra_context = dict(revision_metadata) | |||||
if path: | |||||
extra_context["path"] = f"/{path}" | |||||
if content_data: | if content_data: | ||||
breadcrumbs[-1]["url"] = None | breadcrumbs[-1]["url"] = None | ||||
content_size = content_data["length"] | content_size = content_data["length"] | ||||
mimetype = content_data["mimetype"] | mimetype = content_data["mimetype"] | ||||
if content_data["raw_data"]: | if content_data["raw_data"]: | ||||
content_display_data = prepare_content_for_display( | content_display_data = prepare_content_for_display( | ||||
content_data["raw_data"], content_data["mimetype"], path | content_data["raw_data"], content_data["mimetype"], path | ||||
) | ) | ||||
content = content_display_data["content_data"] | content = content_display_data["content_data"] | ||||
language = content_display_data["language"] | language = content_display_data["language"] | ||||
mimetype = content_display_data["mimetype"] | mimetype = content_display_data["mimetype"] | ||||
query_params = {} | query_params = {} | ||||
if path: | if path: | ||||
filename = path_info[-1]["name"] | filename = path_info[-1]["name"] | ||||
query_params["filename"] = filename | query_params["filename"] = filename | ||||
extra_context["filename"] = filename | |||||
top_right_link = { | top_right_link = { | ||||
"url": reverse( | "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, | ||||
), | ), | ||||
"icon": swh_object_icons["content"], | "icon": swh_object_icons["content"], | ||||
"text": "Raw File", | "text": "Raw File", | ||||
} | } | ||||
swh_objects.append({"type": "content", "id": file_info["target"]}) | swh_objects.append( | ||||
SWHObjectInfo(object_type=CONTENT, object_id=file_info["target"]) | |||||
) | |||||
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"] | ||||
else: | else: | ||||
for d in dirs: | for d in dirs: | ||||
if d["type"] == "rev": | if d["type"] == "rev": | ||||
d["url"] = reverse( | d["url"] = reverse( | ||||
Show All 24 Lines | else: | ||||
"url": get_revision_log_url(sha1_git, snapshot_context), | "url": get_revision_log_url(sha1_git, snapshot_context), | ||||
"icon": swh_object_icons["revisions history"], | "icon": swh_object_icons["revisions history"], | ||||
"text": "History", | "text": "History", | ||||
} | } | ||||
vault_cooking["directory_context"] = True | vault_cooking["directory_context"] = True | ||||
vault_cooking["directory_id"] = dir_id | vault_cooking["directory_id"] = dir_id | ||||
swh_objects.append({"type": "directory", "id": dir_id}) | swh_objects.append(SWHObjectInfo(object_type=DIRECTORY, object_id=dir_id)) | ||||
diff_revision_url = reverse( | diff_revision_url = reverse( | ||||
"diff-revision", | "diff-revision", | ||||
url_args={"sha1_git": sha1_git}, | url_args={"sha1_git": sha1_git}, | ||||
query_params={ | query_params={ | ||||
"origin_url": origin_url, | "origin_url": origin_url, | ||||
"timestamp": timestamp, | "timestamp": timestamp, | ||||
"visit_id": visit_id, | "visit_id": visit_id, | ||||
}, | }, | ||||
) | ) | ||||
if snapshot_id: | if snapshot_id: | ||||
swh_objects.append({"type": "snapshot", "id": snapshot_id}) | swh_objects.append(SWHObjectInfo(object_type=SNAPSHOT, object_id=snapshot_id)) | ||||
swh_ids = get_swh_persistent_ids(swh_objects, snapshot_context) | swhids_info = get_swhids_info(swh_objects, snapshot_context, extra_context) | ||||
heading = "Revision - %s - %s" % ( | heading = "Revision - %s - %s" % ( | ||||
sha1_git[:7], | sha1_git[:7], | ||||
textwrap.shorten(message_lines[0], width=70), | textwrap.shorten(message_lines[0], width=70), | ||||
) | ) | ||||
if snapshot_context: | if snapshot_context: | ||||
context_found = "snapshot: %s" % snapshot_context["snapshot_id"] | context_found = "snapshot: %s" % snapshot_context["snapshot_id"] | ||||
if origin_info: | if origin_info: | ||||
context_found = "origin: %s" % origin_info["url"] | context_found = "origin: %s" % origin_info["url"] | ||||
heading += " - %s" % context_found | heading += " - %s" % context_found | ||||
return render( | return render( | ||||
request, | request, | ||||
"browse/revision.html", | "browse/revision.html", | ||||
{ | { | ||||
"heading": heading, | "heading": heading, | ||||
"swh_object_id": swh_ids[0]["swh_id"], | "swh_object_id": swhids_info[0]["swhid"], | ||||
"swh_object_name": "Revision", | "swh_object_name": "Revision", | ||||
"swh_object_metadata": revision_metadata, | "swh_object_metadata": revision_metadata, | ||||
"message_header": message_lines[0], | "message_header": message_lines[0], | ||||
"message_body": "\n".join(message_lines[1:]), | "message_body": "\n".join(message_lines[1:]), | ||||
"parents": parents, | "parents": parents, | ||||
"snapshot_context": snapshot_context, | "snapshot_context": snapshot_context, | ||||
"dirs": dirs, | "dirs": dirs, | ||||
"files": files, | "files": files, | ||||
"content": content, | "content": content, | ||||
"content_size": content_size, | "content_size": content_size, | ||||
"max_content_size": content_display_max_size, | "max_content_size": content_display_max_size, | ||||
"filename": filename, | "filename": filename, | ||||
"encoding": content_data.get("encoding"), | "encoding": content_data.get("encoding"), | ||||
"mimetype": mimetype, | "mimetype": mimetype, | ||||
"language": language, | "language": language, | ||||
"readme_name": readme_name, | "readme_name": readme_name, | ||||
"readme_url": readme_url, | "readme_url": readme_url, | ||||
"readme_html": readme_html, | "readme_html": readme_html, | ||||
"breadcrumbs": breadcrumbs, | "breadcrumbs": breadcrumbs, | ||||
"top_right_link": top_right_link, | "top_right_link": top_right_link, | ||||
"vault_cooking": vault_cooking, | "vault_cooking": vault_cooking, | ||||
"diff_revision_url": diff_revision_url, | "diff_revision_url": diff_revision_url, | ||||
"show_actions_menu": True, | "show_actions_menu": True, | ||||
"swh_ids": swh_ids, | "swhids_info": swhids_info, | ||||
"error_code": error_code, | "error_code": error_code, | ||||
"error_message": error_message, | "error_message": error_message, | ||||
"error_description": error_description, | "error_description": error_description, | ||||
}, | }, | ||||
status=error_code, | status=error_code, | ||||
) | ) |