From 4342b9047e02d51780fd9d96c80df8f25ea31cd2 Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Fri, 4 Feb 2022 16:32:07 +0100 Subject: [PATCH] [ADD] extendable: New addon to alllow usage of the Extendable python package into odoo --- extendable/README.rst | 96 ++++ extendable/__init__.py | 1 + extendable/__manifest__.py | 16 + extendable/models/__init__.py | 2 + .../models/extendable_registry_loader.py | 68 +++ extendable/models/ir_http.py | 30 ++ extendable/readme/CONTRIBUTORS.rst | 1 + extendable/readme/DESCRIPTION.rst | 3 + extendable/readme/ROADMAP.rst | 3 + extendable/readme/USAGE.rst | 2 + extendable/registry.py | 9 + extendable/static/description/index.html | 434 ++++++++++++++++++ extendable/tests/__init__.py | 0 extendable/tests/common.py | 62 +++ requirements.txt | 1 + setup/extendable/odoo/addons/extendable | 1 + setup/extendable/setup.py | 6 + 17 files changed, 735 insertions(+) create mode 100644 extendable/README.rst create mode 100644 extendable/__init__.py create mode 100644 extendable/__manifest__.py create mode 100644 extendable/models/__init__.py create mode 100644 extendable/models/extendable_registry_loader.py create mode 100644 extendable/models/ir_http.py create mode 100644 extendable/readme/CONTRIBUTORS.rst create mode 100644 extendable/readme/DESCRIPTION.rst create mode 100644 extendable/readme/ROADMAP.rst create mode 100644 extendable/readme/USAGE.rst create mode 100644 extendable/registry.py create mode 100644 extendable/static/description/index.html create mode 100644 extendable/tests/__init__.py create mode 100644 extendable/tests/common.py create mode 120000 setup/extendable/odoo/addons/extendable create mode 100644 setup/extendable/setup.py diff --git a/extendable/README.rst b/extendable/README.rst new file mode 100644 index 000000000..be338f112 --- /dev/null +++ b/extendable/README.rst @@ -0,0 +1,96 @@ +========== +Extendable +========== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Frest--framework-lightgray.png?logo=github + :target: https://github.com/OCA/rest-framework/tree/14.0/extendable + :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-14-0/rest-framework-14-0-extendable + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/271/14.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This addon allows you to use the `Extendable library`_ within Odoo. + +.. _Extendable library: : + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +By default, all extendable classes defined into your odoo addons are automatically +loaded by the current addon according to the dependency graph computed by Odoo. + +Known issues / Roadmap +====================== + +The `roadmap `_ +and `known issues `_ can +be found on GitHub. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* ACSONE SA/NV + +Contributors +~~~~~~~~~~~~ + +* Laurent Mignon + +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-lmignon| image:: https://github.com/lmignon.png?size=40px + :target: https://github.com/lmignon + :alt: lmignon + +Current `maintainer `__: + +|maintainer-lmignon| + +This module is part of the `OCA/rest-framework `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/extendable/__init__.py b/extendable/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/extendable/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/extendable/__manifest__.py b/extendable/__manifest__.py new file mode 100644 index 000000000..6e4ff7555 --- /dev/null +++ b/extendable/__manifest__.py @@ -0,0 +1,16 @@ +# Copyright 2022 ACSONE SA/NV +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) + +{ + "name": "Extendable", + "summary": """ + Extendable classes registry loader for Odoo""", + "version": "14.0.1.0.0", + "development_status": "Beta", + "maintainers": ["lmignon"], + "license": "LGPL-3", + "author": "ACSONE SA/NV, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/rest-framework", + "depends": [], + "external_dependencies": {"python": ["extendable"]}, +} diff --git a/extendable/models/__init__.py b/extendable/models/__init__.py new file mode 100644 index 000000000..a1ae8666f --- /dev/null +++ b/extendable/models/__init__.py @@ -0,0 +1,2 @@ +from . import extendable_registry_loader +from . import ir_http diff --git a/extendable/models/extendable_registry_loader.py b/extendable/models/extendable_registry_loader.py new file mode 100644 index 000000000..993c85506 --- /dev/null +++ b/extendable/models/extendable_registry_loader.py @@ -0,0 +1,68 @@ +# Copyright 2022 ACSONE SA/NV +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) + +""" + +Extendale classes Loader +======================== + +Load the extendable classes at the build of a registry. + +""" +from typing import List, Optional + +import odoo +from odoo import api, models + +from extendable.registry import ExtendableClassesRegistry + +from ..registry import _extendable_registries_database + + +class ExtendableRegistryLoader(models.AbstractModel): + _name = "extendable.registry.loader" + _description = "Extendable Registry Loader" + + def _register_hook(self): + # This method is called by Odoo when the registry is built, + # so in case the registry is rebuilt (cache invalidation, ...), + # we have to to rebuild the extendable classes. We use a new + # registry so we have an empty cache and we'll add extendable classes + # in it. + registry = self._init_global_registry() + self.build_registry(registry) + + @api.model + def _init_global_registry(self): + registry = ExtendableClassesRegistry() + _extendable_registries_database[self.env.cr.dbname] = registry + return registry + + @api.model + def build_registry( + self, + registry: ExtendableClassesRegistry, + states: Optional[List[str]] = None, + exclude_addons: Optional[List[str]] = None, + ): + if not states: + states = ("installed", "to upgrade") + # lookup all the installed (or about to be) addons and generate + # the graph, so we can load the components following the order + # of the addons' dependencies + graph = odoo.modules.graph.Graph() + graph.add_module(self.env.cr, "base") + + 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) + module_matchings = [] + for m in graph: + module_matchings.append(f"odoo.addons.{m.name}.*") + registry.init_registry(module_matchings) diff --git a/extendable/models/ir_http.py b/extendable/models/ir_http.py new file mode 100644 index 000000000..577179c46 --- /dev/null +++ b/extendable/models/ir_http.py @@ -0,0 +1,30 @@ +# Copyright 2022 ACSONE SA/NV +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) + +from contextlib import contextmanager + +from odoo import models +from odoo.http import request + +from extendable import context + +from ..registry import _extendable_registries_database + + +class IrHttp(models.AbstractModel): + _inherit = "ir.http" + + @classmethod + def _dispatch(cls): + with cls._extendable_context_registry(): + return super()._dispatch() + + @classmethod + @contextmanager + def _extendable_context_registry(cls): + registry = _extendable_registries_database.get(request.env.cr.dbname, {}) + token = context.extendable_registry.set(registry) + try: + yield + finally: + context.extendable_registry.reset(token) diff --git a/extendable/readme/CONTRIBUTORS.rst b/extendable/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..172b2d223 --- /dev/null +++ b/extendable/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Laurent Mignon diff --git a/extendable/readme/DESCRIPTION.rst b/extendable/readme/DESCRIPTION.rst new file mode 100644 index 000000000..f03f64e1f --- /dev/null +++ b/extendable/readme/DESCRIPTION.rst @@ -0,0 +1,3 @@ +This addon allows you to use the `Extendable library`_ within Odoo. + +.. _Extendable library: : diff --git a/extendable/readme/ROADMAP.rst b/extendable/readme/ROADMAP.rst new file mode 100644 index 000000000..67186ed02 --- /dev/null +++ b/extendable/readme/ROADMAP.rst @@ -0,0 +1,3 @@ +The `roadmap `_ +and `known issues `_ can +be found on GitHub. diff --git a/extendable/readme/USAGE.rst b/extendable/readme/USAGE.rst new file mode 100644 index 000000000..45f7a97a5 --- /dev/null +++ b/extendable/readme/USAGE.rst @@ -0,0 +1,2 @@ +By default, all extendable classes defined into your odoo addons are automatically +loaded by the current addon according to the dependency graph computed by Odoo. diff --git a/extendable/registry.py b/extendable/registry.py new file mode 100644 index 000000000..6c831ca2d --- /dev/null +++ b/extendable/registry.py @@ -0,0 +1,9 @@ +# Copyright 2022 ACSONE SA/NV +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) + + +class ExtendableRegistriesDatabase(dict): + """Holds an extendable classses registry for each database""" + + +_extendable_registries_database = ExtendableRegistriesDatabase() diff --git a/extendable/static/description/index.html b/extendable/static/description/index.html new file mode 100644 index 000000000..63cd3713c --- /dev/null +++ b/extendable/static/description/index.html @@ -0,0 +1,434 @@ + + + + + + +Extendable + + + +
+

