Skip to content

Commit

Permalink
[FIX] use datamodel instead of extendable_pydantic in order to keep p…
Browse files Browse the repository at this point in the history
…ydantic<2
  • Loading branch information
Clément Mombereau committed Feb 5, 2025
1 parent 57a3cfe commit 7d12437
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 84 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# generated from manifests external_dependencies
extendable_pydantic
marshmallow_objects
1 change: 1 addition & 0 deletions sale_import_base/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
from . import models
from . import datamodels
7 changes: 3 additions & 4 deletions sale_import_base/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
"website": "https://github.com/akretion/sale-import",
"depends": [
"queue_job_chunk",
"datamodel",
"sale_channel_partner",
"sale_exception",
"extendable",
"onchange_helper",
],
"license": "AGPL-3",
"data": [
Expand All @@ -22,7 +23,5 @@
],
"demo": ["demo/demo.xml"],
"installable": True,
"external_dependencies": {
"python": ["extendable_pydantic"],
},
"external_dependencies": {"python": ["marshmallow_objects"]},
}
7 changes: 7 additions & 0 deletions sale_import_base/datamodels/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from . import sale_order_line
from . import sale_order_invoice
from . import sale_order_amount
from . import sale_order_address
from . import sale_order_customer
from . import sale_order
from . import sale_order_payment
19 changes: 19 additions & 0 deletions sale_import_base/datamodels/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) Akretion 2020
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
from odoo.addons.datamodel import fields
from odoo.addons.datamodel.core import Datamodel


class SaleOrderDatamodel(Datamodel):
_name = "sale.order"

name = fields.Str(required=True)
address_customer = fields.NestedModel("sale.order.customer", required=True)
address_shipping = fields.NestedModel("sale.order.address", required=True)
address_invoicing = fields.NestedModel("sale.order.address", required=True)
lines = fields.NestedModel("sale.order.line", many=True, required=True)
amount = fields.NestedModel("sale.order.amount")
invoice = fields.NestedModel("sale.order.invoice")
payment = fields.NestedModel("sale.order.payment")
pricelist_id = fields.Integer()
date_order = fields.Date()
20 changes: 20 additions & 0 deletions sale_import_base/datamodels/sale_order_address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) Akretion 2020
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from odoo.addons.datamodel import fields
from odoo.addons.datamodel.core import Datamodel


class SaleOrderAddressDatamodel(Datamodel):
_name = "sale.order.address"

name = fields.Str(required=True)
street = fields.Str(required=True)
street2 = fields.Str(allow_none=True)
zip = fields.Str(required=True)
city = fields.Str(required=True)
email = fields.Email()
state_code = fields.Str()
country_code = fields.Str(required=True)
phone = fields.Str()
mobile = fields.Str()
12 changes: 12 additions & 0 deletions sale_import_base/datamodels/sale_order_amount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) Akretion 2020
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
from odoo.addons.datamodel import fields
from odoo.addons.datamodel.core import Datamodel


class SaleOrderAmountDatamodel(Datamodel):
_name = "sale.order.amount"

amount_tax = fields.Decimal()
amount_untaxed = fields.Decimal()
amount_total = fields.Decimal()
12 changes: 12 additions & 0 deletions sale_import_base/datamodels/sale_order_customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) Akretion 2020
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from odoo.addons.datamodel import fields
from odoo.addons.datamodel.core import Datamodel


class SaleOrderCustomerDatamodel(Datamodel):
_inherit = "sale.order.address"
_name = "sale.order.customer"

external_id = fields.Str(required=True)
11 changes: 11 additions & 0 deletions sale_import_base/datamodels/sale_order_invoice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) Akretion 2020
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
from odoo.addons.datamodel import fields
from odoo.addons.datamodel.core import Datamodel


class SaleOrderInvoiceDatamodel(Datamodel):
_name = "sale.order.invoice"

date = fields.Date(required=True)
number = fields.Str(required=True)
15 changes: 15 additions & 0 deletions sale_import_base/datamodels/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (c) Akretion 2020
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from odoo.addons.datamodel import fields
from odoo.addons.datamodel.core import Datamodel


class SaleOrderLineDatamodel(Datamodel):
_name = "sale.order.line"

product_code = fields.Str(required=True)
qty = fields.Decimal(required=True)
price_unit = fields.Decimal(required=True)
description = fields.Str()
discount = fields.Decimal()
15 changes: 15 additions & 0 deletions sale_import_base/datamodels/sale_order_payment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (c) Akretion 2020
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from odoo.addons.datamodel import fields
from odoo.addons.datamodel.core import Datamodel


