From ef1d774208efc2bc80e3897c4641d7ff0aab5739 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20BEAU?=
Date: Fri, 24 Apr 2020 14:56:29 +0200
Subject: [PATCH] [IMP] add helper for getting a self env with the tech user
---
base_technical_user/README.rst | 15 ++++++++
base_technical_user/models/__init__.py | 1 +
base_technical_user/models/models.py | 24 +++++++++++++
base_technical_user/readme/USAGE.rst | 15 ++++++++
.../static/description/index.html | 12 +++++++
base_technical_user/tests/__init__.py | 1 +
base_technical_user/tests/test_sudo_tech.py | 34 +++++++++++++++++++
7 files changed, 102 insertions(+)
create mode 100644 base_technical_user/models/models.py
create mode 100644 base_technical_user/tests/__init__.py
create mode 100644 base_technical_user/tests/test_sudo_tech.py
diff --git a/base_technical_user/README.rst b/base_technical_user/README.rst
index 2b67e7605..348faa999 100644
--- a/base_technical_user/README.rst
+++ b/base_technical_user/README.rst
@@ -44,6 +44,21 @@ 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)
+
Bug Tracker
===========
diff --git a/base_technical_user/models/__init__.py b/base_technical_user/models/__init__.py
index aff44f335..fe04d2730 100644
--- a/base_technical_user/models/__init__.py
+++ b/base_technical_user/models/__init__.py
@@ -1 +1,2 @@
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 000000000..e0f4aa6c1
--- /dev/null
+++ b/base_technical_user/models/models.py
@@ -0,0 +1,24 @@
+# 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
+
+
+class Base(models.AbstractModel):
+ _inherit = "base"
+
+ 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
diff --git a/base_technical_user/readme/USAGE.rst b/base_technical_user/readme/USAGE.rst
index 2ed2ad738..9441e914b 100644
--- a/base_technical_user/readme/USAGE.rst
+++ b/base_technical_user/readme/USAGE.rst
@@ -1,2 +1,17 @@
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)
diff --git a/base_technical_user/static/description/index.html b/base_technical_user/static/description/index.html
index 07cf484f3..9d06bff0d 100644
--- a/base_technical_user/static/description/index.html
+++ b/base_technical_user/static/description/index.html
@@ -391,6 +391,18 @@ batch processes.
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
+
+
+self_tech = self.sudo_tech()
+
+If you want to raise an error if the tech user in not configured just call it with
+
+self_tech = self.sudo_tech(raise_if_missing)
+
diff --git a/base_technical_user/tests/__init__.py b/base_technical_user/tests/__init__.py
new file mode 100644
index 000000000..2775d6506
--- /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 000000000..316762fc4
--- /dev/null
+++ b/base_technical_user/tests/test_sudo_tech.py
@@ -0,0 +1,34 @@
+# 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 SUPERUSER_ID
+from odoo.exceptions import UserError
+from odoo.tests import SavepointCase
+
+
+class SudoTechCase(SavepointCase):
+ @classmethod
+ def setUpClass(cls):
+ super(SudoTechCase, cls).setUpClass()
+ cls.user_tech = (
+ cls.env["res.users"]
+ .with_context(tracking_disable=True, no_reset_password=True)
+ .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)