Skip to content

Commit

Permalink
[IMP] Use server.env.techname.mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-dacosta committed Sep 8, 2023
1 parent 13b3844 commit d45b268
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
10 changes: 10 additions & 0 deletions auth_api_key_server_env/migrations/16.0.1.0.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from odoo import SUPERUSER_ID, api


def migrate(cr, version):
env = api.Environment(cr, SUPERUSER_ID, {})
keys = env["auth.api.key"].search(
[("tech_name", "=", False), ("name", "!=", False)]
)
for key in keys:
key.write({"tech_name": key._normalize_tech_name(key.name)})
7 changes: 2 additions & 5 deletions auth_api_key_server_env/models/auth_api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,17 @@
class AuthApiKey(models.Model):

_name = "auth.api.key"
_inherit = ["auth.api.key", "server.env.mixin"]
_inherit = ["auth.api.key", "server.env.techname.mixin", "server.env.mixin"]

def _server_env_section_name(self):
"""Name of the section in the configuration files
We override the default implementation to keep the compatibility
with the previous implementation of auth_api_key. The section name
into the configuration file must be formatted as
'api_key_{name}'
"""
self.ensure_one()
return "api_key_{}".format(self.name)
return "api_key_{}".format(self.tech_name)

@property
def _server_env_fields(self):
Expand Down
1 change: 1 addition & 0 deletions auth_api_key_server_env/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* Simone Orsi <[email protected]>
* Florian da Costa <[email protected]>
2 changes: 1 addition & 1 deletion auth_api_key_server_env/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ section to your configuration file according to the following convention:

.. code-block:: ini
[api_key_<Record Name>]
[api_key_<record.tech_name>]
key=my_api_key
11 changes: 8 additions & 3 deletions auth_api_key_server_env/tests/test_auth_api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ def setUpClass(cls, *args, **kwargs):
cls.AuthApiKey = cls.env["auth.api.key"]
cls.demo_user = cls.env.ref("base.user_demo")
cls.api_key_from_env = cls.AuthApiKey.create(
{"name": "from_env", "key": "dummy", "user_id": cls.demo_user.id}
{
"name": "From Env",
"key": "dummy",
"user_id": cls.demo_user.id,
"tech_name": "test_env",
}
)
cls.api_key_from_env.refresh()
serv_config.add_section("api_key_from_env")
serv_config.set("api_key_from_env", "key", "api_key_from_env")
serv_config.add_section("api_key_test_env")
serv_config.set("api_key_test_env", "key", "api_key_from_env")

def test_lookup_key_from_env(self):
self.assertEqual(
Expand Down

0 comments on commit d45b268

Please sign in to comment.