Skip to content

Commit

Permalink
Gracefully handle uploading with wrong encoding (fixes #63)
Browse files Browse the repository at this point in the history
  • Loading branch information
aptiko committed Oct 23, 2017
1 parent 6e9daba commit 139f054
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions enhydris/hcore/forms.py
Expand Up @@ -413,7 +413,12 @@ def clean_data(self):
self.cleaned_data['data'].seek(0)
s = self.cleaned_data['data'].read()
if isinstance(s, bytes):
s = s.decode('utf-8')
try:
s = s.decode('utf-8')
except UnicodeDecodeError as e:
raise forms.ValidationError(_(
'The data has incorrect encoding; it must be utf-8. The '
'full error was: "{}"').format(str(e)))

# Skip possible header
data = StringIO(s)
Expand All @@ -425,7 +430,7 @@ def clean_data(self):
if ('=' not in line) and (not line.isspace()):
data.seek(pos)
break
s = data.read() # Read from the current position onwards
s = data.read() # Read from the current position onwards
self.cleaned_data['data'] = StringIO(s)

try:
Expand Down
9 changes: 9 additions & 0 deletions enhydris/hcore/tests/test_views.py
Expand Up @@ -777,6 +777,15 @@ def test_timeseries_can_be_deleted_with_post(self):
self.assertEqual(r.status_code, 200)
self.assertEqual(Timeseries.objects.all().count(), 0)

def test_timeseries_with_wrong_encoding_causes_graceful_error(self):
file_dict = {'data': SimpleUploadedFile('filename', b'\xC0\xC0\xC0')}
post_dict = {'gentity': self.station.pk, 'variable': self.var.pk,
'unit_of_measurement': self.unit.pk,
'time_zone': self.tz.pk
}
form = TimeseriesDataForm(post_dict, file_dict, instance=self.ts)
self.assertFalse(form.is_valid())


@override_settings(ENHYDRIS_USERS_CAN_ADD_CONTENT=True)
class OpenVTestCase(TestCase):
Expand Down

0 comments on commit 139f054

Please sign in to comment.