Commit 9dae4a61 authored by fred's avatar fred

add new command to load users from a csv file

parent 94417368
import csv
from optparse import make_option
from django.core.management.base 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:
continue
if firstname and lastname:
username = slugify(u'%s-%s' % (firstname, lastname))
elif firstname or lastname:
username = slugify(firstname or lastname)
else:
continue
print username, email, emission_slugs
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
user = User.objects.create_user(username, email)
user.set_password('panik')
user.first_name = firstname
user.last_name = lastname
emissions = []
for slug in emission_slugs:
try:
emissions.append(Emission.objects.get(slug=slug))
except Emission.DoesNotExist:
print 'E: missing emission:', slug
user.emissions = emissions
user.save()
Markdown is supported
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