From c1db4c7b3daf16a7c866c0a8af30ea01f323aa49 Mon Sep 17 00:00:00 2001
From: eeintech <eeintech@eeinte.ch>
Date: Mon, 19 Jul 2021 15:14:08 -0400
Subject: [PATCH] Try to catch encoding error, fixed CI

---
 InvenTree/InvenTree/fields.py |  7 ++++---
 InvenTree/common/files.py     | 25 ++++++++++++++-----------
 2 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/InvenTree/InvenTree/fields.py b/InvenTree/InvenTree/fields.py
index 65664eee6b..81c4f7ba0b 100644
--- a/InvenTree/InvenTree/fields.py
+++ b/InvenTree/InvenTree/fields.py
@@ -20,7 +20,6 @@ from djmoney.forms.fields import MoneyField
 from djmoney.models.validators import MinMoneyValidator
 
 import InvenTree.helpers
-import common.settings
 
 
 class InvenTreeURLFormField(FormURLField):
@@ -42,9 +41,11 @@ class InvenTreeURLField(models.URLField):
 
 def money_kwargs():
     """ returns the database settings for MoneyFields """
+    from common.settings import currency_code_mappings, currency_code_default
+
     kwargs = {}
-    kwargs['currency_choices'] = common.settings.currency_code_mappings()
-    kwargs['default_currency'] = common.settings.currency_code_default()
+    kwargs['currency_choices'] = currency_code_mappings()
+    kwargs['default_currency'] = currency_code_default()
     return kwargs
 
 
diff --git a/InvenTree/common/files.py b/InvenTree/common/files.py
index 45b6c80050..39c97dca6c 100644
--- a/InvenTree/common/files.py
+++ b/InvenTree/common/files.py
@@ -53,17 +53,20 @@ class FileManager:
 
         ext = os.path.splitext(file.name)[-1].lower().replace('.', '')
 
-        if ext in ['csv', 'tsv', ]:
-            # These file formats need string decoding
-            raw_data = file.read().decode('utf-8')
-            # Reset stream position to beginning of file
-            file.seek(0)
-        elif ext in ['xls', 'xlsx', 'json', 'yaml', ]:
-            raw_data = file.read()
-            # Reset stream position to beginning of file
-            file.seek(0)
-        else:
-            raise ValidationError(_(f'Unsupported file format: {ext.upper()}'))
+        try:
+            if ext in ['csv', 'tsv', ]:
+                # These file formats need string decoding
+                raw_data = file.read().decode('utf-8')
+                # Reset stream position to beginning of file
+                file.seek(0)
+            elif ext in ['xls', 'xlsx', 'json', 'yaml', ]:
+                raw_data = file.read()
+                # Reset stream position to beginning of file
+                file.seek(0)
+            else:
+                raise ValidationError(_(f'Unsupported file format: {ext.upper()}'))
+        except UnicodeEncodeError:
+            raise ValidationError(_('Error reading file (invalid encoding)'))
 
         try:
             cleaned_data = tablib.Dataset().load(raw_data, format=ext)