Commit 9dae4a61 authored by fred's avatar fred
Browse files

add new command to load users from a csv file

parent 94417368
import csv
from optparse import make_option
from import BaseCommand, CommandError
from django.utils.text import slugify
from ...models import User, Emission
class Command(BaseCommand):
args = 'filename'
def handle(self, filename, verbosity, **kwargs):
self.verbose = (verbosity > 1)
for line in csv.reader(file(filename)):
lastname, firstname, email = [unicode(x.strip(), 'utf-8') for x in line[:3]]
emission_slugs = [x.strip().lower() for x in line[6:] if x]
if not email or not emission_slugs:
if firstname and lastname:
username = slugify(u'%s-%s' % (firstname, lastname))
elif firstname or lastname:
username = slugify(firstname or lastname)
print username, email, emission_slugs
user = User.objects.get(username=username)
except User.DoesNotExist:
user = User.objects.create_user(username, email)
user.first_name = firstname
user.last_name = lastname
emissions = []
for slug in emission_slugs:
except Emission.DoesNotExist:
print 'E: missing emission:', slug
user.emissions = emissions
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment