mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-20 19:08:21 +00:00
udpate export excel
This commit is contained in:
@@ -7,3 +7,4 @@ Djblets==0.6.14
|
|||||||
django-compressor==1.4
|
django-compressor==1.4
|
||||||
django-statici18n==1.1.2
|
django-statici18n==1.1.2
|
||||||
git+git://github.com/haiwen/django-constance.git@bde7f7cdfd0ed1631a6817fd4cd76f37bf54fe35#egg=django-constance[database]
|
git+git://github.com/haiwen/django-constance.git@bde7f7cdfd0ed1631a6817fd4cd76f37bf54fe35#egg=django-constance[database]
|
||||||
|
openpyxl==2.3.0
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
|
import openpyxl
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -7,27 +8,26 @@ def write_xls(sheet_name, head, data_list):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import xlwt
|
wb = openpyxl.Workbook()
|
||||||
except ImportError as e:
|
ws = wb.get_active_sheet()
|
||||||
|
except Exception as e:
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
wb = xlwt.Workbook(encoding='utf-8')
|
ws.title = sheet_name
|
||||||
ws = wb.add_sheet(sheet_name)
|
|
||||||
|
|
||||||
# prepare table head
|
|
||||||
head_style = xlwt.XFStyle()
|
|
||||||
head_style.font.bold = True
|
|
||||||
row_num = 0
|
row_num = 0
|
||||||
|
|
||||||
# write table head
|
# write table head
|
||||||
for col_num in xrange(len(head)):
|
for col_num in xrange(len(head)):
|
||||||
ws.write(row_num, col_num, head[col_num], head_style)
|
c = ws.cell(row = row_num + 1, column = col_num + 1)
|
||||||
|
c.value = head[col_num]
|
||||||
|
|
||||||
# write table data
|
# write table data
|
||||||
for row in data_list:
|
for row in data_list:
|
||||||
row_num += 1
|
row_num += 1
|
||||||
for col_num in xrange(len(row)):
|
for col_num in xrange(len(row)):
|
||||||
ws.write(row_num, col_num, row[col_num])
|
c = ws.cell(row = row_num + 1, column = col_num + 1)
|
||||||
|
c.value = row[col_num]
|
||||||
|
|
||||||
return wb
|
return wb
|
||||||
|
@@ -600,23 +600,11 @@ def sys_useradmin_export_excel(request):
|
|||||||
is_pro = False
|
is_pro = False
|
||||||
|
|
||||||
if is_pro:
|
if is_pro:
|
||||||
if CALC_SHARE_USAGE:
|
head = [_("Email"), _("Status"), _("Role"), _("Create At"),
|
||||||
head = [_("Email"), _("Status"), _("Role"), _("Space Used"),
|
_("Last Login"), _("Admin"), _("LDAP(imported)"),]
|
||||||
_("Space Quota"), _("Share Used"), _("Share Quota"),
|
|
||||||
_("Create At"), _("Last Login"), _("Admin"), _("LDAP(imported)"),]
|
|
||||||
else:
|
|
||||||
head = [_("Email"), _("Status"), _("Role"), _("Space Used"),
|
|
||||||
_("Space Quota"), _("Create At"), _("Last Login"),
|
|
||||||
_("Admin"), _("LDAP(imported)"),]
|
|
||||||
else:
|
else:
|
||||||
if CALC_SHARE_USAGE:
|
head = [_("Email"), _("Status"), _("Create At"),
|
||||||
head = [_("Email"), _("Status"), _("Space Used"),
|
_("Last Login"), _("Admin"), _("LDAP(imported)"),]
|
||||||
_("Space Quota"), _("Share Used"), _("Share Quota"),
|
|
||||||
_("Create At"), _("Last Login"), _("Admin"), _("LDAP(imported)"),]
|
|
||||||
else:
|
|
||||||
head = [_("Email"), _("Status"), _("Space Used"),
|
|
||||||
_("Space Quota"), _("Create At"), _("Last Login"),
|
|
||||||
_("Admin"), _("LDAP(imported)"),]
|
|
||||||
|
|
||||||
data_list = []
|
data_list = []
|
||||||
|
|
||||||
@@ -633,18 +621,8 @@ def sys_useradmin_export_excel(request):
|
|||||||
else:
|
else:
|
||||||
status = _('Inactive')
|
status = _('Inactive')
|
||||||
|
|
||||||
_populate_user_quota_usage(user)
|
create_at = tsstr_sec(user.ctime) if user.ctime else ''
|
||||||
space_usage = filesizeformat(user.space_usage)
|
last_login = user.last_login.strftime("%Y-%m-%d %H:%M:%S") if \
|
||||||
space_quota = filesizeformat(user.space_quota) if \
|
|
||||||
user.space_quota > 0 else ''
|
|
||||||
|
|
||||||
if CALC_SHARE_USAGE:
|
|
||||||
share_usage = filesizeformat(user.share_usage)
|
|
||||||
share_quota = filesizeformat(user.share_quota) if \
|
|
||||||
user.share_quota > 0 else ''
|
|
||||||
|
|
||||||
create_at = tsstr_sec(user.ctime) if user.ctime else '--'
|
|
||||||
last_login = translate_seahub_time_str(user.last_login) if \
|
|
||||||
user.last_login else ''
|
user.last_login else ''
|
||||||
|
|
||||||
is_admin = _('Yes') if user.is_staff else ''
|
is_admin = _('Yes') if user.is_staff else ''
|
||||||
@@ -656,22 +634,11 @@ def sys_useradmin_export_excel(request):
|
|||||||
else:
|
else:
|
||||||
role = _('Default')
|
role = _('Default')
|
||||||
|
|
||||||
if CALC_SHARE_USAGE:
|
row = [user.email, status, role, create_at,
|
||||||
row = [user.email, status, role, space_usage, space_quota,
|
last_login, is_admin, ldap_import]
|
||||||
share_usage, share_quota, create_at, last_login,
|
|
||||||
is_admin, ldap_import]
|
|
||||||
else:
|
|
||||||
row = [user.email, status, role, space_usage, space_quota,
|
|
||||||
create_at, last_login, is_admin, ldap_import]
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if CALC_SHARE_USAGE:
|
row = [user.email, status, create_at, last_login,
|
||||||
row = [user.email, status, space_usage, space_quota,
|
is_admin, ldap_import]
|
||||||
share_usage, share_quota, create_at, last_login,
|
|
||||||
is_admin, ldap_import]
|
|
||||||
else:
|
|
||||||
row = [user.email, status, space_usage, space_quota,
|
|
||||||
create_at, last_login, is_admin, ldap_import]
|
|
||||||
|
|
||||||
data_list.append(row)
|
data_list.append(row)
|
||||||
|
|
||||||
@@ -681,7 +648,7 @@ def sys_useradmin_export_excel(request):
|
|||||||
return HttpResponseRedirect(next)
|
return HttpResponseRedirect(next)
|
||||||
|
|
||||||
response = HttpResponse(mimetype='application/ms-excel')
|
response = HttpResponse(mimetype='application/ms-excel')
|
||||||
response['Content-Disposition'] = 'attachment; filename=users.xls'
|
response['Content-Disposition'] = 'attachment; filename=users.xlsx'
|
||||||
wb.save(response)
|
wb.save(response)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@@ -1402,7 +1369,8 @@ def sys_group_admin_export_excel(request):
|
|||||||
head = [_("Name"), _("Creator"), _("Create At")]
|
head = [_("Name"), _("Creator"), _("Create At")]
|
||||||
data_list = []
|
data_list = []
|
||||||
for grp in groups:
|
for grp in groups:
|
||||||
row = [grp.group_name, grp.creator_name, tsstr_sec(grp.timestamp)]
|
create_at = tsstr_sec(grp.timestamp) if grp.timestamp else ''
|
||||||
|
row = [grp.group_name, grp.creator_name, create_at]
|
||||||
data_list.append(row)
|
data_list.append(row)
|
||||||
|
|
||||||
wb = write_xls(_('groups'), head, data_list)
|
wb = write_xls(_('groups'), head, data_list)
|
||||||
@@ -1411,7 +1379,7 @@ def sys_group_admin_export_excel(request):
|
|||||||
return HttpResponseRedirect(next)
|
return HttpResponseRedirect(next)
|
||||||
|
|
||||||
response = HttpResponse(mimetype='application/ms-excel')
|
response = HttpResponse(mimetype='application/ms-excel')
|
||||||
response['Content-Disposition'] = 'attachment; filename=groups.xls'
|
response['Content-Disposition'] = 'attachment; filename=groups.xlsx'
|
||||||
wb.save(response)
|
wb.save(response)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
@@ -132,3 +132,23 @@ class SudoModeTest(BaseTestCase):
|
|||||||
})
|
})
|
||||||
self.assertEqual(302, resp.status_code)
|
self.assertEqual(302, resp.status_code)
|
||||||
self.assertRedirects(resp, reverse('sys_useradmin'))
|
self.assertRedirects(resp, reverse('sys_useradmin'))
|
||||||
|
|
||||||
|
|
||||||
|
class SysGroupAdminExportExcelTest(BaseTestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.login_as(self.admin)
|
||||||
|
|
||||||
|
def test_can_export_excel(self):
|
||||||
|
resp = self.client.get(reverse('sys_group_admin_export_excel'))
|
||||||
|
self.assertEqual(200, resp.status_code)
|
||||||
|
assert 'application/ms-excel' in resp._headers['content-type']
|
||||||
|
|
||||||
|
|
||||||
|
class SysUserAdminExportExcelTest(BaseTestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.login_as(self.admin)
|
||||||
|
|
||||||
|
def test_can_export_excel(self):
|
||||||
|
resp = self.client.get(reverse('sys_useradmin_export_excel'))
|
||||||
|
self.assertEqual(200, resp.status_code)
|
||||||
|
assert 'application/ms-excel' in resp._headers['content-type']
|
||||||
|
Reference in New Issue
Block a user