From 3db970980765ea0cb493ab64fa9b0bac5cbcc3b9 Mon Sep 17 00:00:00 2001
From: Martin Mares <mj@ucw.cz>
Date: Thu, 28 Sep 2023 15:56:46 +0200
Subject: [PATCH] =?UTF-8?q?O=C5=A1et=C5=99en=C3=AD=20chyb=20p=C5=99i=20kon?=
 =?UTF-8?q?verzi=20do=20cp1250?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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.
---
 mo/jobs/submit.py | 2 +-
 mo/web/imports.py | 2 +-
 mo/web/table.py   | 6 +++---
 mo/web/util.py    | 2 +-
 4 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/mo/jobs/submit.py b/mo/jobs/submit.py
index 1a5c7560..8a8f6b4b 100644
--- a/mo/jobs/submit.py
+++ b/mo/jobs/submit.py
@@ -76,7 +76,7 @@ def handle_download_submits(the_job: TheJob):
             cnt += 1
             full_name = u.full_name()
             ascii_name = (unicodedata.normalize('NFD', full_name)
-                          .encode('ascii', 'ignore')
+                          .encode('ascii', errors='ignore')
                           .decode('utf-8'))
             fn = f'{task_code}_{cnt:04d}_{u.user_id}_{p.paper_id}_{ascii_name}.pdf'
             fn = werkzeug.utils.secure_filename(fn)
diff --git a/mo/web/imports.py b/mo/web/imports.py
index a9c19e04..b5cf4366 100644
--- a/mo/web/imports.py
+++ b/mo/web/imports.py
@@ -48,7 +48,7 @@ def generic_import_page(form: ImportForm, imp: Optional[Import], redirect_url: s
                 flash('Vyberte si prosím soubor', 'danger')
         elif form.get_template.data:
             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.headers.add('Content-Disposition', 'attachment; filename=OSMO-' + imp.template_basename + '.' + fmt.get_extension())
             return resp
diff --git a/mo/web/table.py b/mo/web/table.py
index 2d69af23..a18bd2c6 100644
--- a/mo/web/table.py
+++ b/mo/web/table.py
@@ -239,7 +239,7 @@ class Table:
             r = [row.get(c.key) for c in export_columns]
             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]:
         out = io.StringIO()
@@ -252,12 +252,12 @@ class Table:
 
             nrows += 1
             if nrows >= 100:
-                yield out.getvalue().encode(fmt.get_charset())
+                yield out.getvalue().encode(fmt.get_charset(), errors='replace')
                 out.seek(0)
                 out.truncate()
                 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:
         try:
diff --git a/mo/web/util.py b/mo/web/util.py
index 5e3d56eb..19c2bf61 100644
--- a/mo/web/util.py
+++ b/mo/web/util.py
@@ -76,7 +76,7 @@ def _task_paper_filename(user: db.User, paper: db.Paper) -> str:
 
     full_name = user.full_name()
     ascii_name = (unicodedata.normalize('NFD', full_name)
-                  .encode('ascii', 'ignore')
+                  .encode('ascii', errors='ignore')
                   .decode('utf-8'))
 
     if paper.type == db.PaperType.solution:
-- 
GitLab