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 #73

Open
wants to merge 91 commits into
base: 12.0
Choose a base branch
from
Open
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
76519ef
Add endpoint_route_handler
simahawk Oct 25, 2021
6ddd0e5
endpoint_route_handler: reduce log noise
simahawk Nov 18, 2021
d88ae78
endpoint_route_handler 14.0.1.0.1
OCA-git-bot Nov 18, 2021
30713ff
endpoint_route_handler: fix archive/unarchive
simahawk Dec 28, 2021
722a02d
endpoint_route_handler: fix multi env handling
simahawk Dec 29, 2021
dc101df
endpoint_route_handler 14.0.1.0.2
OCA-git-bot Jan 12, 2022
c2e88f6
Misc fix of authorship name
simahawk Jan 14, 2022
4cc9e49
endpoint_route_handler 14.0.1.0.3
OCA-git-bot Jan 14, 2022
08ad79f
endpoint_route_handler: fix rules by group
simahawk Jan 19, 2022
5d1099b
endpoint_route_handler 14.0.1.0.4
OCA-git-bot Jan 19, 2022
1b551e9
endpoint_route_handler: dev status = Beta
simahawk Apr 4, 2022
80b0c13
endpoint_route_handler 14.0.1.1.0
OCA-git-bot Apr 4, 2022
6655036
endpoint_route_handler: move to OCA/web-api
simahawk Jun 15, 2022
b412db7
[UPD] README.rst
OCA-git-bot Jul 15, 2022
9d05424
[UPD] Update endpoint_route_handler.pot
Jul 15, 2022
07c6146
[UPD] Update endpoint_route_handler.pot
Jul 15, 2022
1fb9561
endpoint_route_handler: fix cross worker lookup
simahawk Jun 15, 2022
70b8304
endpoint_route_handler: add flag to control sync
simahawk Jul 25, 2022
5e4b3e7
endpoint_route_handler: add constraints
simahawk Jul 27, 2022
c9e52d5
endpoint_route_handler: add auto timestamp to routes
simahawk Nov 1, 2022
f6e05cd
endpoint_route_handler: fix typo in validator
simahawk Jul 25, 2022
3867577
endpoint_route_handler: add new tool model
simahawk Feb 16, 2023
7053204
endpoint_route_handler: use sequence as version
simahawk Mar 1, 2023
b52303b
endpoint_route_handler: log table setup and wipe
simahawk Mar 8, 2023
05c1a4a
endpoint_route_handler: fail gracefully when sync field not ready
simahawk Mar 8, 2023
efbd60f
endpoint_route_handler: get rid of register_hook
simahawk Mar 27, 2023
fe73cee
endpoint_route_handler: fix auto_not_found param
simahawk Apr 21, 2023
785f257
endpoint_route_handler: 14.0.2.0.0
simahawk May 11, 2023
6e7c6cd
[UPD] Update endpoint_route_handler.pot
May 11, 2023
5e52707
[UPD] README.rst
OCA-git-bot May 11, 2023
5501233
endpoint_route_handler: update ROADMAP
simahawk May 12, 2023
6456651
[UPD] README.rst
OCA-git-bot May 12, 2023
dd45511
endpoint_route_handler 14.0.2.0.1
OCA-git-bot May 12, 2023
d9d3703
[UPD] README.rst
OCA-git-bot Sep 3, 2023
dcafb3c
Added translation using Weblate (Italian)
mymage Feb 29, 2024
f0a765f
[FIX] endpoint_route_handler: Use dedicated cursor for registry
grindtildeath Mar 4, 2024
f76dc07
[BOT] post-merge updates
OCA-git-bot Jul 4, 2024
bd5c2f6
[IMP] endpoint_route_handler: add request_content_type application_js…
OriolMForgeFlow Jul 17, 2023
29dfe35
[BOT] post-merge updates
OCA-git-bot Jul 8, 2024
02ea26b
[IMP]endpoint_route_handler: black, isort, prettier
GuillemCForgeFlow Nov 7, 2024
fb07222
[BKP][ADD]endpoint_route_handler
GuillemCForgeFlow Oct 29, 2024
82ec7a5
[IMP] endpoint_route_handler: pre-commit stuff
JordiMForgeFlow Nov 22, 2024
b6679d8
[MIG] endpoint_route_handler: Migration to V12
JordiMForgeFlow Nov 22, 2024
5d5c44f
Add endpoint module
simahawk Sep 13, 2021
705fe8a
endpoint: add cross model constraint
simahawk Oct 14, 2021
c500c8b
endpoint: split out route handling
simahawk Oct 25, 2021
342047f
[FIX] endpoint: fix loading of demo data
sebalix Nov 9, 2021
0c61b81
endpoint 14.0.1.0.1
OCA-git-bot Nov 9, 2021
5661a0f
endpoint: add tests for archive/unarchive
simahawk Dec 28, 2021
8e6cec1
endpoint: update tests
simahawk Dec 29, 2021
e048798
endpoint: improve search/tree views
simahawk Nov 12, 2021
a555ba1
endpoint 14.0.1.0.2
OCA-git-bot Jan 12, 2022
a990259
endpoint 14.0.1.1.0
OCA-git-bot Jan 12, 2022
0f01ac3
Misc fix of authorship name
simahawk Jan 14, 2022
b5fd2c9
endpoint 14.0.1.1.1
OCA-git-bot Jan 14, 2022
80a146c
endpoint: block all RPC calls
simahawk Feb 18, 2022
e43078d
endpoint 14.0.1.3.0
OCA-git-bot Mar 11, 2022
bc8304a
endpoint: move to web-api
simahawk Jun 15, 2022
3faa917
[UPD] Update endpoint.pot
Jul 15, 2022
6a1752a
[UPD] README.rst
OCA-git-bot Jul 15, 2022
e52af29
Added translation using Weblate (French)
klodr Sep 15, 2022
8b8e3c3
endpoint: adapt to endpoint_route_handler
simahawk Jun 15, 2022
6a0192c
endpoint: use registry_sync flag and improve tests
simahawk Jul 25, 2022
f2807cf
endpoint: adapt to new sync behavior
simahawk Jul 26, 2022
46831f1
endpoint_route_handler: use sequence as version
simahawk Mar 1, 2023
248400e
endpoint: use model explicitly to find endpoints
simahawk Apr 21, 2023
68b0577
endpoint: 14.0.2.0.0
simahawk May 11, 2023
75808c8
[UPD] Update endpoint.pot
May 11, 2023
16d8111
[UPD] README.rst
OCA-git-bot May 11, 2023
b3858c5
Update translation files
weblate May 11, 2023
5d21793
endpoint: update ROADMAP
simahawk May 12, 2023
da37517
[UPD] README.rst
OCA-git-bot May 12, 2023
2bf6926
endpoint 14.0.2.0.1
OCA-git-bot May 12, 2023
2922284
endpoint: fix duplicate
simahawk May 25, 2023
2312a22
endpoint 14.0.2.0.2
OCA-git-bot May 25, 2023
637695c
[UPD] README.rst
OCA-git-bot Sep 3, 2023
2fcff6b
Added translation using Weblate (Italian)
mymage Feb 29, 2024
b55ab45
endpoint: add log function
simahawk Jul 2, 2024
a89c3d0
[IMP] endpoint: endpoint multi-company
JordiMForgeFlow Jun 5, 2024
3173b51
[BOT] post-merge updates
OCA-git-bot Jul 4, 2024
18dc959
[UPD] Update endpoint.pot
Jul 4, 2024
1f2f1d4
[BOT] post-merge updates
OCA-git-bot Jul 4, 2024
390f59c
Update translation files
weblate Jul 4, 2024
1926771
endpoint: add basic mixin view
simahawk Jul 23, 2024
7ffb8ba
[UPD] Update endpoint.pot
Jul 25, 2024
e675dee
[BOT] post-merge updates
OCA-git-bot Jul 25, 2024
dfd43ea
Update translation files
weblate Jul 25, 2024
013a2e2
[IMP]endpoint: black, isort, prettier
GuillemCForgeFlow Nov 7, 2024
5e393f5
[BKP][ADD]endpoint
GuillemCForgeFlow Oct 29, 2024
14a3371
[IMP] endpoint: pre-commit stuff
JordiMForgeFlow Nov 22, 2024
ed87628
[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/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