Changeset View
Standalone View
swh/graphql/resolvers/scalars.py
# Copyright (C) 2022 The Software Heritage developers | # Copyright (C) 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 General Public License version 3, or any later version | # License: GNU 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 datetime import datetime | import datetime | ||||
from typing import Optional, Union | |||||
from ariadne import ScalarType | from ariadne import ScalarType | ||||
from swh.graphql.errors import InvalidInputError | from swh.graphql.errors import InvalidInputError | ||||
from swh.graphql.utils import utils | from swh.graphql.utils import utils | ||||
from swh.model import hashutil | from swh.model import hashutil | ||||
from swh.model.exceptions import ValidationError | from swh.model.exceptions import ValidationError | ||||
from swh.model.model import TimestampWithTimezone | from swh.model.model import TimestampWithTimezone | ||||
from swh.model.swhids import CoreSWHID | from swh.model.swhids import CoreSWHID | ||||
datetime_scalar = ScalarType("DateTime") | datetime_scalar = ScalarType("DateTime") | ||||
swhid_scalar = ScalarType("SWHID") | swhid_scalar = ScalarType("SWHID") | ||||
id_scalar = ScalarType("ID") | id_scalar = ScalarType("ID") | ||||
content_hash_scalar = ScalarType("ContentHash") | content_hash_scalar = ScalarType("ContentHash") | ||||
@id_scalar.serializer | @id_scalar.serializer | ||||
def serialize_id(value) -> str: | def serialize_id(value) -> str: | ||||
if type(value) is str: | if type(value) is str: | ||||
value = value.encode() | value = value.encode() | ||||
return value.hex() | return value.hex() | ||||
@datetime_scalar.serializer | @datetime_scalar.serializer | ||||
def serialize_datetime(value): | def serialize_datetime( | ||||
vlorentz: use `isinstance` | |||||
# FIXME, handle error and return None | value: Optional[Union[datetime.datetime, TimestampWithTimezone]] | ||||
if type(value) == TimestampWithTimezone: | ) -> Optional[str]: | ||||
if isinstance(value, TimestampWithTimezone): | |||||
try: | |||||
Not Done Inline ActionsWhat other types can value be? vlorentz: What other types can `value` be? | |||||
Done Inline ActionsCan be an instance of TimestampWithTimezone or a python datetime. Any other type will return a None. jayeshv: Can be an instance of TimestampWithTimezone or a python datetime. Any other type will return a… | |||||
Not Done Inline Actionswhy not two different functions, one for each type? vlorentz: why not two different functions, one for each type? | |||||
Done Inline ActionsThis same function is used to serialize datetimes from both user generated (date field in the Date object) and db dates (date in revision object). jayeshv: This same function is used to serialize datetimes from both user generated (date field in the… | |||||
Done Inline ActionsFix the typo
jayeshv: Fix the typo
> This same function is used to serialize datetimes from both user generated (date… | |||||
value = value.to_datetime() | value = value.to_datetime() | ||||
if type(value) == datetime: | except ValueError: | ||||
return utils.get_formatted_date(value) | # Return None in case of an error | ||||
return None | pass | ||||
return utils.get_formatted_date(value) if type(value) == datetime.datetime else None | |||||
@swhid_scalar.value_parser | @swhid_scalar.value_parser | ||||
def validate_swhid(value): | def validate_swhid(value): | ||||
try: | try: | ||||
swhid = CoreSWHID.from_string(value) | swhid = CoreSWHID.from_string(value) | ||||
except ValidationError as e: | except ValidationError as e: | ||||
raise InvalidInputError("Invalid SWHID", e) | raise InvalidInputError("Invalid SWHID", e) | ||||
Show All 18 Lines |
use isinstance