bzr revid: hmo@tinyerp.com-20110629064347-ehyg06x7ph0jnt3m
This commit is contained in:
Harry (OpenERP)
2011-06-29 12:13:47 +05:30
6 changed files with 250 additions and 110 deletions
+2 -1
View File
@@ -71,7 +71,6 @@
'res/partner/wizard/partner_clear_ids_view.xml',
'res/partner/wizard/partner_wizard_ean_check_view.xml',
'res/partner/partner_data.xml',
'res/ir_property_view.xml',
'security/base_security.xml',
'publisher_warranty/publisher_warranty_view.xml',
@@ -96,6 +95,8 @@
'test/test_ir_rule.yml', # <-- These tests modify/add/delete ir_rules.
'test/test_config_users.yml',
'test/test_ir_values.yml',
'test/test_edi_documents.yml',
],
'installable': True,
'active': True,
+153 -108
View File
@@ -20,11 +20,14 @@
##############################################################################
from osv import osv,fields
import json
import hashlib
import json
import time
import base64
import urllib2
import openerp.release as release
edi_module = 'edi_import'
def safe_unique_id(model, database_id):
"""Generate a unique string to represent a (model,database id) pair
@@ -56,7 +59,7 @@ def safe_unique_id(model, database_id):
class ir_edi_document(osv.osv):
_name = 'ir.edi.document'
_description = 'To represent the EDI export of any OpenERP record, it could be an invoice, a project task, etc.
_description = 'To represent the EDI Document of any OpenERP record.'
_columns = {
'name': fields.char("EDI token", size = 128, help="EDI Token is a unique identifier for the EDI document."),
'document': fields.text("Document", help="hold the serialization of the EDI document.")
@@ -81,7 +84,7 @@ class ir_edi_document(osv.osv):
perform a JSON serialization of a list of dicts prepared by generate_edi() and return a UTF-8 encoded string that could be passed to deserialize()
:param edi_dicts: it's list of edi_dict
"""
serialized_list = json.dumps(edi_dicts)
serialized_list = json.dumps(edi_documents)
return serialized_list
def generate_edi(self, cr, uid, records, context=None):
@@ -104,13 +107,11 @@ class ir_edi_document(osv.osv):
records = self.name_search(cr, uid, edi_token, context=context)
if records:
edi = self.browse(cr, uid, records[0], context=context)
record = records[0][0]
edi = self.browse(cr, uid, record, context=context)
return edi.document
else:
raise osv.except_osv(_('Error !'),
_('Desired EDI Document does not Exist'))
else:
pass
def load_edi(self, cr, uid, edi_documents, context=None):
"""
@@ -134,9 +135,10 @@ class ir_edi_document(osv.osv):
perform JSON deserialization from an edi document string, and returns a list of dicts
"""
edi_document = json.loads(edi_document_string)
return edi_document
def export(self, cr, uid, records, context=None):
def export_edi(self, cr, uid, records, context=None):
"""
The method handles the flow of the edi document generation and store it in
the database and return the edi_token of the particular document
@@ -159,7 +161,7 @@ class ir_edi_document(osv.osv):
exported_ids.append(token)
return exported_ids
def import(self, cr, uid, edi_document=None, edi_url=None, context=None):
def import_edi(self, cr, uid, edi_document=None, edi_url=None, context=None):
"""
The method handles the flow of importing particular edi document and
updates the database values on the basis of the edi document using
@@ -173,13 +175,13 @@ class ir_edi_document(osv.osv):
if edi_url and not edi_document:
edi_document = urllib2.urlopen(edi_url).read()
assert edi_document, _('EDI Document should be provided')
assert edi_document, _('EDI Document should be provided')
edi_documents = self.deserialize(edi_document)
return self.load_edi(cr, uid, edi_documents, context=context)
ir_edi_document()
class edi(object):
_name = 'edi'
_description = 'edi document handler'
@@ -188,7 +190,22 @@ class edi(object):
Classes that inherit from this mixin class should override the
``edi_import()`` and ``edi_export()`` methods to implement their
specific behavior, based on the primitives provided by this superclass."""
def edi_xml_id(self, cr, uid, record, context=None):
model_data_pool = self.pool.get('ir.model.data')
uuid = safe_unique_id(record._name, record.id)
data_ids = model_data_pool.search(cr, uid, [('res_id','=',record.id),('model','=',record._name)])
if len(data_ids):
xml_record_id = data_ids[0]
else:
xml_record_id = model_data_pool.create(cr, uid, {
'name': uuid,
'model': record._name,
'module': edi_module,
'res_id': record.id}, context=context)
xml_record = model_data_pool.browse(cr, uid, xml_record_id, context=context)
db_uuid = '%s:%s.%s' % (uuid, xml_record.module, xml_record.name)
return db_uuid
def edi_metadata(self, cr, uid, records, context=None):
"""Return a list representing the boilerplate EDI structure for
@@ -203,9 +220,20 @@ class edi(object):
- __version': a list of components for the version
- __attachments': a list (possibly empty) of dicts describing the files attached to this record.
"""
# generic implementation!
if context is None:
context = {}
data_ids = []
attachment_object = self.pool.get('ir.attachment')
edi_dict_list = []
db_uuid = ''
version = []
for ver in release.major_version.split('.'):
try:
ver = int(ver)
except:
pass
version.append(ver)
for record in records:
attachment_ids = attachment_object.search(cr, uid, [('res_model','=', record._name), ('res_id', '=', record.id)])
attachment_dict_list = []
@@ -215,14 +243,20 @@ class edi(object):
'content': base64.encodestring(attachment.datas),
'file_name': attachment.datas_fname,
})
db_uuid = self.edi_xml_id(cr, uid, record, context=context)
edi_dict = {
'__model' : record._name,
'__module' : record._module,
'__id': record.id,
'__last_update': record.write_date, #TODO: convert into UTC
'__version': [], # ?
'__attachments': attachment_dict_list
'__id': db_uuid,
'__last_update': False, #record.write_date, #TODO: convert into UTC
}
if not context.get('o2m_export'):
edi_dict.update({
'__model' : record._name,
'__module' : record._module,
'__version': version,
'__attachments': attachment_dict_list
})
edi_dict_list.append(edi_dict)
return edi_dict_list
@@ -234,18 +268,13 @@ class edi(object):
Exmaple: ['db-uuid:xml-id', 'Partner Name']
"""
# generic implementation!
data_object = self.pool.get('ir.model.data')
db_uuid = safe_unique_id(record._name, record.id)
xml_ids = data_object.search(cr, uid, [('model','=',record._name),('res_id','=',record.id)])
if xml_ids:
xml_id = data_object.browse(cr, uid, xml_ids[0], context=context)
xml_id = xml_id.name
db_uuid = '%s:%s' %(db_uuid, xml_id)
name = record.name
db_uuid = self.edi_xml_id(cr, uid, record, context=context)
relation_model_pool = self.pool.get(record._name)
name = relation_model_pool.name_get(cr, uid, [record.id], context=context)
name = name and name[0][1] or False
return [db_uuid, name]
def edi_o2m(self, cr, uid, records, context=None):
def edi_o2m(self, cr, uid, records, edi_struct=None, context=None):
"""Return a list representing a O2M EDI value for
the browse_records from the given browse_record_list.
@@ -259,12 +288,17 @@ class edi(object):
"""
# generic implementation!
dict_list = []
if context is None:
context = {}
ctx = context.copy()
ctx.update({'o2m_export':True})
for record in records:
model_obj = self.pool.get(record._name)
dict_list += model_obj.edi_export(cr, uid, [record.id], context=context)
dict_list += model_obj.edi_export(cr, uid, [record], edi_struct=edi_struct, context=ctx)
return dict_list
def edi_m2m(self, cr, uid, records, context=None):
"""Return a list representing a M2M EDI value for
the browse_records from the given browse_record_list.
@@ -279,14 +313,13 @@ class edi(object):
"""
# generic implementation!
dict_list = []
for record in records:
dict_list.append(self.edi_o2m(cr, uid, record, context=None))
dict_list.append(self.edi_o2m(cr, uid, [record], context=None))
return dict_list
def edi_export(self, cr, uid, ids, edi_struct=None, context=None):
def edi_export(self, cr, uid, records, edi_struct=None, context=None):
"""Returns a list of dicts representing an edi.document containing the
browse_records with ``ids``, using the generic algorithm.
:param edi_struct: if provided, edi_struct should be a dictionary
@@ -306,34 +339,45 @@ class edi(object):
be included in the exported data.
"""
# generic implementation!
if context is None:
context = {}
_fields = self.fields_get(cr, uid, context=context)
fields_to_export = edi_struct and edi_struct.keys() or _fields.key()
fields_to_export = edi_struct and edi_struct.keys() or _fields.keys()
edi_dict_list = []
for row in self.browse(cr, uid, ids, fields_to_export, context):
value = None
for row in records:
edi_dict = {}
edi_dict.update(self.edi_metadata(cr, uid, [row.id], context=context)[0])
edi_dict.update(self.edi_metadata(cr, uid, [row], context=context)[0])
for field in fields_to_export:
cols = self._fields[field]
if cols['type'] == 'many2one':
m2o_record = row.getattrs(field)
value = self.m2o(cr, uid, m2o_record, context=context)
cols = _fields[field]
record = getattr(row, field)
if not record:
continue
if _fields[field].has_key('function') or _fields[field].has_key('related_columns'):
# Do not Export Function Fields and related fields
continue
elif cols['type'] == 'many2one':
value = self.edi_m2o(cr, uid, record, context=context)
elif cols['type'] == 'many2many':
m2m_records = row.getattrs(field)
value = self.m2m(cr, uid, m2m_records, context=context)
value = self.edi_m2m(cr, uid, record, context=context)
elif cols['type'] == 'one2many':
o2m_records = row.getattrs(field)
value = self.o2m(cr, uid, o2m_records, context=context)
value = self.edi_o2m(cr, uid, record, edi_struct=edi_struct.get(field, {}), context=context )
else:
value = row.getattrs(field)
value = record
edi_dict[field] = value
edi_dict_list.append(edi_dict)
return edi_dict_list
def edi_import_relation(self, cr, uid, relation_model, relation_value, values={}, context=None):
relation_object = self.pool.get(relation_model)
relation_ids = relation_object.name_search(cr, uid, relation_value, context=context)
if relation_ids and len(relation_ids) == 1:
relation_id = relation_ids[0][0]
else:
values.update({'name': relation_value})
relation_id = relation_object.create(cr, uid, values, context=context)
return relation_id
def edi_import(self, cr, uid, edi_document, context=None):
@@ -383,19 +427,23 @@ class edi(object):
connect it to the parent record via a write value like (4, db_id).
"""
# generic implementation!
fields = edi_document.keys()
fields_to_import = []
data_line = []
model_data = self.pool.get('ir.model.data')
_fields = self.fields_get(cr, uid, context=context)
current_module = 'edi_import'
values = {}
for field in edi_document.keys():
if not field.startwith('__'):
field_to_import.append(field)
if not field.startswith('__'):
fields_to_import.append(field)
edi_field_value = edi_document[field]
if _fields[field]['type'] in ('many2one', 'many2many'):
if not edi_field_value:
continue
if _fields[field].has_key('function') or _fields[field].has_key('related_columns'):
# DO NOT IMPORT FUNCTION FIELD AND RELATED FIELD
continue
elif _fields[field]['type'] in ('many2one', 'many2many'):
if _fields[field]['type'] == 'many2one':
edi_parent_documents = [edi_field_value]
else:
@@ -407,46 +455,42 @@ class edi(object):
#Look in ir.model.data for a record that matches the db_id.
#If found, replace the m2o value with the correct database ID and stop.
#If not found, continue to next step
db_uuid, xml_id = tuple(edi_parent_document[0].split(':'))
data_ids = model_data.name_search(cr, uid, xml_id)
if data_ids:
d = model_data.browse(cr, uid, data_ids[0], context=context)
parent_lines.append(d.res_id)
if edi_parent_document[0].find(':') != -1 and edi_parent_document[1] != None:
db_uuid, xml_id = tuple(edi_parent_document[0].split(':'))
data_ids = model_data.name_search(cr, uid, xml_id)
if data_ids:
for data in model_data.browse(cr, uid, [data_ids[0][0]], context=context):
parent_lines.append(data.res_id)
else:
#Perform name_search(name) to look for a record that matches the
#given m2o name. If only one record is found, create the missing
#ir.model.data record to link it to the db_id, and the replace the m2o
#value with the correct database ID, then stop. If zero result or
#multiple results are found, go to next step.
#Create the new record using the only field value that is known: the
#name, and create the ir.model.data entry to map to it.
relation_model = _fields[field]['relation']
relation_id = self.edi_import_relation(cr, uid, relation_model, edi_parent_document[1], context=context)
relation_object = self.pool.get(relation_model)
model_data.create(cr, uid, {
'name': xml_id,
'model': relation_object._name,
'module':relation_object._module,
'res_id':relation_id
}, context=context)
parent_lines.append(relation_id)
if len(parent_lines):
if _fields[field]['type'] == 'many2one':
values[field] = parent_lines[0]
else:
#Perform name_search(name) to look for a record that matches the
#given m2o name. If only one record is found, create the missing
#ir.model.data record to link it to the db_id, and the replace the m2o
#value with the correct database ID, then stop. If zero result or
#multiple results are found, go to next step.
#Create the new record using the only field value that is known: the
#name, and create the ir.model.data entry to map to it.
relation_object = self.get.pool(_fields[field]['relation'])
relation_ids = relation_object.name_search(cr, uid, edi_parent_document[1])
if relation_ids and len(relation_ids) == 1:
relation_id = relation_ids[0]
else:
relation_id = relation_object.create(cr, uid, {'name': edi_parent_document[1]}, context=context)
model_data.create(cr, uid, {
'name': xml_id,
'model': relation_object._name,
#'module':relation_object._module,
'res_id':relation_id,
},context=context)
parent_lines.append(relation_id)
if _fields[field]['type'] == 'many2one':
values[field] = parent_lines[0]
else:
many2many_ids = []
for m2m_id in parent_lines:
many2many_ids.append((4,m2m_id))
values[field] = many2many_ids
many2many_ids = []
for m2m_id in parent_lines:
many2many_ids.append((4,m2m_id))
values[field] = many2many_ids
elif _fields[field]['type'] == 'one2many':
#Look for a record that matches the db_id provided in the __id field. If
#found, keep the corresponding database id, and connect it to the parent
@@ -456,16 +500,17 @@ class edi(object):
#and use it to connect to the parent via a write value like (4, db_id).
relations = []
relation_object = self.get.pool(_fields[field]['relation'])
relation_object = self.pool.get(_fields[field]['relation'])
for edi_relation_document in edi_field_value:
db_uuid, xml_id = tuple(edi_relation_document.get('__id').split(':'))
data_ids = model_data.name_search(cr, uid, xml_id)
if data_ids:
d = model_data.browse(cr, uid, data_ids[0], context=context)
relations.append(d.res_id)
else:
r = relation_object.edi_import(cr, uid, edi_relation_document, context=context)
relations.append(r)
if edi_relation_document['__id'].find(':') != -1:
db_uuid, xml_id = tuple(edi_relation_document['__id'].split(':'))
data_ids = model_data.name_search(cr, uid, xml_id)
if data_ids:
for data in model_data.browse(cr,uid,[data_ids[0][0]]):
relations.append(data.res_id)
else:
r = relation_object.edi_import(cr, uid, edi_relation_document, context=context)
relations.append(r)
one2many_ids = []
for o2m_id in relations:
one2many_ids.append((4,o2m_id))
@@ -473,5 +518,5 @@ class edi(object):
else:
values[field] = edi_field_value
return model_data._update(cr, uid, self._name, current_module, values, context=context)
return model_data._update(cr, uid, self._name, edi_module, values, context=context)
# vim: ts=4 sts=4 sw=4 si et
+1 -1
View File
@@ -23,7 +23,7 @@ import tools
from partner import *
from crm import *
import edi_partner
import wizard
import report
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
#
# 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 osv import fields,osv
from base.ir import ir_edi
class res_partner(osv.osv, ir_edi.edi):
_inherit = "res.partner"
res_partner()
class res_partner_address(osv.osv, ir_edi.edi):
_inherit = "res.partner.address"
res_partner_address()
class res_partner_bank(osv.osv, ir_edi.edi):
_inherit = "res.partner.bank"
res_partner_bank()
@@ -0,0 +1,14 @@
-
Tesing of EDI functionality
-
!python {model: ir.edi.document}: |
partner_obj = self.pool.get('res.partner')
partner_ids = partner_obj.search(cr, uid, [('name','=','OpenERP S.A.')])
if partner_ids:
partners = partner_obj.browse(cr, uid, partner_ids)
tokens = self.export_edi(cr, uid, partners)
for token in tokens:
document = self.get_document(cr, uid, token, context=context)
a = self.import_edi(cr, uid, edi_document = document)
+42
View File
@@ -37,6 +37,47 @@ import openerp.modules
import locale
import logging
from cStringIO import StringIO
import threading
class edi(netsvc.ExportService):
def exp_get_edi_document(self, edi_token):
db_name = getattr(threading.currentThread(), 'dbname', None)
if db_name:
cr = pooler.get_db_only(db_name).cursor()
else:
raise Exception("No database cursor found!")
pool = pooler.get_pool(db_name)
edi_pool = pool.get('ir.edi.document')
return edi_pool.get_document(cr, 1, edi_token)
def exp_import_edi_document(self, db, uid, passwd, edi_document, context=None):
cr = pooler.get_db_only(db).cursor()
pool = pooler.get_pool(db)
edi_pool = pool.get('ir.edi.document')
return edi_pool.import_edi(cr, uid, edi_document=edi_document, context=context)
def exp_import_edi_url(self, db, uid, passwd, edi_url, context=None):
cr = pooler.get_db_only(db).cursor()
pool = pooler.get_pool(db)
edi_pool = pool.get('ir.edi.document')
return edi_pool.import_edi(cr, uid, edi_url=edi_url, context=context)
def __init__(self, name="edi"):
netsvc.ExportService.__init__(self, name)
self.joinGroup("web-services")
def dispatch(self, method, auth, params):
if method in ['import_edi_document', 'import_edi_url']:
(db, uid, passwd ) = params[0:3]
security.check(db, uid, passwd)
elif method in ['get_edi_document']:
# params = params
# No security check for these methods
pass
else:
raise KeyError("Method not found: %s" % method)
fn = getattr(self, 'exp_'+method)
return fn(*params)
class db(netsvc.ExportService):
def __init__(self, name="db"):
@@ -763,6 +804,7 @@ def start_web_services():
objects_proxy()
wizard()
report_spool()
edi()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: