fix: Resolve Django lazy translation strings (Promise) to python str before exporting to openpyxl cells
This commit is contained in:
parent
5b316f2d8a
commit
d3ddb1ec8a
2 changed files with 8 additions and 2 deletions
|
|
@ -19,8 +19,11 @@ import io
|
|||
_ILLEGAL_CHARS_RE = re.compile(r'[\x00-\x08\x0b\x0c\x0e-\x1f]')
|
||||
|
||||
|
||||
from django.utils.functional import Promise
|
||||
def _sanitize_cell_value(value):
|
||||
"""Supprime les caractères illégaux d'une valeur avant de l'écrire dans une cellule Excel."""
|
||||
if isinstance(value, Promise):
|
||||
value = str(value)
|
||||
if isinstance(value, str):
|
||||
return _ILLEGAL_CHARS_RE.sub('', value)
|
||||
return value
|
||||
|
|
@ -160,7 +163,7 @@ def generate_interventions_excel(start_date=None, end_date=None, output_format='
|
|||
# Écrire les en-têtes avec style
|
||||
for col_num, header in enumerate(headers, 1):
|
||||
cell = ws.cell(row=1, column=col_num)
|
||||
cell.value = header
|
||||
cell.value = str(header)
|
||||
cell.font = Font(bold=True, color="FFFFFF")
|
||||
cell.fill = PatternFill(start_color="366092", end_color="366092", fill_type="solid")
|
||||
cell.alignment = Alignment(horizontal="center", vertical="center")
|
||||
|
|
|
|||
|
|
@ -1000,7 +1000,10 @@ def export_interventions(request):
|
|||
|
||||
_illegal_chars_re = re.compile(r'[\x00-\x08\x0b\x0c\x0e-\x1f]')
|
||||
|
||||
from django.utils.functional import Promise
|
||||
def _sanitize_xlsx(value):
|
||||
if isinstance(value, Promise):
|
||||
value = str(value)
|
||||
if isinstance(value, str):
|
||||
return _illegal_chars_re.sub('', value)
|
||||
return value
|
||||
|
|
@ -1010,7 +1013,7 @@ def export_interventions(request):
|
|||
ws.title = 'Interventions'
|
||||
|
||||
# headers
|
||||
ws.append([h for h, _ in columns])
|
||||
ws.append([str(h) for h, _ in columns])
|
||||
# rows
|
||||
for i in items:
|
||||
ws.append([_sanitize_xlsx(getter(i)) for _, getter in columns])
|
||||
|
|
|
|||
Loading…
Reference in a new issue