You've already forked Promocode-API
mirror of
https://github.com/devitq/Promocode-API.git
synced 2026-05-22 19:47:11 +00:00
fix: postchecks fix, added validation to target type
btw small refactoring
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import datetime
|
||||
import uuid
|
||||
from typing import ClassVar, Literal
|
||||
from typing import Any, ClassVar, Literal
|
||||
|
||||
from ninja import ModelSchema, Schema
|
||||
from pydantic import Field, StrictInt
|
||||
from pydantic import Field, StrictInt, field_validator
|
||||
from pydantic_extra_types.country import CountryAlpha2
|
||||
|
||||
from apps.business.models import Business
|
||||
@@ -64,6 +64,16 @@ class CreatePromocodeIn(ModelSchema):
|
||||
Promocode.promo_common.field.name,
|
||||
]
|
||||
|
||||
@field_validator("target", mode="before")
|
||||
def validate_target(cls, value: Any) -> Any:
|
||||
if not isinstance(value, dict) and not isinstance(
|
||||
value,
|
||||
PromocodeTarget,
|
||||
):
|
||||
err = "The 'target' field must be a valid object."
|
||||
raise ValueError(err)
|
||||
return value
|
||||
|
||||
|
||||
class CreatePromocodeOut(Schema):
|
||||
id: uuid.UUID
|
||||
@@ -113,6 +123,17 @@ class PatchPromocodeIn(Schema):
|
||||
active_from: datetime.date | None = None
|
||||
active_until: datetime.date | None = None
|
||||
|
||||
@staticmethod
|
||||
@field_validator("target", mode="before")
|
||||
def validate_target(value: Any) -> Any:
|
||||
if not isinstance(value, dict) and not isinstance(
|
||||
value,
|
||||
PromocodeTarget,
|
||||
):
|
||||
err = "The 'target' field must be a valid object."
|
||||
raise TypeError(err)
|
||||
return value
|
||||
|
||||
|
||||
class PromocodeStatsForCountry(Schema):
|
||||
country: str
|
||||
|
||||
@@ -12,9 +12,7 @@ def map_promocode_to_schema(promocode: Promocode) -> schemas.PromocodeViewOut:
|
||||
target=schemas.PromocodeTargetViewOut(
|
||||
age_from=promocode.target.age_from,
|
||||
age_until=promocode.target.age_until,
|
||||
country=promocode.target.country_raw
|
||||
if promocode.target.country_raw
|
||||
else None,
|
||||
country=promocode.target.country_raw or None,
|
||||
categories=promocode.target.categories,
|
||||
),
|
||||
max_count=promocode.max_count,
|
||||
|
||||
@@ -170,9 +170,7 @@ def feed(
|
||||
category_lower = filters.category.lower()
|
||||
|
||||
def matches_category(promocode: Promocode) -> bool:
|
||||
categories = (
|
||||
promocode.target.categories or []
|
||||
)
|
||||
categories = promocode.target.categories or []
|
||||
return any(
|
||||
category.lower() == category_lower for category in categories
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user