Skip to content

Commit

Permalink
[MIG] base_rest, base_rest_auth_api_key, base_rest_datamodel, base_re…
Browse files Browse the repository at this point in the history
…st_demo, base_rest_pydantic, datamodel, extendable: Migration to 16.0

Co-authored-by: Nikul-OSI <[email protected]>
  • Loading branch information
StefanRijnhart and Nikul-OSI committed Dec 7, 2022
1 parent 29adb3d commit b320af1
Show file tree
Hide file tree
Showing 32 changed files with 265 additions and 233 deletions.
1 change: 1 addition & 0 deletions base_rest/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from . import models
from . import components
from . import http
9 changes: 5 additions & 4 deletions base_rest/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"summary": """
Develop your own high level REST APIs for Odoo thanks to this addon.
""",
"version": "15.0.1.2.0",
"version": "16.0.1.0.0",
"development_status": "Beta",
"license": "LGPL-3",
"author": "ACSONE SA/NV, " "Odoo Community Association (OCA)",
Expand All @@ -18,18 +18,19 @@
"views/base_rest_view.xml",
],
"assets": {
"web.assets_common": [
"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",
],
},
"demo": [],
"external_dependencies": {
"python": [
"cerberus",
"pyquerystring",
"parse-accept-language",
"apispec>=4.0.0",
# adding version causes missing-manifest-dependency false positives
"apispec",
]
},
"installable": True,
Expand Down
6 changes: 3 additions & 3 deletions base_rest/apispec/base_rest_service_apispec.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ def _get_plugins(self):

def _add_method_path(self, method):
description = textwrap.dedent(method.__doc__ or "")
routing = method.routing
routing = method.original_routing
for paths, method in routing["routes"]:
for path in paths:
self.path(
path,
operations={method.lower(): {"summary": description}},
routing=routing,
original_routing=routing,
)

def generate_paths(self):
for _name, method in inspect.getmembers(self._service, inspect.ismethod):
routing = getattr(method, "routing", None)
routing = getattr(method, "original_routing", None)
if not routing:
continue
self._add_method_path(method)
6 changes: 3 additions & 3 deletions base_rest/apispec/rest_method_param_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ def init_spec(self, spec):
self.openapi_version = spec.openapi_version

def operation_helper(self, path=None, operations=None, **kwargs):
routing = kwargs.get("routing")
routing = kwargs.get("original_routing")
if not routing:
super(RestMethodParamPlugin, self).operation_helper(
path, operations, **kwargs
)
if not operations:
return
for method, params in operations.items():
parameters = self._generate_pamareters(routing, method, params)
parameters = self._generate_parameters(routing, method, params)
if parameters:
params["parameters"] = parameters
responses = self._generate_responses(routing, method, params)
if responses:
params["responses"] = responses

def _generate_pamareters(self, routing, method, params):
def _generate_parameters(self, routing, method, params):
parameters = params.get("parameters", [])
# add default paramters provided by the sevice
parameters.extend(self._default_parameters)
Expand Down
2 changes: 1 addition & 1 deletion base_rest/apispec/rest_method_security_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def init_spec(self, spec):
spec.components.security_scheme("user", user_scheme)

def operation_helper(self, path=None, operations=None, **kwargs):
routing = kwargs.get("routing")
routing = kwargs.get("original_routing")
if not routing:
super(RestMethodSecurityPlugin, self).operation_helper(
path, operations, **kwargs
Expand Down
4 changes: 2 additions & 2 deletions base_rest/components/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _prepare_input_params(self, method, params):
method_name = method.__name__
if hasattr(method, "skip_secure_params"):
return params
routing = getattr(method, "routing", None)
routing = getattr(method, "original_routing", None)
if not routing:
_logger.warning(
"Method %s is not a public method of service %s",
Expand Down Expand Up @@ -122,7 +122,7 @@ def _prepare_response(self, method, result):
method_name = method.__name__
if hasattr(method, "skip_secure_response"):
return result
routing = getattr(method, "routing", None)
routing = getattr(method, "original_routing", None)
output_param = routing["output_param"]
if not output_param:
_logger.warning(
Expand Down
72 changes: 34 additions & 38 deletions base_rest/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from werkzeug.exceptions import BadRequest

from odoo import models
from odoo.http import Controller, ControllerType, Response, request
from odoo.http import Controller, Response, request

from odoo.addons.component.core import WorkContext, _get_addon_name

Expand All @@ -25,43 +25,7 @@ def __init__(self, name, env):
self.id = None


class RestControllerType(ControllerType):

# pylint: disable=E0213
def __init__(cls, name, bases, attrs): # noqa: B902
if (
"RestController" in globals()
and RestController in bases
and Controller not in bases
):
# to be registered as a controller into the ControllerType,
# our RestConrtroller must be a direct child of Controller
bases += (Controller,)
super(RestControllerType, cls).__init__(name, bases, attrs)
if "RestController" not in globals() or not any(
issubclass(b, RestController) for b in bases
):
return
# register the rest controller into the rest controllers registry
root_path = getattr(cls, "_root_path", None)
collection_name = getattr(cls, "_collection_name", None)
if root_path and collection_name:
cls._module = _get_addon_name(cls.__module__)
_rest_controllers_per_module[cls._module].append(
{
"root_path": root_path,
"collection_name": collection_name,
"controller_class": cls,
}
)
_logger.debug(
"Added rest controller %s for module %s",
_rest_controllers_per_module[cls._module][-1],
cls._module,
)


class RestController(Controller, metaclass=RestControllerType):
class RestController(Controller):
"""Generic REST Controller
This controller is the base controller used by as base controller for all the REST
Expand Down Expand Up @@ -130,6 +94,38 @@ class ControllerB(ControllerB):

_component_context_provider = "component_context_provider"

@classmethod
def __init_subclass__(cls):
if (
"RestController" in globals()
and RestController in cls.__bases__
and Controller not in cls.__bases__
):
# Ensure that Controller's __init_subclass__ kicks in.
cls.__bases__ += (Controller,)
super().__init_subclass__()
if "RestController" not in globals() or not any(
issubclass(b, RestController) for b in cls.__bases__
):
return
# register the rest controller into the rest controllers registry
root_path = getattr(cls, "_root_path", None)
collection_name = getattr(cls, "_collection_name", None)
if root_path and collection_name:
cls._module = _get_addon_name(cls.__module__)
_rest_controllers_per_module[cls._module].append(
{
"root_path": root_path,
"collection_name": collection_name,
"controller_class": cls,
}
)
_logger.debug(
"Added rest controller %s for module %s",
_rest_controllers_per_module[cls._module][-1],
cls._module,
)

def _get_component_context(self, collection=None):
"""
This method can be inherited to add parameter into the component
Expand Down
Loading

0 comments on commit b320af1

Please sign in to comment.