forked from OCA/connector-magento
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.py
81 lines (70 loc) · 3.07 KB
/
connector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Guewen Baconnier
# Copyright 2013-2015 Camptocamp SA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp import models, fields
from openerp.addons.connector.connector import ConnectorEnvironment
from openerp.addons.connector.checkpoint import checkpoint
def get_environment(session, model_name, backend_id):
""" Create an environment to work with. """
backend_record = session.env['magento.backend'].browse(backend_id)
env = ConnectorEnvironment(backend_record, session, model_name)
lang = backend_record.default_lang_id
lang_code = lang.code if lang else 'en_US'
if lang_code == session.context.get('lang'):
return env
else:
with env.session.change_context(lang=lang_code):
return env
class MagentoBinding(models.AbstractModel):
""" Abstract Model for the Bindigs.
All the models used as bindings between Magento and OpenERP
(``magento.res.partner``, ``magento.product.product``, ...) should
``_inherit`` it.
"""
_name = 'magento.binding'
_inherit = 'external.binding'
_description = 'Magento Binding (abstract)'
# openerp_id = openerp-side id must be declared in concrete model
backend_id = fields.Many2one(
comodel_name='magento.backend',
string='Magento Backend',
required=True,
ondelete='restrict',
)
# fields.Char because 0 is a valid Magento ID
magento_id = fields.Char(string='ID on Magento')
_sql_constraints = [
('magento_uniq', 'unique(backend_id, magento_id)',
'A binding already exists with the same Magento ID.'),
]
def add_checkpoint(session, model_name, record_id, backend_id):
""" Add a row in the model ``connector.checkpoint`` for a record,
meaning it has to be reviewed by a user.
:param session: current session
:type session: :class:`openerp.addons.connector.session.ConnectorSession`
:param model_name: name of the model of the record to be reviewed
:type model_name: str
:param record_id: ID of the record to be reviewed
:type record_id: int
:param backend_id: ID of the Magento Backend
:type backend_id: int
"""
return checkpoint.add_checkpoint(session, model_name, record_id,
'magento.backend', backend_id)