bump version, update realease, convert enums to vals

This commit is contained in:
collerek
2021-02-03 14:26:40 +01:00
parent a028e96f3e
commit 867d480728
7 changed files with 89 additions and 25 deletions

View File

@ -65,7 +65,7 @@ class UndefinedType: # pragma no cover
Undefined = UndefinedType()
__version__ = "0.9.0"
__version__ = "0.9.1"
__all__ = [
"Integer",
"BigInteger",

View File

@ -1,6 +1,7 @@
import datetime
import decimal
import uuid
from enum import Enum
from typing import (
Any,
Dict,
@ -88,13 +89,19 @@ def check_if_field_has_choices(field: Type[BaseField]) -> bool:
return hasattr(field, "choices") and bool(field.choices)
def convert_choices_if_needed(
def convert_choices_if_needed( # noqa: CCR001
field: Type["BaseField"], values: Dict
) -> Tuple[Any, List]:
"""
Converts dates to isoformat as fastapi can check this condition in routes
and the fields are not yet parsed.
Converts enums to list of it's values.
Converts uuids to strings.
Converts decimal to float with given scale.
:param field: ormar field to check with choices
:type field: Type[BaseField]
:param values: current values of the model to verify
@ -103,7 +110,8 @@ def convert_choices_if_needed(
:rtype: Tuple[Any, List]
"""
value = values.get(field.name, ormar.Undefined)
choices = list(field.choices)
choices = [o.value if isinstance(o, Enum) else o for o in field.choices]
if field.__type__ in [datetime.datetime, datetime.date, datetime.time]:
value = value.isoformat() if not isinstance(value, str) else value
choices = [o.isoformat() for o in field.choices]
@ -111,7 +119,7 @@ def convert_choices_if_needed(
value = str(value) if not isinstance(value, str) else value
choices = [str(o) for o in field.choices]
elif field.__type__ == decimal.Decimal:
precision = field.precision # type: ignore
precision = field.scale # type: ignore
value = (
round(float(value), precision)
if isinstance(value, decimal.Decimal)