You are here: Home / Documentation / How To's / Script to assign many users to a group

Script to assign many users to a group

by nguyen — published Jun 22, 2012 04:30 PM, last modified Aug 16, 2016 10:50 AM

File format for users.csv:

Kim Nguyen <nguyen@uwosh.edu>

Script to use:

# name;email

from Products.CMFCore.utils import getToolByName
users = context['users.csv'].data.split('\n')
pg = getToolByName(context, 'portal_groups')
index = 1
imported_count = 0

thegroup = '2012cohort'

for user in users:
    user = user.strip('>')
    tokens = user.split('<')
    if len(tokens) == 2:
        name, email = tokens
        email = email.strip(),
        try:
            pg.addPrincipalToGroup(email, thegroup)
            print "Successfully added %s (%s) to group %s" % (name, email, thegroup)
            imported_count += 1
        except ValueError, e:
            print "Couldn't add %s: %s" % (email, e)
    else:
        print "Could not parse line %d because it had the following contents: '%s'" % (index, user)
    index += 1

print "Imported %d users (from %d lines of CSV)" % (imported_count, index)

return printed
 

 

Navigation