Skip to content

Commit

Permalink
[IMP] Use StrictExtendableBaseModel in schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
marielejeune committed Jul 31, 2023
1 parent 04aac45 commit 5d8fd2c
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 29 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# generated from manifests external_dependencies
extendable-pydantic>=1.0.0
extendable-pydantic>=1.1.0
extendable_pydantic>=1.0.0
fastapi
openupgradelib
pydantic>=2.0.0
2 changes: 1 addition & 1 deletion shopinvader_api_cart/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"python": [
"fastapi",
"pydantic>=2.0.0",
"extendable-pydantic>=1.0.0",
"extendable-pydantic>=1.1.0",
]
},
"pre_init_hook": "pre_init_hook",
Expand Down
20 changes: 10 additions & 10 deletions shopinvader_api_cart/schemas/amount.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Copyright 2023 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from extendable_pydantic import ExtendableModelMeta
from pydantic import BaseModel, Field
from pydantic import Field

from odoo.tools.float_utils import float_round

from odoo.addons.extendable_fastapi import StrictExtendableBaseModel

class SaleAmount(BaseModel, metaclass=ExtendableModelMeta):

class SaleAmount(StrictExtendableBaseModel):
tax: float = Field(description="Tax amount")
untaxed: float = Field(description="Amount untaxed")
total: float = Field(description="Total amount")
Expand All @@ -17,7 +18,7 @@ class SaleAmount(BaseModel, metaclass=ExtendableModelMeta):
@classmethod
def from_sale_order(cls, sale_order):
precision = sale_order.currency_id.decimal_places
return cls(
return cls.model_construct(
discount_total=float_round(sale_order.discount_total, precision),
total_without_discount=float_round(
sale_order.price_total_no_discount, precision
Expand All @@ -28,25 +29,24 @@ def from_sale_order(cls, sale_order):
)

@classmethod
def from_sale_order_line(cls, order_line, **kwargs):
def from_sale_order_line(cls, order_line):
precision = order_line.order_id.currency_id.decimal_places
return cls(
return cls.model_construct(
discount_total=float_round(order_line.discount_total, precision),
total_without_discount=float_round(
order_line.price_total_no_discount, precision
),
tax=float_round(order_line.price_tax, precision),
untaxed=float_round(order_line.price_subtotal, precision),
total=float_round(order_line.price_total, precision),
**kwargs
)


class SaleLineAmount(SaleAmount):
price: float = Field(description="Unit price")

@classmethod
def from_sale_order_line(cls, order_line, **kwargs):
kwargs["price"] = order_line.price_unit
res = super().from_sale_order_line(order_line, **kwargs)
def from_sale_order_line(cls, order_line):
res = super().from_sale_order_line(order_line)
res.price = order_line.price_unit
return res
12 changes: 5 additions & 7 deletions shopinvader_api_cart/schemas/cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
from datetime import datetime
from typing import List

from extendable_pydantic import ExtendableModelMeta
from pydantic import BaseModel

from odoo.addons.extendable_fastapi import StrictExtendableBaseModel
from odoo.addons.shopinvader_schema_address.schemas import (
BillingAddress,
ShippingAddress,
Expand All @@ -16,17 +14,17 @@
from .sale_order_line import SaleOrderLine


class CartTransaction(BaseModel, metaclass=ExtendableModelMeta):
class CartTransaction(StrictExtendableBaseModel):
uuid: str | None = None
qty: float
product_id: int


class CartSyncInput(BaseModel, metaclass=ExtendableModelMeta):
class CartSyncInput(StrictExtendableBaseModel):
transactions: List[CartTransaction]


class CartResponse(BaseModel, metaclass=ExtendableModelMeta):
class CartResponse(StrictExtendableBaseModel):
uuid: str | None = None
id: int
state: str
Expand All @@ -41,7 +39,7 @@ class CartResponse(BaseModel, metaclass=ExtendableModelMeta):
@classmethod
def from_cart(cls, odoo_rec):

return cls(
return cls.model_construct(
uuid=odoo_rec.uuid or None,
id=odoo_rec.id,
state=odoo_rec.state,
Expand Down
7 changes: 3 additions & 4 deletions shopinvader_api_cart/schemas/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# Copyright 2023 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from extendable_pydantic import ExtendableModelMeta
from pydantic import BaseModel
from odoo.addons.extendable_fastapi import StrictExtendableBaseModel

from .amount import SaleLineAmount


class SaleOrderLine(BaseModel, metaclass=ExtendableModelMeta):
class SaleOrderLine(StrictExtendableBaseModel):
id: int
product_id: int
name: str
Expand All @@ -16,7 +15,7 @@ class SaleOrderLine(BaseModel, metaclass=ExtendableModelMeta):

@classmethod
def from_sale_order_line(cls, odoo_rec):
return cls(
return cls.model_construct(
id=odoo_rec.id,
product_id=odoo_rec.product_id.id,
name=odoo_rec.name,
Expand Down
7 changes: 1 addition & 6 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,4 @@ vcrpy-unittest
unittest2 # For shopinvader test_controller, which inherits component
odoo-test-helper
httpx # For FastAPI tests
odoo-addon-shopinvader-schema-address @ git+https://github.com/shopinvader/odoo-shopinvader@refs/pull/1361/head#subdirectory=setup/shopinvader_schema_address
odoo-addon-shopinvader-api-address @ git+https://github.com/shopinvader/odoo-shopinvader@refs/pull/1361/head#subdirectory=setup/shopinvader_api_address
odoo-addon-shopinvader-address @ git+https://github.com/shopinvader/odoo-shopinvader@refs/pull/1361/head#subdirectory=setup/shopinvader_address
odoo-addon-extendable @ git+https://github.com/oca/rest-framework@refs/pull/348/head#subdirectory=setup/extendable
odoo-addon-fastapi @ git+https://github.com/oca/rest-framework@refs/pull/348/head#subdirectory=setup/fastapi
odoo-addon-extendable-fastapi @ git+https://github.com/oca/rest-framework@refs/pull/348/head#subdirectory=setup/extendable_fastapi
odoo-addon-extendable-fastapi @ git+https://github.com/oca/rest-framework@refs/pull/364/head#subdirectory=setup/extendable_fastapi

0 comments on commit 5d8fd2c

Please sign in to comment.