Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cope with bad JSON

  • Loading branch information...
commit 7495a2ba0db09104bd4ef33cea1cd575161ad93e 1 parent aa3945f
@andymckay andymckay authored
View
7 metrics/management/commands/migrate.py
@@ -49,10 +49,11 @@ def handle(self, *args, **options):
uuid = 'global_stats:%s' % row[0]
if not Metric.objects.filter(uuid=uuid).exists():
try:
- Metric.objects.create(uuid=uuid, date=row[3],
- name=fix_name(row[1]),
- value=row[2])
+ obj = Metric.objects.create(uuid=uuid, date=row[3],
+ name=fix_name(row[1]),
+ value=row[2])
count['imported'] += 1
+ obj.index()
except:
count['errors'] += 1
if options['raise']:
View
10 metrics/resources.py
@@ -1,12 +1,13 @@
import json
from django.core.exceptions import ObjectDoesNotExist
+from django import http
from elasticutils import get_es
from tastypie import fields
-from tastypie.exceptions import NotFound
-from tastypie.resources import Resource, ModelResource
from tastypie.authorization import Authorization
+from tastypie.exceptions import ImmediateHttpResponse, NotFound
+from tastypie.resources import Resource, ModelResource
from models import Metric
@@ -32,7 +33,10 @@ class Meta:
def obj_get_list(self, request, **kwargs):
es = get_es()
- query = json.loads(request.body)
+ try:
+ query = json.loads(request.body)
+ except ValueError:
+ raise ImmediateHttpResponse(response=http.HttpResponseBadRequest())
result = es.search(query, 'monolith')
return [Hit(**data.get('_source', data.get('fields', {})))
for data in result['hits']['hits']]
View
21 metrics/tests.py
@@ -23,16 +23,20 @@ def get(self, uri, format='json', data='',
content_type = self.get_content_type(format)
kwargs['content_type'] = content_type
- if data is not None:
- data = self.serializer.serialize(data, format=content_type)
-
req = {
- 'CONTENT_LENGTH': len(data),
+ 'CONTENT_LENGTH': 0,
'CONTENT_TYPE': 'application/json',
'PATH_INFO': unquote(parsed[2]),
'REQUEST_METHOD': 'GET',
- 'wsgi.input': FakePayload(data),
}
+
+ if data is not None:
+ data = self.serializer.serialize(data, format=content_type)
+ req.update({
+ 'CONTENT_LENGTH': len(data),
+ 'wsgi.input': FakePayload(data)
+ })
+
response = self.client.request(**req)
return response
@@ -54,7 +58,6 @@ def test_unindex(self, delete):
assert delete.called
-
class TestIndexResource(ResourceTestCase):
fixtures = ['metrics.json']
url = '/es/index/1/'
@@ -85,6 +88,10 @@ def test_get_no_json(self, search):
eq_(res.status_code, 200)
eq_(len(json.loads(res.content)['objects']), 0)
+ def test_get_bad_json(self, search):
+ res = self.api_client.get(self.url, format='json', data=None)
+ eq_(res.status_code, 400)
+
def test_get_filter(self, search):
data = {'filter': {'term': {'name': 'addons.downloads.count.total'}}}
search.return_value = {u'hits': {u'hits': [
@@ -113,5 +120,3 @@ def test_get_fields(self, search):
result = data['objects'][0]
eq_(result['date'], None)
eq_(result['value'], 2681)
-
-
Please sign in to comment.
Something went wrong with that request. Please try again.