-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Basic /prices POST path * Add authentication on price endpoint * Fix currency validation & serialization * Add validator on product_code & location_osm_id
- Loading branch information
Showing
3 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,47 @@ | ||
from datetime import date | ||
from datetime import datetime | ||
|
||
from pydantic import BaseModel | ||
from pydantic import ConfigDict | ||
from pydantic import Field | ||
from pydantic import field_serializer | ||
from pydantic import field_validator | ||
from sqlalchemy_utils import Currency | ||
|
||
from app.enums import PriceLocationOSMType | ||
|
||
|
||
class UserBase(BaseModel): | ||
model_config = ConfigDict(from_attributes=True) | ||
|
||
user_id: str | ||
token: str | ||
|
||
|
||
class PriceCreate(BaseModel): | ||
model_config = ConfigDict(from_attributes=True, arbitrary_types_allowed=True) | ||
|
||
product_code: str = Field(min_length=1, pattern="^[0-9]+$") | ||
price: float | ||
currency: str | Currency | ||
location_osm_id: int = Field(gt=0) | ||
location_osm_type: PriceLocationOSMType | ||
date: date | ||
|
||
@field_validator("currency") | ||
def currency_is_valid(cls, v): | ||
try: | ||
return Currency(v).code | ||
except ValueError: | ||
raise ValueError("not a valid currency code") | ||
|
||
@field_serializer("currency") | ||
def serialize_currency(self, currency: Currency, _info): | ||
if type(currency) is Currency: | ||
return currency.code | ||
return currency | ||
|
||
|
||
class PriceBase(PriceCreate): | ||
# owner: str | ||
created: datetime |