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

[13.0][BKP][ADD]endpoint & endpoint_route_handler #67

Open
wants to merge 87 commits into
base: 13.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
785c0f9
Add endpoint_route_handler
simahawk Oct 25, 2021
19a8333
endpoint_route_handler: reduce log noise
simahawk Nov 18, 2021
d619233
endpoint_route_handler 14.0.1.0.1
OCA-git-bot Nov 18, 2021
90360bb
endpoint_route_handler: fix archive/unarchive
simahawk Dec 28, 2021
957380f
endpoint_route_handler: fix multi env handling
simahawk Dec 29, 2021
b414099
endpoint_route_handler 14.0.1.0.2
OCA-git-bot Jan 12, 2022
ae05d72
Misc fix of authorship name
simahawk Jan 14, 2022
30c5d7f
endpoint_route_handler 14.0.1.0.3
OCA-git-bot Jan 14, 2022
411b6fa
endpoint_route_handler: fix rules by group
simahawk Jan 19, 2022
4ece50e
endpoint_route_handler 14.0.1.0.4
OCA-git-bot Jan 19, 2022
cd5ecab
endpoint_route_handler: dev status = Beta
simahawk Apr 4, 2022
d6aa692
endpoint_route_handler 14.0.1.1.0
OCA-git-bot Apr 4, 2022
02204f0
endpoint_route_handler: move to OCA/web-api
simahawk Jun 15, 2022
e07030c
[UPD] README.rst
OCA-git-bot Jul 15, 2022
cc9f81a
[UPD] Update endpoint_route_handler.pot
Jul 15, 2022
a455464
[UPD] Update endpoint_route_handler.pot
Jul 15, 2022
b68a779
endpoint_route_handler: fix cross worker lookup
simahawk Jun 15, 2022
f2c4b36
endpoint_route_handler: add flag to control sync
simahawk Jul 25, 2022
a430b7d
endpoint_route_handler: add constraints
simahawk Jul 27, 2022
29727d8
endpoint_route_handler: add auto timestamp to routes
simahawk Nov 1, 2022
c7c69b5
endpoint_route_handler: fix typo in validator
simahawk Jul 25, 2022
e5c1d44
endpoint_route_handler: add new tool model
simahawk Feb 16, 2023
14fec82
endpoint_route_handler: use sequence as version
simahawk Mar 1, 2023
0852705
endpoint_route_handler: log table setup and wipe
simahawk Mar 8, 2023
40620c6
endpoint_route_handler: fail gracefully when sync field not ready
simahawk Mar 8, 2023
f12fd51
endpoint_route_handler: get rid of register_hook
simahawk Mar 27, 2023
2c3d528
endpoint_route_handler: fix auto_not_found param
simahawk Apr 21, 2023
fca8008
endpoint_route_handler: 14.0.2.0.0
simahawk May 11, 2023
b3ad580
[UPD] Update endpoint_route_handler.pot
May 11, 2023
b24c6b6
[UPD] README.rst
OCA-git-bot May 11, 2023
3b898dd
endpoint_route_handler: update ROADMAP
simahawk May 12, 2023
97ce491
[UPD] README.rst
OCA-git-bot May 12, 2023
447617b
endpoint_route_handler 14.0.2.0.1
OCA-git-bot May 12, 2023
6320c4c
[UPD] README.rst
OCA-git-bot Sep 3, 2023
d4a7667
Added translation using Weblate (Italian)
mymage Feb 29, 2024
0fc7145
[FIX] endpoint_route_handler: Use dedicated cursor for registry
grindtildeath Mar 4, 2024
dc9a4c2
[BOT] post-merge updates
OCA-git-bot Jul 4, 2024
8c75a9e
[IMP] endpoint_route_handler: add request_content_type application_js…
OriolMForgeFlow Jul 17, 2023
4719d7e
[BOT] post-merge updates
OCA-git-bot Jul 8, 2024
7e35644
[IMP]endpoint_route_handler: black, isort, prettier
GuillemCForgeFlow Nov 7, 2024
dd0630a
[BKP][ADD]endpoint_route_handler
GuillemCForgeFlow Oct 29, 2024
e6cb46b
Add endpoint module
simahawk Sep 13, 2021
99f5f6c
endpoint: add cross model constraint
simahawk Oct 14, 2021
3e47c65
endpoint: split out route handling
simahawk Oct 25, 2021
6e9a933
[FIX] endpoint: fix loading of demo data
sebalix Nov 9, 2021
8c6a853
endpoint 14.0.1.0.1
OCA-git-bot Nov 9, 2021
c383a5d
endpoint: add tests for archive/unarchive
simahawk Dec 28, 2021
89c3251
endpoint: update tests
simahawk Dec 29, 2021
3066fc1
endpoint: improve search/tree views
simahawk Nov 12, 2021
1c8e52b
endpoint 14.0.1.0.2
OCA-git-bot Jan 12, 2022
bb1270f
endpoint 14.0.1.1.0
OCA-git-bot Jan 12, 2022
693e70d
Misc fix of authorship name
simahawk Jan 14, 2022
a06eb8c
endpoint 14.0.1.1.1
OCA-git-bot Jan 14, 2022
db69870
endpoint: block all RPC calls
simahawk Feb 18, 2022
0ac5a37
endpoint 14.0.1.3.0
OCA-git-bot Mar 11, 2022
0a01f18
endpoint: move to web-api
simahawk Jun 15, 2022
12d17ed
[UPD] Update endpoint.pot
Jul 15, 2022
fda38ad
[UPD] README.rst
OCA-git-bot Jul 15, 2022
eec8e53
Added translation using Weblate (French)
klodr Sep 15, 2022
03bcdb7
endpoint: adapt to endpoint_route_handler
simahawk Jun 15, 2022
762ac46
endpoint: use registry_sync flag and improve tests
simahawk Jul 25, 2022
e2eb629
endpoint: adapt to new sync behavior
simahawk Jul 26, 2022
ac2e497
endpoint_route_handler: use sequence as version
simahawk Mar 1, 2023
5e5e5f5
endpoint: use model explicitly to find endpoints
simahawk Apr 21, 2023
6276114
endpoint: 14.0.2.0.0
simahawk May 11, 2023
5343c2a
[UPD] Update endpoint.pot
May 11, 2023
ebfe40a
[UPD] README.rst
OCA-git-bot May 11, 2023
a5c7d51
Update translation files
weblate May 11, 2023
513df3f
endpoint: update ROADMAP
simahawk May 12, 2023
6f48511
[UPD] README.rst
OCA-git-bot May 12, 2023
1db2e54
endpoint 14.0.2.0.1
OCA-git-bot May 12, 2023
57b98b0
endpoint: fix duplicate
simahawk May 25, 2023
b004468
endpoint 14.0.2.0.2
OCA-git-bot May 25, 2023
c0a019f
[UPD] README.rst
OCA-git-bot Sep 3, 2023
f881b45
Added translation using Weblate (Italian)
mymage Feb 29, 2024
b747b0a
endpoint: add log function
simahawk Jul 2, 2024
b5162b6
[IMP] endpoint: endpoint multi-company
JordiMForgeFlow Jun 5, 2024
12f2daa
[BOT] post-merge updates
OCA-git-bot Jul 4, 2024
2ceaf21
[UPD] Update endpoint.pot
Jul 4, 2024
dd2485d
[BOT] post-merge updates
OCA-git-bot Jul 4, 2024
e5d64fe
Update translation files
weblate Jul 4, 2024
63cac4d
endpoint: add basic mixin view
simahawk Jul 23, 2024
e3b3b8e
[UPD] Update endpoint.pot
Jul 25, 2024
0d8567e
[BOT] post-merge updates
OCA-git-bot Jul 25, 2024
55b1f5c
Update translation files
weblate Jul 25, 2024
125f46e
[IMP]endpoint: black, isort, prettier
GuillemCForgeFlow Nov 7, 2024
93f2416
[BKP][ADD]endpoint
GuillemCForgeFlow Oct 29, 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
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,3 @@ repos:
args:
- --rcfile=.pylintrc-mandatory
additional_dependencies: *pylint_deps

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": "13.0.1.0.0",
"license": "LGPL-3",
"development_status": "Beta",
"author": "Camptocamp,Odoo Community Association (OCA)",
"maintainers": ["simahawk"],
"website": "https://github.com/OCA/web-api",
"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