From 893853ce8a323103efd3c0eb4d4dc55e9cec4fc6 Mon Sep 17 00:00:00 2001 From: Robert Helmer Date: Tue, 4 Feb 2014 13:01:58 -0800 Subject: [PATCH 1/5] bug 915317 - gccrashes model --- webapp-django/crashstats/crashstats/models.py | 21 +++++++++++++++++++ .../crashstats/crashstats/tests/test_views.py | 14 +++++++++++-- webapp-django/crashstats/crashstats/urls.py | 7 ++++++- webapp-django/crashstats/crashstats/views.py | 16 ++++++++------ 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/webapp-django/crashstats/crashstats/models.py b/webapp-django/crashstats/crashstats/models.py index ea121193c3..25f7982508 100644 --- a/webapp-django/crashstats/crashstats/models.py +++ b/webapp-django/crashstats/crashstats/models.py @@ -1414,3 +1414,24 @@ class LagLog(SocorroMiddleware): # never anything sensitive API_WHITELIST = None + + +class GCCrashes(SocorroMiddleware): + + cache_seconds = 60 + + URL_PREFIX = '/gccrashes/' + required_params = ( + ('product', str), + ('version', str), + ) + + possible_params = ( + ('from_date', datetime.date), + ('to_date', datetime.date), + ) + + API_WHITELIST = ( + 'hits', + 'total', + ) diff --git a/webapp-django/crashstats/crashstats/tests/test_views.py b/webapp-django/crashstats/crashstats/tests/test_views.py index 6c374996df..f195af225f 100644 --- a/webapp-django/crashstats/crashstats/tests/test_views.py +++ b/webapp-django/crashstats/crashstats/tests/test_views.py @@ -691,14 +691,24 @@ def mocked_get(**options): @mock.patch('requests.get') def test_gccrashes_json(self, rget): - url = reverse('crashstats.gccrashes_json') + url = reverse('crashstats.gccrashes_json', + kwargs={'product': 'WaterWolf', + 'version': '1.0', + 'start_date': '2014-01-27', + 'end_date': '2014-02-04' + }) def mocked_get(url, **options): ok_('/product/WaterWolf/' in url) if 'gccrashes/' in url: return Response(""" { - "gccrashes": "", + "hits": [ + [ + "20140203000001", + 366 + ] + ], "total": 1 } """) diff --git a/webapp-django/crashstats/crashstats/urls.py b/webapp-django/crashstats/crashstats/urls.py index fad1f2754e..967e0c4305 100644 --- a/webapp-django/crashstats/crashstats/urls.py +++ b/webapp-django/crashstats/crashstats/urls.py @@ -5,8 +5,12 @@ from . import views, feeds products = r'/products/(?P\w+)' +product = r'/product/(?P\w+)' versions = r'/versions/(?P[;\w\.()]+)' +version = r'/version/(?P[\w\.()]+)' crash_type = r'/crash_type/(?P\w+)' +start_date = r'/start_date/(?P[0-9]{4}-[0-9]{2}-[0-9]{2})' +end_date = r'/end_date/(?P[0-9]{4}-[0-9]{2}-[0-9]{2})' date_range_type = r'/date_range_type/(?P\w+)' # putting a * on the following regex so we allow URLs to be things like # `.../os_name/` without any default value which the view function will @@ -194,7 +198,8 @@ url(r'^gccrashes' + products + versions + '$', views.gccrashes, name='crashstats.gccrashes'), - url(r'^gccrashes/json_data$', + url(r'^gccrashes/json_data' + product + version + start_date + + end_date + '$', views.gccrashes_json, name='crashstats.gccrashes_json'), url(r'^login/$', diff --git a/webapp-django/crashstats/crashstats/views.py b/webapp-django/crashstats/crashstats/views.py index 4ae2fc5a95..709cddd568 100644 --- a/webapp-django/crashstats/crashstats/views.py +++ b/webapp-django/crashstats/crashstats/views.py @@ -2098,13 +2098,17 @@ def gccrashes(request, product, versions=None, default_context=None): @utils.json_view @pass_default_context -def gccrashes_json(request, default_context=None): - json_response = { - 'gccrashes': '', - 'total': 1 - } +def gccrashes_json(request, product, version, start_date, end_date, + default_context=None): + api = models.GCCrashes() + result = api.get( + product=product, + version=version, + from_date=start_date, + to=end_date, + ) - return json_response + return result @pass_default_context From a688dbb6ba8678188f063d6fc7f913d4ac9af7f9 Mon Sep 17 00:00:00 2001 From: Robert Helmer Date: Tue, 4 Feb 2014 14:55:30 -0800 Subject: [PATCH 2/5] pep8 --- .../crashstats/crashstats/tests/test_views.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/webapp-django/crashstats/crashstats/tests/test_views.py b/webapp-django/crashstats/crashstats/tests/test_views.py index f195af225f..b0b4526d30 100644 --- a/webapp-django/crashstats/crashstats/tests/test_views.py +++ b/webapp-django/crashstats/crashstats/tests/test_views.py @@ -691,12 +691,13 @@ def mocked_get(**options): @mock.patch('requests.get') def test_gccrashes_json(self, rget): - url = reverse('crashstats.gccrashes_json', - kwargs={'product': 'WaterWolf', - 'version': '1.0', - 'start_date': '2014-01-27', - 'end_date': '2014-02-04' - }) + url = reverse( + 'crashstats.gccrashes_json', + kwargs={'product': 'WaterWolf', + 'version': '1.0', + 'start_date': '2014-01-27', + 'end_date': '2014-02-04'} + ) def mocked_get(url, **options): ok_('/product/WaterWolf/' in url) From 54bf4478174fa1885ad025bdf8a4701225673cb5 Mon Sep 17 00:00:00 2001 From: Robert Helmer Date: Tue, 4 Feb 2014 14:57:45 -0800 Subject: [PATCH 3/5] unicode not str --- webapp-django/crashstats/crashstats/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/webapp-django/crashstats/crashstats/models.py b/webapp-django/crashstats/crashstats/models.py index 25f7982508..f2896d15d9 100644 --- a/webapp-django/crashstats/crashstats/models.py +++ b/webapp-django/crashstats/crashstats/models.py @@ -1422,8 +1422,8 @@ class GCCrashes(SocorroMiddleware): URL_PREFIX = '/gccrashes/' required_params = ( - ('product', str), - ('version', str), + ('product', unicode), + ('version', unicode), ) possible_params = ( From 34527b4024609d3ee02009c484e82e603e98d704 Mon Sep 17 00:00:00 2001 From: Robert Helmer Date: Tue, 4 Feb 2014 15:19:32 -0800 Subject: [PATCH 4/5] use existing products/versions regexes and santization --- webapp-django/crashstats/crashstats/tests/test_views.py | 9 +++------ webapp-django/crashstats/crashstats/urls.py | 4 +--- webapp-django/crashstats/crashstats/views.py | 4 ++-- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/webapp-django/crashstats/crashstats/tests/test_views.py b/webapp-django/crashstats/crashstats/tests/test_views.py index b0b4526d30..a9524fc115 100644 --- a/webapp-django/crashstats/crashstats/tests/test_views.py +++ b/webapp-django/crashstats/crashstats/tests/test_views.py @@ -694,13 +694,13 @@ def test_gccrashes_json(self, rget): url = reverse( 'crashstats.gccrashes_json', kwargs={'product': 'WaterWolf', - 'version': '1.0', + 'versions': '1.0', 'start_date': '2014-01-27', 'end_date': '2014-02-04'} ) def mocked_get(url, **options): - ok_('/product/WaterWolf/' in url) + ok_('/products/WaterWolf/' in url) if 'gccrashes/' in url: return Response(""" { @@ -718,10 +718,7 @@ def mocked_get(url, **options): rget.side_effect = mocked_get - response = self.client.get(url, { - 'product': 'WaterWolf', - 'version': '20.0' - }) + response = self.client.get(url) ok_(response.status_code, 200) ok_('application/json' in response['content-type']) diff --git a/webapp-django/crashstats/crashstats/urls.py b/webapp-django/crashstats/crashstats/urls.py index 967e0c4305..3a028a12dc 100644 --- a/webapp-django/crashstats/crashstats/urls.py +++ b/webapp-django/crashstats/crashstats/urls.py @@ -5,9 +5,7 @@ from . import views, feeds products = r'/products/(?P\w+)' -product = r'/product/(?P\w+)' versions = r'/versions/(?P[;\w\.()]+)' -version = r'/version/(?P[\w\.()]+)' crash_type = r'/crash_type/(?P\w+)' start_date = r'/start_date/(?P[0-9]{4}-[0-9]{2}-[0-9]{2})' end_date = r'/end_date/(?P[0-9]{4}-[0-9]{2}-[0-9]{2})' @@ -198,7 +196,7 @@ url(r'^gccrashes' + products + versions + '$', views.gccrashes, name='crashstats.gccrashes'), - url(r'^gccrashes/json_data' + product + version + start_date + + url(r'^gccrashes/json_data' + products + versions + start_date + end_date + '$', views.gccrashes_json, name='crashstats.gccrashes_json'), diff --git a/webapp-django/crashstats/crashstats/views.py b/webapp-django/crashstats/crashstats/views.py index 709cddd568..a13915a884 100644 --- a/webapp-django/crashstats/crashstats/views.py +++ b/webapp-django/crashstats/crashstats/views.py @@ -2098,12 +2098,12 @@ def gccrashes(request, product, versions=None, default_context=None): @utils.json_view @pass_default_context -def gccrashes_json(request, product, version, start_date, end_date, +def gccrashes_json(request, product, versions, start_date, end_date, default_context=None): api = models.GCCrashes() result = api.get( product=product, - version=version, + version=versions, from_date=start_date, to=end_date, ) From d6adfb2b8d100beb26d1531389612348637e5e99 Mon Sep 17 00:00:00 2001 From: Robert Helmer Date: Tue, 4 Feb 2014 15:38:16 -0800 Subject: [PATCH 5/5] fix test --- webapp-django/crashstats/crashstats/tests/test_views.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/webapp-django/crashstats/crashstats/tests/test_views.py b/webapp-django/crashstats/crashstats/tests/test_views.py index a9524fc115..590770e3eb 100644 --- a/webapp-django/crashstats/crashstats/tests/test_views.py +++ b/webapp-django/crashstats/crashstats/tests/test_views.py @@ -694,13 +694,12 @@ def test_gccrashes_json(self, rget): url = reverse( 'crashstats.gccrashes_json', kwargs={'product': 'WaterWolf', - 'versions': '1.0', + 'versions': '20.0', 'start_date': '2014-01-27', 'end_date': '2014-02-04'} ) def mocked_get(url, **options): - ok_('/products/WaterWolf/' in url) if 'gccrashes/' in url: return Response(""" {