Merge pull request #2286 from FinnStutzenstein/Issue2075

Fixing motion CSV import (fixes #2075, fixes #2165)
This commit is contained in:
Emanuel Schütze 2016-08-23 23:17:32 +02:00 committed by GitHub
commit 5a092710c9
2 changed files with 114 additions and 20 deletions

View File

@ -1185,11 +1185,12 @@ angular.module('OpenSlidesApp.motions.site', ['OpenSlidesApp.motions', 'OpenSlid
.controller('MotionImportCtrl', [
'$scope',
'$q',
'gettext',
'Category',
'Motion',
'User',
function($scope, gettext, Category, Motion, User) {
function($scope, $q, gettext, Category, Motion, User) {
// set initial data for csv import
$scope.motions = [];
$scope.separator = ',';
@ -1296,39 +1297,129 @@ angular.module('OpenSlidesApp.motions.site', ['OpenSlidesApp.motions', 'OpenSlid
});
});
// Counter for creations
$scope.usersCreated = 0;
$scope.categoriesCreated = 0;
// import from csv file
$scope.import = function () {
$scope.csvImporting = true;
// Reset counters
$scope.usersCreated = 0;
$scope.categoriesCreated = 0;
var importedUsers = [];
var importedCategories = [];
// collect users and categories
angular.forEach($scope.motions, function (motion){
if (!motion.importerror) {
// create new user if not exists
// collect user if not exists
if (!motion.submitters_id && motion.submitter) {
var index = motion.submitter.indexOf(' ');
var user = {
first_name: motion.submitter.substr(0, index),
last_name: motion.submitter.substr(index+1),
groups: []
groups_id: []
};
User.create(user).then(
function(success) {
// set new user id
motion.submitters_id = [success.id];
importedUsers.push(user);
}
);
}
// create new category if not exists
// collect category if not exists
if (!motion.category_id && motion.category) {
var category = {
name: motion.category,
prefix: motion.category.charAt(0)
};
Category.create(category).then(
importedCategories.push(category);
}
}
});
// TODO (Issue #2293):
// fix _.uniqWith(importedXXX, _.isEqual);
// (You need lodash version >= 4.0.0)
// unique users
var importedUsersUnique = [];
importedUsers.forEach(function (u1) {
var unique = true;
importedUsersUnique.forEach(function (u2) {
if (u1.first_name == u2.first_name &&
u1.last_name == u2.last_name) {
unique = false;
}
});
if (unique) {
importedUsersUnique.push(u1);
}
});
// unique categories
var importedCategoriesUnique = [];
importedCategories.forEach(function (c1) {
var unique = true;
importedCategoriesUnique.forEach(function (c2) {
if (c1.name == c2.name) {
unique = false;
}
});
if (unique) {
importedCategoriesUnique.push(c1);
}
});
// Promises for users and categories
var createPromises = [];
// create users and categories
importedUsersUnique.forEach(function (user) {
createPromises.push(User.create(user).then(
function (success) {
// set new category id
motion.category_id = [success.id];
user.id = success.id;
$scope.usersCreated++;
}
);
));
});
importedCategoriesUnique.forEach(function (category) {
createPromises.push(Category.create(category).then(
function (success) {
category.id = success.id;
$scope.categoriesCreated++;
}
));
});
// wait for users and categories to create
$q.all(createPromises).then( function() {
angular.forEach($scope.motions, function (motion) {
if (!motion.importerror) {
// now, add user
if (!motion.submitters_id && motion.submitter) {
var index = motion.submitter.indexOf(' ');
var first_name = motion.submitter.substr(0, index);
var last_name = motion.submitter.substr(index+1);
// search for user, set id.
importedUsersUnique.forEach(function (user) {
if (user.first_name == first_name &&
user.last_name == last_name) {
motion.submitters_id = [user.id];
}
});
}
// add category
if (!motion.category_id && motion.category) {
var name = motion.category;
// search for category, set id.
importedCategoriesUnique.forEach(function (category) {
if (category.name == name) {
motion.category_id = category.id;
}
});
}
// finally create motion
Motion.create(motion).then(
function(success) {
motion.imported = true;
@ -1336,6 +1427,7 @@ angular.module('OpenSlidesApp.motions.site', ['OpenSlidesApp.motions', 'OpenSlid
);
}
});
});
$scope.csvimported = true;
};
$scope.clear = function () {

View File

@ -121,6 +121,8 @@
<i class="fa fa-check-circle fa-lg"></i>
{{ motionsImported.length }}
<translate>motions were successfully imported.</translate>
(<translate>Users created</translate>: {{ usersCreated }},
<translate>Categories created</translate>: {{ categoriesCreated }})
</div>
<div class="spacer">