下面这一段代码是zope的External Method。主要是分析XML文件,向zope批量导入数据。
注意:get_transaction().commit() ,这是把ZODB的temporary Database里的数据搬到main Database。这样在分析XML文件时,创建的对象才能立即生效。
# coding=utf-8
from urllib import urlopen
import xml.sax.handler
import xml.sax
import time
try:
from transaction import get as get_transaction
except ImportError:
pass
class PDENewContent:
def __init__(self,context):
self.context = context
def pdeNewPerson(self,erp_id = 0,attributes = {}):
new_erpid = 0
portal_type = 'Person'
portal = self.context.getPortalObject()
module = portal.getDefaultModule(portal_type)
person = None
if erp_id == 0:
person = module.newContent(portal_type = portal_type)
new_erpid = person.id
else:
person = module.restrictedTraverse('person_module/' + str(erp_id))
#set pid
if attributes.has_key("autoid"):
person.edit(pid = attributes["autoid"])
#set first_name
if attributes.has_key("p_na"):
person.edit(first_name = attributes["p_na"])
#set Gender
if attributes.has_key("gender"):
if attributes["gender"] == u"1":
person.edit(gender = 'female')
elif attributes["gender"] == u"0":
person.edit(gender = 'male')
#set birthday
if attributes.has_key("birthday"):
person.edit(birthday = attributes["birthday"])
#set Address
default_address = None
if 'default_address' in person.objectIds():
default_address = person['default_address']
else:
default_address = person.newContent(portal_type = 'Address',id = 'default_address')
if attributes.has_key("Home_Address"):
default_address.edit(street_address = attributes["Home_Address"])
#set WorkFlow
if person.getValidationState() == 'draft':
if attributes.has_key("state") and attributes["state"] == u"1":
module.portal_workflow.doActionFor(person,'validate_action',comment='')
class PersonHandler(xml.sax.handler.ContentHandler):
def __init__(self,context,portal_type):
self.newContent = PDENewContent(context = context)
#get all ERP5 person's pid
persons = context.getPortalObject().getDefaultModule(portal_type).contentValues(portal_type = portal_type)
self.pids = {}
for p in persons:
pid = getattr(p,'pid',0)
if pid <> 0:
self.pids[pid] = p.id
#create log file
self.log_file = open('/home/master/instances/var/development-site/Extensions/log1.txt','wb')
def __del__(self):
self.log_file.close()
def startElement(self, name, attributes):
#Create person
if name == "person":
autoid = attributes["autoid"]
if autoid in self.pids.keys():
self.newContent.pdeNewPerson(erp_id = self.pids[autoid],attributes = attributes)
# commit any changes that may be pending.
get_transaction().commit()
else:
#if person not in person_module create new person
newid = self.newContent.pdeNewPerson(attributes = attributes)
if newid <> 0 :
self.pids[autoid] = newid
#log
self.log_file.write(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) + ' ' + autoid + ' ' + attributes["p_no"] + '\n')
def importXML(self):
handler = PersonHandler(context = self,portal_type = 'Person')
try:
webpage = urlopen('ftp://127.0.0.1/person.xml')
#text = webpage.read().decode('gbk').encode('UTF-8')
#text = text.replace('gb2312', 'utf-8')
#text = '<?xml version="1.0" encoding="utf-8"?> <doc> ' + text + ' </doc>'
parser = xml.sax.parse(webpage,handler)
finally:
webpage.close()
<?xml version="1.0" encoding="utf-8"?> <doc> <person gender="0" state="1" autoid="112702" p_no="Z0001" p_na="张三" birthday="08//11/1988" Home_Address="广东" /> <person gender="0" state="0" autoid="107500" p_no="Z0002" p_na="李四" birthday="10/1/1979" Home_Address="广东" /> <person gender="0" state="0" autoid="107501" p_no="Z0003" p_na="王五" birthday="07/22/1987" Home_Address="广东" /> </doc>