Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.0][BKP] endpoint & endpoint_route_handler #1073

Closed
wants to merge 91 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
7e644c1
Add endpoint_route_handler
simahawk Oct 25, 2021
4380094
endpoint_route_handler: reduce log noise
simahawk Nov 18, 2021
d64a359
endpoint_route_handler 14.0.1.0.1
OCA-git-bot Nov 18, 2021
5d37c52
endpoint_route_handler: fix archive/unarchive
simahawk Dec 28, 2021
60a45a7
endpoint_route_handler: fix multi env handling
simahawk Dec 29, 2021
4abc203
endpoint_route_handler 14.0.1.0.2
OCA-git-bot Jan 12, 2022
61899c3
Misc fix of authorship name
simahawk Jan 14, 2022
c80504c
endpoint_route_handler 14.0.1.0.3
OCA-git-bot Jan 14, 2022
c7c3c3d
endpoint_route_handler: fix rules by group
simahawk Jan 19, 2022
0dfa156
endpoint_route_handler 14.0.1.0.4
OCA-git-bot Jan 19, 2022
9fd23e3
endpoint_route_handler: dev status = Beta
simahawk Apr 4, 2022
5dd8a20
endpoint_route_handler 14.0.1.1.0
OCA-git-bot Apr 4, 2022
0d62686
endpoint_route_handler: move to OCA/web-api
simahawk Jun 15, 2022
b3a3a96
[UPD] README.rst
OCA-git-bot Jul 15, 2022
0e0189c
[UPD] Update endpoint_route_handler.pot
Jul 15, 2022
bd77acd
[UPD] Update endpoint_route_handler.pot
Jul 15, 2022
9c8f5c4
endpoint_route_handler: fix cross worker lookup
simahawk Jun 15, 2022
4275d5d
endpoint_route_handler: add flag to control sync
simahawk Jul 25, 2022
2b66240
endpoint_route_handler: add constraints
simahawk Jul 27, 2022
919b332
endpoint_route_handler: add auto timestamp to routes
simahawk Nov 1, 2022
d75fca8
endpoint_route_handler: fix typo in validator
simahawk Jul 25, 2022
7b41ed7
endpoint_route_handler: add new tool model
simahawk Feb 16, 2023
f4ff262
endpoint_route_handler: use sequence as version
simahawk Mar 1, 2023
e34488e
endpoint_route_handler: log table setup and wipe
simahawk Mar 8, 2023
d3ff60b
endpoint_route_handler: fail gracefully when sync field not ready
simahawk Mar 8, 2023
1039a84
endpoint_route_handler: get rid of register_hook
simahawk Mar 27, 2023
622baad
endpoint_route_handler: fix auto_not_found param
simahawk Apr 21, 2023
47f1875
endpoint_route_handler: 14.0.2.0.0
simahawk May 11, 2023
071eca7
[UPD] Update endpoint_route_handler.pot
May 11, 2023
98ad00c
[UPD] README.rst
OCA-git-bot May 11, 2023
9f4f4c6
endpoint_route_handler: update ROADMAP
simahawk May 12, 2023
8221998
[UPD] README.rst
OCA-git-bot May 12, 2023
7caefd5
endpoint_route_handler 14.0.2.0.1
OCA-git-bot May 12, 2023
a772001
[UPD] README.rst
OCA-git-bot Sep 3, 2023
0f11e03
Added translation using Weblate (Italian)
mymage Feb 29, 2024
91457f9
[FIX] endpoint_route_handler: Use dedicated cursor for registry
grindtildeath Mar 4, 2024
6921f32
[BOT] post-merge updates
OCA-git-bot Jul 4, 2024
8cd6794
[IMP] endpoint_route_handler: add request_content_type application_js…
OriolMForgeFlow Jul 17, 2023
fd27acb
[BOT] post-merge updates
OCA-git-bot Jul 8, 2024
7bfbe0a
[IMP]endpoint_route_handler: black, isort, prettier
GuillemCForgeFlow Nov 7, 2024
a841f89
[BKP][ADD]endpoint_route_handler
GuillemCForgeFlow Oct 29, 2024
4533c1a
[IMP] endpoint_route_handler: pre-commit stuff
JordiMForgeFlow Nov 22, 2024
d68be15
[MIG] endpoint_route_handler: Migration to V12
JordiMForgeFlow Nov 22, 2024
635a3a1
Add endpoint module
simahawk Sep 13, 2021
37ca1fe
endpoint: add cross model constraint
simahawk Oct 14, 2021
2f1f869
endpoint: split out route handling
simahawk Oct 25, 2021
8a7b0ea
[FIX] endpoint: fix loading of demo data
sebalix Nov 9, 2021
4eeb109
endpoint 14.0.1.0.1
OCA-git-bot Nov 9, 2021
d06b7c5
endpoint: add tests for archive/unarchive
simahawk Dec 28, 2021
93a8732
endpoint: update tests
simahawk Dec 29, 2021
a3df1cf
endpoint: improve search/tree views
simahawk Nov 12, 2021
f6ec0fd
endpoint 14.0.1.0.2
OCA-git-bot Jan 12, 2022
0f2170f
endpoint 14.0.1.1.0
OCA-git-bot Jan 12, 2022
c236695
Misc fix of authorship name
simahawk Jan 14, 2022
8d67b1e
endpoint 14.0.1.1.1
OCA-git-bot Jan 14, 2022
3a80446
endpoint: block all RPC calls
simahawk Feb 18, 2022
664e509
endpoint 14.0.1.3.0
OCA-git-bot Mar 11, 2022
827ddc9
endpoint: move to web-api
simahawk Jun 15, 2022
20ad845
[UPD] Update endpoint.pot
Jul 15, 2022
003f9b3
[UPD] README.rst
OCA-git-bot Jul 15, 2022
3f050af
Added translation using Weblate (French)
klodr Sep 15, 2022
9832736
endpoint: adapt to endpoint_route_handler
simahawk Jun 15, 2022
940a0b5
endpoint: use registry_sync flag and improve tests
simahawk Jul 25, 2022
241720f
endpoint: adapt to new sync behavior
simahawk Jul 26, 2022
e1b28c9
endpoint_route_handler: use sequence as version
simahawk Mar 1, 2023
6594d85
endpoint: use model explicitly to find endpoints
simahawk Apr 21, 2023
9572eec
endpoint: 14.0.2.0.0
simahawk May 11, 2023
ad7503f
[UPD] Update endpoint.pot
May 11, 2023
5533315
[UPD] README.rst
OCA-git-bot May 11, 2023
1a40d70
Update translation files
weblate May 11, 2023
71204b5
endpoint: update ROADMAP
simahawk May 12, 2023
7df922a
[UPD] README.rst
OCA-git-bot May 12, 2023
695f9cf
endpoint 14.0.2.0.1
OCA-git-bot May 12, 2023
e016eb8
endpoint: fix duplicate
simahawk May 25, 2023
3f08920
endpoint 14.0.2.0.2
OCA-git-bot May 25, 2023
6b51822
[UPD] README.rst
OCA-git-bot Sep 3, 2023
a94e7a6
Added translation using Weblate (Italian)
mymage Feb 29, 2024
16b6d81
endpoint: add log function
simahawk Jul 2, 2024
675930f
[IMP] endpoint: endpoint multi-company
JordiMForgeFlow Jun 5, 2024
94928d2
[BOT] post-merge updates
OCA-git-bot Jul 4, 2024
fe936f8
[UPD] Update endpoint.pot
Jul 4, 2024
296094a
[BOT] post-merge updates
OCA-git-bot Jul 4, 2024
d2cde5a
Update translation files
weblate Jul 4, 2024
6a3b182
endpoint: add basic mixin view
simahawk Jul 23, 2024
c72d4a3
[UPD] Update endpoint.pot
Jul 25, 2024
020bfa0
[BOT] post-merge updates
OCA-git-bot Jul 25, 2024
ddae4f6
Update translation files
weblate Jul 25, 2024
de02a3c
[IMP]endpoint: black, isort, prettier
GuillemCForgeFlow Nov 7, 2024
ee09f07
[BKP][ADD]endpoint
GuillemCForgeFlow Oct 29, 2024
e9657e7
[IMP] endpoint: pre-commit stuff
JordiMForgeFlow Nov 22, 2024
a2a67d8
[MIG] endpoint: Migration to V12
JordiMForgeFlow Nov 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions endpoint/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
========
Endpoint
========

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:2da9c7b7ea950dc9d9b00ebf3d2464b02005513cfb12202d9bc8b4b329f2af70
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb--api-lightgray.png?logo=github
:target: https://github.com/OCA/web-api/tree/14.0/endpoint
:alt: OCA/web-api
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/web-api-14-0/web-api-14-0-endpoint
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/web-api&target_branch=14.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

