From d75e736be7ff8c6b02a513521644140d28f89b49 Mon Sep 17 00:00:00 2001 From: rene Date: Thu, 24 Nov 2011 19:38:25 +0100 Subject: [PATCH] Add patchup-helper to assist csv.Sniffer() --- openslides/application/views.py | 2 ++ openslides/participant/views.py | 1 + openslides/utils/csv_ext.py | 16 ++++++++++++++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/openslides/application/views.py b/openslides/application/views.py index 827f1fcf6..f300d424f 100644 --- a/openslides/application/views.py +++ b/openslides/application/views.py @@ -13,6 +13,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 @@ -455,6 +456,7 @@ def application_import(request): applications_modified = 0 with transaction.commit_on_success(): dialect = csv.Sniffer().sniff(request.FILES['csvfile'].readline()) + dialect = utils.csv_ext.patchup(dialect) request.FILES['csvfile'].seek(0) for (lno, line) in enumerate(csv.reader(request.FILES['csvfile'], dialect=dialect)): # basic input verification diff --git a/openslides/participant/views.py b/openslides/participant/views.py index d6bcb71db..b89f11995 100644 --- a/openslides/participant/views.py +++ b/openslides/participant/views.py @@ -319,6 +319,7 @@ def user_import(request): profile.delete() i = -1 dialect = csv.Sniffer().sniff(request.FILES['csvfile'].readline()) + dialect = utils.csv_ext.patchup(dialect) request.FILES['csvfile'].seek(0) for line in csv.reader(request.FILES['csvfile'], dialect=dialect): i += 1 diff --git a/openslides/utils/csv_ext.py b/openslides/utils/csv_ext.py index d5eb97d4b..30b0bf23e 100644 --- a/openslides/utils/csv_ext.py +++ b/openslides/utils/csv_ext.py @@ -12,9 +12,21 @@ from csv import Dialect, excel, register_dialect -class excel_semikolon(excel): - delimiter = ';' +class excel_semikolon(Dialect): + delimiter = ';' + doublequote = True + lineterminator = '\r\n' + quotechar = '"' + quoting = 0 + skipinitialspace = False +def patchup(dialect): + if dialect: + if dialect.delimiter == excel_semikolon.delimiter and \ + dialect.quotechar == excel_semikolon.quotechar: + # walks like a duck and talks like a duck.. must be one + dialect.doublequote = True + return dialect register_dialect("excel_semikolon", excel_semikolon)