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 from __future__ import with_statement
import csv import csv
import utils.csv_ext
from django.shortcuts import redirect from django.shortcuts import redirect
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
@ -453,7 +454,9 @@ def application_import(request):
applications_generated = 0 applications_generated = 0
applications_modified = 0 applications_modified = 0
with transaction.commit_on_success(): 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 # basic input verification
if lno < 1: if lno < 1:
continue continue

View File

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