Skip to content

Commit

Permalink
Merge branch 'release/0.3.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
abidibo committed May 28, 2024
2 parents b2f2d15 + a0907bc commit aad8efb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
23 changes: 13 additions & 10 deletions admin_export_action/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
from openpyxl.styles import Font
from openpyxl.utils import get_column_letter
from openpyxl.workbook import Workbook
from openpyxl.writer.excel import save_virtual_workbook
from tempfile import NamedTemporaryFile

from six import BytesIO, text_type
from six import text_type

from .introspection import get_model_from_path_string
from .config import get_config
Expand Down Expand Up @@ -224,14 +224,17 @@ def list_to_workbook(data, title='report', header=None, widths=None):
def build_xlsx_response(wb, title="report"):
""" Take a workbook and return a xlsx file response """
title = generate_filename(title, '.xlsx')
myfile = BytesIO()
myfile.write(save_virtual_workbook(wb))
response = HttpResponse(
myfile.getvalue(),
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = 'attachment; filename=%s' % title
response['Content-Length'] = myfile.tell()
return response
response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
with NamedTemporaryFile() as tmp:
wb.save(tmp.name)
tmp.seek(0)
response = HttpResponse(
tmp.read(),
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = 'attachment; filename=%s' % title
response['Content-Length'] = tmp.tell()

return response


def list_to_xlsx_response(data, title='report', header=None,
Expand Down
12 changes: 8 additions & 4 deletions admin_export_action/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,23 @@ def get_context_data(self, **kwargs):
context['opts'] = model_class._meta
context['queryset'] = queryset
context['model_ct'] = self.request.GET['ct']
context[
'related_fields'] = introspection.get_relation_fields_from_model(
model_class)

related = introspection.get_relation_fields_from_model(model_class)
context['related_fields'] = sorted(related, key=lambda x: x.verbose_name if hasattr(x, 'verbose_name') else x.get_accessor_name())

# extra context
try:
model_admin = admin.site._registry[model_class]
except KeyError:
raise ValueError("Model %r not registered with admin" %
model_class)

context.update(model_admin.admin_site.each_context(self.request))

context.update(introspection.get_fields(model_class, field_name, path))
struct = introspection.get_fields(model_class, field_name, path)
context.update({
"fields": sorted(struct.get("fields"), key=lambda x: x.verbose_name if hasattr(x, 'verbose_name') else x.name),
})
return context

def post(self, request, **kwargs):
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name='django-admin-export-action',
version='0.3.1',
version='0.3.2',
packages=['admin_export_action'],
include_package_data=True,
license='MIT License',
Expand Down Expand Up @@ -41,6 +41,8 @@
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Topic :: Software Development',
],
project_urls={
Expand Down

0 comments on commit aad8efb

Please sign in to comment.