Skip to content

Instantly share code, notes, and snippets.

@guewen
Created January 28, 2014 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guewen/3528fe4be6bf527ed9d9 to your computer and use it in GitHub Desktop.
Save guewen/3528fe4be6bf527ed9d9 to your computer and use it in GitHub Desktop.
# adds a user to be used by the connector
class res_company(orm.Model):
_inherit = 'res.company'
_columns = {
'connector_user_id': fields.many2one('res.users',
string='Connector User'),
}
def copy_data(self, cr, uid, id, default=None, context=None):
if default is None:
default = {}
default.update({
'connector_user_id': False,
})
return super(res_company, self).copy_data(
cr, uid, id, default=default, context=context)
# split the run() method to have a _import() method, we'll be able to extend it with super
class FooBarImportSynchronizer(ImportSynchronizer):
""" Base importer for FooBar """
def must_skip(self):
...
def _before_import(self):
...
def _is_uptodate(self, binding_id):
...
def _import_dependencies(self):
...
def _map_data(self):
...
def _validate_data(self, data):
...
def _get_binding_id(self):
...
def _create_data(self, map_record, **kwargs):
...
def _create(self, data):
...
def _update_data(self, map_record, **kwargs):
...
def _update(self, binding_id, data):
...
def _after_import(self, binding_id):
...
def run(self, foo_id, force=False, record=None):
self.foo_id = foo_id
if record is not None:
self.foo_record = record
else:
try:
self.foo_record = self._get_foo_data()
except IDMissingInBackend:
return _('Record does no longer exist in Foo')
binding_id = self._get_binding_id()
reason = self.must_skip()
if reason:
return reason
if not force and self._is_uptodate(binding_id):
return _('Already up-to-date.')
self._before_import()
# import the missing linked resources
self._import_dependencies()
self._import(binding_id)
def _import(self, binding_id):
""" Import the external record.
Can be inherited to modify for instance the session
(change current user, values in context, ...)
"""
map_record = self._map_data()
if binding_id:
record = self._update_data(map_record)
self._update(binding_id, record)
else:
record = self._create_data(map_record)
binding_id = self._create(record)
self.binder.bind(self.foo_id, binding_id)
self._after_import(binding_id)
# implementation of an import: get the store of the sale and import it using the configured user.
@foo
class SaleOrderImport(FooBarImportSynchronizer):
_model_name = 'foo.sale.order'
def _import(self, binding_id):
fshop_binder = self.get_binder_for_model('foo.shop')
fshop_id = fshop_binder.to_openerp(self.foo_record['shop_id'])
fshop = self.session.browse('foo.shop', fshop_id)
user = fshop.company_id.connector_user_id
if user:
with self.session.change_user(user.id):
super(SaleOrderImport, self)._import(binding_id)
else:
super(SaleOrderImport, self)._import(binding_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment