Switch to python-provided CSV module - implements #66

This commit is contained in:
rene 2011-11-22 18:49:36 +01:00
parent bbb61a9091
commit c448ce95bd
3 changed files with 63 additions and 32 deletions

View File

@ -12,6 +12,7 @@
from __future__ import with_statement
import csv
import utils.csv_ext
from django.shortcuts import redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
@ -453,7 +454,9 @@ def application_import(request):
applications_generated = 0
applications_modified = 0
with transaction.commit_on_success():
for (lno, line) in enumerate(csv.reader(request.FILES['csvfile'])):
dialect = csv.Sniffer().sniff(request.FILES['csvfile'].readline())
request.FILES['csvfile'].seek(0)
for (lno, line) in enumerate(csv.reader(request.FILES['csvfile'], dialect=dialect)):
# basic input verification
if lno < 1:
continue

View File

@ -11,6 +11,7 @@
"""
import csv
import utils.csv_ext
from urllib import urlencode
try:
from urlparse import parse_qs
@ -26,6 +27,7 @@ from django.contrib.auth.forms import SetPasswordForm
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _, ungettext
from django.db import transaction
from participant.models import Profile
from participant.api import gen_username, gen_password
@ -309,39 +311,45 @@ def user_import(request):
if request.method == 'POST':
form = UserImportForm(request.POST, request.FILES)
if form.is_valid():
profiles = Profile.objects.all()
for profile in profiles:
profile.user.delete()
profile.delete()
i = -1
for line in request.FILES['csvfile']:
i += 1
if i > 0:
(first_name, last_name, gender, group, type, committee) = line.strip().split(',')
user = User()
user.last_name = last_name
user.first_name = first_name
user.username = gen_username(first_name, last_name)
#user.email = email
user.save()
profile = Profile()
profile.user = user
profile.gender = gender
profile.group = group
profile.type = type
profile.committee = committee
profile.firstpassword = gen_password()
profile.user.set_password(profile.firstpassword)
profile.save()
try:
with transaction.commit_on_success():
profiles = Profile.objects.all()
for profile in profiles:
profile.user.delete()
profile.delete()
i = -1
dialect = csv.Sniffer().sniff(request.FILES['csvfile'].readline())
request.FILES['csvfile'].seek(0)
for line in csv.reader(request.FILES['csvfile'], dialect=dialect):
i += 1
if i > 0:
(first_name, last_name, gender, group, type, committee) = line[:6]
user = User()
user.last_name = last_name
user.first_name = first_name
user.username = gen_username(first_name, last_name)
#user.email = email
user.save()
profile = Profile()
profile.user = user
profile.gender = gender
profile.group = group
profile.type = type
profile.committee = committee
profile.firstpassword = gen_password()
profile.user.set_password(profile.firstpassword)
profile.save()
if type == 'delegate':
delegate = Group.objects.get(name='Delegierte')
user.groups.add(delegate)
else:
observer = Group.objects.get(name='Beobachter')
user.groups.add(observer)
if type == 'delegate':
delegate = Group.objects.get(name='Delegierte')
user.groups.add(delegate)
else:
observer = Group.objects.get(name='Beobachter')
user.groups.add(observer)
messages.success(request, _('%d new participants were successfully imported.') % i)
messages.success(request, _('%d new participants were successfully imported.') % i)
except csv.Error:
message.error(request, _('Import aborted because of severe errors in the input file.'))
else:
messages.error(request, _('Please check the form for errors.'))
else:

View File

@ -0,0 +1,20 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
openslides.utils.csv_ext
~~~~~~~~~~~~~~~~~~~~~~~~
Additional dialect definitions for pythons CSV module.
:copyright: 2011 by the OpenSlides team, see AUTHORS.
:license: GNU GPL, see LICENSE for more details.
"""
from csv import Dialect, excel, register_dialect
class excel_semikolon(excel):
delimiter = ';'
register_dialect("excel_semikolon", excel_semikolon)