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

[IMP] create_recursive_abstract: when creating items via import of th… #182

Merged
Merged
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 create_recursive_abstract/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"author": "GRAP",
"website": "https://github.com/grap/grap-odoo-incubator",
"license": "AGPL-3",
"depends": ["base"],
"depends": ["base_import"],
"installable": True,
}
1 change: 1 addition & 0 deletions create_recursive_abstract/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import create_recursive_mixin
from . import base_import_import
15 changes: 15 additions & 0 deletions create_recursive_abstract/models/base_import_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (C) 2024 - Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import models


class BaseImportImport(models.TransientModel):
_inherit = "base_import.import"

def execute_import(self, fields, columns, options, dryrun=False):
self.ensure_one()
return super(

Check warning on line 13 in create_recursive_abstract/models/base_import_import.py

View check run for this annotation

Codecov / codecov/patch

create_recursive_abstract/models/base_import_import.py#L12-L13

Added lines #L12 - L13 were not covered by tests
BaseImportImport, self.with_context(imported_model=self.res_model)
).execute_import(fields, columns, options, dryrun=dryrun)
27 changes: 20 additions & 7 deletions create_recursive_abstract/models/create_recursive_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,35 @@
- then, create an item named 'Child' with the parent found as parent_id.
"""
vals = {}
self._create_recursive_alter_vals(vals, name)

item = self.create(vals)
return item.name_get()[0]

@api.model
def _create_recursive_alter_vals(self, vals, name=False):
name = name and name or vals.get("name", "")
if "/" in name:
splitted_name = name.split("/")
parent_name = (" / ".join([x.strip() for x in splitted_name[:-1]])).strip()
item_name = splitted_name[-1:][0].strip()
parent_id = self._create_recursive_get_or_create_parent_id(parent_name)
vals = {"name": item_name, "parent_id": parent_id}
vals.update({"name": item_name, "parent_id": parent_id})
else:
vals = {"name": name}
item = self.create(vals)
return item.name_get()[0]
vals.update({"name": name})

Check warning on line 53 in create_recursive_abstract/models/create_recursive_mixin.py

View check run for this annotation

Codecov / codecov/patch

create_recursive_abstract/models/create_recursive_mixin.py#L53

Added line #L53 was not covered by tests

@api.model_create_multi
def create(self, vals_list):
for value in vals_list:
if "name" in value:
value["name"] = value["name"].replace("/", "-").strip()
if self.env.context.get("imported_model") == self._name:
# Creation from import of model
# create parents if doesn't exist
for vals in vals_list:
self._create_recursive_alter_vals(vals)
else:
# Regular creation, removing bad "/"
for vals in vals_list:
if "name" in vals:
vals["name"] = vals["name"].replace("/", "-").strip()
return super().create(vals_list)

def write(self, vals):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,11 @@ def test_name_create(self):
categ = self.model.browse(res[0])
self.assertEqual(categ.name, "New Category")
self.assertEqual(categ.parent_id, self.child_item)

def test_create_via_import(self):
categ = self.model.with_context(imported_model=self.model._name).create(
{"name": "Parent / Child"}
)
self.assertEqual(categ.name, "Child")
self.assertTrue(categ.parent_id)
self.assertEqual(categ.parent_id.name, "Parent")
Loading