mirror of
https://github.com/pretix/pretix.git
synced 2026-05-05 15:14:04 +00:00
XLSX exports: Strip all illegal characters
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import io
|
import io
|
||||||
|
import re
|
||||||
import tempfile
|
import tempfile
|
||||||
from collections import OrderedDict, namedtuple
|
from collections import OrderedDict, namedtuple
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
@@ -10,11 +11,21 @@ from django.db.models import QuerySet
|
|||||||
from django.utils.formats import localize
|
from django.utils.formats import localize
|
||||||
from django.utils.translation import gettext, gettext_lazy as _
|
from django.utils.translation import gettext, gettext_lazy as _
|
||||||
from openpyxl import Workbook
|
from openpyxl import Workbook
|
||||||
from openpyxl.cell.cell import KNOWN_TYPES
|
from openpyxl.cell.cell import KNOWN_TYPES, ILLEGAL_CHARACTERS_RE
|
||||||
|
|
||||||
from pretix.base.models import Event
|
from pretix.base.models import Event
|
||||||
|
|
||||||
|
|
||||||
|
def excel_safe(val):
|
||||||
|
if not isinstance(val, KNOWN_TYPES):
|
||||||
|
val = str(val)
|
||||||
|
|
||||||
|
if isinstance(val, str):
|
||||||
|
val = re.sub(ILLEGAL_CHARACTERS_RE, '', val)
|
||||||
|
|
||||||
|
return val
|
||||||
|
|
||||||
|
|
||||||
class BaseExporter:
|
class BaseExporter:
|
||||||
"""
|
"""
|
||||||
This is the base class for all data exporters
|
This is the base class for all data exporters
|
||||||
@@ -181,7 +192,7 @@ class ListExporter(BaseExporter):
|
|||||||
total = line.total
|
total = line.total
|
||||||
continue
|
continue
|
||||||
ws.append([
|
ws.append([
|
||||||
str(val) if not isinstance(val, KNOWN_TYPES) else val
|
excel_safe(val) if not isinstance(val, KNOWN_TYPES) else val
|
||||||
for val in line
|
for val in line
|
||||||
])
|
])
|
||||||
if total:
|
if total:
|
||||||
@@ -301,7 +312,7 @@ class MultiSheetListExporter(ListExporter):
|
|||||||
total = line.total
|
total = line.total
|
||||||
continue
|
continue
|
||||||
ws.append([
|
ws.append([
|
||||||
str(val) if not isinstance(val, KNOWN_TYPES) else val
|
excel_safe(val)
|
||||||
for val in line
|
for val in line
|
||||||
])
|
])
|
||||||
if total:
|
if total:
|
||||||
|
|||||||
Reference in New Issue
Block a user