Return to Snippet

Revision: 5058
at February 8, 2008 08:32 by sgraber


Initial Code
# An external method to import user
import csv

# the full path to your csv file
fileName = "c:/temp/test.csv"

def importUsers(self):
   reader = csv.reader(open(fileName, "r"))
   pr = self.portal_registration
   pg = self.portal_groups
   out = []

   # if your csv file contains a header line that
   # explains the contents of each column
   ignoreLine = 1
   for row in reader:
       if ignoreLine:
           ignoreLine = 0
           continue

       # check we have exactly 4 items
       assert len(row) == 4
       id, name, email, groups = row
       groups = groups.split(',')

       # make a password
       password = pr.generatePassword()
       try:
           # add in member
           pr.addMember(id = id,
               password = password,
               roles = ["Member",],
               properties = {
                   'fullname': name,
                   'username': id,
                   'email': None,
                   }
               )
           # groups are separated by commas
           for groupId in groups:
               group = pg.getGroupById(groupId)
               group.addMember(id)

           out.append("Added user %s" % id)

       except ValueError, msg:
           # if we skipped this user for a reason, tell the person
           out.append("Skipped %s, reason: %s" % (id, msg))

   # return something
   return "\n".join(out)

Initial URL


Initial Description


Initial Title
Import Members from CSV

Initial Tags


Initial Language
Python