Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
@@ -1,7 +1,6 @@
exclude: |
(?x)
# NOT INSTALLABLE ADDONS
^base_rest/|
^base_rest_auth_api_key/|
^base_rest_pydantic/|
^extendable/|
Expand Down
13 changes: 7 additions & 6 deletions base_rest/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Base Rest
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:198b97806a0a8c480e6c8f26f3ce3199cba9978f8d152ecf93c80091323d1805
!! source digest: sha256:9e4588157b2a0c1f427679ba2a04ae87646f52c849579fafb07a52da6f7f577b
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand All @@ -21,13 +21,13 @@ Base Rest
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github
:target: https://github.com/OCA/rest-framework/tree/18.0/base_rest
:target: https://github.com/OCA/rest-framework/tree/19.0/base_rest
:alt: OCA/rest-framework
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/rest-framework-18-0/rest-framework-18-0-base_rest
:target: https://translation.odoo-community.org/projects/rest-framework-19-0/rest-framework-19-0-base_rest
: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/rest-framework&target_branch=18.0
:target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=19.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|
Expand Down Expand Up @@ -404,7 +404,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/rest-framework/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/rest-framework/issues/new?body=module:%20base_rest%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/rest-framework/issues/new?body=module:%20base_rest%0Aversion:%2019.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.

Expand All @@ -421,6 +421,7 @@ Contributors

- Laurent Mignon <[email protected]>
- Sébastien Beau <[email protected]>
- Michael Tietz (MT Software) <[email protected]>

Maintainers
-----------
Expand All @@ -435,6 +436,6 @@ 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.

