2011-07-31 10:46:29 +02:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
openslides.participant.api
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Useful functions for the participant app.
|
|
|
|
|
|
2013-02-16 16:19:20 +01:00
|
|
|
|
:copyright: 2011–2013 by OpenSlides team, see AUTHORS.
|
2011-07-31 10:46:29 +02:00
|
|
|
|
:license: GNU GPL, see LICENSE for more details.
|
|
|
|
|
"""
|
2012-04-25 22:29:19 +02:00
|
|
|
|
|
2012-08-10 19:49:46 +02:00
|
|
|
|
import csv
|
2013-09-25 10:01:01 +02:00
|
|
|
|
from random import choice
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
2012-08-10 19:49:46 +02:00
|
|
|
|
from django.db import transaction
|
2012-11-24 14:01:21 +01:00
|
|
|
|
from django.utils.translation import ugettext as _
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
2012-08-10 19:49:46 +02:00
|
|
|
|
from openslides.utils import csv_ext
|
|
|
|
|
|
2013-09-25 10:01:01 +02:00
|
|
|
|
from .models import Group, User
|
2012-11-23 09:35:41 +01:00
|
|
|
|
|
|
|
|
|
|
2011-09-03 17:17:29 +02:00
|
|
|
|
def gen_password():
|
2012-07-07 15:26:00 +02:00
|
|
|
|
"""
|
2013-06-21 09:51:17 +02:00
|
|
|
|
Generates a random passwort.
|
2012-07-07 15:26:00 +02:00
|
|
|
|
"""
|
2012-09-13 11:50:06 +02:00
|
|
|
|
chars = "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"
|
|
|
|
|
size = 8
|
|
|
|
|
|
|
|
|
|
return ''.join([choice(chars) for i in range(size)])
|
2011-09-03 17:17:29 +02:00
|
|
|
|
|
|
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
|
def gen_username(first_name, last_name):
|
2012-07-07 15:26:00 +02:00
|
|
|
|
"""
|
2013-06-21 09:51:17 +02:00
|
|
|
|
Generates a username from a first- and lastname.
|
2012-07-07 15:26:00 +02:00
|
|
|
|
"""
|
2013-06-21 09:51:17 +02:00
|
|
|
|
first_name = first_name.strip()
|
|
|
|
|
last_name = last_name.strip()
|
|
|
|
|
|
|
|
|
|
if first_name and last_name:
|
|
|
|
|
base_name = " ".join((first_name, last_name))
|
|
|
|
|
else:
|
|
|
|
|
base_name = first_name or last_name
|
|
|
|
|
if not base_name:
|
|
|
|
|
raise ValueError('Either \'first_name\' or \'last_name\' can not be '
|
|
|
|
|
'empty')
|
|
|
|
|
|
|
|
|
|
if not User.objects.filter(username=base_name).exists():
|
|
|
|
|
return base_name
|
|
|
|
|
|
|
|
|
|
counter = 0
|
2011-07-31 10:46:29 +02:00
|
|
|
|
while True:
|
2013-06-21 09:51:17 +02:00
|
|
|
|
counter += 1
|
|
|
|
|
test_name = "%s %d" % (base_name, counter)
|
|
|
|
|
if not User.objects.filter(username=test_name).exists():
|
|
|
|
|
return test_name
|
2012-08-10 19:49:46 +02:00
|
|
|
|
|
2012-08-11 10:51:52 +02:00
|
|
|
|
|
2012-08-10 19:49:46 +02:00
|
|
|
|
def import_users(csv_file):
|
|
|
|
|
error_messages = []
|
|
|
|
|
count_success = 0
|
|
|
|
|
try:
|
|
|
|
|
# check for valid encoding (will raise UnicodeDecodeError if not)
|
|
|
|
|
csv_file.read().decode('utf-8')
|
|
|
|
|
csv_file.seek(0)
|
|
|
|
|
|
|
|
|
|
with transaction.commit_on_success():
|
|
|
|
|
dialect = csv.Sniffer().sniff(csv_file.readline())
|
|
|
|
|
dialect = csv_ext.patchup(dialect)
|
|
|
|
|
csv_file.seek(0)
|
|
|
|
|
|
2012-08-11 10:51:52 +02:00
|
|
|
|
for (line_no, line) in enumerate(csv.reader(csv_file,
|
|
|
|
|
dialect=dialect)):
|
2012-08-10 19:49:46 +02:00
|
|
|
|
if line_no:
|
|
|
|
|
try:
|
2013-04-09 11:21:55 +02:00
|
|
|
|
(title, first_name, last_name, gender, email, groups,
|
|
|
|
|
structure_level, committee, about_me, comment, is_active) = line[:11]
|
2012-08-10 19:49:46 +02:00
|
|
|
|
except ValueError:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
error_messages.append(_('Ignoring malformed line %d in import file.') % (line_no + 1))
|
2012-08-10 19:49:46 +02:00
|
|
|
|
continue
|
2012-08-12 12:52:38 +02:00
|
|
|
|
user = User()
|
2013-04-09 11:21:55 +02:00
|
|
|
|
user.title = title
|
2012-08-10 19:49:46 +02:00
|
|
|
|
user.last_name = last_name
|
|
|
|
|
user.first_name = first_name
|
|
|
|
|
user.username = gen_username(first_name, last_name)
|
|
|
|
|
user.gender = gender
|
2013-04-09 11:21:55 +02:00
|
|
|
|
user.email = email
|
2012-11-19 23:30:09 +01:00
|
|
|
|
user.structure_level = structure_level
|
2012-08-10 19:49:46 +02:00
|
|
|
|
user.committee = committee
|
2013-04-09 11:21:55 +02:00
|
|
|
|
user.about_me = about_me
|
2012-08-10 19:49:46 +02:00
|
|
|
|
user.comment = comment
|
2013-04-09 11:21:55 +02:00
|
|
|
|
if is_active == '1':
|
|
|
|
|
user.is_active = True
|
|
|
|
|
else:
|
|
|
|
|
user.is_active = False
|
2012-10-30 23:10:23 +01:00
|
|
|
|
user.default_password = gen_password()
|
2012-08-10 19:49:46 +02:00
|
|
|
|
user.save()
|
2013-04-09 11:21:55 +02:00
|
|
|
|
for groupid in groups:
|
|
|
|
|
try:
|
|
|
|
|
if groupid != ",":
|
|
|
|
|
Group.objects.get(pk=groupid).user_set.add(user)
|
|
|
|
|
except ValueError:
|
|
|
|
|
error_messages.append(_('Ignoring malformed group id in line %d.') % (line_no + 1))
|
|
|
|
|
continue
|
|
|
|
|
except Group.DoesNotExist:
|
2013-04-22 19:59:05 +02:00
|
|
|
|
error_messages.append(_('Group id %(id)s does not exists (line %(line)d).') % {'id': groupid, 'line': line_no + 1})
|
2013-04-09 11:21:55 +02:00
|
|
|
|
continue
|
2012-08-10 19:49:46 +02:00
|
|
|
|
user.reset_password()
|
|
|
|
|
count_success += 1
|
|
|
|
|
except csv.Error:
|
2013-05-12 00:56:40 +02:00
|
|
|
|
error_messages.append(_('Import aborted because of severe errors in the input file.'))
|
2012-08-10 19:49:46 +02:00
|
|
|
|
except UnicodeDecodeError:
|
2013-05-12 00:56:40 +02:00
|
|
|
|
error_messages.append(_('Import file has wrong character encoding, only UTF-8 is supported!'))
|
2012-08-10 19:49:46 +02:00
|
|
|
|
return (count_success, error_messages)
|
2012-11-23 09:35:41 +01:00
|
|
|
|
|
|
|
|
|
|
2013-03-12 20:58:22 +01:00
|
|
|
|
def get_registered_group():
|
|
|
|
|
"""
|
2013-05-15 23:26:24 +02:00
|
|
|
|
Returns the group 'Registered' (pk=2).
|
2013-03-12 20:58:22 +01:00
|
|
|
|
"""
|
2013-05-15 23:26:24 +02:00
|
|
|
|
return Group.objects.get(pk=2)
|
2013-06-16 12:00:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_or_reset_admin_user():
|
|
|
|
|
group_staff = Group.objects.get(pk=4)
|
|
|
|
|
try:
|
|
|
|
|
admin = User.objects.get(username="admin")
|
|
|
|
|
except User.DoesNotExist:
|
|
|
|
|
admin = User()
|
|
|
|
|
admin.username = 'admin'
|
|
|
|
|
admin.last_name = 'Administrator'
|
|
|
|
|
|
|
|
|
|
admin.default_password = 'admin'
|
|
|
|
|
admin.set_password(admin.default_password)
|
|
|
|
|
admin.save()
|
|
|
|
|
admin.groups.add(group_staff)
|