-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] add helper for getting a self env with the tech user
- Loading branch information
1 parent
6a3d406
commit 6a12997
Showing
5 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
* Sébastien BEAU <[email protected]> | ||
|
||
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. | ||
To contribute to this module, please visit http://odoo-community.org. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
# -*- coding: utf-8 -*- | ||
from . import res_company | ||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# -*- 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 | ||
|
||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import test_sudo_tech |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |