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 authored and HviorForgeFlow committed May 27, 2020
1 parent 0e5ea53 commit 7dd273c
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"restructuredtext.confPath": ""
}
49 changes: 24 additions & 25 deletions base_technical_user/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ Base Technical User
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github
:target: https://github.com/OCA/server-tools/tree/12.0/base_technical_user
:alt: OCA/server-tools
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-tools-12-0/server-tools-12-0-base_technical_user
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/149/12.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|
.. |badge3| image:: https://img.shields.io/badge/github-shopinvader%2Fserver--tools-lightgray.png?logo=github
:target: https://github.com/shopinvader/server-tools/tree/13.0/base_technical_user
:alt: shopinvader/server-tools

|badge1| |badge2| |badge3|

This module extends the functionality of company management.
It allows you to bind a technical user on the company in order to use it in
Expand All @@ -44,13 +38,28 @@ 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
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_.
Bugs are tracked on `GitHub Issues <https://github.com/shopinvader/server-tools/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20base_technical_user%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/shopinvader/server-tools/issues/new?body=module:%20base_technical_user%0Aversion:%2013.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Expand All @@ -70,16 +79,6 @@ Contributors
Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

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.

This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/12.0/base_technical_user>`_ project on GitHub.
This module is part of the `shopinvader/server-tools <https://github.com/shopinvader/server-tools/tree/13.0/base_technical_user>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
You are welcome to contribute.
1 change: 1 addition & 0 deletions base_technical_user/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import res_company
from . import models
25 changes: 25 additions & 0 deletions base_technical_user/models/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- 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
15 changes: 15 additions & 0 deletions base_technical_user/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -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)
27 changes: 17 additions & 10 deletions base_technical_user/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ <h1 class="title">Base Technical User</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/server-tools/tree/12.0/base_technical_user"><img alt="OCA/server-tools" src="https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/server-tools-12-0/server-tools-12-0-base_technical_user"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/149/12.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/shopinvader/server-tools/tree/13.0/base_technical_user"><img alt="shopinvader/server-tools" src="https://img.shields.io/badge/github-shopinvader%2Fserver--tools-lightgray.png?logo=github" /></a></p>
<p>This module extends the functionality of company management.
It allows you to bind a technical user on the company in order to use it in
batch processes.</p>
Expand All @@ -391,13 +391,25 @@ <h1 class="title">Base Technical User</h1>
<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>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/server-tools/issues">GitHub Issues</a>.
<p>Bugs are tracked on <a class="reference external" href="https://github.com/shopinvader/server-tools/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/server-tools/issues/new?body=module:%20base_technical_user%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/shopinvader/server-tools/issues/new?body=module:%20base_technical_user%0Aversion:%2013.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand All @@ -416,13 +428,8 @@ <h2><a class="toc-backref" href="#id5">Contributors</a></h2>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#id6">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>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.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/server-tools/tree/12.0/base_technical_user">OCA/server-tools</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/shopinvader/server-tools/tree/13.0/base_technical_user">shopinvader/server-tools</a> project on GitHub.</p>
<p>You are welcome to contribute.</p>
</div>
</div>
</div>
Expand Down
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
35 changes: 35 additions & 0 deletions base_technical_user/tests/test_sudo_tech.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- 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"]
.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)

0 comments on commit 7dd273c

Please sign in to comment.