2013-05-08 18:07:09 +02:00
|
|
|
import os
|
2014-08-16 09:25:18 +02:00
|
|
|
from io import BytesIO
|
2013-05-08 18:07:09 +02:00
|
|
|
|
2015-03-26 05:36:10 +01:00
|
|
|
from openslides.motions.csv_import import import_motions
|
|
|
|
from openslides.motions.models import Category, Motion
|
2014-10-11 14:34:49 +02:00
|
|
|
from openslides.users.models import User
|
2013-05-12 00:47:49 +02:00
|
|
|
from openslides.utils.test import TestCase
|
2015-01-22 18:29:12 +01:00
|
|
|
from openslides.config.api import config
|
2013-05-08 18:07:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CSVImport(TestCase):
|
|
|
|
def setUp(self):
|
2014-10-11 14:34:49 +02:00
|
|
|
# User1
|
|
|
|
self.user1 = User.objects.create_user('Admin_ieY0Eereimeimeizuosh', 'eHiK1aiRahxaix0Iequ2')
|
2013-05-12 00:47:49 +02:00
|
|
|
|
|
|
|
# Normal user
|
2014-10-11 14:34:49 +02:00
|
|
|
self.normal_user = User.objects.create_user('User_CiuNgo4giqueeChie5oi', 'eihi1Eequaek4eagaiKu')
|
2013-05-12 00:47:49 +02:00
|
|
|
|
|
|
|
# Category
|
2014-10-11 14:34:49 +02:00
|
|
|
Category.objects.create(name='Bildung', prefix='B1')
|
|
|
|
Category.objects.create(name='Bildung', prefix='B2')
|
2013-05-12 00:47:49 +02:00
|
|
|
|
|
|
|
def test_example_file_de(self):
|
2015-01-22 18:29:12 +01:00
|
|
|
# Set config to sort names by first_name because the example csv-file
|
|
|
|
# expect this.
|
|
|
|
config['users_sort_users_by_first_name'] = True
|
2013-07-08 10:11:48 +02:00
|
|
|
special_user = User.objects.create_user(username='Harry_Holland',
|
2013-05-12 00:47:49 +02:00
|
|
|
password='iegheeChaje7guthie4a',
|
2013-07-08 10:11:48 +02:00
|
|
|
first_name='Harry',
|
|
|
|
last_name='Holland')
|
2013-05-12 00:47:49 +02:00
|
|
|
for i in range(2):
|
2013-07-08 10:11:48 +02:00
|
|
|
username = 'John_Doe_%d' % i
|
2013-05-12 00:47:49 +02:00
|
|
|
User.objects.create_user(username=username,
|
|
|
|
password='default',
|
2013-07-08 10:11:48 +02:00
|
|
|
first_name='John',
|
|
|
|
last_name='Doe')
|
2013-05-08 18:07:09 +02:00
|
|
|
|
2015-01-22 18:29:12 +01:00
|
|
|
csv_dir = os.path.join(os.path.dirname(__file__), '..', '..', '..', 'extras', 'csv-examples')
|
2013-05-12 00:47:49 +02:00
|
|
|
self.assertEqual(Motion.objects.count(), 0)
|
2014-08-16 09:25:18 +02:00
|
|
|
with open(csv_dir + '/motions-demo_de.csv', 'rb') as f:
|
2014-03-27 20:30:15 +01:00
|
|
|
success_message, warning_message, error_message = import_motions(
|
2014-10-11 14:34:49 +02:00
|
|
|
csvfile=f, default_submitter=self.normal_user, override=False, importing_person=self.user1)
|
2013-07-08 10:11:48 +02:00
|
|
|
self.assertEqual(Motion.objects.count(), 11)
|
2013-05-12 00:47:49 +02:00
|
|
|
|
|
|
|
motion1 = Motion.objects.get(pk=1)
|
|
|
|
self.assertEqual(motion1.identifier, '1')
|
|
|
|
self.assertEqual(motion1.title, u'Entlastung des Vorstandes')
|
|
|
|
self.assertEqual(motion1.text, u'Die Versammlung möge beschließen, den Vorstand für seine letzte Legislaturperiode zu entlasten.')
|
|
|
|
self.assertEqual(motion1.reason, u'Bericht erfolgt mündlich.')
|
|
|
|
self.assertEqual(len(motion1.submitter.all()), 1)
|
|
|
|
self.assertEqual(motion1.submitter.all()[0].person, self.normal_user)
|
|
|
|
self.assertTrue(motion1.category is None)
|
2014-03-27 20:30:15 +01:00
|
|
|
self.assertTrue('Submitter unknown.' in warning_message)
|
|
|
|
self.assertTrue('Category unknown.' in warning_message)
|
2013-05-12 00:47:49 +02:00
|
|
|
|
|
|
|
motion2 = Motion.objects.get(pk=2)
|
2013-07-08 10:11:48 +02:00
|
|
|
self.assertEqual(motion2.identifier, 'SA 1')
|
2013-05-12 00:47:49 +02:00
|
|
|
self.assertEqual(motion2.title, u'Satzungsänderung § 2 Abs. 3')
|
2013-07-08 10:11:48 +02:00
|
|
|
self.assertHTMLEqual(motion2.text, u'''<p>Die Versammlung möge beschließen, die Satzung in § 2 Abs. 3 wie folgt zu ändern:</p>
|
|
|
|
<p>Es wird vor dem Wort "Zweck" das Wort "gemeinnütziger" eingefügt.</p>''')
|
2013-05-12 00:47:49 +02:00
|
|
|
self.assertEqual(motion2.reason, u'Die Änderung der Satzung ist aufgrund der letzten Erfahrungen eine sinnvolle Maßnahme, weil ...')
|
|
|
|
self.assertEqual(len(motion2.submitter.all()), 1)
|
|
|
|
self.assertEqual(motion2.submitter.all()[0].person, special_user)
|
2014-05-18 20:17:42 +02:00
|
|
|
self.assertEqual(motion2.category.name, u"Satzungsanträge") # category is created automatically
|
2013-05-12 00:47:49 +02:00
|
|
|
|
2013-07-08 10:11:48 +02:00
|
|
|
# check user 'John Doe'
|
2014-03-27 20:30:15 +01:00
|
|
|
self.assertTrue('Several suitable submitters found.' in warning_message)
|
2013-07-08 10:11:48 +02:00
|
|
|
# check category 'Bildung'
|
2014-03-27 20:30:15 +01:00
|
|
|
self.assertTrue('Several suitable categories found.' in warning_message)
|
2013-05-08 18:07:09 +02:00
|
|
|
|
2013-05-12 00:47:49 +02:00
|
|
|
def test_malformed_file(self):
|
2014-08-16 09:25:18 +02:00
|
|
|
csv_file = BytesIO()
|
|
|
|
csv_file.write(bytes('Header\nMalformed data,\n,Title,Text,,,\n', 'utf8'))
|
2014-03-27 20:30:15 +01:00
|
|
|
success_message, warning_message, error_message = import_motions(
|
2014-10-11 14:34:49 +02:00
|
|
|
csvfile=csv_file, default_submitter=self.normal_user.id, override=False)
|
2014-03-27 20:30:15 +01:00
|
|
|
self.assertEqual(success_message, '')
|
|
|
|
self.assertTrue('Line is malformed.' in error_message)
|
2013-05-08 18:07:09 +02:00
|
|
|
|
2013-05-12 00:47:49 +02:00
|
|
|
def test_wrong_encoding(self):
|
2014-08-16 09:25:18 +02:00
|
|
|
csv_file = BytesIO(bytes('Müller', 'iso-8859-15'))
|
2014-03-27 20:30:15 +01:00
|
|
|
success_message, warning_message, error_message = import_motions(
|
|
|
|
csvfile=csv_file,
|
2014-10-11 14:34:49 +02:00
|
|
|
default_submitter=self.normal_user.id,
|
2014-03-27 20:30:15 +01:00
|
|
|
override=False)
|
|
|
|
self.assertEqual(success_message, '')
|
2014-08-16 09:25:18 +02:00
|
|
|
self.assertIn('Import file has wrong character encoding, only UTF-8 is supported!', error_message)
|