Skip to content

Commit d127aa6

Browse files
committed
Merge PR #69 into 18.0
Signed-off-by simahawk
2 parents aaffbf5 + 31582b3 commit d127aa6

29 files changed

+2709
-0
lines changed

endpoint_route_handler/README.rst

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
======================
2+
Endpoint route handler
3+
======================
4+
5+
..
6+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7+
!! This file is generated by oca-gen-addon-readme !!
8+
!! changes will be overwritten. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
!! source digest: sha256:2fbca7b2865b964017fc753b0f6e44107ec69bbdb0b2c9f4b613292e95632ddb
11+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
14+
:target: https://odoo-community.org/page/development-status
15+
:alt: Beta
16+
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
17+
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
18+
:alt: License: LGPL-3
19+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb--api-lightgray.png?logo=github
20+
:target: https://github.com/OCA/web-api/tree/18.0/endpoint_route_handler
21+
:alt: OCA/web-api
22+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23+
:target: https://translation.odoo-community.org/projects/web-api-18-0/web-api-18-0-endpoint_route_handler
24+
:alt: Translate me on Weblate
25+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/web-api&target_branch=18.0
27+
:alt: Try me on Runboat
28+
29+
|badge1| |badge2| |badge3| |badge4| |badge5|
30+
31+
Technical module that provides a base handler for adding and removing
32+
controller routes on the fly.
33+
34+
Can be used as a mixin or as a tool.
35+
36+
**Table of contents**
37+
38+
.. contents::
39+
:local:
40+
41+
Usage
42+
=====
43+
44+
As a mixin
45+
----------
46+
47+
Use standard Odoo inheritance:
48+
49+
::
50+
51+
class MyModel(models.Model):
52+
_name = "my.model"
53+
_inherit = "endpoint.route.handler"
54+
55+
Once you have this, each my.model record will generate a route. You can
56+
have a look at the endpoint module to see a real life example.
57+
58+
The options of the routing rules are defined by the method
59+
\_default_endpoint_options. Here's an example from the endpoint module:
60+
61+
::
62+
63+
def _default_endpoint_options_handler(self):
64+
return {
65+
"klass_dotted_path": "odoo.addons.endpoint.controllers.main.EndpointController",
66+
"method_name": "auto_endpoint",
67+
"default_pargs": (self.route,),
68+
}
69+
70+
As you can see, you have to pass the references to the controller class
71+
and the method to use when the endpoint is called. And you can prepare
72+
some default arguments to pass. In this case, the route of the current
73+
record.
74+
75+
As a tool
76+
---------
77+
78+
Initialize non stored route handlers and generate routes from them. For
79+
instance:
80+
81+
::
82+
83+
route_handler = self.env["endpoint.route.handler.tool"]
84+
endpoint_handler = MyController()._my_handler
85+
vals = {
86+
"name": "My custom route",
87+
"route": "/my/custom/route",
88+
"request_method": "GET",
89+
"auth_type": "public",
90+
}
91+
new_route = route_handler.new(vals)
92+
new_route._register_controller()
93+
94+
You can override options and define - for instance - a different
95+
controller method:
96+
97+
::
98+
99+
options = {
100+
"handler": {
101+
"klass_dotted_path": "odoo.addons.my_module.controllers.SpecialController",
102+
"method_name": "my_special_handler",
103+
}
104+
}
105+
new_route._register_controller(options=options)
106+
107+
Of course, what happens when the endpoint gets called depends on the
108+
logic defined on the controller method.
109+
110+
In both cases (mixin and tool) when a new route is generated or an
111+
existing one is updated, the ir.http.routing_map (which holds all Odoo
112+
controllers) will be updated.
113+
114+
You can see a real life example on shopfloor.app model.
115+
116+
Known issues / Roadmap
117+
======================
118+
119+
- add api docs helpers
120+
121+
- allow multiple HTTP methods on the same endpoint
122+
123+
- multiple values for route and methods
124+
125+
keep the same in the ui for now, later own we can imagine a
126+
multi-value selection or just add text field w/ proper validation
127+
and cleanup
128+
129+
remove the route field in the table of endpoint_route
130+
131+
support a comma separated list of routes maybe support comma
132+
separated list of methods use only routing.routes for generating
133+
the rule sort and freeze its values to update the endpoint hash
134+
135+
catch dup route exception on the sync to detect duplicated routes
136+
and use the endpoint_hash to retrieve the real record (note: we
137+
could store more info in the routing information which will stay in
138+
the map)
139+
140+
for customizing the rule behavior the endpoint the hook is to
141+
override the registry lookup
142+
143+
make EndpointRule class overridable on the registry
144+
145+
NOTE in v16 we won't care anymore about odoo controller so the lookup of
146+
the controller can be simplified to a basic py obj that holds the
147+
routing info.
148+
149+
Bug Tracker
150+
===========
151+
152+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/web-api/issues>`_.
153+
In case of trouble, please check there if your issue has already been reported.
154+
If you spotted it first, help us to smash it by providing a detailed and welcomed
155+
`feedback <https://github.com/OCA/web-api/issues/new?body=module:%20endpoint_route_handler%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
156+
157+
Do not contact contributors directly about support or help with technical issues.
158+
159+
Credits
160+
=======
161+
162+
Authors
163+
-------
164+
165+
* Camptocamp
166+
167+
Contributors
168+
------------
169+
170+
- Simone Orsi <simone.orsi@camptocamp.com>
171+
- Nguyen Minh Chien <chien@trobz.com>
172+
173+
Maintainers
174+
-----------
175+
176+
This module is maintained by the OCA.
177+
178+
.. image:: https://odoo-community.org/logo.png
179+
:alt: Odoo Community Association
180+
:target: https://odoo-community.org
181+
182+
OCA, or the Odoo Community Association, is a nonprofit organization whose
183+
mission is to support the collaborative development of Odoo features and
184+
promote its widespread use.
185+
186+
.. |maintainer-simahawk| image:: https://github.com/simahawk.png?size=40px
187+
:target: https://github.com/simahawk
188+
:alt: simahawk
189+
190+
Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:
191+
192+
|maintainer-simahawk|
193+
194+
This module is part of the `OCA/web-api <https://github.com/OCA/web-api/tree/18.0/endpoint_route_handler>`_ project on GitHub.
195+
196+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

