Browse Source

[IMP] add helper for getting a self env with the tech user

12.0-mig-module_prototyper_last
Sébastien BEAU 5 years ago
committed by HviorForgeFlow
parent
commit
ef1d774208
  1. 15
      base_technical_user/README.rst
  2. 1
      base_technical_user/models/__init__.py
  3. 24
      base_technical_user/models/models.py
  4. 15
      base_technical_user/readme/USAGE.rst
  5. 12
      base_technical_user/static/description/index.html
  6. 1
      base_technical_user/tests/__init__.py
  7. 34
      base_technical_user/tests/test_sudo_tech.py

15
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
===========

1
base_technical_user/models/__init__.py

@ -1 +1,2 @@
from . import res_company
from . import models

24
base_technical_user/models/models.py

@ -0,0 +1,24 @@
# Copyright 2020 Akretion (http://www.akretion.com).
# @author Sébastien BEAU <sebastien.beau@akretion.com>
# 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

15
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)

12
base_technical_user/static/description/index.html

@ -391,6 +391,18 @@ batch processes.</p>
<h1><a class="toc-backref" href="#id1">Usage</a></h1>
<p>If you install the module, you will find a tab on the company form allowing
to define the technical user.</p>
<p>In your code you can use the following helper that will return you</p>
<ul class="simple">
<li>a self with the user tech if configured</li>
<li>or a self with sudo user</li>
</ul>
<pre class="code python literal-block">
<span class="n">self_tech</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sudo_tech</span><span class="p">()</span>
</pre>
<p>If you want to raise an error if the tech user in not configured just call it with</p>
<pre class="code python literal-block">
<span class="n">self_tech</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sudo_tech</span><span class="p">(</span><span class="n">raise_if_missing</span><span class="p">)</span>
</pre>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#id2">Bug Tracker</a></h1>

1
base_technical_user/tests/__init__.py

@ -0,0 +1 @@
from . import test_sudo_tech

34
base_technical_user/tests/test_sudo_tech.py

@ -0,0 +1,34 @@
# Copyright 2020 Akretion (http://www.akretion.com).
# @author Sébastien BEAU <sebastien.beau@akretion.com>
# 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)
Loading…
Cancel
Save