Skip to content

Commit

Permalink
[MIG] Migration to Pydantic v2
Browse files Browse the repository at this point in the history
  • Loading branch information
marielejeune committed Jul 31, 2023
1 parent 6286e96 commit 83abd89
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 80 deletions.
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# generated from manifests external_dependencies
extendable-pydantic
extendable-pydantic>=1.0.0
fastapi
openupgradelib
pydantic
pydantic>=2.0.0
4 changes: 2 additions & 2 deletions shopinvader_api_cart/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"external_dependencies": {
"python": [
"fastapi",
"pydantic",
"extendable-pydantic",
"pydantic>=2.0.0",
"extendable-pydantic>=1.0.0",
]
},
"pre_init_hook": "pre_init_hook",
Expand Down
40 changes: 16 additions & 24 deletions shopinvader_api_cart/schemas/amount.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

from odoo.tools.float_utils import float_round

from odoo.addons.pydantic import utils


class SaleAmount(BaseModel, metaclass=ExtendableModelMeta):
tax: float = Field(description="Tax amount")
Expand All @@ -18,35 +16,29 @@ class SaleAmount(BaseModel, metaclass=ExtendableModelMeta):

@classmethod
def from_sale_order(cls, sale_order):
res = cls.construct()
precision = sale_order.currency_id.decimal_places
res.discount_total = float_round(sale_order.discount_total, precision)
res.total_without_discount = float_round(
sale_order.price_total_no_discount, precision
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
),
tax=float_round(sale_order.amount_tax, precision),
untaxed=float_round(sale_order.amount_untaxed, precision),
total=float_round(sale_order.amount_total, precision),
)
res.tax = float_round(sale_order.amount_tax, precision)
res.untaxed = float_round(sale_order.amount_untaxed, precision)
res.total = float_round(sale_order.amount_total, precision)

return res

@classmethod
def from_sale_order_line(cls, order_line):
res = cls.construct()
precision = order_line.order_id.currency_id.decimal_places
res.discount_total = float_round(order_line.discount_total, precision)
res.total_without_discount = float_round(
order_line.price_total_no_discount, precision
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),
)
res.tax = float_round(order_line.price_tax, precision)
res.untaxed = float_round(order_line.price_subtotal, precision)
res.total = float_round(order_line.price_total, precision)

return res

class Config:
orm_mode = True
getter_dict = utils.GenericOdooGetter


class SaleLineAmount(SaleAmount):
Expand Down
64 changes: 27 additions & 37 deletions shopinvader_api_cart/schemas/cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from extendable_pydantic import ExtendableModelMeta
from pydantic import BaseModel

from odoo.addons.pydantic import utils
from odoo.addons.shopinvader_schema_address.schemas import (
BillingAddress,
ShippingAddress,
Expand All @@ -18,57 +17,48 @@


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

class Config:
orm_mode = True
getter_dict = utils.GenericOdooGetter


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


class CartResponse(BaseModel, metaclass=ExtendableModelMeta):
uuid: str | None
uuid: str | None = None
id: int
state: str
name: str
date_order: datetime
lines: List[SaleOrderLine]
amount: SaleAmount | None
delivery: ShippingAddress | None
invoicing: BillingAddress | None
note: str | None

class Config:
orm_mode = True
getter_dict = utils.GenericOdooGetter
amount: SaleAmount | None = None
delivery: ShippingAddress | None = None
invoicing: BillingAddress | None = None
note: str | None = None

@classmethod
def from_cart(cls, odoo_rec):
res = cls.construct()
res.uuid = odoo_rec.uuid or None
res.id = odoo_rec.id
res.state = odoo_rec.state
res.name = odoo_rec.name
res.date_order = odoo_rec.date_order
res.lines = [
SaleOrderLine.from_sale_order_line(line) for line in odoo_rec.order_line
]
res.amount = SaleAmount.from_sale_order(odoo_rec)
res.delivery = (
ShippingAddress.from_res_partner(odoo_rec.partner_shipping_id)
if odoo_rec.partner_shipping_id
else None
return cls.model_construct(
uuid=odoo_rec.uuid or None,
id=odoo_rec.id,
state=odoo_rec.state,
name=odoo_rec.name,
date_order=odoo_rec.date_order,
lines=[
SaleOrderLine.from_sale_order_line(line) for line in odoo_rec.order_line
],
amount=SaleAmount.from_sale_order(odoo_rec),
delivery=(
ShippingAddress.from_res_partner(odoo_rec.partner_shipping_id)
if odoo_rec.partner_shipping_id
else None
),
invoicing=(
BillingAddress.from_res_partner(odoo_rec.partner_invoice_id)
if odoo_rec.partner_invoice_id
else None
),
note=odoo_rec.note or None,
)
res.invoicing = (
BillingAddress.from_res_partner(odoo_rec.partner_invoice_id)
if odoo_rec.partner_invoice_id
else None
)
res.note = odoo_rec.note or None

return res
23 changes: 8 additions & 15 deletions shopinvader_api_cart/schemas/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,22 @@
from extendable_pydantic import ExtendableModelMeta
from pydantic import BaseModel

from odoo.addons.pydantic import utils

from .amount import SaleLineAmount


class SaleOrderLine(BaseModel, metaclass=ExtendableModelMeta):
id: int
product_id: int
name: str
amount: SaleLineAmount | None
amount: SaleLineAmount | None = None
qty: float

class Config:
orm_mode = True
getter_dict = utils.GenericOdooGetter

@classmethod
def from_sale_order_line(cls, odoo_rec):
res = cls.construct()
res.id = odoo_rec.id
res.product_id = odoo_rec.product_id
res.name = odoo_rec.name
res.amount = SaleLineAmount.from_sale_order_line(odoo_rec)
res.qty = odoo_rec.product_uom_qty

return res
return cls.model_construct(
id=odoo_rec.id,
product_id=odoo_rec.product_id,
name=odoo_rec.name,
amount=SaleLineAmount.from_sale_order_line(odoo_rec),
qty=odoo_rec.product_uom_qty,
)

0 comments on commit 83abd89

Please sign in to comment.