endpoint_route_handler/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import models
2+
from .post_init_hook import post_init_hook
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2021 Camptocamp SA
2+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
3+
4+
{
5+
"name": "Endpoint route handler",
6+
"summary": """Provide mixin and tool to generate custom endpoints on the fly.""",
7+
"version": "18.0.1.0.0",
8+
"license": "LGPL-3",
9+
"development_status": "Beta",
10+
"author": "Camptocamp,Odoo Community Association (OCA)",
11+
"maintainers": ["simahawk"],
12+
"website": "https://github.com/OCA/web-api",
13+
"data": [
14+
"security/ir.model.access.csv",
15+
],
16+
"post_init_hook": "post_init_hook",
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import main
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2021 Camptocamp SA
2+
# @author: Simone Orsi <simone.orsi@camptocamp.com>
3+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
4+
5+
6+
import logging
7+
8+
from werkzeug.exceptions import NotFound
9+
10+
from odoo import http
11+
12+
_logger = logging.getLogger(__file__)
13+
14+
15+
class EndpointNotFoundController(http.Controller):
16+
def auto_not_found(self, endpoint_route, **params):
17+
_logger.error("Non registered endpoint for %s", endpoint_route)
18+
raise NotFound()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright 2022 Camptocamp SA
2+
# @author: Simone Orsi <simone.orsi@camptocamp.com>
3+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
4+
5+
6+
class EndpointHandlerNotFound(Exception):
7+
"""Raise when an endpoint handler is not found."""

0 commit comments

Comments
 (0)