Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][RFC] l10n_br_cte_spec: rename model spec_models to spec_mixin and multi-schema refactor #3467

Draft
wants to merge 3 commits into
base: 14.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion l10n_br_cte_spec/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from . import spec_models
from . import spec_mixin
from . import v4_0
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
class CteSpecMixin(models.AbstractModel):
_description = "Abstract Model"
_name = "spec.mixin.cte"
_field_prefix = "cte40_"
_schema_name = "cte"
_schema_version = "4.0.0"
_odoo_module = "l10n_br_cte"
_spec_module = "odoo.addons.l10n_br_cte_spec.models.v4_0.cte_tipos_basico_v4_00"
_binding_module = "nfelib.cte.bindings.v4_0.cte_v4_00"
_spec_tab_name = "cte"
_cte40_odoo_module = (
"odoo.addons.l10n_br_cte_spec.models.v4_0.cte_tipos_basico_v4_00"
)
_cte40_binding_module = "nfelib.cte.bindings.v4_0.cte_v4_00"

brl_currency_id = fields.Many2one(
comodel_name="res.currency",
Expand Down
53 changes: 25 additions & 28 deletions l10n_br_cte_spec/tests/test_cte_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

import nfelib
import pkg_resources
from nfelib.cte.bindings.v4_0.cte_v4_00 import Cte
from xsdata.formats.dataclass.parsers import XmlParser
from nfelib.cte.bindings.v4_0.cte_v4_00 import Tcte

from odoo import api
from odoo.tests import TransactionCase
from odoo.tests import SavepointCase

from ..models import spec_models
from ..models import spec_mixin

tz_datetime = re.compile(r".*[-+]0[0-9]:00$")

Expand All @@ -38,11 +37,7 @@ def build_attrs_fake(self, node, create_m2o=False):
value = getattr(node, fname)
if value is None:
continue
key = "%s%s" % (
self._field_prefix,
fname,
)

key = f"cte40_{fname}"
if (
fspec.type == str or not any(["." in str(i) for i in fspec.type.__args__])
) and not str(fspec.type).startswith("typing.List"):
Expand All @@ -66,17 +61,16 @@ def build_attrs_fake(self, node, create_m2o=False):
if fields.get(key) and fields[key].get("related"):
key = fields[key]["related"][0]
comodel_name = fields[key]["relation"]
comodel = self.env.get(comodel_name)
else:
comodel = None
for name in self.env.keys():
if (
hasattr(self.env[name], "_binding_type")
and self.env[name]._binding_type == binding_type
):
comodel = self.env[name]
clean_type = binding_type.lower()
comodel_name = f"cte.40.{clean_type.split('.')[-1]}"
comodel = self.env.get(comodel_name)

if comodel is None: # example skip ICMS100 class
continue
comodel_name = f"cte.40.tcte_{clean_type.split('.')[-1]}"
comodel = self.env.get(comodel_name)
if comodel is None:
continue

if not str(fspec.type).startswith("typing.List"):
# m2o
Expand Down Expand Up @@ -116,13 +110,13 @@ def match_or_create_m2o_fake(self, comodel, new_value, create_m2o=False):
return comodel.new(new_value).id


# spec_models.CteSpecMixin._update_cache = _update_cache
spec_models.CteSpecMixin.build_fake = build_fake
spec_models.CteSpecMixin.build_attrs_fake = build_attrs_fake
spec_models.CteSpecMixin.match_or_create_m2o_fake = match_or_create_m2o_fake
# spec_mixin.CteSpecMixin._update_cache = _update_cache
spec_mixin.CteSpecMixin.build_fake = build_fake
spec_mixin.CteSpecMixin.build_attrs_fake = build_attrs_fake
spec_mixin.CteSpecMixin.match_or_create_m2o_fake = match_or_create_m2o_fake


class CTeImportTest(TransactionCase):
class CTeImportTest(SavepointCase):
def test_import_cte(self):
res_items = (
"cte",
Expand All @@ -131,8 +125,11 @@ def test_import_cte(self):
"43120178408960000182570010000000041000000047-cte.xml",
)
resource_path = "/".join(res_items)
nfe_stream = pkg_resources.resource_stream(nfelib.__name__, resource_path)
parser = XmlParser()
binding = parser.from_string(nfe_stream.read().decode(), Cte)
cte = self.env["cte.40.tcte"].build_fake(binding, create=False)
self.assertEqual(cte.cte40_infCte.cte40_emit.cte40_CNPJ, "78408960000182")
cte_stream = pkg_resources.resource_stream(nfelib.__name__, resource_path)
binding = Tcte.from_xml(cte_stream.read().decode())
cte = (
self.env["cte.40.tcte_infcte"]
.with_context(tracking_disable=True, edoc_type="in", lang="pt_BR")
.build_fake(binding.infCte, create=False)
)
self.assertEqual(cte.cte40_emit.cte40_CNPJ, "78408960000182")
Loading