Commit a1545b88 authored by Michal Čihař's avatar Michal Čihař

Support for exporting stats as JSONP.

Fixes #577
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 9acfec4d
......@@ -70,7 +70,11 @@ Weblate provides various exports to allow you further process the data.
.. http:get:: /exports/stats/(string:project)/(string:component)/
Retrieves statistics for given component in JSON format.
:query integer indent: pretty printed indentation
:query string jsonp: JSONP callback function to wrap the data
Retrieves statistics for given component in JSON format. Optionally as
JSONP when you specify the callback in the ``jsonp`` parameter.
You can get pretty-printed output by appending ``?indent=1`` to the
request.
......
......@@ -23,6 +23,7 @@ Released on ? 2014.
* Compatibility with older Git versions.
* Improved ACL support.
* Added buttons for per language quotes and other special chars.
* Support for exporting stats as JSONP.
weblate 1.9
-----------
......
......@@ -59,6 +59,13 @@ class ExportsViewTest(ViewTestCase):
parsed = json.loads(response.content)
self.assertEqual(parsed[0]['name'], 'Czech')
def test_export_stats_jsonp(self):
response = self.client.get(
reverse('export_stats', kwargs=self.kw_subproject),
{'jsonp': 'test_callback'}
)
self.assertContains(response, 'test_callback(')
def test_data(self):
response = self.client.get(
reverse('data_root')
......
......@@ -269,6 +269,10 @@ def export_stats(request, project, subproject):
except (ValueError, KeyError):
indent = None
jsonp = None
if 'jsonp' in request.GET and request.GET['jsonp']:
jsonp = request.GET['jsonp']
response = []
for trans in subprj.translation_set.all():
response.append({
......@@ -288,11 +292,20 @@ def export_stats(request, project, subproject):
'url': trans.get_share_url(),
'url_translate': get_site_url(trans.get_absolute_url()),
})
json_data = json.dumps(
response,
default=json_dt_handler,
indent=indent,
)
if jsonp:
return HttpResponse(
'{0}({1})'.format(
jsonp,
json_data,
),
content_type='application/javascript'
)
return HttpResponse(
json.dumps(
response,
default=json_dt_handler,
indent=indent,
),
json_data,
content_type='application/json'
)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment