Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
55 lines (47 sloc) 2.2 KB
#!/usr/bin/env python
#---------------------------------------------------------------------------------#
#---for adding users from a csv file----------------------------------------------#
#---------------------------------------------------------------------------------#
from django.core.management import setup_environ
from django.contrib.auth.models import User
from django.core.validators import email_re
from django.db import IntegrityError
import csv
from learning_outcomes.models import UserProfile
from learning import settings
setup_environ(settings)
#---------------------------------------------------------------------------------#
#---------------------------------------------------------------------------------#
#--Opening the csv file and storing the data in variable 'c'----------------------#
#---------------------------------------------------------------------------------#
c=[]
with open('/home/saad/learning/user_data.csv','rb') as f:
readers = csv.reader(f)
for row in readers:
c.append(row)
default_password = 'hse_col'
#--Traversing each line in c and each line will be added to the variable users------#
#--each line would be in format email,first name, is school [True/False]------------#
#--values will be delimited by commas-----------------------------------------------#
for users in c:
#--------------------------------------------------------------------------------------------------------#
#---------------splitting email and storing it in tokens this is done to relate Child object with a user-#
#--------------------------------------------------------------------------------------------------------#
email = users[0]
if email_re.match(email):
tokens = email.split('@')
username = tokens[0]
try:
user = User.objects.create_user(username,email,default_password)
user.is_staff=False
user.first_name=users[1]
user.save()
uinfo = UserProfile(user=user) #---creating a UserProfile object and relating it to the present user
if users[2]=='False': #---converting string False to boolean false
users[2]=bool(False)
elif users[2]=='True':
users[2]=bool(True)
uinfo.is_school = bool(users[2])
uinfo.save()
except IntegrityError:
print username,email