Quantcast
Channel: Planet Plone - Where Developers And Integrators Write
Viewing all articles
Browse latest Browse all 3535

UW Oshkosh How-To's: Mass or batch user create, via Script (Python)

$
0
0

As inspired by http://plone.org/documentation/kb/batch-adding-users

but the file format I used is simpler.

Script Source Code (full email addresses specified)

The script reads in a full email address for each new user.

The file format it expects is:

Kim Nguyen <nguyen@uwosh.edu>
Joe The Shark <joetheshark@uwosh.edu>

The script source code:

# Create new members with properties supplied from a CSV file.
# The script expects a File object with id `users.csv` in the same folder
# it resides.
#
# The format of the CSV needs to be:
#
# Firstname Lastname <email@address.com>
#
# created 2006-11-03 by Tom Lazar <tom@tomster.org>, http://tomster.org/
# under a BSD-style licence (i.e. use as you wish but don't sue me)

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

for user in users:
    user = user.strip('>')
    tokens = user.split('<')
    if len(tokens) == 2:
        name, email = tokens
        properties = {
            'username' : email,
            'fullname' : name,
            'email' : email.strip(),
        }
        try:
            passwd = regtool.generatePassword()
            regtool.addMember(email, passwd, properties=properties)
            print "Successfully added %s (%s) with email %s" % (name, email, email)
            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
 

Script Source Code (user IDs specified, full email addresses implied)

The script which reads in just the user ID for each new user, and assumes a particular form of email address, e.g. always @psu.edu.

The file format is:

My Taylor <mytaylor29>

The script source code is below. 

You need to modify the EMAILPATTERN variable, which is appended to each user ID to form the complete email address. 

In the above example file, mytaylor29 is the user ID, and if you set EMAILPATTERN to '@psu.edu', the script assumes that the full email address is mytaylor29@psu.edu.

# Create new members with properties supplied from a CSV file.
# The script expects a File object with id `users.csv` in the same folder
# it resides.
#
# The format of the CSV needs to be:
#
# Firstname Lastname <userid>
#
# Set the email address pattern in the variable EMAILPATTERN below
# e.g. @psu.edu so the user's email address will be userid@psu.edu
#
# created 2006-11-03 by Tom Lazar , http://tomster.org/
# under a BSD-style licence (i.e. use as you wish but don't sue me)

from Products.CMFCore.utils import getToolByName
users = context['users.csv'].data.split('\n')
regtool = getToolByName(context, 'portal_registration')
index = 1
imported_count = 0
EMAILPATTERN = '@psu.edu'

for user in users:
    user = user.strip('>')
    tokens = user.split('<')
    if len(tokens) == 2:
        name, userid = tokens
        email = userid.strip() + EMAILPATTERN
        properties = {
            'username' : userid.strip(),
            'fullname' : name,
            'email' : email,
        }
        try:
            passwd = regtool.generatePassword()
            regtool.addMember(userid, passwd, properties=properties)
            print "Successfully added %s (%s) with email %s" % (name, email, email)
            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

Viewing all articles
Browse latest Browse all 3535

Trending Articles