Home Documentation Setting Up a Development Server Adding Students Programatically
Document Actions

Adding Students Programatically

by Tom Hoffman last modified 2006-07-24 16:46

Importing from a file.

WARNING:  This HOWTO is broken & needs to be updated for the new demographics package.  Coming soon...

In this example, I am going to parse a comma separated value file containing a list of student names.  In this example, the file uses the following format:

userid, last name, first name
...

Once you look at this and subsequent examples, you can decide whether or not you want to make your files conform to this structure, or if it would be easier to change the script to parse the structure of the files you've got.  If you've got XML files, for example, you could incorporate the XML parsing directly into your import script.

In my case, I've placed a file named "blue.csv" which contains the list of student ids and names in the setup-data directory we already created in the root of our SchoolTool checkout.

To parse this file, I change students.py, from the previous example, to the following code:

import os, csv

import zope.interface
from schooltool.setupdata.interfaces import ISetupDataPlugin
from schooltool.restclient.restclient import SchoolToolClient

server_address = 'localhost'
server_port = 7001 # this is the REST port
username = 'manager'
password = 'schooltool' # update this if you've changed it
filename = 'setup-data/blue.csv' # the name of your file

class Students(object):

zope.interface.implements(ISetupDataPlugin)
name = 'students'
dependencies = ()

def generate(self, app):
stc = SchoolToolClient(server_address, server_port)
stc.setUser(username, password)
data = open(filename)
reader = csv.reader(data)
for row in reader:
name = "%s %s" % (row[2].strip(), row[1].strip())
print "Adding %s (%s)" % (name, row[0])
stc.createPerson(name, row[0].strip())

Restrart SchoolTool and generate sample data as you did previously.  Assuming you've got an appropriate CSV file, you should see the students being added on the terminal screen, and subsequently under /persons via the web interface.

There isn't a lot going on here.  The most important thing to understand is that we're using the REST web service client (SchoolToolClient) to create the students, and the csv module to easily parse the file.  You'll have to set the manager password and filename at the beginning of the script and perhaps adjust some of the other variables.  It is a good idea to include the print statements for debugging purposes.

This establishes one pattern we'll use throughout the setup process.


Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: