Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/utils.py
# Copyright (C) 2015-2022 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, Union | from typing import Any, Dict, List, Optional, Tuple, Union | ||||
from django.http import HttpRequest | from django.http import HttpRequest | ||||
from swh.model.model import Origin | |||||
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.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. | ||||
▲ Show 20 Lines • Show All 260 Lines • ▼ Show 20 Lines | |||||
) -> 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 additional urls | ||||
""" | """ | ||||
origin_dict = dict(origin) | origin_dict = dict(origin) | ||||
if "url" in origin_dict: | if "url" in origin_dict: | ||||
origin_dict["origin_visits_url"] = reverse( | origin_dict["origin_visits_url"] = reverse( | ||||
"api-1-origin-visits", | "api-1-origin-visits", | ||||
url_args={"origin_url": origin_dict["url"]}, | url_args={"origin_url": origin_dict["url"]}, | ||||
request=request, | request=request, | ||||
) | ) | ||||
origin_dict["metadata_authorities_url"] = reverse( | |||||
"api-1-raw-extrinsic-metadata-swhid-authorities", | |||||
url_args={"target": Origin(url=origin_dict["url"]).swhid()}, | |||||
request=request, | |||||
) | |||||
return origin_dict | 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]]: | ||||
▲ Show 20 Lines • Show All 49 Lines • Show Last 20 Lines |