Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/converters.py
# Copyright (C) 2015-2021 The Software Heritage developers | # Copyright (C) 2015-2021 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 datetime | import datetime | ||||
import json | import json | ||||
from typing import Any, Dict, Union | from typing import Any, Dict, Union | ||||
from django.core.serializers.json import DjangoJSONEncoder | from django.core.serializers.json import DjangoJSONEncoder | ||||
from swh.core.utils import decode_with_escape | from swh.core.utils import decode_with_escape | ||||
from swh.model import hashutil | from swh.model import hashutil | ||||
from swh.model.model import RawExtrinsicMetadata, Release, Revision | from swh.model.model import ( | ||||
RawExtrinsicMetadata, | |||||
Release, | |||||
Revision, | |||||
TimestampWithTimezone, | |||||
) | |||||
from swh.model.swhids import ObjectType | from swh.model.swhids import ObjectType | ||||
from swh.storage.interface import PartialBranches | from swh.storage.interface import PartialBranches | ||||
from swh.web.common.typing import OriginInfo, OriginVisitInfo | from swh.web.common.typing import OriginInfo, OriginVisitInfo | ||||
def _group_checksums(data): | def _group_checksums(data): | ||||
"""Groups checksums values computed from hash functions used in swh | """Groups checksums values computed from hash functions used in swh | ||||
and stored in data dict under a single entry 'checksums' | and stored in data dict under a single entry 'checksums' | ||||
▲ Show 20 Lines • Show All 96 Lines • ▼ Show 20 Lines | ): | ||||
def convert_date(v): | def convert_date(v): | ||||
""" | """ | ||||
Args: | Args: | ||||
v (dict or datatime): either: | v (dict or datatime): either: | ||||
- a dict with three keys: | - a dict with three keys: | ||||
- timestamp (dict or integer timestamp) | - timestamp (dict or integer timestamp) | ||||
- offset | - offset_bytes | ||||
- negative_utc | |||||
- or, a datetime | - or, a datetime | ||||
We convert it to a human-readable string | We convert it to a human-readable string | ||||
""" | """ | ||||
if not v: | if not v: | ||||
return v | return v | ||||
if isinstance(v, datetime.datetime): | if isinstance(v, datetime.datetime): | ||||
return v.isoformat() | return v.isoformat() | ||||
tz = datetime.timezone(datetime.timedelta(minutes=v["offset"])) | v = v.copy() | ||||
swh_timestamp = v["timestamp"] | if isinstance(v["timestamp"], float): | ||||
if isinstance(swh_timestamp, dict): | v["timestamp"] = int(v["timestamp"]) | ||||
date = datetime.datetime.fromtimestamp(swh_timestamp["seconds"], tz=tz) | return TimestampWithTimezone.from_dict(v).to_datetime().isoformat() | ||||
else: | |||||
date = datetime.datetime.fromtimestamp(swh_timestamp, tz=tz) | |||||
datestr = date.isoformat() | |||||
if v["offset"] == 0 and v["negative_utc"]: | |||||
# remove the rightmost + and replace it with a - | |||||
return "-".join(datestr.rsplit("+", 1)) | |||||
return datestr | |||||
if not dict_swh: | if not dict_swh: | ||||
return dict_swh | return dict_swh | ||||
new_dict = {} | new_dict = {} | ||||
for key, value in dict_swh.items(): | for key, value in dict_swh.items(): | ||||
if key in blacklist or (key in removables_if_empty and not value): | if key in blacklist or (key in removables_if_empty and not value): | ||||
continue | continue | ||||
▲ Show 20 Lines • Show All 248 Lines • Show Last 20 Lines |