Re: Importing contacts from delimited files
Adam Tauno Williams <[email protected]> Fri, 13 Apr 2007 13:33:54 -0400
| Newsgroups | gmane.comp.cms.opengroupware.xmlrpc.devel |
|---|---|
| Message-ID | <[email protected]> |
On Fri, 2007-04-13 at 13:30 -0400, Adam Tauno Williams wrote:
> > Adam from the users@ mailing list recommended I hop on over here for a few
> > questions/help with XML-RPC scripts.
> > I was told its possible and actually VERY easy to import a number of contacts
> > from a delimited file using Python/XML-RPC scripts. I was wondering if someone
> > could provide some help as to how this is actually done.
> Here is one simple example -
> #!/usr/bin/env python
>
> import sys, csv, xmlrpclib, pprint
> server = xmlrpclib.Server('http://{USER}:{PASSWORD}@{HOST}/RPC2')
> reader = csv.reader(open("{FILENAME}", "rb"), delimiter='|',
> quoting=csv.QUOTE_NONE)
Here is a more complicated example that creates both contacts and
enterprises, breaking enterprises by changes in the ZIP+4, and it sets
the permissions on all created objects to specific teams -
#!/usr/bin/env python
import sys, csv, xmlrpclib, pprint
server = xmlrpclib.Server('http://adam:********@gourd-amber/RPC2')
reader = csv.reader(open("MECustomerListforOGoLoad.aaaf.csv", "rb"),
delimiter='|', quoting=csv.QUOTE_NONE)
previousZIP = ''
for row in reader:
currentZIP = row[5]
if currentZIP != previousZIP or len(currentZIP) == 5:
print "%s: ZIP code changed" %row[0]
result = {}
if len(row[14]) > 0:
fSpec = {}
fSpec['qualifier'] = "bankCode = '%s' " % row[14]
result = server.enterprise.fetch(fSpec)
if len(result) == 0:
print "Creating new enterprise"
enterprise = {}
enterprise['name'] = row[1]
enterprise['bankCode'] = row[14]
enterprise['url'] = row[9]
enterprise['addresses'] = {}
enterprise['addresses']['ship'] = {}
enterprise['addresses']['ship']['name1'] = row[1]
enterprise['addresses']['ship']['street'] = row[2]
enterprise['addresses']['ship']['city'] = row[3]
enterprise['addresses']['ship']['state'] = row[4]
enterprise['addresses']['ship']['zip'] = row[5]
enterprise['addresses']['ship']['country'] = 'USA'
enterprise['addresses']['ship']['type'] = 'ship'
enterprise['extendedKeys'] = ['salesperson', 'division', 'mailmktcode']
enterprise['extendedAttrs'] = {}
enterprise['extendedAttrs']['salesperson'] = row[16]
enterprise['extendedAttrs']['division'] = row[17]
enterprise['extendedAttrs']['mailmktcode'] = row[15]
enterprise['phones'] = {}
enterprise['phones']['01_tel'] = {}
enterprise['phones']['01_tel']['number'] = row[7]
enterprise['phones']['01_tel']['info'] = ''
enterprise['phones']['01_tel']['type'] = '01_tel'
enterprise['phones']['10_fax'] = {}
enterprise['phones']['10_fax']['number'] = row[8]
enterprise['phones']['10_fax']['info'] = ''
enterprise['phones']['10_fax']['type'] = '10_fax'
enterpriseId = server.enterprise.insert(enterprise)
print "Enterprise Id %s created " % enterpriseId
server.access.setOperationOnObjectForAccess('rw', enterpriseId, 955240);
server.access.setOperationOnObjectForAccess('rw', enterpriseId, 11530);
print " Permission applied to enterprise."
else:
print "Enterprise with bankCode already exists"
enterpriseId = result[0]['id'].split('/')[4]
print "Enterprise Id %s " % enterpriseId
else:
print "%s: ZIP code unchanged" %row[0]
previousZIP = currentZIP
if len(row[10]) > 0:
contact = {}
contact['firstname'] = row[11]
contact['name'] = row[12]
contact['nickname'] = row[10]
contact['isPrivate'] = '0'
contact['extendedKeys'] = ['organization', 'job_title', 'mailmktcode',
'salesperson', 'division']
contact['extendedAttrs'] = {}
contact['extendedAttrs']['organization'] = row[1]
contact['extendedAttrs']['job_title'] = row[13]
contact['extendedAttrs']['mailmktcode'] = row[15]
contact['extendedAttrs']['salesperson'] = row[16]
contact['extendedAttrs']['division'] = row[17]
contact['phones'] = {}
contact['phones']['01_tel'] = {}
contact['phones']['01_tel']['number'] = row[7]
contact['phones']['01_tel']['info'] = ''
contact['phones']['01_tel']['type'] = '01_tel'
contact['phones']['10_fax'] = {}
contact['phones']['10_fax']['number'] = row[8]
contact['phones']['10_fax']['info'] = ''
contact['phones']['10_fax']['type'] = '10_fax'
contact['addresses'] = {}
contact['addresses']['mailing'] = {}
contact['addresses']['mailing']['name1'] = row[1]
contact['addresses']['mailing']['name2'] = row[10]
contact['addresses']['mailing']['street'] = row[2]
contact['addresses']['mailing']['city'] = row[3]
contact['addresses']['mailing']['state'] = row[4]
contact['addresses']['mailing']['zip'] = row[5]
contact['addresses']['mailing']['country'] = 'USA'
contact['addresses']['mailing']['type'] = 'mailing'
contactId = server.person.insert(contact)
print " Contact Id %s created " % contactId
server.enterprise.insertPerson(enterpriseId, contactId);
print " Contact Id %s added to Enterprise Id %s " % (contactId,
enterpriseId)
server.access.setOperationOnObjectForAccess('rw', contactId, 955240);
server.access.setOperationOnObjectForAccess('rw', contactId, 11530);
print " Permissions applied to contact."
signature.asc
(application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (GNU/Linux) iD8DBQBGH78CLRePpNle04MRAprAAJ0bhpbK3PbTESPq4wv3z/Vy88EBHACfVDf1 xKlcPPaLyYLUHDcV6aM7DIQ= =OTdL -----END PGP SIGNATURE-----