Skip to content

Commit

Permalink
Merge PR #169 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by legalsylvain
  • Loading branch information
github-grap-bot committed Dec 13, 2024
2 parents 54a523b + 446ae5e commit 6762621
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
4 changes: 3 additions & 1 deletion partner_hide_technical_company/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ def post_init_hook(cr, pool):
env = Environment(cr, SUPERUSER_ID, {})
ResCompany = env["res.company"]
companies = ResCompany.with_context(active_test=False).search([])
companies.mapped("partner_id").write({"is_odoo_company": True})
companies.mapped("partner_id").with_context(action_from_res_company=True).write(
{"is_odoo_company": True}
)
4 changes: 2 additions & 2 deletions partner_hide_technical_company/i18n/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ msgstr "Est une société dans Odoo"
#: code:addons/partner_hide_technical_company/models/res_partner.py:0
#, python-format
msgid ""
"You must be part of the group Administration / Access Rights to update partners associated to companies.\n"
"You can only update company partners via company form. Companies: \n"
"- %s"
msgstr "Vous devez être membre du groupe 'Administration / Groupe d'accès' pour mettre à jour les partenaires associés aux sociétés.\n"
msgstr "Vous pouvez seulement mettre à jour les partenaires de société via le formulaire de société. Sociétés: \n"
"- %s"
13 changes: 12 additions & 1 deletion partner_hide_technical_company/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ class ResCompany(models.Model):

@api.model_create_multi
def create(self, vals_list):
return super(ResCompany, self.with_context(is_odoo_company=True)).create(
res = super(ResCompany, self.with_context(action_from_res_company=True)).create(
vals_list
)
return res.with_context(action_from_res_company=False)

def write(self, vals):
return super(ResCompany, self.with_context(action_from_res_company=True)).write(
vals
)

def unlink(self):
return super(
ResCompany, self.with_context(action_from_res_company=True)
).unlink()
20 changes: 10 additions & 10 deletions partner_hide_technical_company/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ResPartner(models.Model):
# Overload Section
@api.model_create_multi
def create(self, vals_list):
if self.env.context.get("is_odoo_company"):
if self.env.context.get("action_from_res_company"):
for vals in vals_list:
vals["is_odoo_company"] = True
return super().create(vals_list)
Expand All @@ -36,23 +36,23 @@ def unlink(self):

# Custom section
def _check_technical_partner_access_company(self):
if self.env.context.get("action_from_res_company"):
return

# We use SUPERUSER_ID to be sure to not skip some users, due to
# some custom access rules deployed on databases
ResCompany = self.env["res.company"].sudo()
companies = ResCompany.with_context(active_test=False).search(
[("partner_id", "in", self.ids)]
)
if len(companies):
# Check if current user has correct access right
if not self.env.user.has_group("base.group_erp_manager"):
raise UserError(
_(
"You must be part of the group Administration / Access"
" Rights to update partners associated to"
" companies.\n- %s"
)
% ("\n- ".join(companies.mapped("name")))
raise UserError(
_(
"You can only update company partners via company form."
" Companies: \n- %s"
)
% ("\n- ".join(companies.mapped("name")))
)

# Overload the private _search function:
# This function is used by the other ORM functions
Expand Down
19 changes: 11 additions & 8 deletions partner_hide_technical_company/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ class TestModule(TransactionCase):
def setUpClass(cls):
super().setUpClass()
cls.ResPartner = cls.env["res.partner"]
cls.ResCompany = cls.env["res.company"]
cls.demo_user = cls.env.ref("base.user_demo")
cls.main_company = cls.env.ref("base.main_company")
cls.company_name = "Users technical_partner_access - res.company"
cls.company = cls.env["res.company"].create({"name": cls.company_name})

def test_01_company_part(self):
company = self.ResCompany.create({"name": self.company_name})
def test_01_search_partner(self):
# Check access without context (by search)
result = self.ResPartner.search([("name", "=", self.company_name)])
self.assertEqual(
Expand Down Expand Up @@ -50,9 +47,15 @@ def test_01_company_part(self):
"Name Search company partner should return result with context",
)

# Without Correct access right, should fail
def test_02_write_partner(self):
# With incorrect way, should fail
with self.assertRaises(UserError):
company.partner_id.with_user(self.demo_user).write({"name": "Test"})
self.company.partner_id.write({"name": "RENAMED"})

# With Correct access right, should success
company.partner_id.write({"name": "Test"})
self.company.write({"name": "RENAMED"})

def test_03_unlink_partner(self):
# With incorrect way, should fail
with self.assertRaises(UserError):
self.company.partner_id.unlink()

0 comments on commit 6762621

Please sign in to comment.