Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/utils.py
# Copyright (C) 2015-2020 The Software Heritage developers | # Copyright (C) 2015-2022 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 | ||||
from typing import Any, Dict, List, Optional, Tuple | from typing import Any, Dict, List, Optional, Tuple, Union | ||||
from django.http import HttpRequest | from django.http import HttpRequest | ||||
from swh.web.common.query import parse_hash | from swh.web.common.query import parse_hash | ||||
from swh.web.common.typing import OriginInfo | |||||
from swh.web.common.utils import resolve_branch_alias, reverse | from swh.web.common.utils import resolve_branch_alias, reverse | ||||
def filter_field_keys(data, field_keys): | def filter_field_keys(data, field_keys): | ||||
"""Given an object instance (directory or list), and a csv field keys | """Given an object instance (directory or list), and a csv field keys | ||||
to filter on. | to filter on. | ||||
Return the object instance with filtered keys. | Return the object instance with filtered keys. | ||||
▲ Show 20 Lines • Show All 112 Lines • ▼ Show 20 Lines | ) -> Dict[str, str]: | ||||
c["content_url"] = reverse( | c["content_url"] = reverse( | ||||
"api-1-content", url_args={"q": "sha1:%s" % c["id"]}, request=request | "api-1-content", url_args={"q": "sha1:%s" % c["id"]}, request=request | ||||
) | ) | ||||
return c | return c | ||||
def enrich_content( | def enrich_content( | ||||
content: Dict[str, Any], | content: Dict[str, Any], | ||||
request: Optional[HttpRequest] = None, | |||||
top_url: Optional[bool] = False, | top_url: Optional[bool] = False, | ||||
query_string: Optional[str] = None, | query_string: Optional[str] = None, | ||||
request: Optional[HttpRequest] = None, | |||||
) -> Dict[str, str]: | ) -> Dict[str, str]: | ||||
"""Enrich content with links to: | """Enrich content with links to: | ||||
- data_url: its raw data | - data_url: its raw data | ||||
- filetype_url: its filetype information | - filetype_url: its filetype information | ||||
- language_url: its programming language information | - language_url: its programming language information | ||||
- license_url: its licensing information | - license_url: its licensing information | ||||
Args: | Args: | ||||
▲ Show 20 Lines • Show All 118 Lines • ▼ Show 20 Lines | if "branches" in snapshot: | ||||
branch = resolve_branch_alias(snapshot, v) | branch = resolve_branch_alias(snapshot, v) | ||||
if branch: | if branch: | ||||
branch = enrich_object(branch, request) | branch = enrich_object(branch, request) | ||||
v["target_url"] = branch["target_url"] | v["target_url"] = branch["target_url"] | ||||
return snapshot | return snapshot | ||||
def enrich_origin( | def enrich_origin( | ||||
origin: Dict[str, Any], request: Optional[HttpRequest] = None | origin: Union[Dict[str, Any], OriginInfo], request: Optional[HttpRequest] = None | ||||
) -> Dict[str, Any]: | ) -> Dict[str, Any]: | ||||
"""Enrich origin dict with link to its visits | """Enrich origin dict with link to its visits | ||||
Args: | Args: | ||||
origin: the origin as a dict | origin: the origin as a dict | ||||
request: Absolute URIs will be generated if provided | request: Absolute URIs will be generated if provided | ||||
Returns: | Returns: | ||||
An enriched origin dict filled with an additional url | An enriched origin dict filled with an additional url | ||||
""" | """ | ||||
if "url" in origin: | origin_dict = dict(origin) | ||||
origin["origin_visits_url"] = reverse( | if "url" in origin_dict: | ||||
origin_dict["origin_visits_url"] = reverse( | |||||
"api-1-origin-visits", | "api-1-origin-visits", | ||||
url_args={"origin_url": origin["url"]}, | url_args={"origin_url": origin_dict["url"]}, | ||||
request=request, | request=request, | ||||
) | ) | ||||
return origin | return origin_dict | ||||
def enrich_origin_search_result( | def enrich_origin_search_result( | ||||
origin_search_result: Tuple[List[Dict[str, Any]], Optional[str]], | origin_search_result: Tuple[List[Dict[str, Any]], Optional[str]], | ||||
request: Optional[HttpRequest] = None, | request: Optional[HttpRequest] = None, | ||||
) -> Tuple[List[Dict[str, Any]], Optional[str]]: | ) -> Tuple[List[Dict[str, Any]], Optional[str]]: | ||||
"""Enrich origin search result with additional links | """Enrich origin search result with additional links | ||||
Args: | Args: | ||||
origin_search_result: tuple returned when searching origins | origin_search_result: tuple returned when searching origins | ||||
request: Absolute URIs will be generated if provided | request: Absolute URIs will be generated if provided | ||||
Returns: | Returns: | ||||
An enriched origin search result filled with additional urls | An enriched origin search result filled with additional urls | ||||
""" | """ | ||||
origins, page_token = origin_search_result | origins, page_token = origin_search_result | ||||
return [enrich_origin(origin, request=request) for origin in origins], page_token | return [enrich_origin(origin, request=request) for origin in origins], page_token | ||||
def enrich_origin_visit( | def enrich_origin_visit( | ||||
origin_visit: Dict[str, Any], | origin_visit: Dict[str, Any], | ||||
*, | |||||
with_origin_link: bool, | |||||
with_origin_visit_link: bool, | |||||
request: Optional[HttpRequest] = None, | request: Optional[HttpRequest] = None, | ||||
with_origin_link: bool = False, | |||||
with_origin_visit_link: bool = False, | |||||
) -> Dict[str, Any]: | ) -> Dict[str, Any]: | ||||
"""Enrich origin visit dict with additional links | """Enrich origin visit dict with additional links | ||||
Args: | Args: | ||||
origin_visit: the origin visit as a dict | origin_visit: the origin visit as a dict | ||||
with_origin_link: whether to add link to origin | with_origin_link: whether to add link to origin | ||||
with_origin_visit_link: whether to add link to origin visit | with_origin_visit_link: whether to add link to origin visit | ||||
request: Absolute URIs will be generated if provided | request: Absolute URIs will be generated if provided | ||||
Show All 23 Lines |