Handle duplicates during user import

This commit is contained in:
Finn Stutzenstein 2016-08-24 13:32:37 +02:00
parent e9c5194a17
commit 78ec30d6b2
3 changed files with 161 additions and 20 deletions

View File

@ -684,6 +684,16 @@ img {
background-color: #317796;
}
.dropdown-entries {
white-space: nowrap;
}
.dropdown-entries > li {
padding: 5px 10px 5px 10px;
width: 100%;
cursor: pointer;
}
.slimDropDown {
padding-left: 4px !important;
padding-right: 4px !important;

View File

@ -98,6 +98,9 @@ angular.module('OpenSlidesApp.users.site', ['OpenSlidesApp.users'])
resolve: {
groups: function(Group) {
return Group.findAll();
},
users: function(User) {
return User.findAll();
}
}
})
@ -646,10 +649,12 @@ angular.module('OpenSlidesApp.users.site', ['OpenSlidesApp.users'])
.controller('UserImportCtrl', [
'$scope',
'$q',
'gettext',
'gettextCatalog',
'User',
'Group',
function($scope, gettext, User, Group) {
function($scope, $q, gettext, gettextCatalog, User, Group) {
// import from textarea
$scope.importByLine = function () {
$scope.usernames = $scope.userlist[0].split("\n");
@ -707,9 +712,17 @@ angular.module('OpenSlidesApp.users.site', ['OpenSlidesApp.users'])
$scope.pageChanged = function() {
$scope.limitBegin = ($scope.currentPage - 1) * $scope.itemsPerPage;
};
$scope.duplicateActions = [
'keep original',
'override new',
'create duplicate'
];
// detect if csv file is loaded
$scope.$watch('csv.result', function () {
// All user objects are already loaded via the resolve statement from ui-router.
var users = User.getAll();
$scope.users = [];
$scope.duplicates = 0;
var quotionRe = /^"(.*)"$/;
angular.forEach($scope.csv.result, function (user) {
// title
@ -735,6 +748,8 @@ angular.module('OpenSlidesApp.users.site', ['OpenSlidesApp.users'])
// number
if (user.number) {
user.number = user.number.replace(quotionRe, '$1');
} else {
user.number = "";
}
// groups
if (user.groups) {
@ -782,20 +797,102 @@ angular.module('OpenSlidesApp.users.site', ['OpenSlidesApp.users'])
} else {
user.is_committee = false;
}
// Check for duplicates
user.duplicate = false;
users.forEach(function(user_) {
if (user_.first_name == user.first_name &&
user_.last_name == user.last_name &&
user_.structure_level == user.structure_level) {
if (user.duplicate) {
// there are multiple duplicates!
user.duplicate_info += '\n' + gettextCatalog.getString('There are more than one duplicates of this user!');
} else {
user.duplicate = true;
user.duplicateAction = $scope.duplicateActions[1];
user.duplicate_info = '';
if (user_.title)
user.duplicate_info += user_.title + ' ';
if (user_.first_name)
user.duplicate_info += user_.first_name;
if (user_.first_name && user_.last_name)
user.duplicate_info += ' ';
if (user_.last_name)
user.duplicate_info += user_.last_name;
user.duplicate_info += ' (';
if (user_.number)
user.duplicate_info += gettextCatalog.getString('number') + ': ' + user_.number + ', ';
if (user_.structure_level)
user.duplicate_info += gettextCatalog.getString('structure level') + ': ' + user_.structure_level + ', ';
user.duplicate_info += gettextCatalog.getString('username') + ': ' + user_.username + ') '+
gettextCatalog.getString('already exists.');
$scope.duplicates++;
}
}
});
$scope.users.push(user);
});
$scope.calcStats();
});
// Stats
$scope.calcStats = function() {
// not imported: if importerror or duplicate->keep original
$scope.usersWillNotBeImported = 0;
// imported: all others
$scope.usersWillBeImported = 0;
$scope.users.forEach(function(user) {
if (user.importerror || (user.duplicate && user.duplicateAction == $scope.duplicateActions[0])) {
$scope.usersWillNotBeImported++;
} else {
$scope.usersWillBeImported++;
}
});
};
$scope.setGlobalAction = function (action) {
$scope.users.forEach(function (user) {
if (user.duplicate)
user.duplicateAction = action;
});
$scope.calcStats();
};
// import from csv file
$scope.import = function () {
$scope.csvImporting = true;
var existingUsers = User.getAll();
angular.forEach($scope.users, function (user) {
if (!user.importerror) {
User.create(user).then(
function(success) {
user.imported = true;
}
);
// Do nothing on duplicateAction==duplicateActions[0] (keep original)
if (user.duplicate && (user.duplicateAction == $scope.duplicateActions[1])) {
// delete existing user
var deletePromises = [];
existingUsers.forEach(function(user_) {
if (user_.first_name == user.first_name &&
user_.last_name == user.last_name &&
user_.structure_level == user.structure_level) {
deletePromises.push(User.destroy(user_.id));
}
});
$q.all(deletePromises).then(function() {
User.create(user).then(
function(success) {
user.imported = true;
}
);
});
} else if (!user.duplicate ||
(user.duplicateAction == $scope.duplicateActions[2])) {
// create user
User.create(user).then(
function(success) {
user.imported = true;
}
);
}
}
});
$scope.csvimported = true;

View File

@ -81,7 +81,7 @@
<div ng-show="csv.result">
<h3 translate>Preview</h3>
<table ng-if="!csvImporting" class="table table-striped table-bordered table-condensed">
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th>
@ -95,14 +95,32 @@
<th translate>Comment
<th translate>Is active
<th translate>Is committee</th>
<th ng-if="duplicates > 0" translate>
<i class="fa fa-exclamation-triangle text-danger"></i>
<strong class="text-danger" ng-if="duplicates == 1">1 Duplicate</strong>
<strong class="text-danger" ng-if="duplicates > 1">{{ duplicates }} Duplicates</strong>
<div uib-dropdown>
<button type="button" class="btn btn-default btn-danger btn-sm" uib-dropdown-toggle>
<translate>Set global action</translate>
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-entries">
<li role="menuitem" ng-repeat="action in duplicateActions" class="pointer"
ng-click="setGlobalAction(action)">
<translate>{{ action }}</translate>
</li>
</ul>
</div>
</th>
<tbody>
<tr ng-repeat="user in users | limitTo : itemsPerPage : limitBegin">
<td class="minimum"
ng-class="{ 'text-danger': user.importerror, 'text-success': user.imported }">
<span ng-if="user.importerror">
ng-class="{ 'text-danger': (user.importerror || user.duplicateAction == duplicateActions[0]), 'text-success': user.imported }">
<span ng-if="user.importerror || user.duplicateAction == duplicateActions[0]">
<i class="fa fa-exclamation-triangle fa-lg"></i>
</span>
<span ng-if="!user.importerror && !user.imported">
<span ng-if="!user.importerror && !user.imported && user.duplicateAction != duplicateActions[0]">
<i class="fa fa-check-circle-o fa-lg"></i>
</span>
<span ng-if="user.imported">
@ -130,25 +148,41 @@
<div ng-repeat="groupname in user.groupnames">
{{ groupname | translate }}
</div>
<td style="max-width: 130px;">
<span uib-tooltip="{{ user.comment }}">
{{ user.comment | limitTo: 30 }}{{ user.comment.length > 30 ? '...' : '' }}
</span>
<td>
{{ user.comment }}
<i class="fa pointer" ng-class="user.is_active ? 'fa-check-square-o' : 'fa-square-o'"
ng-click="user.is_active = !user.is_active"></i>
<td>
<i ng-if="user.is_active" class="fa fa-check-square-o"></i>
<td>
<i ng-if="user.is_committee" class="fa fa-check-square-o"></i>
<i class="fa pointer" ng-class="user.is_committee ? 'fa-check-square-o' : 'fa-square-o'"
ng-click="user.is_committee = !user.is_committee"></i>
<td ng-if="duplicates > 0">
<div ng-if="user.duplicate" uib-tooltip="{{ user.duplicate_info }}" uib-dropdown>
<button type="button" class="btn btn-default btn-sm"
uib-dropdown-toggle ng-class="user.duplicateAction == duplicateActions[0] ? 'btn-warning' : (user.duplicateAction == duplicateActions[1] ? 'btn-danger' : 'btn-success')">
{{ user.duplicateAction }}
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-entries">
<li role="menuitem" ng-repeat="action in duplicateActions" class="pointer"
ng-click="user.duplicateAction = action; calcStats()">
<i class="fa fa-check" ng-if="user.duplicateAction == action"></i>
<translate>{{ action }}</translate>
</li>
</ul>
</table>
<uib-pagination total-items="users.length" items-per-page="itemsPerPage" ng-model="currentPage" ng-change="pageChanged()"></uib-pagination>
<div class="text-danger">
<div ng-repeat="user in usersFailed = (users | filter:{importerror:true})"></div>
<i class="fa fa-exclamation-triangle"></i>
{{ usersFailed.length }}
{{ usersWillNotBeImported }}
<translate>participants will be not imported.</translate>
</div>
<div>
<div ng-repeat="user in usersPassed = (users | filter:{importerror:false})"></div>
<i class="fa fa-check-circle-o fa-lg"></i>
{{ users.length - usersFailed.length }}
{{ usersWillBeImported }}
<translate>participants will be imported.</translate>
</div>
<div ng-repeat="user in usersImported = (users | filter:{imported:true})"></div>
@ -160,11 +194,11 @@
</div>
<div class="spacer">
<button ng-if="!csvImporting" ng-click="clear()" class="btn btn-default" translate>
<button ng-click="clear()" class="btn btn-default" translate>
Clear preview
</button>
<button ng-if="!csvImporting" ng-click="import()" class="btn btn-primary" translate>
Import {{ users.length - usersFailed.length }} participants
Import {{ usersWillBeImported }} participants
</button>
</div>
<div class="spacer">