From 77ff0298f12dde1736f6944741ff590463d762f2 Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Thu, 9 Nov 2023 14:25:50 +0100 Subject: [PATCH] Order import: Catch CSV parsing errors during iteration (PRETIXEU-9AW) --- src/pretix/control/views/orderimport.py | 29 ++++++++++++++++--------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/pretix/control/views/orderimport.py b/src/pretix/control/views/orderimport.py index 0e5cfbd845..6650658fbb 100644 --- a/src/pretix/control/views/orderimport.py +++ b/src/pretix/control/views/orderimport.py @@ -32,6 +32,7 @@ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations under the License. +import csv import logging from datetime import timedelta @@ -146,16 +147,24 @@ class ProcessView(EventPermissionRequiredMixin, AsyncAction, FormView): else: charset = None try: - return parse_csv(self.file.file, 1024 * 1024, charset=charset) - except UnicodeDecodeError: - messages.warning( - self.request, - _( - "We could not identify the character encoding of the CSV file. " - "Some characters were replaced with a placeholder." + try: + c = parse_csv(self.file.file, 1024 * 1024, charset=charset) + if c is not None: + return list(c) + except UnicodeDecodeError: + messages.warning( + self.request, + _( + "We could not identify the character encoding of the CSV file. " + "Some characters were replaced with a placeholder." + ) ) - ) - return parse_csv(self.file.file, 1024 * 1024, "replace", charset=charset) + c = parse_csv(self.file.file, 1024 * 1024, "replace", charset=charset) + if c is not None: + return list(c) + except csv.Error: + logger.exception("Could not parse CSV file") + return None def get(self, request, *args, **kwargs): if 'async_id' in request.GET and settings.HAS_CELERY: @@ -193,5 +202,5 @@ class ProcessView(EventPermissionRequiredMixin, AsyncAction, FormView): ctx = super().get_context_data(**kwargs) ctx['file'] = self.file ctx['parsed'] = self.parsed - ctx['sample_rows'] = list(self.parsed)[:3] + ctx['sample_rows'] = self.parsed[:3] return ctx