Skip to content
Snippets Groups Projects
Commit 3db97098 authored by Martin Mareš's avatar Martin Mareš
Browse files

Ošetření chyb při konverzi do cp1250

Propagování chyby přes všechny vrstvy vychází dost komplikovaně ...
nakonec jsem to obešel tak, že všem `encode` dávám `errors="replace"`,
takže místo nereprezentovatelných znaků vyrábí otazníčky.

Myslím, že je to nakonec pro uživatele lepší, protože otazníčku si
všimne a nemusí složitě hledat, které jméno účastníka obsahuje divný
znak.

A kdo chce, aby tyhle problémy nemusel řešit, má stále možnost použít
UTF-8 ;)

Closes #327.
parent 2144c8b7
No related branches found
No related tags found
No related merge requests found
...@@ -76,7 +76,7 @@ def handle_download_submits(the_job: TheJob): ...@@ -76,7 +76,7 @@ def handle_download_submits(the_job: TheJob):
cnt += 1 cnt += 1
full_name = u.full_name() full_name = u.full_name()
ascii_name = (unicodedata.normalize('NFD', full_name) ascii_name = (unicodedata.normalize('NFD', full_name)
.encode('ascii', 'ignore') .encode('ascii', errors='ignore')
.decode('utf-8')) .decode('utf-8'))
fn = f'{task_code}_{cnt:04d}_{u.user_id}_{p.paper_id}_{ascii_name}.pdf' fn = f'{task_code}_{cnt:04d}_{u.user_id}_{p.paper_id}_{ascii_name}.pdf'
fn = werkzeug.utils.secure_filename(fn) fn = werkzeug.utils.secure_filename(fn)
......
...@@ -48,7 +48,7 @@ def generic_import_page(form: ImportForm, imp: Optional[Import], redirect_url: s ...@@ -48,7 +48,7 @@ def generic_import_page(form: ImportForm, imp: Optional[Import], redirect_url: s
flash('Vyberte si prosím soubor', 'danger') flash('Vyberte si prosím soubor', 'danger')
elif form.get_template.data: elif form.get_template.data:
out = imp.get_template() out = imp.get_template()
resp = app.make_response(out.encode(fmt.get_charset())) resp = app.make_response(out.encode(fmt.get_charset(), errors='replace'))
resp.content_type = fmt.get_content_type() resp.content_type = fmt.get_content_type()
resp.headers.add('Content-Disposition', 'attachment; filename=OSMO-' + imp.template_basename + '.' + fmt.get_extension()) resp.headers.add('Content-Disposition', 'attachment; filename=OSMO-' + imp.template_basename + '.' + fmt.get_extension())
return resp return resp
......
...@@ -239,7 +239,7 @@ class Table: ...@@ -239,7 +239,7 @@ class Table:
r = [row.get(c.key) for c in export_columns] r = [row.get(c.key) for c in export_columns]
writer.writerow(r) writer.writerow(r)
return out.getvalue().encode(fmt.get_charset()) return out.getvalue().encode(fmt.get_charset(), errors='replace')
def to_csv_stream(self, fmt: FileFormat, export_columns: List[Column]) -> Iterable[bytes]: def to_csv_stream(self, fmt: FileFormat, export_columns: List[Column]) -> Iterable[bytes]:
out = io.StringIO() out = io.StringIO()
...@@ -252,12 +252,12 @@ class Table: ...@@ -252,12 +252,12 @@ class Table:
nrows += 1 nrows += 1
if nrows >= 100: if nrows >= 100:
yield out.getvalue().encode(fmt.get_charset()) yield out.getvalue().encode(fmt.get_charset(), errors='replace')
out.seek(0) out.seek(0)
out.truncate() out.truncate()
nrows = 0 nrows = 0
yield out.getvalue().encode(fmt.get_charset()) yield out.getvalue().encode(fmt.get_charset(), errors='replace')
def send_as(self, format: Union[FileFormat, str], streaming: bool = False, args: Union[None, MultiDict, ImmutableMultiDict] = None) -> Response: def send_as(self, format: Union[FileFormat, str], streaming: bool = False, args: Union[None, MultiDict, ImmutableMultiDict] = None) -> Response:
try: try:
......
...@@ -76,7 +76,7 @@ def _task_paper_filename(user: db.User, paper: db.Paper) -> str: ...@@ -76,7 +76,7 @@ def _task_paper_filename(user: db.User, paper: db.Paper) -> str:
full_name = user.full_name() full_name = user.full_name()
ascii_name = (unicodedata.normalize('NFD', full_name) ascii_name = (unicodedata.normalize('NFD', full_name)
.encode('ascii', 'ignore') .encode('ascii', errors='ignore')
.decode('utf-8')) .decode('utf-8'))
if paper.type == db.PaperType.solution: if paper.type == db.PaperType.solution:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment