Skip to content

Commit

Permalink
Merge pull request haiwen#1514 from haiwen/user-batch-op
Browse files Browse the repository at this point in the history
[system admin] users: added 'set quota / delete users in batch'
  • Loading branch information
llj authored Mar 22, 2017
2 parents 5d08286 + e2e9e6c commit 2568d72
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 30 deletions.
2 changes: 1 addition & 1 deletion media/js/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function showConfirm(title, content, yesCallback) {

$cont.html('<h3>' + title + '</h3><p>' + content + '</p>');
$popup.modal({appendTo: '#main'});
$('#simplemodal-container').css({'height':'auto'});
$('#simplemodal-container').css({'width':'auto', 'height':'auto'});

$yesBtn.click(yesCallback);
}
Expand Down
18 changes: 15 additions & 3 deletions seahub/templates/sysadmin/sys_user_admin_ldap_imported.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,30 @@
<li class="tabnav-tab tabnav-tab-cur"><a href="{% url 'sys_useradmin_ldap_imported' %}">{% trans "LDAP(imported)" %}</a></li>
<li class="tabnav-tab"><a href="{% url 'sys_useradmin_admins' %}">{% trans "Admins" %}</a></li>
</ul>
<a class="sf-btn-link btn-white js-export-excel fright" href="{% url "sys_useradmin_export_excel" %}">{% trans "Export Excel" %}</a>

<div class="js-op-for-all fright">
<button id="export-excel">{% trans "Export Excel" %}</button>
</div>
<div class="js-op-for-selected fright hide">
<button id="set-quota-btn">{% trans "Set quota" %}</button>
<button id="delete-users-btn">{% trans "Delete users" %}</button>
</div>
</div>

{% if users %}
<table>
<tr>
<th width="36%">{% trans "Email" %}</th>
<th width="3%"><input type="checkbox" /></th>
<th width="33%">{% trans "Email" %}</th>
<th width="12%">{% trans "Status" %}</th>
<th width="18%">{% trans "Space Used / Quota" %}</th>
<th width="18%">{% trans "Last Login" %}</th>
<th width="16%">{% trans "Operations" %}</th>
<th width="16%"></th>
</tr>

{% for user in users %}
<tr data-userid="{{user.email}}">
<td><input type="checkbox" /></td>
<td>
<a href="{% url 'user_info' user.email %}">{{ user.email }}</a>
{% if user.name %}<br />{{ user.name }}{% endif %}
Expand Down Expand Up @@ -98,6 +107,9 @@ <h2 class="alc">{% trans "No LDAP users have been imported" %}</h2>

{% block extra_script %}
<script type="text/javascript">
$("#export-excel").click(function() {
location.href = "{% url 'sys_useradmin_export_excel' %}";
});
{% include "sysadmin/useradmin_js.html" %}
</script>
{% endblock %}
7 changes: 6 additions & 1 deletion seahub/templates/sysadmin/sys_useradmin.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@
<li class="tabnav-tab"><a href="{% url 'sys_useradmin' %}?filter=paid">Paid</a></li>
{% endif %}
</ul>
<div class="fright">

<div class="js-op-for-all fright">
<button id="import-users-btn">{% trans "Import users" %}</button>
<button id="add-user-btn">{% trans "Add user" %}</button>
<button id="export-excel">{% trans "Export Excel" %}</button>
</div>
<div class="js-op-for-selected fright hide">
<button id="set-quota-btn">{% trans "Set quota" %}</button>
<button id="delete-users-btn">{% trans "Delete users" %}</button>
</div>
</div>

<form id="add-user-form" action="" method="post" class="hide">{% csrf_token %}
Expand Down
9 changes: 8 additions & 1 deletion seahub/templates/sysadmin/sys_useradmin_admins.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@
{% endif %}
<li class="tabnav-tab tabnav-tab-cur"><a href="{% url 'sys_useradmin_admins' %}">{% trans "Admins" %}</a></li>
</ul>
<button id="add-admin-btn" class="fright">{% trans "Add admin" %}</button>

<div class="js-op-for-all fright">
<button id="add-admin-btn">{% trans "Add admin" %}</button>
</div>
<div class="js-op-for-selected fright hide">
<button id="set-quota-btn">{% trans "Set quota" %}</button>
<button id="delete-users-btn">{% trans "Delete users" %}</button>
</div>
</div>

