mirror of https://github.com/jumpserver/jumpserver
export csv
parent
7dfde3a3c5
commit
5af97c969b
|
@ -5,8 +5,8 @@
|
|||
<div class="dt-buttons btn-group">
|
||||
<a class="btn btn-default buttons-pdf" tabindex="0" href="#">
|
||||
<span>PDF</span></a>
|
||||
<a class="btn btn-default buttons-excel" tabindex="0" href="#">
|
||||
<span>Excel</span>
|
||||
<a class="btn btn-default buttons-csv" tabindex="0" href="#">
|
||||
<span>CSV</span>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -88,13 +88,24 @@ $(document).ready(function(){
|
|||
};
|
||||
var table = jumpserver.initDataTable(options);
|
||||
|
||||
$('.buttons-pdf').click(function () {
|
||||
$('.buttons-csv').click(function () {
|
||||
var users = [];
|
||||
var rows = table.rows('.selected').data();
|
||||
$.each(rows, function (index, obj) {
|
||||
users.push(obj.id)
|
||||
});
|
||||
console.log(users)
|
||||
$.ajax({
|
||||
url: "{% url "users:export-user-csv" %}",
|
||||
method: 'POST',
|
||||
data: JSON.stringify({users_id: users}),
|
||||
dataType: "json",
|
||||
success: function (data, textStatus) {
|
||||
window.open(data.redirect)
|
||||
},
|
||||
error: function () {
|
||||
toastr.error('Export failed');
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
}).on('click', '#btn_bulk_update', function(){
|
||||
|
|
|
@ -40,5 +40,5 @@ urlpatterns = [
|
|||
name='user-group-asset-permission-create'),
|
||||
url(r'^user-group/(?P<pk>[0-9]+)/assets', views.UserGroupGrantedAssetView.as_view(),
|
||||
name='user-group-granted-asset'),
|
||||
url(r'down-csv/', views.down_csv),
|
||||
url(r'^export/user/csv/', views.ExportUserCsvView.as_view(), name='export-user-csv'),
|
||||
]
|
||||
|
|
|
@ -1,22 +1,25 @@
|
|||
# ~*~ coding: utf-8 ~*~
|
||||
|
||||
from __future__ import unicode_literals
|
||||
from io import BytesIO
|
||||
import json
|
||||
import uuid
|
||||
|
||||
import unicodecsv as csv
|
||||
from django import forms
|
||||
from django.utils import timezone
|
||||
from django.core.cache import cache
|
||||
from django.contrib.auth import login as auth_login, logout as auth_logout
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.contrib.messages.views import SuccessMessageMixin
|
||||
from django.core.files.storage import default_storage
|
||||
from django.http import HttpResponseRedirect, HttpResponse
|
||||
from django.http import HttpResponseRedirect, HttpResponse, JsonResponse
|
||||
from django.shortcuts import reverse, redirect
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.utils.translation import ugettext as _
|
||||
from django.urls import reverse_lazy
|
||||
from django.views import View
|
||||
from django.views.decorators.cache import never_cache
|
||||
from django.views.decorators.csrf import csrf_protect
|
||||
from django.views.decorators.csrf import csrf_protect, csrf_exempt
|
||||
from django.views.decorators.debug import sensitive_post_parameters
|
||||
from django.views.generic.base import TemplateView
|
||||
from django.views.generic.list import ListView
|
||||
|
@ -33,6 +36,7 @@ from .hands import write_login_log_async
|
|||
from . import forms
|
||||
|
||||
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
|
@ -533,16 +537,36 @@ class BulkImportUserView(AdminUserRequiredMixin, JSONResponseMixin, FormView):
|
|||
return self.render_json_response(data)
|
||||
|
||||
|
||||
def down_csv(request):
|
||||
response = HttpResponse(content_type='application/csv')
|
||||
response['Content-Disposition'] = 'attachment; filename="users-%s.csv"' % \
|
||||
timezone.localtime(timezone.now()).strftime('%Y-%m-%d')
|
||||
writer = csv.writer(response)
|
||||
header = [u"你好", 'username', 'email',
|
||||
_('user group'), _('role'), _('phone'), _('wechat'), _('comment')]
|
||||
writer.writerow(header)
|
||||
for user in User.objects.all():
|
||||
writer.writerow([user.name, user.username, user.email, ','.join([group.name for group in user.groups.all()]),
|
||||
user.role, user.phone, user.wechat, user.comment])
|
||||
return response
|
||||
@method_decorator(csrf_exempt, name='dispatch')
|
||||
class ExportUserCsvView(View):
|
||||
def get(self, request, *args, **kwargs):
|
||||
spm = request.GET.get('spm', '')
|
||||
print(spm)
|
||||
users_id = cache.get(spm)
|
||||
if not users_id and not isinstance(users_id, list):
|
||||
return HttpResponse('May be expired', status=404)
|
||||
|
||||
users = User.objects.filter(id__in=users_id)
|
||||
filename = 'users-%s.csv' % timezone.localtime(timezone.now()).strftime('%Y-%m-%d_%H:%M:%D')
|
||||
response = HttpResponse(content_type='application/csv')
|
||||
response['Content-Disposition'] = 'attachment; filename="%s"' % filename
|
||||
writer = csv.writer(response, delimiter=str(","), lineterminator='\n',
|
||||
quoting=csv.QUOTE_ALL, dialect='excel')
|
||||
header = [_("name"), _('username'), _('email'), _('user group'),
|
||||
_('role'), _('phone'), _('wechat'), _('comment')]
|
||||
writer.writerow(header)
|
||||
for user in users:
|
||||
writer.writerow([user.name, user.username, user.email, ','.join([group.name for group in user.groups.all()]),
|
||||
user.role, user.phone, user.wechat, user.comment])
|
||||
return response
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
try:
|
||||
print(request.body)
|
||||
users_id = json.loads(request.body).get('users_id', [])
|
||||
except ValueError:
|
||||
return HttpResponse('Json object not valid', status=400)
|
||||
spm = uuid.uuid4().get_hex()
|
||||
cache.set(spm, users_id, 300)
|
||||
url = reverse('users:export-user-csv') + '?spm=%s' % spm
|
||||
return JsonResponse({'redirect': url})
|
||||
|
|
Loading…
Reference in New Issue