Skip to content

Commit

Permalink
[MIG] endpoint_route_handler: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenminhchien committed Jan 2, 2024
1 parent a4c4774 commit d782087
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 38 deletions.
1 change: 1 addition & 0 deletions endpoint_route_handler/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ Contributors
------------

- Simone Orsi <[email protected]>
- Nguyen Minh Chien <[email protected]>

Maintainers
-----------
Expand Down
2 changes: 1 addition & 1 deletion endpoint_route_handler/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"name": "Endpoint route handler",
"summary": """Provide mixin and tool to generate custom endpoints on the fly.""",
"version": "16.0.1.1.0",
"version": "17.0.1.0.0",
"license": "LGPL-3",
"development_status": "Beta",
"author": "Camptocamp,Odoo Community Association (OCA)",
Expand Down
3 changes: 2 additions & 1 deletion endpoint_route_handler/models/endpoint_route_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def _compute_endpoint_hash(self):
{fname: rec[fname] for fname in self._routing_impacting_fields()}
for rec in self
]
for rec, vals in zip(self, values):
for rec, vals in zip(self, values, strict=True):
vals.pop("id", None)
rec.endpoint_hash = hash(tuple(vals.values()))

Expand Down Expand Up @@ -202,6 +202,7 @@ def _register_single_controller(self, options=None, key=None, init=False):
"""Shortcut to register one single controller."""
rule = self._make_controller_rule(options=options, key=key)
self._endpoint_registry.update_rules([rule], init=init)
self.env.registry.clear_cache("routing")
self._logger.debug(
"Registered controller %s (auth: %s)", self.route, self.auth_type
)
Expand Down
2 changes: 2 additions & 0 deletions endpoint_route_handler/models/endpoint_route_sync_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def _register_controllers(self, init=False, options=None):
return
rules = self._prepare_endpoint_rules(options=options)
self._endpoint_registry.update_rules(rules, init=init)
self.env.registry.clear_cache("routing")
_logger.debug(
"%s registered controllers: %s",
self._name,
Expand All @@ -99,6 +100,7 @@ def _unregister_controllers(self):
if not self:
return
self._endpoint_registry.drop_rules(self._registered_endpoint_rule_keys())
self.env.registry.clear_cache("routing")

def _routing_impacting_fields(self, options=None):
"""Return list of fields that have impact on routing for current record."""
Expand Down
33 changes: 11 additions & 22 deletions endpoint_route_handler/models/ir_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import werkzeug

from odoo import http, models
from odoo import http, models, tools

from ..registry import EndpointRegistry

Expand All @@ -21,12 +21,11 @@ class IrHttp(models.AbstractModel):
def _endpoint_route_registry(cls, env):
return EndpointRegistry.registry_for(env.cr)

@classmethod
def _generate_routing_rules(cls, modules, converters):
def _generate_routing_rules(self, modules, converters):
# Override to inject custom endpoint rules.
return chain(
super()._generate_routing_rules(modules, converters),
cls._endpoint_routing_rules(),
self._endpoint_routing_rules(),
)

@classmethod
Expand All @@ -39,29 +38,19 @@ def _endpoint_routing_rules(cls):
for url in endpoint_rule.routing["routes"]:
yield (url, endpoint)

@classmethod
@tools.ormcache("key", "cls._endpoint_route_last_version()", cache="routing")
def routing_map(cls, key=None):
last_version = cls._get_routing_map_last_version(http.request.env)
if not hasattr(cls, "_routing_map"):
# routing map just initialized, store last update for this env
cls._endpoint_route_last_version = last_version
elif cls._endpoint_route_last_version < last_version:
_logger.info("Endpoint registry updated, reset routing map")
cls._routing_map = {}
cls._rewrite_len = {}
cls._endpoint_route_last_version = last_version
return super().routing_map(key=key)
res = super().routing_map(key=key)
return res

@classmethod
def _get_routing_map_last_version(cls, env):
return cls._endpoint_route_registry(env).last_version()
def _endpoint_route_last_version(cls):
res = cls._get_routing_map_last_version(http.request.env)
return res

@classmethod
def _clear_routing_map(cls):
res = super()._clear_routing_map()
if hasattr(cls, "_endpoint_route_last_version"):
cls._endpoint_route_last_version = 0
return res
def _get_routing_map_last_version(cls, env):
return cls._endpoint_route_registry(env).last_version()

@classmethod
def _auth_method_user_endpoint(cls):
Expand Down
4 changes: 2 additions & 2 deletions endpoint_route_handler/post_init_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
_logger = logging.getLogger(__name__)


def post_init_hook(cr, registry):
def post_init_hook(env):
# this is the trigger that sends notifications when jobs change
_logger.info("Create table")
EndpointRegistry._setup_db(cr)
EndpointRegistry._setup_db(env.cr)
1 change: 1 addition & 0 deletions endpoint_route_handler/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Simone Orsi \<<[email protected]>\>
- Nguyen Minh Chien \<<[email protected]>\>
2 changes: 1 addition & 1 deletion endpoint_route_handler/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def to_dict(self):
def to_row(self):
row = self.to_dict()
for k, v in row.items():
if isinstance(v, (dict, list)):
if isinstance(v, (dict | list)):
row[k] = json.dumps(v)
return row

Expand Down
2 changes: 1 addition & 1 deletion endpoint_route_handler/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down Expand Up @@ -503,6 +502,7 @@ <h2><a class="toc-backref" href="#toc-entry-7">Authors</a></h2>
<h2><a class="toc-backref" href="#toc-entry-8">Contributors</a></h2>
<ul class="simple">
<li>Simone Orsi &lt;<a class="reference external" href="mailto:simone.orsi&#64;camptocamp.com">simone.orsi&#64;camptocamp.com</a>&gt;</li>
<li>Nguyen Minh Chien &lt;<a class="reference external" href="mailto:chien&#64;trobz.com">chien&#64;trobz.com</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
12 changes: 4 additions & 8 deletions endpoint_route_handler/tests/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def make_new_route(env, **kw):

class TestEndpoint(CommonEndpoint):
def tearDown(self):
self.env["ir.http"]._clear_routing_map()
EndpointRegistry.wipe_registry_for(self.env.cr)
super().tearDown()

Expand Down Expand Up @@ -70,7 +69,6 @@ def test_as_tool_register_single_controller(self):
new_route.route += "/new"
with self._get_mocked_request():
new_route._register_single_controller(options=options, init=True)
rmap = self.env["ir.http"]._clear_routing_map()
rmap = self.env["ir.http"].routing_map()
self.assertNotIn("/my/test/route", [x.rule for x in rmap._rules])
self.assertIn("/my/test/route/new", [x.rule for x in rmap._rules])
Expand All @@ -95,7 +93,6 @@ def test_as_tool_register_controllers(self):
new_route.route += "/new"
with self._get_mocked_request():
new_route._register_controllers(options=options, init=True)
rmap = self.env["ir.http"]._clear_routing_map()
rmap = self.env["ir.http"].routing_map()
self.assertNotIn("/my/test/route", [x.rule for x in rmap._rules])
self.assertIn("/my/test/route/new", [x.rule for x in rmap._rules])
Expand All @@ -121,7 +118,6 @@ def test_as_tool_register_controllers_dynamic_route(self):
class TestEndpointCrossEnv(CommonEndpoint):
def setUp(self):
super().setUp()
self.env["ir.http"]._clear_routing_map()
EndpointRegistry.wipe_registry_for(self.env.cr)

@mute_logger("odoo.addons.base.models.ir_http", "odoo.modules.registry")
Expand All @@ -147,10 +143,10 @@ def test_cross_env_consistency(self):
env1["ir.http"].routing_map()
env2["ir.http"].routing_map()
self.assertEqual(
env1["ir.http"]._endpoint_route_last_version, last_version0
env1["ir.http"]._endpoint_route_last_version(), last_version0
)
self.assertEqual(
env2["ir.http"]._endpoint_route_last_version, last_version0
env2["ir.http"]._endpoint_route_last_version(), last_version0
)
rmap = self.env["ir.http"].routing_map()
self.assertIn(route, [x.rule for x in rmap._rules])
Expand All @@ -167,10 +163,10 @@ def test_cross_env_consistency(self):
rmap = env2["ir.http"].routing_map()
self.assertIn(route, [x.rule for x in rmap._rules])
self.assertTrue(
env1["ir.http"]._endpoint_route_last_version > last_version0
env1["ir.http"]._endpoint_route_last_version() > last_version0
)
self.assertTrue(
env2["ir.http"]._endpoint_route_last_version > last_version0
env2["ir.http"]._endpoint_route_last_version() > last_version0
)

# TODO: test unregister
1 change: 0 additions & 1 deletion endpoint_route_handler/tests/test_endpoint_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def setUp(self):

def tearDown(self):
EndpointRegistry.wipe_registry_for(self.env.cr)
self.env["ir.http"]._clear_routing_map()
super().tearDown()

def _make_new_route(self, options=None, **kw):
Expand Down
2 changes: 1 addition & 1 deletion endpoint_route_handler/tests/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def test_endpoint_lookup_ko(self):
}
rule = self._make_rules(stop=2, options=options)[0]
with self.assertRaises(EndpointHandlerNotFound):
rule.endpoint # pylint: disable=pointless-statement
rule.endpoint # pylint: disable=pointless-statement # noqa: B018

def test_endpoint_lookup_ok(self):
rule = self._make_rules(stop=2)[0]
Expand Down

0 comments on commit d782087

Please sign in to comment.