quick search:
 

export portal_memberdata to file

Submitted by: runyaga
Last Edited: 2004-10-14

Category: CMF

Average rating is: 5.0 out of 5 (1 ratings)

Description:
if you are integrating your memberdata to another system you may need to write it out to a CSV file. this recipe will get you there and you could modify the external method to do what you like.

Source (Text):
#create a file in the /Extensions directory called member_csv.py
def write_csv(self, file='/tmp/memberdata.csv'):
    ''' writes the memberdata of a portal to a csv ascii file '''
    props = []
    membership = getattr(self, 'portal_membership')

    for p in dir(self.portal_memberdata):
        if p[:1]!='_': props.append(p)

    fd = open(file, 'w')
    props.insert(0, 'id')
    for prop in props:
        fd.write(prop + ',')
    fd.write ('\n')

    for id in membership.listMemberIds():
        for prop in props:
            user = membership.getMemberById(id)
            if hasattr(user, prop):
                fd.write( str(getattr(user, prop)) + ',')
            else:
                write (' ,')
        fd.write('\n')

    fd.close()

#create a External Method inside the CMF portal called exportDataToCSV
#Id: exportDataToCSV
#module name: member_csv
#function name: write_csv

#create a Script (Python) called exportMemberData
exportDataToCSV(context)
return 'exported successfully'

#you can just 'Test' your exportMemberData Script for it to execute #the External Method.  It creates a file in /tmp called memberdata.csv

Explanation:
creates a list of member properties you want to export for each member
inside the CMF. usually these properties are in the portal_membership
tools, and they dont begin with a _

we open the file and write out all the property names and then we cycle
through each member in the portal, getting their MemberObject using the
portal_membership tool and then test to see if if each property is
available on the MemberObject if it is it writes it to the file.

we close the file. there is no exception handling. to change the location
of the file just change it in the function signature.


Comments:

here's an improved csv creation script by cleath - 2004-10-14
#prints out a CSV pf member and member properties
#this makes a Microsoft CSV file-- use text delimiter="
text = ''
memberProperties = context.portal_memberdata.propertyIds()
memberProperties.insert(0, 'id')

for p in memberProperties:
    #write out the first line
    text += p + ', '
#remove trailing comma
text=text[:-2]

text += chr(13)+chr(10) #first line has been written
for member in context.portal_membership.listMembers():
    for p in memberProperties:
        if hasattr(member, p):
            myAttr = getattr(member,p)
            if same_type(myAttr,[]):
                #it's a list
                text+='"%s"'%str(myAttr)
            elif same_type(myAttr,''):
                #it's a string
                mystr=str(myAttr)
                mystr = mystr.replace('"','""')
                text+='"%s"'%mystr
            else:
                text+='%s'%str(myAttr)

        text+=', '
    text=text[:-2]
    text+=chr(13)+chr(10)

return text
 
Re: here's an improved csv creation script by damon - 2004-10-14
"Improved export portal_memberdata to file" on this site has an improved version of this improved version ;-)