class SaleOrderPaymentDatamodel(Datamodel):
_name = "sale.order.payment"

mode = fields.Str(required=True)
amount = fields.Decimal(required=True)
reference = fields.Str(required=True)
currency_code = fields.Str(required=True)
acquirer_reference = fields.Str()
34 changes: 29 additions & 5 deletions sale_import_base/models/sale_channel_importer.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Copyright (c) Akretion 2020
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
from marshmallow_objects import ValidationError as MarshmallowValidationError

from odoo import _, fields, models
from odoo.exceptions import ValidationError

from .schemas import SaleOrder


class SaleChannelImporter(models.TransientModel):
_name = "sale.channel.importer"
Expand Down Expand Up @@ -37,7 +36,11 @@ def _manage_existing_so(self, existing_so, data):
def run(self):
# Get validated sale order
formatted_data = self._get_formatted_data()
data = SaleOrder(**formatted_data).model_dump()
try:
so_datamodel_load = self.env.datamodels["sale.order"].load(formatted_data)
except MarshmallowValidationError as e:
raise ValidationError(e) from e
data = so_datamodel_load.dump()
existing_so = self._get_existing_so(data)
if existing_so:
self._manage_existing_so(existing_so, data)
Expand Down Expand Up @@ -87,7 +90,23 @@ def _prepare_sale_vals(self, data):
so_vals["name"] = data["name"]
if data.get("date_order"):
so_vals["date_order"] = data["date_order"]
return so_vals

# We need to save the queue.job.chunk before to play_onchanges
# otherwise it is detached from self
chunk_id = self.chunk_id

onchange_fields = [
"payment_mode_id",
"workflow_process_id",
"fiscal_position_id",
"partner_id",
"partner_shipping_id",
"partner_invoice_id",
"company_id",
]
result = self.env["sale.order"].play_onchanges(so_vals, onchange_fields)
self.chunk_id = chunk_id
return result

def _process_partner(self, customer_data):
partner = self._find_partner(customer_data)
Expand Down Expand Up @@ -220,7 +239,12 @@ def _prepare_sale_line(self, line_data, sale_order):
if line_data.get("description"):
vals["name"] = line_data["description"]

return vals
# We need to save the queue.job.chunk before to play_onchanges
# otherwise it is detached from self
chunk_id = self.chunk_id
result = self.env["sale.order.line"].play_onchanges(vals, ["product_id"])
self.chunk_id = chunk_id
return result

def _finalize(self, new_sale_order, raw_import_data):
"""Extend to add final operations"""
Expand Down
65 changes: 0 additions & 65 deletions sale_import_base/models/schemas.py

This file was deleted.

1 change: 0 additions & 1 deletion sale_import_base/pydantic/__init__.py

This file was deleted.

14 changes: 6 additions & 8 deletions sale_import_base/tests/common_sale_order_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from odoo.tests import tagged

from odoo.addons.account.tests.common import AccountTestInvoicingCommon
from odoo.addons.extendable.tests.common import ExtendableMixin
from odoo.addons.datamodel.tests.common import SavepointDatamodelCase

from .data import full, invalid, minimum, mixed

Expand Down Expand Up @@ -75,15 +75,11 @@ def setup_company_data(cls, company_name, chart_template=None, **kwargs):


@tagged("post_install", "-at_install")
class SaleImportCase(TestSaleCommonNoDuplicates, ExtendableMixin):
class SaleImportCase(TestSaleCommonNoDuplicates, SavepointDatamodelCase):
@classmethod
def setUpClass(cls):
super(SaleImportCase, cls).setUpClass()
cls.init_extendable_registry()
account_user = cls.env.user
cls.env = cls.env(user=cls.env.ref("base.user_root"))
cls.setUpPaymentAcquirer()
cls.env = cls.env(user=account_user)
cls.setUpMisc()
cls.setUpProducts()
cls.fiscal_pos_a.auto_apply = True
Expand All @@ -101,8 +97,10 @@ def setUpClass(cls):

@classmethod
def get_created_sales(cls):
return cls.env["sale.order"].sudo().search(
[("id", ">", cls.last_sale_id)], order="id desc"
return (
cls.env["sale.order"]
.sudo()
.search([("id", ">", cls.last_sale_id)], order="id desc")
)

@classmethod
Expand Down

0 comments on commit 7d12437

Please sign in to comment.