Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/utils.py
Show All 11 Lines | |||||
from django.core.cache import cache | from django.core.cache import cache | ||||
from django.utils.safestring import mark_safe | from django.utils.safestring import mark_safe | ||||
from django.utils.html import escape | from django.utils.html import escape | ||||
import sentry_sdk | import sentry_sdk | ||||
from swh.web.common import highlightjs, service | from swh.web.common import highlightjs, service | ||||
from swh.web.common.exc import http_status_code_message | from swh.web.common.exc import http_status_code_message | ||||
from swh.web.common.identifiers import get_swh_persistent_id | |||||
from swh.web.common.utils import ( | from swh.web.common.utils import ( | ||||
reverse, | reverse, | ||||
format_utc_iso_date, | format_utc_iso_date, | ||||
swh_object_icons, | |||||
rst_to_html, | rst_to_html, | ||||
) | ) | ||||
from swh.web.config import get_config | from swh.web.config import get_config | ||||
def get_directory_entries(sha1_git): | def get_directory_entries(sha1_git): | ||||
"""Function that retrieves the content of a directory | """Function that retrieves the content of a directory | ||||
from the archive. | from the archive. | ||||
▲ Show 20 Lines • Show All 720 Lines • ▼ Show 20 Lines | if readme_name and readme_name.endswith(".rst"): | ||||
rst_doc = request_content(readme_sha1) | rst_doc = request_content(readme_sha1) | ||||
readme_html = rst_to_html(rst_doc["raw_data"]) | readme_html = rst_to_html(rst_doc["raw_data"]) | ||||
cache.set(cache_entry_id, readme_html) | cache.set(cache_entry_id, readme_html) | ||||
except Exception as exc: | except Exception as exc: | ||||
sentry_sdk.capture_exception(exc) | sentry_sdk.capture_exception(exc) | ||||
readme_html = "Readme bytes are not available" | readme_html = "Readme bytes are not available" | ||||
return readme_name, readme_url, readme_html | return readme_name, readme_url, readme_html | ||||
def get_swh_persistent_ids(swh_objects, snapshot_context=None): | |||||
""" | |||||
Returns a list of dict containing info related to persistent | |||||
identifiers of swh objects. | |||||
Args: | |||||
swh_objects (list): a list of dict with the following keys: | |||||
* type: swh object type | |||||
(content/directory/release/revision/snapshot) | |||||
* id: swh object id | |||||
snapshot_context (dict): optional parameter describing the snapshot in | |||||
which the object has been found | |||||
Returns: | |||||
list: a list of dict with the following keys: | |||||
* object_type: the swh object type | |||||
(content/directory/release/revision/snapshot) | |||||
* object_icon: the swh object icon to use in HTML views | |||||
* swh_id: the computed swh object persistent identifier | |||||
* swh_id_url: the url resolving the persistent identifier | |||||
* show_options: boolean indicating if the persistent id options | |||||
must be displayed in persistent ids HTML view | |||||
""" | |||||
swh_ids = [] | |||||
for swh_object in swh_objects: | |||||
if not swh_object["id"]: | |||||
continue | |||||
swh_id = get_swh_persistent_id(swh_object["type"], swh_object["id"]) | |||||
show_options = swh_object["type"] == "content" or ( | |||||
snapshot_context and snapshot_context["origin_info"] is not None | |||||
) | |||||
object_icon = swh_object_icons[swh_object["type"]] | |||||
swh_ids.append( | |||||
{ | |||||
"object_type": swh_object["type"], | |||||
"object_id": swh_object["id"], | |||||
"object_icon": object_icon, | |||||
"swh_id": swh_id, | |||||
"swh_id_url": reverse("browse-swh-id", url_args={"swh_id": swh_id}), | |||||
"show_options": show_options, | |||||
} | |||||
) | |||||
return swh_ids |