Skip to content

Commit

Permalink
add: reqeust to context when rendering excel exports
Browse files Browse the repository at this point in the history
  • Loading branch information
saemideluxe committed Nov 13, 2023
1 parent 251bebe commit 45e4546
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions basxbread/utils/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.utils.html import strip_tags


def generate_excel(rows, columns):
def generate_excel(rows, columns, request=None):
"""
columns: dict with {<columnname>: formatting_function(row)}
"""
Expand All @@ -29,7 +29,7 @@ def generate_excel(rows, columns):
for i, cell in enumerate(columndata[1:]):
value = columns[columnname](rows[i])
if isinstance(value, hg.BaseElement):
value = hg.render(value, {})
value = hg.render(value, {"request": request})

cleaned = html.unescape(
strip_tags(newline_regex.sub(r"\n", str(value or "")))
Expand Down
13 changes: 8 additions & 5 deletions basxbread/views/browse.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def default_bulkactions(model, columns=["__all__"]):
"excel",
label=_("Excel"),
iconname="download",
action=lambda request, qs: export(qs, columns),
action=lambda request, qs: export(qs, columns, request=request),
permissions=[f"{model._meta.app_label}.view_{model._meta.model_name}"],
),
BulkAction(
Expand Down Expand Up @@ -419,7 +419,7 @@ def deletelink(return_to_current=True, **attributes):


# helper function to export a queryset to excel
def export(queryset, columns):
def export(queryset, columns, request=None):
if "__all__" in columns:
columns = filter_fieldlist(queryset.model, columns)
columndefinitions = {}
Expand All @@ -439,12 +439,15 @@ def export(queryset, columns):
)

columndefinitions[
hg.render(hg.BaseElement(column.header), {"model": queryset.model})
hg.render(
hg.BaseElement(column.header),
{"model": queryset.model, "request": request},
)
] = lambda row, column=column: hg.render(
hg.BaseElement(column.cell), {"row": row}
hg.BaseElement(column.cell), {"row": row, "request": request}
)

workbook = generate_excel(queryset, columndefinitions)
workbook = generate_excel(queryset, columndefinitions, request=request)
workbook.title = queryset.model._meta.verbose_name
return xlsxresponse(workbook, workbook.title)

Expand Down

0 comments on commit 45e4546

Please sign in to comment.