Switch to python-provided CSV module - implements #66
This commit is contained in:
parent
bbb61a9091
commit
c448ce95bd
@ -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
|
||||||
|
@ -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,15 +311,19 @@ 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():
|
||||||
|
try:
|
||||||
|
with transaction.commit_on_success():
|
||||||
profiles = Profile.objects.all()
|
profiles = Profile.objects.all()
|
||||||
for profile in profiles:
|
for profile in profiles:
|
||||||
profile.user.delete()
|
profile.user.delete()
|
||||||
profile.delete()
|
profile.delete()
|
||||||
i = -1
|
i = -1
|
||||||
for line in request.FILES['csvfile']:
|
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
|
i += 1
|
||||||
if i > 0:
|
if i > 0:
|
||||||
(first_name, last_name, gender, group, type, committee) = line.strip().split(',')
|
(first_name, last_name, gender, group, type, committee) = line[:6]
|
||||||
user = User()
|
user = User()
|
||||||
user.last_name = last_name
|
user.last_name = last_name
|
||||||
user.first_name = first_name
|
user.first_name = first_name
|
||||||
@ -342,6 +348,8 @@ def user_import(request):
|
|||||||
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:
|
||||||
|
20
openslides/utils/csv_ext.py
Normal file
20
openslides/utils/csv_ext.py
Normal 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)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user