From 77180a5bccd77b0a6ccdc972682689b76168e4a6 Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Wed, 5 Oct 2011 02:33:38 +0200 Subject: [PATCH] [IMP] edi: bugfixes/cleanup after more review bzr revid: odo@openerp.com-20111005003338-6ol0kd8espdfaq32 --- addons/edi/__init__.py | 3 +- addons/edi/models/edi.py | 20 ++++++--- addons/edi/models/res_company.py | 87 ++++++-------------------------------- 3 files changed, 29 insertions(+), 81 deletions(-) diff --git a/addons/edi/__init__.py b/addons/edi/__init__.py index 4e266ec..4a78085 100644 --- a/addons/edi/__init__.py +++ b/addons/edi/__init__.py @@ -20,4 +20,5 @@ ############################################################################## import models -import edi_service \ No newline at end of file +import edi_service +from models.edi import EDIMixin, edi_document \ No newline at end of file diff --git a/addons/edi/models/edi.py b/addons/edi/models/edi.py index 6191275..2394dd7 100644 --- a/addons/edi/models/edi.py +++ b/addons/edi/models/edi.py @@ -206,6 +206,11 @@ class EDIMixin(object): ``edi_import()`` and ``edi_export()`` methods to implement their specific behavior, based on the primitives provided by this mixin.""" + def _edi_requires_attributes(self, attributes, edi_document): + for attribute in attributes: + assert edi_document.get(attribute),\ + 'Attribute `%s` is required in %s EDI documents' % (attribute, self._name) + # private method, not RPC-exposed as it creates ir.model.data entries as # SUPERUSER based on its parameters def _edi_external_id(self, cr, uid, record, existing_id=None, existing_module=None, @@ -241,7 +246,7 @@ class EDIMixin(object): """ ir_model_data = self.pool.get('ir.model.data') db_uuid = self.pool.get('ir.config_parameter').get_param(cr, uid, 'database.uuid') - ext_id = self.get_external_id(cr, uid, [record.id])[record.id] + ext_id = record.get_external_id()[record.id] if not ext_id: ext_id = existing_id or safe_unique_id(db_uuid, record._name, record.id) # ID is unique cross-db thanks to db_uuid (already included in existing_module) @@ -454,9 +459,13 @@ class EDIMixin(object): module = ext_id_members['module'] ext_id = ext_id_members['id'] ext_module = '%s:%s' % (module, ext_id_members['db_uuid']) + modules = [ext_module] + if ext_id_members['db_uuid'] == db_uuid: + # local records may also be registered without the db_uuid + modules.append(module) data_ids = ir_model_data.search(cr, uid, [('model','=',model), ('name','=',ext_id), - ('module','in',[ext_module,module])]) + ('module','in',modules)]) if data_ids: model = self.pool.get(model) data = ir_model_data.browse(cr, uid, data_ids[0], context=context) @@ -476,7 +485,7 @@ class EDIMixin(object): value in the target model, assign it the given external_id, and return the new database ID """ - _logger.debug("%s: Importing EDI relationship [%r,%r]", self._name, external_id, value) + _logger.debug("%s: Importing EDI relationship [%r,%r]", model, external_id, value) target = self._edi_get_object_by_external_id(cr, uid, external_id, model, context=context) need_new_ext_id = False if not target: @@ -488,7 +497,8 @@ class EDIMixin(object): _logger.debug("%s: Importing EDI relationship [%r,%r] - name not found, creating it!", self._name, external_id, value) # also need_new_ext_id here, but already been set above - res_id, name = self.name_create(cr, uid, value, context=context) + model = self.pool.get(model) + res_id, name = model.name_create(cr, uid, value, context=context) target = model.browse(cr, uid, res_id, context=context) if need_new_ext_id: ext_id_members = split_external_id(external_id) @@ -537,7 +547,7 @@ class EDIMixin(object): existing_id = self._edi_get_object_by_external_id(cr, uid, ext_id_members['full'], self._name, context=context) if existing_id: _logger.info("'%s' EDI Document with ID '%s' is already known, skipping import!", self._name, ext_id_members['full']) - return + return existing_id.id record_values = {} o2m_todo = {} # o2m values are processed after their parent already exists diff --git a/addons/edi/models/res_company.py b/addons/edi/models/res_company.py index a187532..b81f5aa 100644 --- a/addons/edi/models/res_company.py +++ b/addons/edi/models/res_company.py @@ -27,83 +27,20 @@ class res_company(osv.osv): itself is not EDI-exportable""" _inherit = "res.company" - def edi_export_address(self, cr, uid, records, edi_address_struct=None, context=None): - """Returns a dict representation of the address of each company record, suitable for + def edi_export_address(self, cr, uid, company, edi_address_struct=None, context=None): + """Returns a dict representation of the address of the company record, suitable for inclusion in an EDI document, and matching the given edi_address_struct if provided. + The first found address is returned, in order of preference: invoice, contact, default. - :param list(browse_record) records: list of companies to export - :rtype: list(dict) - :return: list of dicts, where each dict contains the address representation for - the company record as the same index in ``records``. + :param browse_record company: company to export + :return: dict containing the address representation for the company record, or + an empty dict if no address can be found """ - if context is None: - context = {} res_partner = self.pool.get('res.partner') res_partner_address = self.pool.get('res.partner.address') - results = [] - for company in records: - res = res_partner.address_get(cr, uid, [company.partner_id.id], ['default', 'contact', 'invoice']) - addr_id = res['invoice'] or res['contact'] or res['default'] - result = {} - if addr_id: - address = res_partner_address.browse(cr, uid, addr_id, context=context) - result = res_partner_address.edi_export(cr, uid, [address], edi_struct=edi_address_struct, context=ctx)[0] - resuls.append(result) - return [] - -# TODO: CHECK below, seems useless -# def edi_import_as_partner(self, cr, uid, edi_document, values=None, context=None): -# """ -# import company as a new partner -# company_address data used to add address to new partner -# -# edi_document is a dict to have company datas -# edi_document = { -# 'company_address': { -# 'street': True, -# 'street2': True, -# 'zip': True, -# 'city': True, -# 'state_id': True, -# 'country_id': True, -# 'email': True, -# 'phone': True, -# -# }, -# 'company_id': True, -# } -# values is a dict to have other datas of partner which are need to import of partner record -# values = { -# 'customer': True, -# 'supplier': True, -# } -# """ -# if values is None: -# values = {} -# partner_model = 'res.partner' -# partner_pool = self.pool.get(partner_model) -# xml_id = edi_document['company_id'][0] -# company_address = edi_document.get('company_address', False) -# partner_name = edi_document['company_id'][1] -# partner = partner_pool.edi_get_object(cr, uid, xml_id, partner_model, context=context) -# if not partner: -# partner = partner_pool.edi_get_object_by_name(cr, uid, partner_name, partner_model, context=context) -# -# if partner: - #FIXME -# record_xml = partner_pool._get_external_id(cr, uid, [partner.id], context=context) -# if record_xml: -# module, xml_id = record_xml -# xml_id = '%s.%s' % (module, xml_id) -# -# edi_document_partner = { -# '__model': partner_model, -# '__id' : xml_id, -# 'name' : partner_name, -# } -# if company_address: -# edi_document_partner['address'] = [company_address] -# -# edi_document_partner.update(values) -# return partner_pool.edi_import(cr, uid, edi_document_partner, context=context) -# + addresses = res_partner.address_get(cr, uid, [company.partner_id.id], ['default', 'contact', 'invoice']) + addr_id = addresses['invoice'] or addresses['contact'] or addresses['default'] + if addr_id: + address = res_partner_address.browse(cr, uid, addr_id, context=context) + return res_partner_address.edi_export(cr, uid, [address], edi_struct=edi_address_struct, context=context)[0] + return {} -- 1.7.10.4