Skip to content

Commit 6e3ab38

Browse files
author
Guewen Baconnier
committed
Merge pull request #74 from jssuzanne/8.0
Deprecate install_in_connector()
2 parents 814b099 + 8406a62 commit 6e3ab38

12 files changed

Lines changed: 107 additions & 103 deletions

File tree

connector/CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changelog
22
---------
33

4+
Future
5+
~~~~~~
6+
7+
* method 'install_in_connector' is now deprecated
48

59
3.1.0 (2015-05-15)
610
~~~~~~~~~~~~~~~~~~

connector/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
from . import checkpoint
88
from . import controllers
99
from . import jobrunner
10+
from . import ir_module_module

connector/connector.py

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919
#
2020
##############################################################################
2121

22-
import inspect
2322
import logging
2423
from contextlib import contextmanager
25-
from openerp import models
26-
2724
from .deprecate import log_deprecate, DeprecatedClass
2825

2926
_logger = logging.getLogger(__name__)
@@ -49,42 +46,32 @@ def _get_openerp_module_name(module_path):
4946

5047

5148
def install_in_connector():
52-
""" Installs an OpenERP module in the ``Connector`` framework.
49+
log_deprecate("This call to 'install_in_connector()' has no effect and is "
50+
"not required.")
51+
52+
53+
def is_module_installed(env, module_name):
54+
""" Check if an Odoo addon is installed.
5355
54-
It has to be called once per OpenERP module to plug.
56+
The function might be called before `connector` is even installed;
57+
in such case, `ir_module_module.is_module_installed()` is not available yet
58+
and this is why we first check the installation of `connector` by looking
59+
up for a model in the registry.
5560
56-
Under the cover, it creates a ``openerp.models.AbstractModel`` whose
57-
name is the name of the module with a ``.intalled`` suffix:
58-
``{name_of_the_openerp_module_to_install}.installed``.
61+
:param module_name: name of the addon to check being 'connector' or
62+
an addon depending on it
5963
60-
The connector then uses this model to know when the OpenERP module
61-
is installed or not and whether it should use the ConnectorUnit
62-
classes of this module or not and whether it should fire the
63-
consumers of events or not.
6464
"""
65-
# Get the module of the caller
66-
module = inspect.getmodule(inspect.currentframe().f_back)
67-
openerp_module_name = _get_openerp_module_name(module.__name__)
68-
# Build a new AbstractModel with the name of the module and the suffix
69-
name = "%s.installed" % openerp_module_name
70-
class_name = name.replace('.', '_')
71-
# we need to call __new__ and __init__ in 2 phases because
72-
# __init__ needs to have the right __module__ and _module attributes
73-
model = models.MetaModel.__new__(models.MetaModel, class_name,
74-
(models.AbstractModel,), {'_name': name})
75-
# Update the module of the model, it should be the caller's one
76-
model._module = openerp_module_name
77-
model.__module__ = module.__name__
78-
models.MetaModel.__init__(model, class_name,
79-
(models.AbstractModel,), {'_name': name})
80-
81-
82-
# install the connector itself
83-
install_in_connector()
84-
85-
86-
def is_module_installed(pool, module_name):
87-
return bool(pool.get('%s.installed' % module_name))
65+
if env.registry.get('connector.backend'):
66+
if module_name == 'connector':
67+
# fast-path: connector is necessarily installed because
68+
# the model is in the registry
69+
return True
70+
# for another addon, check in ir.module.module
71+
return env['ir.module.module'].is_module_installed(module_name)
72+
73+
# connector module is not installed neither any sub-addons
74+
return False
8875

8976

9077
def get_openerp_module(cls_or_func):

connector/doc/guides/bootstrap_connector.rst

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,6 @@ Of course, we also need to create the ``__init__.py`` file where we will
6565
put the imports of our python modules.
6666

6767

68-
***********************************
69-
Install the module in the connector
70-
***********************************
71-
72-
Each new module needs to be plugged in the connector's framework.
73-
That's just a matter of following a convention and creating
74-
``connector_coffee/connector.py`` in which you will call the
75-
``install_in_connector`` function::
76-
77-
from openerp.addons.connector.connector import install_in_connector
78-
79-
80-
install_in_connector()
81-
82-
.. warning:: If you miss this line of code, your ConnectorUnit classes won't
83-
be found.
84-
85-
.. note:: The reason for this is that Odoo may import the Python modules
86-
of uninstalled modules, so it automatically registers the
87-
events and ConnectorUnit classes, even for uninstalled
88-
modules.
89-
90-
To prevent this, we use a little trick: create an abstract
91-
model and look in the registry if it is loaded.
92-
93-
9468
********************
9569
Declare the backends
9670
********************

connector/doc/locale/fr/LC_MESSAGES/api.po

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -937,38 +937,6 @@ msgstr ""
937937
"Ainsi nous serons capable de les filtrer en fonction de l'état "
938938
"d'installation des modules."
939939

940-
#: ../../../connector.pydocstring of
941-
#: connector.connector.install_in_connector:1
942-
msgid "Installs an OpenERP module in the ``Connector`` framework."
943-
msgstr "Installe un module OpenERP dans le framework ``Connector``."
944-
945-
#: ../../../connector.pydocstring of
946-
#: connector.connector.install_in_connector:3
947-
msgid "It has to be called once per OpenERP module to plug."
948-
msgstr "Il doit être appelé une fois par module OpenERP à brancher."
949-
950-
#: ../../../connector.pydocstring of
951-
#: connector.connector.install_in_connector:5
952-
msgid ""
953-
"Under the cover, it creates a ``openerp.models.AbstractModel`` whose name is"
954-
" the name of the module with a ``.intalled`` suffix: "
955-
"``{name_of_the_openerp_module_to_install}.installed``."
956-
msgstr ""
957-
"Pour être précis, il crée un ``openerp.models.AbstractModel`` dont le nom "
958-
"est le nom du module avec un suffixe ``.intalled`` : "
959-
"``{name_of_the_openerp_module_to_install}.installed``."
960-
961-
#: ../../../connector.pydocstring of
962-
#: connector.connector.install_in_connector:9
963-
msgid ""
964-
"The connector then uses this model to know when the OpenERP module is "
965-
"installed or not and whether it should use the ConnectorUnit classes of this"
966-
" module or not and whether it should fire the consumers of events or not."
967-
msgstr ""
968-
"Le connecteur utilise ensuite ce modèle pour savoir quand le module OpenERP "
969-
"est installé ou non, s'il doit utiliser les classes ConnectorUnit de ce "
970-
"module ou non et s'il doit allumer les consommateurs d'événements ou non."
971-
972940
#: ../../api/api_event.rst:2
973941
msgid "Event"
974942
msgstr "Événement"

connector/doc/locale/fr/LC_MESSAGES/guides.po

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,11 @@ msgstr "Installation du module dans le connecteur"
6666
msgid ""
6767
"Each new module needs to be plugged in the connector's framework. That's "
6868
"just a matter of following a convention and creating ``connector_coffee/"
69-
"connector.py`` in which you will call the ``install_in_connector`` function::"
69+
"connector.py``"
7070
msgstr ""
7171
"Chaque nouveau module doit être branché dans le framework du connecteur. "
7272
"C'est juste un question de suivre une convention et créer un fichier "
73-
"``connector_coffee/connector.py`` dans lequel vous allez appeler la fonction "
74-
"``install_in_connector``::"
73+
"``connector_coffee/connector.py``"
7574

7675
#: ../../guides/bootstrap_connector.rst:82
7776
msgid ""

connector/ir_module_module.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
##############################################################################
3+
#
4+
# Author: Guewen Baconnier
5+
# Copyright 2012-2013 Camptocamp SA
6+
# Copyright 2015 anybox SA
7+
#
8+
# This program is free software: you can redistribute it and/or modify
9+
# it under the terms of the GNU Affero General Public License as
10+
# published by the Free Software Foundation, either version 3 of the
11+
# License, or (at your option) any later version.
12+
#
13+
# This program is distributed in the hope that it will be useful,
14+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
# GNU Affero General Public License for more details.
17+
#
18+
# You should have received a copy of the GNU Affero General Public License
19+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
#
21+
##############################################################################
22+
23+
from openerp import models, api
24+
from openerp.tools.cache import ormcache
25+
26+
27+
class IrModuleModule(models.Model):
28+
"""Overwrite ir.module.module to add cached method 'is_module_installed'.
29+
This method is cached, because connector will always check if a module is
30+
installed before do action.
31+
32+
All update (write method) on the field state, invalidate the cache
33+
"""
34+
_inherit = 'ir.module.module'
35+
36+
@ormcache(skiparg=1)
37+
def is_module_installed(self, module_name):
38+
states = ('installed', 'to upgrade')
39+
return bool(len(self.search([('name', '=', module_name),
40+
('state', 'in', states)])))
41+
42+
@api.multi
43+
def write(self, values):
44+
res = super(IrModuleModule, self).write(values)
45+
if 'state' in values:
46+
self.clear_caches()
47+
48+
return res

connector/producer.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from .event import (on_record_create,
3737
on_record_write,
3838
on_record_unlink)
39+
from .connector import is_module_installed
3940

4041

4142
create_original = models.BaseModel.create
@@ -45,7 +46,7 @@
4546
@openerp.api.returns('self', lambda value: value.id)
4647
def create(self, vals):
4748
record_id = create_original(self, vals)
48-
if self.pool.get('connector.installed') is not None:
49+
if is_module_installed(self.env, 'connector'):
4950
session = ConnectorSession(self.env.cr, self.env.uid,
5051
context=self.env.context)
5152
on_record_create.fire(session, self._name, record_id.id, vals)
@@ -59,7 +60,7 @@ def create(self, vals):
5960
@openerp.api.multi
6061
def write(self, vals):
6162
result = write_original(self, vals)
62-
if self.pool.get('connector.installed') is not None:
63+
if is_module_installed(self.env, 'connector'):
6364
session = ConnectorSession(self.env.cr, self.env.uid,
6465
context=self.env.context)
6566
if on_record_write.has_consumer_for(session, self._name):
@@ -75,7 +76,7 @@ def write(self, vals):
7576

7677
@openerp.api.multi
7778
def unlink(self):
78-
if self.pool.get('connector.installed') is not None:
79+
if is_module_installed(self.env, 'connector'):
7980
session = ConnectorSession(self.env.cr, self.env.uid,
8081
context=self.env.context)
8182
if on_record_unlink.has_consumer_for(session, self._name):

connector/queue/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ def _find_or_create_channel(self, channel_path):
515515
@api.model
516516
def _register_jobs(self):
517517
for func in JOB_REGISTRY:
518-
if not is_module_installed(self.pool, get_openerp_module(func)):
518+
if not is_module_installed(self.env, get_openerp_module(func)):
519519
continue
520520
func_name = '%s.%s' % (func.__module__, func.__name__)
521521
if not self.search_count([('name', '=', func_name)]):

connector/session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,4 @@ def is_module_installed(self, module_name):
262262
model with name ``module_name.installed`` is loaded in the
263263
registry.
264264
"""
265-
return is_module_installed(self.pool, module_name)
265+
return is_module_installed(self.env, module_name)

0 commit comments

Comments
 (0)