Provide an endpoint framework allowing users to define their own custom endpoint.

Thanks to endpoint mixin the endpoint records are automatically registered as real Odoo routes.

You can easily code what you want in the code snippet.

NOTE: for security reasons any kind of RPC call is blocked on endpoint records.

**Table of contents**

.. contents::
:local:

Configuration
=============

Go to "Technical -> Endpoints" and create a new endpoint.

Known issues / Roadmap
======================

* add validation of request data
* add api docs generation
* handle multiple routes per endpoint

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/web-api/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/web-api/issues/new?body=module:%20endpoint%0Aversion:%2014.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.

Credits
=======

Authors
~~~~~~~

* Camptocamp

Contributors
~~~~~~~~~~~~

* Simone Orsi <[email protected]>

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.

.. |maintainer-simahawk| image:: https://github.com/simahawk.png?size=40px
:target: https://github.com/simahawk
:alt: simahawk

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-simahawk|

This module is part of the `OCA/web-api <https://github.com/OCA/web-api/tree/14.0/endpoint>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
4 changes: 4 additions & 0 deletions endpoint/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .hooks import post_init_hook
from . import controllers
from . import models
from . import tools
21 changes: 21 additions & 0 deletions endpoint/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2021 Camptocamp SA
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

{
"name": "Endpoint",
"summary": """Provide custom endpoint machinery.""",
"version": "12.0.1.0.0",
"license": "LGPL-3",
"development_status": "Beta",
"author": "Camptocamp,Odoo Community Association (OCA)",
"maintainers": ["simahawk"],
"website": "https://github.com/OCA/edi",
"depends": ["endpoint_route_handler", "rpc_helper"],
"data": [
"security/ir.model.access.csv",
"security/ir_rule.xml",
"views/endpoint_view.xml",
],
"demo": ["demo/endpoint_demo.xml"],
"post_init_hook": "post_init_hook",
}
1 change: 1 addition & 0 deletions endpoint/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import main
57 changes: 57 additions & 0 deletions endpoint/controllers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright 2021 Camptocamp SA
# @author: Simone Orsi <[email protected]>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).


import json

from werkzeug.exceptions import NotFound

from odoo import http
from odoo.http import Response, request


class EndpointControllerMixin:
def _handle_endpoint(self, env, model, endpoint_route, **params):
endpoint = self._find_endpoint(env, model, endpoint_route)
if not endpoint:
raise NotFound()
endpoint._validate_request(request)
result = endpoint._handle_request(request)
return self._handle_result(result)

def _handle_result(self, result):
response = result.get("response")
if isinstance(response, Response):
# Full response already provided
return response
payload = result.get("payload", "")
status = result.get("status_code", 200)
headers = result.get("headers", {})
return self._make_json_response(payload, headers=headers, status=status)

# TODO: probably not needed anymore as controllers are automatically registered
def _make_json_response(self, payload, headers=None, status=200, **kw):
# TODO: guess out type?
data = json.dumps(payload)
if headers is None:
headers = {}
headers["Content-Type"] = "application/json"
if request._request_type == "http":
resp = request.make_response(data, headers=headers)
else:
resp = request._json_response(data)
resp.status = str(status)
return resp

def _find_endpoint(self, env, model, endpoint_route):
return env[model]._find_endpoint(endpoint_route)

def auto_endpoint(self, model, endpoint_route, **params):
"""Default method to handle auto-generated endpoints"""
env = request.env
return self._handle_endpoint(env, model, endpoint_route, **params)


class EndpointController(http.Controller, EndpointControllerMixin):
pass
86 changes: 86 additions & 0 deletions endpoint/demo/endpoint_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">

<record id="endpoint_demo_1" model="endpoint.endpoint">
<field name="name">Demo Endpoint 1</field>
<field name="route">/demo/one</field>
<field name="request_method">GET</field>
<field name="exec_mode">code</field>
<field name="code_snippet">
result = {"response": Response("ok")}
</field>
</record>

<record id="endpoint_demo_2" model="endpoint.endpoint">
<field name="name">Demo Endpoint 2</field>
<field name="route">/demo/as_demo_user</field>
<field name="request_method">GET</field>
<field name="auth_type">public</field>
<field name="exec_as_user_id" ref="base.user_demo" />
<field name="exec_mode">code</field>
<field name="code_snippet">
result = {"response": Response("My name is: " + user.name)}
</field>
</record>

<record id="endpoint_demo_3" model="endpoint.endpoint">
<field name="name">Demo Endpoint 3</field>
<field name="route">/demo/json_data</field>
<field name="request_method">GET</field>
<field name="auth_type">public</field>
<field name="exec_as_user_id" ref="base.user_demo" />
<field name="exec_mode">code</field>
<field name="code_snippet">
result = {"payload": {"a": 1, "b": 2}}
</field>
</record>

<record id="endpoint_demo_4" model="endpoint.endpoint">
<field name="name">Demo Endpoint 4</field>
<field name="route">/demo/raise_not_found</field>
<field name="request_method">GET</field>
<field name="auth_type">public</field>
<field name="exec_as_user_id" ref="base.user_demo" />
<field name="exec_mode">code</field>
<field name="code_snippet">
raise werkzeug.exceptions.NotFound()
</field>
</record>

<record id="endpoint_demo_5" model="endpoint.endpoint">
<field name="name">Demo Endpoint 5</field>
<field name="route">/demo/raise_validation_error</field>
<field name="request_method">GET</field>
<field name="auth_type">public</field>
<field name="exec_as_user_id" ref="base.user_demo" />
<field name="exec_mode">code</field>
<field name="code_snippet">
raise exceptions.ValidationError("Sorry, you cannot do this!")
</field>
</record>

<record id="endpoint_demo_6" model="endpoint.endpoint">
<field name="name">Demo Endpoint 6</field>
<field name="route">/demo/value_from_request</field>
<field name="request_method">GET</field>
<field name="auth_type">public</field>
<field name="exec_as_user_id" ref="base.user_demo" />
<field name="exec_mode">code</field>
<field name="code_snippet">
result = {"response": Response(request.params.get("your_name", ""))}
</field>
</record>

<record id="endpoint_demo_7" model="endpoint.endpoint">
<field name="name">Demo Endpoint 7</field>
<field name="route">/demo/bad_method</field>
<field name="request_method">GET</field>
<field name="exec_mode">code</field>
<field name="auth_type">public</field>
<field name="exec_as_user_id" ref="base.user_demo" />
<field name="code_snippet">
result = {"payload": "Method used:" + request.httprequest.method}
</field>
</record>

</odoo>
70 changes: 70 additions & 0 deletions endpoint/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright 2021 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

import logging

from odoo import SUPERUSER_ID, api, tools

from odoo.addons.base.models.ir_model import query_insert

_logger = logging.getLogger(__file__)


def post_init_hook(cr, registry):
env = api.Environment(cr, SUPERUSER_ID, {})
_init_server_action(env)


def _init_server_action(env):
"""Create server action if missing."""
# This is actually a trick to work around this error:
#
# psycopg2.IntegrityError: null value in column "activity_user_type"
# violates not-null constraint
#
# which happens when `mail` is installed,
# since it adds this field as required in DB.
#
# We DO NOT want to depend on mail for this problem...
# hence, here we go with this crazy dance :S
#
# Moreover, we are forced to use a query for this
# because if you use `model.create` you get
#
# ValueError: Invalid field 'activity_user_type' on model 'ir.actions.server'
#
# because the field is not yet in the env if the mail modules is not loaded 1st.
xid = "endpoint.server_action_registry_sync"
rec = env.ref(xid, False)
if rec:
return
model = env.ref("endpoint.model_endpoint_endpoint")
values = {
"name": "Sync registry",
"type": "ir.actions.server",
"model_id": model.id,
"model_name": model.model,
"binding_model_id": model.id,
"binding_type": "action",
"usage": "ir_actions_server",
"state": "code",
"code": """
records.filtered(lambda x: not x.registry_sync).write({"registry_sync": True})
""",
}
if tools.sql.column_exists(env.cr, "ir_act_server", "activity_user_type"):
values["activity_user_type"] = "specific"
ids = query_insert(env.cr, "ir_act_server", [values])

# Finally add an xmlid
module, id_ = xid.split(".", 1)
env["ir.model.data"].create(
{
"name": id_,
"module": module,
"model": "ir.actions.server",
"res_id": ids[0],
"noupdate": True,
}
)
_logger.info("Server action created")
Loading
Loading