This module is part of the `OCA/rest-framework <https://github.com/OCA/rest-framework/tree/18.0/base_rest>`_ project on GitHub.
This module is part of the `OCA/rest-framework <https://github.com/OCA/rest-framework/tree/19.0/base_rest>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
8 changes: 3 additions & 5 deletions base_rest/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"summary": """
Develop your own high level REST APIs for Odoo thanks to this addon.
""",
"version": "18.0.1.1.1",
"version": "19.0.1.0.0",
"development_status": "Beta",
"license": "LGPL-3",
"author": "ACSONE SA/NV, " "Odoo Community Association (OCA)",
"author": "ACSONE SA/NV, Odoo Community Association (OCA)",
"maintainers": [],
"website": "https://github.com/OCA/rest-framework",
"depends": ["component", "web"],
Expand All @@ -20,8 +20,6 @@
"assets": {
"web.assets_frontend": [
"base_rest/static/src/scss/base_rest.scss",
"base_rest/static/src/js/swagger_ui.js",
"base_rest/static/src/js/swagger.js",
],
},
"external_dependencies": {
Expand All @@ -33,5 +31,5 @@
"apispec",
]
},
"installable": False,
"installable": True,
}
28 changes: 23 additions & 5 deletions base_rest/components/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


import logging
import os

from werkzeug.exceptions import NotFound

Expand All @@ -17,6 +18,23 @@
_logger = logging.getLogger(__name__)


# Disable logging for deprecation.
# You might have big projects, with tons of services with no specific decorator,
# that are well tested and very stable and you don't want to touch them.
# Especially if you plan already to move to v16.
# Since this logging can bloat your CI and your log collecting tools,
# here is a way to prevent that and get only a msg at boot.
DISABLE_DEPRECATE_LOG_KEY = "REST_API_METHOD_FIX_DECORATOR_DEPRECATE_LOG_DISABLE"
DISABLE_DEPRECATE_LOG = os.getenv(DISABLE_DEPRECATE_LOG_KEY)
if DISABLE_DEPRECATE_LOG:
msg = (
"%s enabled: "
"implicit service methods are deprecated. "
"Disable this env key and enable debug level to have more details."
)
_logger.warning(msg, DISABLE_DEPRECATE_LOG_KEY)


def to_int(val):
# The javascript VM ducktape only use float and so pass float
# to the api, the werkzeug request interpret params as unicode
Expand Down Expand Up @@ -127,15 +145,16 @@ def _prepare_response(self, method, result):
return result
routing = getattr(method, ROUTING_DECORATOR_ATTR, None)
output_param = routing["output_param"]
if not output_param:
if output_param:
return output_param.to_response(self, result)
elif not output_param and not DISABLE_DEPRECATE_LOG:
_logger.warning(
"DEPRECATED: You must define an output schema for method %s "
"in service %s",
method_name,
self._name,
)
return result
return output_param.to_response(self, result)
return result

def dispatch(self, method_name, *args, params=None):
"""
Expand Down Expand Up @@ -197,8 +216,7 @@ def _get_openapi_default_responses(self):
return {
"400": {"description": "One of the given parameter is not valid"},
"401": {
"description": "The user is not authorized. Authentication "
"is required"
"description": "The user is not authorized. Authentication is required"
},
"404": {"description": "Requested resource not found"},
"403": {
Expand Down
18 changes: 15 additions & 3 deletions base_rest/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# @author Sébastien BEAU <[email protected]>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

import configparser
import datetime
import decimal
import json
Expand All @@ -29,8 +30,8 @@
ValidationError,
)
from odoo.http import (
CSRF_FREE_METHODS,
MISSING_CSRF_WARNING,
SAFE_HTTP_METHODS,
Dispatcher,
SessionExpiredException,
request,
Expand Down Expand Up @@ -63,6 +64,17 @@ def default(self, obj): # pylint: disable=E0202,arguments-differ
BLACKLISTED_LOG_PARAMS = ("password",)


def _is_dev_mode():
"""Read ``dev_mode`` in the ``[base_rest]`` section of the config file.

Since 19.0, Odoo only reads the ``[options]`` section of this file.
"""
parser = configparser.RawConfigParser()
if config.get("config"):
parser.read(config["config"])
return parser.getboolean("base_rest", "dev_mode", fallback=False)


def wrapJsonException(exception, include_description=False, extra_info=None):
"""Wrap exceptions to be rendered as JSON.

Expand All @@ -77,7 +89,7 @@ def wrapJsonException(exception, include_description=False, extra_info=None):
def get_body(environ=None, scope=None):
res = {"code": exception.code, "name": escape(exception.name)}
description = exception.get_description(environ)
if config.get_misc("base_rest", "dev_mode"):
if _is_dev_mode():
# return exception info only if base_rest is in dev_mode
res.update({"traceback": exception.traceback, "description": description})
elif include_description:
Expand Down Expand Up @@ -162,7 +174,7 @@ def dispatch(self, endpoint, args):

# Check for CSRF token for relevant requests
if (
self.request.httprequest.method not in CSRF_FREE_METHODS
self.request.httprequest.method not in SAFE_HTTP_METHODS
and endpoint.routing.get("csrf", True)
):
token = params.pop("csrf_token", None)
Expand Down
10 changes: 5 additions & 5 deletions base_rest/models/rest_service_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

from werkzeug.routing import Map, Rule

import odoo
from odoo import http, models
from odoo.modules import module_graph

from odoo.addons.component.core import WorkContext

Expand Down Expand Up @@ -189,18 +189,18 @@ def build_registry(self, services_registry, states=None, exclude_addons=None):
# dependencies to ensure that controllers defined in a more
# specialized addon and overriding more generic one takes precedences
# on the generic one into the registry
graph = odoo.modules.graph.Graph()
graph.add_module(self.env.cr, "base")
graph = module_graph.ModuleGraph(self.env.cr)
graph.extend(["base"])

query = "SELECT name " "FROM ir_module_module " "WHERE state IN %s "
query = "SELECT name FROM ir_module_module WHERE state IN %s "
params = [tuple(states)]
if exclude_addons:
query += " AND name NOT IN %s "
params.append(tuple(exclude_addons))
self.env.cr.execute(query, params)

module_list = [name for (name,) in self.env.cr.fetchall() if name not in graph]
graph.add_modules(self.env.cr, module_list)
graph.extend(module_list)

for module in graph:
self.load_services(module.name, services_registry)
Expand Down
12 changes: 8 additions & 4 deletions base_rest/restapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ def from_params(self, service, params):
validator = self.get_cerberus_validator(service, "input")
if validator.validate(params):
return validator.document
raise UserError(service.env._("BadRequest %s") % validator.errors)
raise UserError(service.env._("BadRequest %s", str(validator.errors)))

def to_response(self, service, result):
validator = self.get_cerberus_validator(service, "output")
if validator.validate(result):
return validator.document
raise SystemError(service.env._("Invalid Response %s") % validator.errors)
raise SystemError(service.env._("Invalid Response %s", str(validator.errors)))

def to_openapi_query_parameters(self, service, spec):
json_schema = self.to_json_schema(service, spec, "input")
Expand Down Expand Up @@ -276,7 +276,7 @@ def get_cerberus_validator(self, service, direction):
if isinstance(schema, dict):
return Validator(schema, purge_unknown=True)
raise Exception(
service.env._("Unable to get cerberus schema from %s") % self._schema
service.env._("Unable to get cerberus schema from %s", str(self._schema))
)

def to_json_schema(self, service, spec, direction):
Expand Down Expand Up @@ -412,7 +412,11 @@ def from_params(self, service, params):
) # multipart ony sends its parts as string
except json.JSONDecodeError as error:
raise ValidationError(
service.env._(f"{key}'s JSON content is malformed: {error}")
service.env._(
"%(key)s's JSON content is malformed: %(error)s",
key=key,
error=str(error),
)
) from error
param = part.from_params(service, json_param)
params[key] = param
Expand Down
11 changes: 6 additions & 5 deletions base_rest/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
<title>README.rst</title>
<title>Base Rest</title>
<style type="text/css">

/*
Expand Down Expand Up @@ -372,9 +372,9 @@ <h1>Base Rest</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:198b97806a0a8c480e6c8f26f3ce3199cba9978f8d152ecf93c80091323d1805
!! source digest: sha256:9e4588157b2a0c1f427679ba2a04ae87646f52c849579fafb07a52da6f7f577b
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/license-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/rest-framework/tree/18.0/base_rest"><img alt="OCA/rest-framework" src="https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/rest-framework-18-0/rest-framework-18-0-base_rest"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/license-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/rest-framework/tree/19.0/base_rest"><img alt="OCA/rest-framework" src="https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/rest-framework-19-0/rest-framework-19-0-base_rest"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&amp;target_branch=19.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This addon is deprecated and not fully supported anymore from Odoo 16.
Please migrate to the FastAPI migration module. See
<a class="reference external" href="https://github.com/OCA/rest-framework/pull/291">https://github.com/OCA/rest-framework/pull/291</a>.</p>
Expand Down Expand Up @@ -740,7 +740,7 @@ <h2><a class="toc-backref" href="#toc-entry-10">Bug Tracker</a></h2>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/rest-framework/issues">GitHub Issues</a>.
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
<a class="reference external" href="https://github.com/OCA/rest-framework/issues/new?body=module:%20base_rest%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/OCA/rest-framework/issues/new?body=module:%20base_rest%0Aversion:%2019.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand All @@ -756,6 +756,7 @@ <h3><a class="toc-backref" href="#toc-entry-13">Contributors</a></h3>
<ul class="simple">
<li>Laurent Mignon &lt;<a class="reference external" href="mailto:laurent.mignon&#64;acsone.eu">laurent.mignon&#64;acsone.eu</a>&gt;</li>
<li>Sébastien Beau &lt;<a class="reference external" href="mailto:sebastien.beau&#64;akretion.com">sebastien.beau&#64;akretion.com</a>&gt;</li>
<li>Michael Tietz (MT Software) &lt;<a class="reference external" href="mailto:mtietz&#64;mt-software.de">mtietz&#64;mt-software.de</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand All @@ -767,7 +768,7 @@ <h3><a class="toc-backref" href="#toc-entry-14">Maintainers</a></h3>
<p>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.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/rest-framework/tree/18.0/base_rest">OCA/rest-framework</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/rest-framework/tree/19.0/base_rest">OCA/rest-framework</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions base_rest/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
from . import test_cerberus_list_validator
from . import test_cerberus_validator
from . import test_controller_builder
from . import test_http
from . import test_openapi_generator
from . import test_service_context_provider
9 changes: 2 additions & 7 deletions base_rest/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,5 @@ def setUpClass(cls):
super().setUpClass()
cls.setUpRegistry()
cls.base_url = cls.env["ir.config_parameter"].get_param("web.base.url")
cls.registry.enter_test_mode(cls.env.cr)

# pylint: disable=W8110
@classmethod
def tearDownClass(cls):
cls.registry.leave_test_mode()
super().tearDownClass()
# leaves test mode in a class cleanup
cls.registry_enter_test_mode_cls()
6 changes: 0 additions & 6 deletions base_rest/tests/test_controller_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def test_01(self):
implementation, these routes where hardcoded into the base controller.
"""

# pylint: disable=R7980
class TestServiceOldApi(Component):
_inherit = "base.rest.service"
_name = "test.ping.service"
Expand Down Expand Up @@ -246,7 +245,6 @@ def test_02(self):
required method to route the requests to the methods
"""

# pylint: disable=R7980
class TestServiceNewApi(Component):
_inherit = "base.rest.service"
_name = "test.partner.service"
Expand Down Expand Up @@ -340,7 +338,6 @@ def _get_partner_schema(self):
def test_03(self):
"""Check that the controller builder takes care of services inheritance"""

# pylint: disable=R7980
class TestPartnerService(Component):
_inherit = "base.rest.service"
_name = "test.partner.service"
Expand Down Expand Up @@ -467,7 +464,6 @@ def test_04(self):
self._BaseTestController._default_csrf = default_csrf
self._BaseTestController._default_save_session = default_save_session

# pylint: disable=R7980
class TestService(Component):
_inherit = "base.rest.service"
_name = "test.partner.service"
Expand Down Expand Up @@ -584,7 +580,6 @@ def test_05(self):
default_auth = "my_default_auth"
self._BaseTestController._default_auth = default_auth

# pylint: disable=R7980
class TestService(Component):
_inherit = "base.rest.service"
_name = "test.partner.service"
Expand Down Expand Up @@ -631,7 +626,6 @@ def test_06(self):
default_auth = "my_default_auth"
self._BaseTestController._default_auth = default_auth

# pylint: disable=R7980
class TestService(Component):
_inherit = "base.rest.service"
_name = "test.partner.service"
Expand Down
Loading
Loading