-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by simahawk
- Loading branch information
Showing
17 changed files
with
202 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
# @author Simone Orsi <[email protected]> | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). | ||
|
||
import logging | ||
|
||
from odoo import api, fields, models, tools | ||
from odoo.tools import DotDict | ||
|
||
|
@@ -10,6 +12,8 @@ | |
|
||
from ..utils import APP_VERSION, RUNNING_ENV | ||
|
||
_logger = logging.getLogger(__file__) | ||
|
||
|
||
class ShopfloorApp(models.Model): | ||
"""Backend for a Shopfloor app.""" | ||
|
@@ -281,6 +285,13 @@ def _is_component_registry_ready(self): | |
return comp_registry and comp_registry.ready | ||
|
||
def _get_services(self): | ||
forced_services = self.env.context.get("sf_service_components") | ||
if forced_services: | ||
_logger.debug( | ||
"_get_services forced services: %s", | ||
", ".join([x._usage for x in forced_services]), | ||
) | ||
return forced_services | ||
if not self._is_component_registry_ready(): | ||
# No service is available before the registry has been loaded. | ||
# This is a very special case, when the odoo registry is being | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
# Copyright 2021 Camptocamp SA (http://www.camptocamp.com) | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). | ||
# @author Simone Orsi <[email protected]> | ||
import logging | ||
import os | ||
from functools import wraps | ||
|
||
from odoo.modules.module import load_information_from_description_file | ||
from odoo.tools.config import config as odoo_config | ||
|
||
from odoo.addons.base_rest.controllers.main import _PseudoCollection | ||
from odoo.addons.component.core import WorkContext, _component_databases | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
def ensure_model(model_name): | ||
"""Decorator to ensure data method is called w/ the right recordset.""" | ||
|
@@ -65,3 +71,67 @@ def _get_running_env(): | |
|
||
|
||
RUNNING_ENV = _get_running_env() | ||
|
||
|
||
def register_new_services(env, *component_classes, apps=None): | ||
"""Enforce registration of new services straight from their classes. | ||
At some stage (eg: a post install hook) the component registry | ||
might not be fully loaded and the components might be not fully "ready". | ||
For this reason, not all the inherited properties | ||
are granted to be available (eg: _collection) | ||
and they won't be available for `components_registry.lookup` call. | ||
This is why, to ensure that the new services endpoints | ||
are registered in any case we prepare their classes "manually" here | ||
and pass them over to sf.app._get_services. | ||
In this way, if their endpoint_route rows are not there | ||
they will be generated on the fly, | ||
Please, note that this func is very low level | ||
and must be used only in particular situations (such as install hooks). | ||
""" | ||
if odoo_config["test_enable"]: | ||
# When installing modules for tests, there's nothing to do. | ||
# Moreover, it can lead to weird service behavior | ||
# because the classes will be decorated | ||
# but when they are not fully ready. | ||
return | ||
components = load_components_without_registry(env, *component_classes) | ||
if not components: | ||
return | ||
apps = apps or env["shopfloor.app"].search([]) | ||
apps.with_context(sf_service_components=components)._register_controllers() | ||
|
||
|
||
def load_components_without_registry(env, *component_classes): | ||
"""Prepare components' instances w/o real registry lookup.""" | ||
collection = _PseudoCollection("shopfloor.app", env) | ||
rest_service = env["rest.service.registration"] | ||
if not _component_databases.get(env.cr.dbname): | ||
# no registry ready yet (eg: install via cmd line, tests, etc) | ||
_logger.info("component registry not ready yet") | ||
return [] | ||
work = WorkContext(model_name=rest_service._name, collection=collection) | ||
return [comp(work) for comp in component_classes] | ||
|
||
|
||
def purge_endpoints(env, service_usage, endpoint=None): | ||
"""Remove stale services' endpoints routes. | ||
When scenario are removed (eg: module uninstalled) | ||
their routes must be cleaned up. | ||
You can use this function to easily clean them for all apps. | ||
Motivation: registered routes for a given app are taken | ||
from the endpoint_route table via `route_group`. | ||
As the table is populated dynamically with registered components | ||
when a component is removed there's no direct update to the table. | ||
Hence, is the responsibility of the dev to take care of the cleanup. | ||
""" | ||
apps = env["shopfloor.app"].search([]) | ||
for app in apps: | ||
route = app.api_url_for_service(service_usage, endpoint=endpoint) | ||
route = route + ("/" if not endpoint else "") | ||
env.cr.execute("DELETE FROM endpoint_route WHERE route like %s", (f"{route}%",)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from . import actions | ||
from . import models | ||
from . import services | ||
from .hooks import post_init_hook, uninstall_hook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright 2023 Camptocamp SA | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||
|
||
import logging | ||
|
||
from odoo import SUPERUSER_ID, api | ||
|
||
from odoo.addons.shopfloor_base.utils import purge_endpoints, register_new_services | ||
|
||
from .services.delivery_shipment import DeliveryShipment as Service | ||
|
||
_logger = logging.getLogger(__file__) | ||
|
||
|
||
def post_init_hook(cr, registry): | ||
env = api.Environment(cr, SUPERUSER_ID, {}) | ||
_logger.info("Register routes for %s", Service._usage) | ||
register_new_services(env, Service) | ||
|
||
|
||
def uninstall_hook(cr, registry): | ||
env = api.Environment(cr, SUPERUSER_ID, {}) | ||
_logger.info("Refreshing routes for existing apps") | ||
purge_endpoints(env, Service._usage) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from . import actions | ||
from . import services | ||
from .hooks import post_init_hook, uninstall_hook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright 2023 Camptocamp SA | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||
|
||
import logging | ||
|
||
from odoo import SUPERUSER_ID, api | ||
|
||
from odoo.addons.shopfloor_base.utils import purge_endpoints, register_new_services | ||
|
||
from .services.manual_product_transfer import ManualProductTransfer as Service | ||
|
||
_logger = logging.getLogger(__file__) | ||
|
||
|
||
def post_init_hook(cr, registry): | ||
env = api.Environment(cr, SUPERUSER_ID, {}) | ||
_logger.info("Register routes for %s", Service._usage) | ||
register_new_services(env, Service) | ||
|
||
|
||
def uninstall_hook(cr, registry): | ||
env = api.Environment(cr, SUPERUSER_ID, {}) | ||
_logger.info("Refreshing routes for existing apps") | ||
purge_endpoints(env, Service._usage) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from . import services | ||
from . import models | ||
from .hooks import post_init_hook, uninstall_hook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright 2023 Camptocamp SA | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||
|
||
import logging | ||
|
||
from odoo import SUPERUSER_ID, api | ||
|
||
from odoo.addons.shopfloor_base.utils import purge_endpoints, register_new_services | ||
|
||
from .services.reception import Reception as Service | ||
|
||
_logger = logging.getLogger(__file__) | ||
|
||
|
||
def post_init_hook(cr, registry): | ||
env = api.Environment(cr, SUPERUSER_ID, {}) | ||
_logger.info("Register routes for %s", Service._usage) | ||
register_new_services(env, Service) | ||
|
||
|
||
def uninstall_hook(cr, registry): | ||
env = api.Environment(cr, SUPERUSER_ID, {}) | ||
_logger.info("Refreshing routes for existing apps") | ||
purge_endpoints(env, Service._usage) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
from . import services | ||
from .hooks import post_init_hook | ||
from .hooks import post_init_hook, uninstall_hook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,5 @@ | |
"demo/shopfloor_menu_demo.xml", | ||
], | ||
"post_init_hook": "post_init_hook", | ||
"uninstall_hook": "uninstall_hook", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from . import models | ||
from . import services | ||
from .hooks import post_init_hook, uninstall_hook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright 2023 Camptocamp SA | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||
|
||
import logging | ||
|
||
from odoo import SUPERUSER_ID, api | ||
|
||
from odoo.addons.shopfloor_base.utils import purge_endpoints, register_new_services | ||
|
||
from .services.workstation import ShopfloorWorkstation as Service | ||
|
||
_logger = logging.getLogger(__file__) | ||
|
||
|
||
def post_init_hook(cr, registry): | ||
env = api.Environment(cr, SUPERUSER_ID, {}) | ||
_logger.info("Register routes for %s", Service._usage) | ||
register_new_services(env, Service) | ||
|
||
|
||
def uninstall_hook(cr, registry): | ||
env = api.Environment(cr, SUPERUSER_ID, {}) | ||
_logger.info("Refreshing routes for existing apps") | ||
purge_endpoints(env, Service._usage) |