From c448ce95bd07b5702d7d71887206cf15d4953fb2 Mon Sep 17 00:00:00 2001 From: rene Date: Tue, 22 Nov 2011 18:49:36 +0100 Subject: [PATCH] Switch to python-provided CSV module - implements #66 --- openslides/application/views.py | 5 ++- openslides/participant/views.py | 70 ++++++++++++++++++--------------- openslides/utils/csv_ext.py | 20 ++++++++++ 3 files changed, 63 insertions(+), 32 deletions(-) create mode 100644 openslides/utils/csv_ext.py diff --git a/openslides/application/views.py b/openslides/application/views.py index 20eeca439..827f1fcf6 100644 --- a/openslides/application/views.py +++ b/openslides/application/views.py @@ -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 diff --git a/openslides/participant/views.py b/openslides/participant/views.py index 089cb5bac..d6bcb71db 100644 --- a/openslides/participant/views.py +++ b/openslides/participant/views.py @@ -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: diff --git a/openslides/utils/csv_ext.py b/openslides/utils/csv_ext.py new file mode 100644 index 000000000..d5eb97d4b --- /dev/null +++ b/openslides/utils/csv_ext.py @@ -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) +