Extendable

+ + +

Beta License: LGPL-3 OCA/rest-framework Translate me on Weblate Try me on Runbot

+

This addon allows you to use the Extendable library within Odoo.

+

Table of contents

+ +
+

Usage

+

By default, all extendable classes defined into your odoo addons are automatically +loaded by the current addon according to the dependency graph computed by Odoo.

+
+
+

Known issues / Roadmap

+

The roadmap +and known issues can +be found on GitHub.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • ACSONE SA/NV
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

Current maintainer:

+

lmignon

+

This module is part of the OCA/rest-framework project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/extendable/tests/__init__.py b/extendable/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/extendable/tests/common.py b/extendable/tests/common.py new file mode 100644 index 000000000..e1ed45094 --- /dev/null +++ b/extendable/tests/common.py @@ -0,0 +1,62 @@ +# Copyright 2022 ACSONE SA/NV +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) + +from contextlib import contextmanager + +import odoo +from odoo import api +from odoo.tests import common + +from extendable.context import extendable_registry + + +def _get_addon_name(full_name: str) -> str: + # The (Odoo) module name can be in the ``odoo.addons`` namespace + # or not. For instance, module ``sale`` can be imported as + # ``odoo.addons.sale`` (the right way) or ``sale`` (for backward + # compatibility). + module_parts = full_name.split(".") + if len(module_parts) > 2 and module_parts[:2] == ["odoo", "addons"]: + addon_name = full_name.split(".")[2] + else: + addon_name = full_name.split(".")[0] + return addon_name + + +@contextmanager +def new_rollbacked_env(): + registry = odoo.registry(common.get_db_name()) + uid = odoo.SUPERUSER_ID + cr = registry.cursor() + try: + yield api.Environment(cr, uid, {}) + finally: + cr.rollback() # we shouldn't have to commit anything + cr.close() + + +class ExtendableMixin(object): + @classmethod + def setUpExtendable(cls): + with new_rollbacked_env() as env: + builder = env["extendable.registry.loader"] + # build the extendable classes of every installed addons + extendable_registry = builder._init_global_registry() + cls._extendable_registry = extendable_registry + # ensure that we load only the extendable classes of the 'installed' + # modules, not 'to install', which means we load only the + # dependencies of the tested addons, not the siblings or + # children addons + builder.build_registry(extendable_registry, states=("installed",)) + # build the extendable classes of the current tested addon + current_addon = _get_addon_name(cls.__module__) + extendable_registry.init_registry([f"odoo.addons.{current_addon}.*"]) + + # pylint: disable=W8106 + def setUp(self): + # initialize the registry context + token = extendable_registry.set(self._extendable_registry) + + @self.addCleanup + def reset_context(): + extendable_registry.reset(token) diff --git a/requirements.txt b/requirements.txt index af21df273..c24156a6e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,7 @@ apispec apispec>=4.0.0 cerberus +extendable graphene graphql_server jsondiff diff --git a/setup/extendable/odoo/addons/extendable b/setup/extendable/odoo/addons/extendable new file mode 120000 index 000000000..967b59848 --- /dev/null +++ b/setup/extendable/odoo/addons/extendable @@ -0,0 +1 @@ +../../../../extendable \ No newline at end of file diff --git a/setup/extendable/setup.py b/setup/extendable/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/extendable/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)