Import Members from CSV


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # An external method to import user
  2. import csv
  3.  
  4. # the full path to your csv file
  5. fileName = "c:/temp/test.csv"
  6.  
  7. def importUsers(self):
  8. reader = csv.reader(open(fileName, "r"))
  9. pr = self.portal_registration
  10. pg = self.portal_groups
  11. out = []
  12.  
  13. # if your csv file contains a header line that
  14. # explains the contents of each column
  15. ignoreLine = 1
  16. for row in reader:
  17. if ignoreLine:
  18. ignoreLine = 0
  19. continue
  20.  
  21. # check we have exactly 4 items
  22. assert len(row) == 4
  23. id, name, email, groups = row
  24. groups = groups.split(',')
  25.  
  26. # make a password
  27. password = pr.generatePassword()
  28. try:
  29. # add in member
  30. pr.addMember(id = id,
  31. password = password,
  32. roles = ["Member",],
  33. properties = {
  34. 'fullname': name,
  35. 'username': id,
  36. 'email': None,
  37. }
  38. )
  39. # groups are separated by commas
  40. for groupId in groups:
  41. group = pg.getGroupById(groupId)
  42. group.addMember(id)
  43.  
  44. out.append("Added user %s" % id)
  45.  
  46. except ValueError, msg:
  47. # if we skipped this user for a reason, tell the person
  48. out.append("Skipped %s, reason: %s" % (id, msg))
  49.  
  50. # return something
  51. return "\n".join(out)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.