2014-03-27 20:30:15 +01:00
|
|
|
import csv
|
|
|
|
|
|
|
|
from django.db import transaction
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
|
|
from openslides.utils import csv_ext
|
|
|
|
from openslides.utils.utils import html_strong
|
|
|
|
|
|
|
|
from .api import gen_password, gen_username
|
|
|
|
from .models import Group, User
|
|
|
|
|
|
|
|
|
|
|
|
def import_users(csvfile):
|
|
|
|
error_messages = []
|
|
|
|
count_success = 0
|
|
|
|
try:
|
|
|
|
# check for valid encoding (will raise UnicodeDecodeError if not)
|
|
|
|
csvfile.read().decode('utf-8')
|
|
|
|
csvfile.seek(0)
|
|
|
|
|
2014-10-19 10:16:22 +02:00
|
|
|
with transaction.atomic():
|
2014-08-16 09:25:18 +02:00
|
|
|
dialect = csv.Sniffer().sniff(csvfile.readline().decode('utf-8'))
|
2014-03-27 20:30:15 +01:00
|
|
|
dialect = csv_ext.patchup(dialect)
|
|
|
|
csvfile.seek(0)
|
|
|
|
|
2014-08-16 09:25:18 +02:00
|
|
|
for (line_no, line) in enumerate(csv.reader(
|
2014-10-19 10:16:22 +02:00
|
|
|
(line.decode('utf8') for line in csvfile.readlines()), dialect=dialect)):
|
2014-03-27 20:30:15 +01:00
|
|
|
if line_no:
|
|
|
|
try:
|
|
|
|
(title, first_name, last_name, gender, email, groups,
|
|
|
|
structure_level, committee, about_me, comment, is_active) = line[:11]
|
|
|
|
except ValueError:
|
|
|
|
error_messages.append(_('Ignoring malformed line %d in import file.') % (line_no + 1))
|
|
|
|
continue
|
|
|
|
if not first_name and not last_name:
|
|
|
|
error_messages.append(_("In line %d you have to provide either 'first_name' or 'last_name'.") % (line_no + 1))
|
|
|
|
continue
|
|
|
|
user = User()
|
|
|
|
user.title = title
|
|
|
|
user.last_name = last_name
|
|
|
|
user.first_name = first_name
|
|
|
|
user.username = gen_username(first_name, last_name)
|
|
|
|
user.gender = gender
|
|
|
|
user.email = email
|
|
|
|
user.structure_level = structure_level
|
|
|
|
user.committee = committee
|
|
|
|
user.about_me = about_me
|
|
|
|
user.comment = comment
|
|
|
|
if is_active == '1':
|
|
|
|
user.is_active = True
|
|
|
|
else:
|
|
|
|
user.is_active = False
|
|
|
|
user.default_password = gen_password()
|
|
|
|
user.save()
|
2014-06-19 14:13:06 +02:00
|
|
|
for groupid in groups.split(','):
|
2014-03-27 20:30:15 +01:00
|
|
|
try:
|
2014-06-19 14:13:06 +02:00
|
|
|
if groupid and int(groupid):
|
2014-03-27 20:30:15 +01:00
|
|
|
Group.objects.get(pk=groupid).user_set.add(user)
|
2014-06-19 14:13:06 +02:00
|
|
|
except (Group.DoesNotExist, ValueError):
|
|
|
|
error_messages.append(_('Ignoring group id "%(id)s" in line %(line)d which does not exist.') %
|
|
|
|
{'id': groupid, 'line': line_no + 1})
|
2014-03-27 20:30:15 +01:00
|
|
|
continue
|
|
|
|
user.reset_password()
|
2014-10-11 14:34:49 +02:00
|
|
|
user.save()
|
2014-03-27 20:30:15 +01:00
|
|
|
count_success += 1
|
|
|
|
except csv.Error:
|
|
|
|
error_messages.append(_('Import aborted because of severe errors in the input file.'))
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
error_messages.append(_('Import file has wrong character encoding, only UTF-8 is supported!'))
|
|
|
|
|
|
|
|
# Build final success message
|
|
|
|
if count_success:
|
2014-10-11 14:34:49 +02:00
|
|
|
success_message = _('%d new users were successfully imported.') % count_success
|
2014-03-27 20:30:15 +01:00
|
|
|
else:
|
|
|
|
success_message = ''
|
|
|
|
|
|
|
|
# Build final error message with all error items (one bullet point for each csv line)
|
|
|
|
full_error_message = ''
|
|
|
|
if error_messages:
|
|
|
|
full_error_message = "%s <ul>" % html_strong(_("Errors"))
|
|
|
|
for error in error_messages:
|
|
|
|
full_error_message += "<li>%s</li>" % error
|
|
|
|
full_error_message += "</ul>"
|
|
|
|
|
|
|
|
return success_message, '', full_error_message
|