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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Guewen Baconnier
# Copyright 2013 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, api
from . import backend
class ConnectorBackend(models.AbstractModel):
""" An instance of an external backend to synchronize with.
The backends have to ``_inherit`` this model in the connectors
modules.
"""
_name = 'connector.backend'
_description = 'Connector Backend'
_backend_type = None
name = fields.Char(required=True)
# replace by a selection in concrete models
version = fields.Selection(selection=[], required=True)
@api.multi
def get_backend(self):
""" For a record of backend, returns the appropriate instance
of :py:class:`~connector.backend.Backend`.
"""
self.ensure_one()
if self._backend_type is None:
raise ValueError('The backend %s has no _backend_type' % self)
return backend.get_backend(self._backend_type, self.version)
@api.multi
def add_checkpoint(self, model=None, record_id=None, message=None):
"""Add a checkpoint for current backend."""
self.ensure_one()
cp_model = self.env['connector.checkpoint']
res = None
if record_id and model:
res = cp_model.create_from_name(
model, record_id, self._name, self.id, message=message)
elif message:
res = cp_model.create_from_message(
self._name, self.id, message=message)
return res
class ExternalBinding(models.AbstractModel):
""" An abstract model for bindings to external records.
An external binding is a binding between a backend and OpenERP. For
example, for a partner, it could be ``magento.res.partner`` or for a
product, ``magento.product``.
The final model, will be an ``_inherits`` of the OpenERP model and
will ``_inherit`` this model.
It will have a relation to the record (via ``_inherits``) and to the
concrete backend model (``magento.backend`` for instance).
It will also contains all the data relative to the backend for the
record.
It needs to implements at least these fields:
openerp_id
The many2one to the record it links (used by ``_inherits``).
backend_id
The many2one to the backend (for instance ``magento.backend``).
magento_id or prestashop_id or ...
The ID on the backend.
sync_date
Last date of synchronization
The definition of the relations in ``_columns`` is to be done in the
concrete classes because the relations themselves do not exist in
this addon.
For example, for a ``res.partner.category`` from Magento, I would have
(this is a consolidation of all the columns from the abstract models,
in ``magentoerpconnect`` you would not find that)::
class MagentoResPartnerCategory(models.Model):
_name = 'magento.res.partner.category'
_inherits = {'res.partner.category': 'openerp_id'}
openerp_id = fields.Many2one(comodel_name='res.partner.category',
string='Partner Category',
required=True,
ondelete='cascade')
backend_id = fields.Many2one(
comodel_name='magento.backend',
string='Magento Backend',
required=True,
ondelete='restrict')
magento_id = fields.Char(string='ID on Magento')
tax_class_id = fields.Integer(string='Tax Class ID')
_sql_constraints = [
('magento_uniq', 'unique(backend_id, magento_id)',
'Partner Tag with same ID on Magento already exists.'),
]
"""
_name = 'external.binding'
_description = 'External Binding (abstract)'
sync_date = fields.Datetime(string='Last synchronization date')
# add other fields in concrete models