Skip to content

Commit

Permalink
Replace sqlalchemy_utils CurrencyType by custom CurrencyEnum
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Nov 25, 2023
1 parent e3e9098 commit 09b42de
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
6 changes: 6 additions & 0 deletions app/enums.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from enum import Enum

from babel.numbers import list_currencies

CURRENCIES = [(currency, currency) for currency in list_currencies()]

CurrencyEnum = Enum("CurrencyEnum", CURRENCIES)


class LocationOSMType(Enum):
NODE = "NODE"
Expand Down
5 changes: 2 additions & 3 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
from sqlalchemy.sql import func
from sqlalchemy_utils import force_auto_coercion
from sqlalchemy_utils.types.choice import ChoiceType
from sqlalchemy_utils.types.currency import CurrencyType

from app.db import Base
from app.enums import LocationOSMType
from app.enums import CurrencyEnum, LocationOSMType

force_auto_coercion()

Expand Down Expand Up @@ -97,7 +96,7 @@ class Price(Base):
product: Mapped[Product] = relationship(back_populates="prices")

price = Column(Numeric(precision=10, scale=2))
currency = Column(CurrencyType)
currency = Column(ChoiceType(CurrencyEnum))

location_osm_id = Column(BigInteger, index=True)
location_osm_type = Column(ChoiceType(LocationOSMType))
Expand Down
19 changes: 2 additions & 17 deletions app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
BaseModel,
ConfigDict,
Field,
field_serializer,
field_validator,
model_validator,
)
from sqlalchemy_utils import Currency

from app.enums import LocationOSMType
from app.enums import CurrencyEnum, LocationOSMType
from app.models import Price


Expand Down Expand Up @@ -126,7 +124,7 @@ class PriceCreate(BaseModel):
"kilogram or per liter.",
examples=["1.99"],
)
currency: str | Currency = Field(
currency: CurrencyEnum = Field(
description="currency of the price, as a string. "
"The currency must be a valid currency code. "
"See https://en.wikipedia.org/wiki/ISO_4217 for a list of valid currency codes.",
Expand All @@ -152,25 +150,12 @@ class PriceCreate(BaseModel):
examples=[15],
)

@field_validator("currency")
def currency_is_valid(cls, v):
try:
return Currency(v).code
except ValueError:
raise ValueError("not a valid currency code")

@field_validator("labels_tags")
def labels_tags_is_valid(cls, v):
if v is not None:
if len(v) == 0:
raise ValueError("`labels_tags` cannot be empty")

@field_serializer("currency")
def serialize_currency(self, currency: Currency, _info):
if type(currency) is Currency:
return currency.code
return currency

@model_validator(mode="after")
def product_code_and_category_tag_are_exclusive(self):
"""Validator that checks that `product_code` and `category_tag` are
Expand Down

0 comments on commit 09b42de

Please sign in to comment.