<form id="add-admin-form" method="post" name="add-admin-form" class="tab-popup hide">{% csrf_token %}
Expand Down
8 changes: 7 additions & 1 deletion seahub/templates/sysadmin/user_search.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ <h3>{% trans "Search User"%}</h3>
<label>{% trans "Email" %}</label><input type="text" name="email" class="input" value="{{email}}" /><br />
<input type="submit" value="{% trans "Submit" %}" class="submit" />
</form>
<h3>{% trans "Result"%}</h3>
<div class="ovhd">
<h3 class="fleft">{% trans "Result"%}</h3>
<div class="js-op-for-selected fright hide">
<button id="set-quota-btn">{% trans "Set quota" %}</button>
<button id="delete-users-btn">{% trans "Delete users" %}</button>
</div>
</div>
{% if users %}
{% include "sysadmin/useradmin_table.html"%}
{% else %}
Expand Down
184 changes: 166 additions & 18 deletions seahub/templates/sysadmin/useradmin_js.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,112 @@
});
});

{% if user.source == "DB" or user.source == 'LDAPImport' %}
// for 'select'
var $opForAll = $('.js-op-for-all');
var $opForSelected = $('.js-op-for-selected');
$('th [type="checkbox"]').click(function() {
var all_checked = $(this).prop('checked');
if (all_checked) {
$('td [type="checkbox"]').prop('checked', true);
$opForAll.hide();
$opForSelected.show();
} else {
$('td [type="checkbox"]').prop('checked', false);
$opForAll.show();
$opForSelected.hide();
}
});

$('td [type="checkbox"]').click(function() {
if ($('td [type="checkbox"]:checked').length) {
$opForAll.hide();
$opForSelected.show();
} else {
$opForAll.show();
$opForSelected.hide();
}

if ($('td [type="checkbox"]:checked').length == $('td [type="checkbox"]').length) {
$('th [type="checkbox"]').prop('checked', true);
} else {
$('th [type="checkbox"]').prop('checked', false);
}
});

$('#set-quota-btn').click(function() {
$('#set-quota-form').data({'batch': true}).modal();
$('#simplemodal-container').css({'width':'auto', 'height':'auto'});
});

$('#delete-users-btn').click(function() {
var title = "{% trans "Delete User" %}";
var content = "{% trans "Are you sure you want to delete the selected user(s) ?" %}";
var yesCallback = function() {
var emails = [];
$('td [type="checkbox"]:checked').closest('tr').each(function(index, item) {
var email = $(item).attr('data-userid');
if (email != "{{request.user.username|escapejs}}") {
emails.push(email);
}
});
$.ajax({
url: '{% url "api-v2.1-admin-users-batch" %}',
type: 'POST',
cache: false,
data: {
'operation': 'delete-user',
'email': emails
},
traditional: true,
dataType: 'json',
beforeSend: prepareCSRFToken,
success: function(data) {
if (data.success.length) {
var emails = [];
$(data.success).each(function(index, item) {
$('tr[data-userid="' + item.email + '"]').remove();
emails.push(item.email);
});
var msg = "{% trans "Successfully deleted {users}." %}".replace('{users}', HTMLescape(emails.join(', ')));
feedback(msg, 'success');
}

// all selected items are deleted
if ($('td [type="checkbox"]:checked').length == 0) {
$opForAll.show();
$opForSelected.hide();
}

if (data.failed.length) {
var err_msg = '';
$(data.failed).each(function(index, item) {
err_msg += HTMLescape(item.email) + ': ' + item.error_msg + '<br />';
});
setTimeout(function() { feedback(err_msg, 'error'); }, 1500);
}
},
error: function(xhr, textStatus, errorThrown) {
var err_msg;
if (xhr.responseText) {
err_msg = $.parseJSON(xhr.responseText).error_msg;
} else {
err_msg = "{% trans "Failed. Please check the network." %}";
}
feedback(err_msg, 'error');
},
complete: function() {
$.modal.close();
}
});
};
showConfirm(title, content, yesCallback);
});

// edit quota
$('.quota-edit-icon').click(function() {
var email = $(this).closest('tr').attr('data-userid');
var $spaceQuota = $(this).prev('.user-space-quota');
$('#set-quota-form').data({'email': email, '$spaceQuota': $spaceQuota}).modal();
$('#set-quota-form').data({'batch': false, 'email': email, '$spaceQuota': $spaceQuota}).modal();
$('#simplemodal-container').css({'width':'auto', 'height':'auto'});
});

Expand All @@ -101,23 +201,72 @@
var $submitBtn = $('[type="submit"]', $(this));
disable($submitBtn);

