From 83abd898658394f587bfa670f69228b0573d8558 Mon Sep 17 00:00:00 2001 From: mle Date: Wed, 26 Jul 2023 14:11:57 +0200 Subject: [PATCH] [MIG] Migration to Pydantic v2 --- requirements.txt | 4 +- shopinvader_api_cart/__manifest__.py | 4 +- shopinvader_api_cart/schemas/amount.py | 40 +++++------- shopinvader_api_cart/schemas/cart.py | 64 ++++++++----------- .../schemas/sale_order_line.py | 23 +++---- 5 files changed, 55 insertions(+), 80 deletions(-) diff --git a/requirements.txt b/requirements.txt index 6eb88b7e2a..8240b571eb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ # generated from manifests external_dependencies -extendable-pydantic +extendable-pydantic>=1.0.0 fastapi openupgradelib -pydantic +pydantic>=2.0.0 diff --git a/shopinvader_api_cart/__manifest__.py b/shopinvader_api_cart/__manifest__.py index 2252883c54..7e12c3e8ef 100644 --- a/shopinvader_api_cart/__manifest__.py +++ b/shopinvader_api_cart/__manifest__.py @@ -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", diff --git a/shopinvader_api_cart/schemas/amount.py b/shopinvader_api_cart/schemas/amount.py index 9750f0c16e..e402eb2ec6 100644 --- a/shopinvader_api_cart/schemas/amount.py +++ b/shopinvader_api_cart/schemas/amount.py @@ -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") @@ -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): diff --git a/shopinvader_api_cart/schemas/cart.py b/shopinvader_api_cart/schemas/cart.py index 5702c4d419..303b3d8ae8 100644 --- a/shopinvader_api_cart/schemas/cart.py +++ b/shopinvader_api_cart/schemas/cart.py @@ -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, @@ -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 diff --git a/shopinvader_api_cart/schemas/sale_order_line.py b/shopinvader_api_cart/schemas/sale_order_line.py index 3ad80bb368..c0a1be98fb 100644 --- a/shopinvader_api_cart/schemas/sale_order_line.py +++ b/shopinvader_api_cart/schemas/sale_order_line.py @@ -4,8 +4,6 @@ from extendable_pydantic import ExtendableModelMeta from pydantic import BaseModel -from odoo.addons.pydantic import utils - from .amount import SaleLineAmount @@ -13,20 +11,15 @@ 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, + )