diff --git a/base_technical_user/README.rst b/base_technical_user/README.rst index d68044f45b5..d8c00655036 100644 --- a/base_technical_user/README.rst +++ b/base_technical_user/README.rst @@ -20,6 +20,23 @@ Usage If you install the module, you will find a tab on the company form allowing to define the technical user. + +In your code you can use the following helper that will return you + +- a self with the user tech if configured +- or a self with sudo user + +.. code-block:: python + + self_tech = self.sudo_tech() + +If you want to raise an error if the tech user in not configured just call it with + +.. code-block:: python + + self_tech = self.sudo_tech(raise_if_missing) + + Credits ======= @@ -32,6 +49,7 @@ Contributors ------------ * Cédric Pigeon +* Sébastien BEAU Maintainer ---------- @@ -46,4 +64,4 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use. -To contribute to this module, please visit http://odoo-community.org. \ No newline at end of file +To contribute to this module, please visit http://odoo-community.org. diff --git a/base_technical_user/models/__init__.py b/base_technical_user/models/__init__.py index 88de5f995cc..7c6d3cfbc78 100644 --- a/base_technical_user/models/__init__.py +++ b/base_technical_user/models/__init__.py @@ -1,2 +1,3 @@ # -*- coding: utf-8 -*- from . import res_company +from . import models diff --git a/base_technical_user/models/models.py b/base_technical_user/models/models.py new file mode 100644 index 00000000000..fe316ee9476 --- /dev/null +++ b/base_technical_user/models/models.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Copyright 2020 Akretion (http://www.akretion.com). +# @author Sébastien BEAU +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + + +from odoo import models +from odoo.exceptions import UserError + + +def sudo_tech(self, raise_if_missing=False): + self_sudoer = self + tech_user = self.env.user.company_id.user_tech_id + if tech_user: + self_sudoer = self.sudo(tech_user.id) + elif raise_if_missing: + raise UserError("The technical user is missing in the company {}".format(self.env.user.company_id.name)) + return self_sudoer + + +models.BaseModel.sudo_tech = sudo_tech diff --git a/base_technical_user/tests/__init__.py b/base_technical_user/tests/__init__.py new file mode 100644 index 00000000000..2775d65064a --- /dev/null +++ b/base_technical_user/tests/__init__.py @@ -0,0 +1 @@ +from . import test_sudo_tech diff --git a/base_technical_user/tests/test_sudo_tech.py b/base_technical_user/tests/test_sudo_tech.py new file mode 100644 index 00000000000..3135ed5b28a --- /dev/null +++ b/base_technical_user/tests/test_sudo_tech.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# Copyright 2020 Akretion (http://www.akretion.com). +# @author Sébastien BEAU +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + + +from odoo.tests import SavepointCase +from odoo.exceptions import UserError +from odoo import SUPERUSER_ID + + +class SudoTechCase(SavepointCase): + + @classmethod + def setUpClass(cls): + super(SudoTechCase, cls).setUpClass() + cls.user_tech = cls.env["res.users"].create({"login": "tech", "name": "tech"}) + cls.company = cls.env.ref("base.main_company") + cls.env(user=cls.env.ref("base.user_demo").id) + + def test_sudo_tech(self): + self.company.user_tech_id = self.user_tech + self_tech = self.env["res.partner"].sudo_tech() + self.assertEqual(self_tech._uid, self.user_tech.id) + + def test_sudo_tech_missing_return_sudo(self): + self_tech = self.env["res.partner"].sudo_tech() + self.assertEqual(self_tech._uid, SUPERUSER_ID) + + def test_sudo_tech_missing_raise(self): + with self.assertRaises(UserError): + self.env["res.partner"].sudo_tech(raise_if_missing=True)