Skip to content

Commit

Permalink
[IMP] add helper for getting a self env with the tech user
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienbeau committed Apr 24, 2020
1 parent 6a3d406 commit bbf07b7
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
20 changes: 19 additions & 1 deletion base_technical_user/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=======

Expand All @@ -32,6 +49,7 @@ Contributors
------------

* Cédric Pigeon <[email protected]>
* Sébastien BEAU <[email protected]>

Maintainer
----------
Expand All @@ -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.
To contribute to this module, please visit http://odoo-community.org.
1 change: 1 addition & 0 deletions base_technical_user/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# -*- coding: utf-8 -*-
from . import res_company
from . import models
22 changes: 22 additions & 0 deletions base_technical_user/models/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Copyright 2020 Akretion (http://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# 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
1 change: 1 addition & 0 deletions base_technical_user/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_sudo_tech
32 changes: 32 additions & 0 deletions base_technical_user/tests/test_sudo_tech.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
# Copyright 2020 Akretion (http://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# 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)

0 comments on commit bbf07b7

Please sign in to comment.