Skip to content

Commit

Permalink
Merge branch 'master' into 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
aptiko committed Jul 18, 2016
2 parents 625f517 + df8d1b4 commit fe48248
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 24 deletions.
2 changes: 2 additions & 0 deletions doc/general/release-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Changes in 1.x minor- and micro- versions
specify a start and end date.
- Version 1.1.1 puts the navbar inside a {% block %}, so that it can be
overriden in custom skins.
- Version 1.1.2 fixes two bugs when editing time series: appending wasn't
working properly, and start and end dates were shown as editable fields.

Version 1.0
===========
Expand Down
37 changes: 19 additions & 18 deletions enhydris/hcore/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ class TimeseriesForm(ModelForm):

class Meta:
model = Timeseries
exclude = ['datafile']
exclude = ['start_date_utc', 'end_date_utc', 'datafile']

def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
Expand Down Expand Up @@ -406,19 +406,34 @@ def __init__(self, *args, **kwargs):
self.fields["instrument"].empty_label = None

def clean_data(self):
# Check if file contains valid timeseries data.
"""Check if file contains valid timeseries data."""

if ('data' not in self.cleaned_data) or not self.cleaned_data['data']:
return None
self.cleaned_data['data'].seek(0)
s = self.cleaned_data['data'].read()
if isinstance(s, bytes):
s = s.decode('utf-8')

# Skip possible header
data = StringIO(s)
while True:
pos = data.tell()
line = data.readline()
if not line:
break
if ('=' not in line) and (not line.isspace()):
data.seek(pos)
break
s = data.read() # Read from the current position onwards
self.cleaned_data['data'] = StringIO(s)

try:
pd2hts.read_file(StringIO(s))
pd2hts.read(self.cleaned_data['data'])
except Exception as e:
raise forms.ValidationError(str(e))

self.cleaned_data['data'].seek(0)
return self.cleaned_data['data']

def clean(self):
Expand Down Expand Up @@ -479,7 +494,7 @@ def clean(self):
try:
atimeseries.append_data(data)
except Exception as e:
raise forms.ValidationError(_(e.message))
raise forms.ValidationError(str(e))

return self.cleaned_data

Expand All @@ -495,20 +510,6 @@ def save(self, commit=True, *args, **kwargs):
if 'data' in self.cleaned_data and self.cleaned_data['data']:
data = self.cleaned_data['data']
data.seek(0)
s = data.read()
if isinstance(s, bytes):
s = s.decode('utf-8')
data = StringIO(s)

# Skip possible header
while True:
pos = data.tell()
line = data.readline()
if not line:
break
if ('=' not in line) and (not line.isspace()):
data.seek(pos)
break

if self.cleaned_data['data_policy'] == 'A':
pass
Expand Down
26 changes: 26 additions & 0 deletions enhydris/hcore/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django.contrib.auth.models import User
from django.test import TestCase

from model_mommy import mommy

from enhydris.hcore.models import Station, Timeseries


class TimeseriesFormTestCase(TestCase):

def test_no_dates(self):
"""Test that start_date and end_date are excluded
start_date_utc and end_date_utc are derived fields; they should not be
shown in the model form.
"""
self.user = User.objects.create_superuser(
'test', email='[email protected]', password='test')
self.user.save()
self.station = mommy.make(Station, name="mystation", creator=self.user)
self.ts = mommy.make(Timeseries, name="tstest", gentity=self.station)

self.client.login(username='test', password='test')
response = self.client.get('/timeseries/edit/{}/'.format(self.ts.id))
self.assertNotContains(response, 'start_date')
self.assertNotContains(response, 'end_date')
23 changes: 19 additions & 4 deletions enhydris/hcore/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ def check_if_connected_to_old_sqlite():
internal server error, but this would be too much work given that we use
SQLite only for development.
"""
if not isinstance(
connections[DEFAULT_DB_ALIAS],
django.contrib.gis.db.backends.spatialite.base.DatabaseWrapper):
try:
from django.contrib.gis.db.backends.spatialite import base
import sqlite3
except ImportError:
return False
if not isinstance(connections[DEFAULT_DB_ALIAS], base.DatabaseWrapper):
return False
import sqlite3
major, minor, micro = [int(x)
for x in sqlite3.sqlite_version.split('.')[:3]]
return (major < 3) or (major == 3 and minor < 8) or (
Expand Down Expand Up @@ -441,6 +443,19 @@ def test_timeseries_data(self):
self.assertTrue(form.is_valid())
ts = form.save()
ts.save()
self.assertEqual(len(ts.get_data()), 12871)

# Append
with open("enhydris/hcore/tests/tsdata2.hts", "rb") as f:
file_dict = {'data': SimpleUploadedFile(f.name, f.read())}
post_dict = {'gentity': self.station.pk, 'variable': self.var.pk,
'unit_of_measurement': self.unit.pk,
'time_zone': self.tz.pk, 'data_policy': 'A',
}
form = TimeseriesDataForm(post_dict, file_dict, instance=self.ts)
self.assertTrue(form.is_valid())
ts = form.save()
ts.save()
self.assertEqual(len(ts.get_data()), 12872)

# Download
Expand Down
3 changes: 1 addition & 2 deletions enhydris/hcore/tests/tsdata.hts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Version=2
Unit=mm
Count=12872
Count=12871
Title=
Comment=ΑΒΑΣ
Comment=
Expand Down Expand Up @@ -12881,4 +12881,3 @@ Variable=ΒΡΟΧΟΠΤΩΣΗ
1997-03-28 08:00,0,
1997-03-29 08:00,0,
1997-03-30 08:00,12,
1997-03-31 08:00,10,
13 changes: 13 additions & 0 deletions enhydris/hcore/tests/tsdata2.hts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Version=2
Unit=mm
Count=1
Title=
Comment=ΑΒΑΣ
Comment=
Comment=ΜΟΝΑΔΕΣ (mm)
Timezone=EET (UTC+0200)
Time_step=1440,0
Timestamp_offset=0,0
Variable=ΒΡΟΧΟΠΤΩΣΗ

1997-03-31 08:00,10,

0 comments on commit fe48248

Please sign in to comment.