Changeset View
Standalone View
swh/graphql/resolvers/scalars.py
Show All 23 Lines | |||||
def serialize_id(value): | def serialize_id(value): | ||||
if type(value) is bytes: | if type(value) is bytes: | ||||
return value.hex() | return value.hex() | ||||
return value | return value | ||||
@datetime_scalar.serializer | @datetime_scalar.serializer | ||||
def serialize_datetime(value): | def serialize_datetime(value): | ||||
# FIXME, handle error and return None | |||||
if type(value) == TimestampWithTimezone: | if type(value) == TimestampWithTimezone: | ||||
try: | |||||
vlorentz: use `isinstance` | |||||
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 | ||||
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… | |||||
return utils.get_formatted_date(value) if type(value) == 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