var email = $(this).data('email');
var $spaceQuota = $(this).data('$spaceQuota');
$.ajax({
url: '{{ SITE_ROOT }}api2/accounts/' + encodeURIComponent(email) + '/',
type: 'PUT',
var batch = $(this).data('batch');
var options = {};
if (batch) {
var emails = [];
$('td [type="checkbox"]:checked').closest('tr').each(function(index, item) {
emails.push($(item).attr('data-userid'));
});
options = {
url: '{% url "api-v2.1-admin-users-batch" %}',
type: 'POST',
data: {
'operation': 'set-quota',
'email': emails,
'quota_total': space_quota
},
traditional: true,
success: function(data) {
if (data.success.length) {
var emails = [];
$(data.success).each(function(index, item) {
var $tr = $('tr[data-userid="' + item.email + '"]');
var $quota = $('.user-space-quota', $tr);
if (space_quota == 0) {
$quota.html('--');
} else {
$quota.html(quotaSizeFormat(parseInt(item.quota_total), 1));
}
emails.push(item.email);
});
var msg = "{% trans "Successfully set quota for {users}." %}".replace('{users}', HTMLescape(emails.join(', ')));
feedback(msg, 'success');
}
if (data.failed.length) {
var err_msg = '';
$(data.failed).each(function(index, item) {
err_msg += HTMLescape(item.email) + ': ' + item.error_msg + '<br />';
});
setTimeout(function() { feedback(err_msg, 'error'); }, 1500);
}
$.modal.close();
}
};
} else {
var email = $(this).data('email');
var $spaceQuota = $(this).data('$spaceQuota');
options = {
url: '{{ SITE_ROOT }}api2/accounts/' + encodeURIComponent(email) + '/',
type: 'PUT',
data: {'storage': space_quota},
success: function(data) {
if (space_quota == 0) {
$spaceQuota.html('--');
} else {
$spaceQuota.html(quotaSizeFormat(parseInt(data['total']), 1));
}
var msg = "{% trans "Successfully set quota for {user}." %}".replace('{user}', HTMLescape(data.email));
feedback(msg, 'success');
$.modal.close();
}
};
}

$.ajax($.extend({
dataType: 'json',
cache: false,
beforeSend: prepareCSRFToken,
data: {'storage': space_quota},
success: function(data) {
if (space_quota == 0) {
$spaceQuota.html('--');
} else {
$spaceQuota.html(quotaSizeFormat(parseInt(data['total']), 1));
}
$.modal.close();
},
error: function(xhr, textStatus, errorThrown) {
var err_msg;
if (xhr.responseText) {
Expand All @@ -128,11 +277,10 @@
$error.html(err_msg).show();
enable($submitBtn);
}
});
}, options));

return false;
});
{% endif %}

// select shows, but the user doesn't select a value, or doesn't change the permission, click other place to hide the select
$(document).click(function(e) {
Expand Down
10 changes: 5 additions & 5 deletions seahub/templates/sysadmin/useradmin_table.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
{% load seahub_tags i18n %}
<table>
<tr>
<th width="3%"><input type="checkbox" /></th>
{% if is_pro %}
<th width="24%">{% trans "Email" %} / {% trans "Name" %} / {% trans "Contact Email" %}</th>
<th width="21%">{% trans "Email" %} / {% trans "Name" %} / {% trans "Contact Email" %}</th>
<th width="12%">{% trans "Status" %}</th>
<th width="12%">{% trans "Role" %}</th>
{% else %}
<th width="36%">{% trans "Email" %} / {% trans "Name" %} / {% trans "Contact Email" %}</th>
<th width="33%">{% trans "Email" %} / {% trans "Name" %} / {% trans "Contact Email" %}</th>
<th width="12%">{% trans "Status" %}</th>
{% endif %}
<th width="16%">{% trans "Space Used / Quota" %}</th>
<th width="22%">{% trans "Create At / Last Login" %}</th>
<th width="14%">{% trans "Operations" %}</th>
<th width="14%"></th>
</tr>

{% for user in users %}
<tr data-userid="{{user.email}}">
<td><input type="checkbox" /></td>
<td>
<a href="{% url 'user_info' user.email %}">{{ user.email }}</a>
{% if user.name %}<br />{{ user.name }}{% endif %}
Expand Down Expand Up @@ -105,7 +107,6 @@
{% endfor %}
</table>

{% if user.source == "DB" or user.source == 'LDAPImport' %}
<form id="set-quota-form" method="post" action="" class="hide">{% csrf_token %}
<h3>{% trans "Set user storage limit" %}</h3>
<input type="text" name="space_quota" class="input" /> MB
Expand All @@ -116,4 +117,3 @@ <h3>{% trans "Set user storage limit" %}</h3>
<p class="error hide"></p>
<input type="submit" value="{% trans "Submit" %}" class="submit" />
</form>
{% endif %}

0 comments on commit 2568d72

Please sign in to comment.