Skip to content

Commit

Permalink
Merge PR #173 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 17, 2024
2 parents d9f26dd + d3b2ab1 commit 7ddaf94
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 42 deletions.
4 changes: 1 addition & 3 deletions partner_hide_technical_company/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ 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").with_context(action_from_res_company=True).write(
{"is_odoo_company": True}
)
companies.mapped("partner_id").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 can only update company partners via company form. Companies: \n"
"You must be part of the group Administration / Access Rights to update partners associated to companies.\n"
"- %s"
msgstr "Vous pouvez seulement mettre à jour les partenaires de société via le formulaire de société. Sociétés: \n"
msgstr "Vous devez être membre du groupe 'Administration / Groupe d'accès' pour mettre à jour les partenaires associés aux sociétés.\n"
"- %s"
13 changes: 1 addition & 12 deletions partner_hide_technical_company/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,6 @@ class ResCompany(models.Model):

@api.model_create_multi
def create(self, vals_list):
res = super(ResCompany, self.with_context(action_from_res_company=True)).create(
return super(ResCompany, self.with_context(is_odoo_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()
33 changes: 17 additions & 16 deletions partner_hide_technical_company/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,39 @@ class ResPartner(models.Model):
# Overload Section
@api.model_create_multi
def create(self, vals_list):
if self.env.context.get("action_from_res_company"):
if self.env.context.get("is_odoo_company"):
for vals in vals_list:
vals["is_odoo_company"] = True
return super().create(vals_list)

def write(self, vals):
self._check_technical_partner_access_company()
self._check_technical_partner_access_company("write")
return super().write(vals)

def unlink(self):
self._check_technical_partner_access_company()
self._check_technical_partner_access_company("unlink")
return super().unlink()

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

def _check_technical_partner_access_company(self, operation):
# 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)]
ResCompany = self.env["res.company"]
companies = (
ResCompany.sudo()
.with_context(active_test=False)
.search([("partner_id", "in", self.ids)])
)
if len(companies):
raise UserError(
_(
"You can only update company partners via company form."
" Companies: \n- %s"
# Check if current user has correct access right
if not ResCompany.check_access_rights(operation, raise_exception=False):
raise UserError(
_(
"You have no right to update partners associated to"
" companies.\n- %s"
)
% ("\n- ".join(companies.mapped("name")))
)
% ("\n- ".join(companies.mapped("name")))
)

# Overload the private _search function:
# This function is used by the other ORM functions
Expand Down
13 changes: 5 additions & 8 deletions partner_hide_technical_company/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ 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.company_name = "Users technical_partner_access - res.company"
cls.company = cls.env["res.company"].create({"name": cls.company_name})

Expand Down Expand Up @@ -48,14 +50,9 @@ def test_01_search_partner(self):
)

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

# With Correct access right, should success
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()
self.company.partner_id.write({"name": "Test"})
2 changes: 1 addition & 1 deletion recurring_consignment/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def create(self, vals_list):

def _create_consignor_sequence(self):
ResPartner = self.env["res.partner"]
Irsequence = self.env["ir.sequence"]
Irsequence = self.env["ir.sequence"].sudo()
for company in self:
_logger.info(
"Creating consignor sequence for company '%s'" % (company.name)
Expand Down

0 comments on commit 7ddaf94

Please sign in to comment.