Skip to content

Commit

Permalink
Fix crash when accessing nonexistent timeseries data via api (fixes #75)
Browse files Browse the repository at this point in the history
  • Loading branch information
kickapoo authored and aptiko committed Apr 12, 2018
1 parent 2d80c74 commit d94faf1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
10 changes: 10 additions & 0 deletions enhydris/api/tests.py
Expand Up @@ -165,6 +165,16 @@ def testGentityList(self):
default_timezone=None)
self.assertEqual(res_last_modified, ref_datum.last_modified)

@RandomEnhydrisTimeseriesDataDir()
def test_get_nonexisting_timeseries_data_using_url_query(self):
response = self.client.get("/api/tsdata/?pk=9999999999999")
self.assertEqual(response.status_code, 404)

@RandomEnhydrisTimeseriesDataDir()
def test_get_nonexisting_timeseries_data_using_url_parameter(self):
response = self.client.get('/api/tsdata/9999999', follow=True)
self.assertEqual(response.status_code, 404)


class WriteTestCase(TestCase):

Expand Down
13 changes: 8 additions & 5 deletions enhydris/api/views.py
Expand Up @@ -55,11 +55,14 @@ class Tsdata(APIView):
permission_classes = (CanEditOrReadOnly,)

def get(self, request, pk, format=None):
timeseries = models.Timeseries.objects.get(pk=int(pk))
self.check_object_permissions(request, timeseries)
response = HttpResponse(content_type='text/plain')
pd2hts.write(timeseries.get_data(), response)
return response
try:
timeseries = models.Timeseries.objects.get(pk=int(pk))
self.check_object_permissions(request, timeseries)
response = HttpResponse(content_type='text/plain')
pd2hts.write(timeseries.get_data(), response)
return response
except models.Timeseries.DoesNotExist:
raise Http404

def put(self, request, pk, format=None):
try:
Expand Down

0 comments on commit d94faf1

Please sign in to comment.