OpenSlides/openslides/motion/csv_import.py

132 lines
5.1 KiB
Python
Raw Normal View History

2013-05-08 18:07:09 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
openslides.motion.csv_import
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2013-05-12 00:47:49 +02:00
Functions to import motions from a csv file.
2013-05-08 18:07:09 +02:00
:copyright: (c) 20112013 by the OpenSlides team, see AUTHORS.
:license: GNU GPL, see LICENSE for more details.
"""
# TODO: Rename the file to 'csv.py' when we drop python2 support. At the moment
# the name csv has a conflict with the core-module. See:
# http://docs.python.org/2/tutorial/modules.html#intra-package-references
import csv
2013-05-12 00:47:49 +02:00
2013-05-08 18:07:09 +02:00
from django.db import transaction
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_noop
2013-05-08 18:07:09 +02:00
from openslides.utils import csv_ext
2013-05-12 00:47:49 +02:00
from openslides.utils.person.api import Persons
from openslides.utils.utils import html_strong
2013-05-12 00:47:49 +02:00
from .models import Category, Motion
2013-05-12 00:47:49 +02:00
2013-05-08 18:07:09 +02:00
def import_motions(csv_file, default_submitter, override=False, importing_person=None):
2013-05-12 00:47:49 +02:00
"""
Imports motions from a csv file.
2013-05-08 18:07:09 +02:00
2013-05-12 00:47:49 +02:00
The file must be encoded in utf8. The first line (header) is ignored.
If no or multiple submitters found, the default submitter is used. If
a motion with a given identifier already exists, the motion is overridden,
when the flag 'override' is true. If no or multiple categories found,
the category is set to None.
"""
2013-05-08 18:07:09 +02:00
error_messages = []
2013-05-12 00:47:49 +02:00
warning_messages = []
2013-05-08 18:07:09 +02:00
count_success = 0
count_lines = 0
2013-05-12 00:47:49 +02:00
# Check encoding
try:
csv_file.read().decode('utf8')
except UnicodeDecodeError:
return (0, 0, [_('Import file has wrong character encoding, only UTF-8 is supported!')], [])
2013-05-08 18:07:09 +02:00
csv_file.seek(0)
2013-05-12 00:47:49 +02:00
2013-05-08 18:07:09 +02:00
with transaction.commit_on_success():
dialect = csv.Sniffer().sniff(csv_file.readline())
dialect = csv_ext.patchup(dialect)
csv_file.seek(0)
for (line_no, line) in enumerate(csv.reader(csv_file, dialect=dialect)):
warnings = []
2013-05-08 18:07:09 +02:00
if line_no < 1:
# Do not read the header line
continue
count_lines += 1
2013-05-12 00:47:49 +02:00
# Check format
2013-05-08 18:07:09 +02:00
try:
2013-05-12 00:47:49 +02:00
(identifier, title, text, reason, submitter, category) = line[:6]
2013-05-08 18:07:09 +02:00
except ValueError:
error_line = html_strong(_('Line %d of import file:') % (line_no + 1))
msg = _('Line is malformed. Motion not imported. Please check the required values.')
error_messages.append("%s<br>%s" % (error_line, msg))
2013-05-08 18:07:09 +02:00
continue
2013-05-12 00:47:49 +02:00
# Check existing motions according to the identifier
2013-05-08 18:07:09 +02:00
if identifier:
try:
motion = Motion.objects.get(identifier=identifier)
except Motion.DoesNotExist:
2013-05-12 00:47:49 +02:00
motion = Motion(identifier=identifier)
else:
if not override:
error_line = html_strong(_('Line %d of import file:') % (line_no + 1))
msg = _('Identifier already exists. Motion not imported.')
error_messages.append("%s<br>%s" % (error_line, msg))
2013-05-12 00:47:49 +02:00
continue
2013-05-08 18:07:09 +02:00
else:
motion = Motion()
2013-05-12 00:47:49 +02:00
# Insert data
2013-05-08 18:07:09 +02:00
motion.title = title
motion.text = text
motion.reason = reason
2013-05-12 00:47:49 +02:00
if category:
try:
motion.category = Category.objects.get(name=category)
except Category.DoesNotExist:
warnings.append(_('Category unknown. No category is used.'))
2013-05-12 00:47:49 +02:00
except Category.MultipleObjectsReturned:
warnings.append(_('Several suitable categories found. No category is used.'))
2013-05-08 18:07:09 +02:00
motion.save()
2013-05-12 00:47:49 +02:00
# Add submitter
person_found = False
if submitter:
for person in Persons():
if person.clean_name == submitter.decode('utf8'):
if person_found:
warnings.append(_('Several suitable submitters found.'))
2013-05-12 00:47:49 +02:00
person_found = False
break
else:
new_submitter = person
person_found = True
if not person_found:
warnings.append(_('Submitter unknown. Default submitter is used.'))
2013-05-12 00:47:49 +02:00
new_submitter = default_submitter
# show summarized warning message for each import line
if warnings:
warning_line = _('Line %d of import file:') % (line_no + 1)
warning_message_string = "%s<ul>" % html_strong(warning_line)
for w in warnings:
warning_message_string += "<li>%s</li>" % w
warning_message_string += "</ul>"
warning_messages.append(warning_message_string)
2013-05-12 00:47:49 +02:00
motion.clear_submitters()
motion.add_submitter(new_submitter)
motion.write_log(message_list=[ugettext_noop('Motion imported')],
person=importing_person)
2013-05-08 18:07:09 +02:00
count_success += 1
2013-05-12 00:47:49 +02:00
return (count_success, count_lines, error_messages, warning_messages)