Skip to content

Commit

Permalink
Add the ability to set the format in the decorator
Browse files Browse the repository at this point in the history
Geckoboard has deprecated the parameter passing (although it can still be enabled). This change allows you to set
the response format like this:

```python
@NumberWidget(format='json')
def my_view_fun():
  ...
```
  • Loading branch information
robbiehudson committed Sep 26, 2013
1 parent 81ab99f commit 2763a71
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
22 changes: 16 additions & 6 deletions django_geckoboard/decorators.py
Expand Up @@ -38,6 +38,9 @@ class WidgetDecorator(object):
"""
def __new__(cls, *args, **kwargs):
obj = object.__new__(cls)
obj._format = None
if 'format' in kwargs:
obj._format = kwargs.pop('format')
obj.data = kwargs
try:
return obj(args[0])
Expand All @@ -54,7 +57,7 @@ def _wrapped_view(request, *args, **kwargs):
self.data.update(data)
except ValueError:
self.data = data
content, content_type = _render(request, self.data)
content, content_type = _render(request, self.data, self._format)
return HttpResponse(content, content_type=content_type)
wrapper = wraps(view_func, assigned=available_attrs(view_func))
return csrf_exempt(wrapper(_wrapped_view))
Expand Down Expand Up @@ -427,11 +430,18 @@ def _is_api_key_correct(request):
return False


def _render(request, data):
"""Render the data to Geckoboard based on the format request parameter."""
format = request.POST.get('format', '')
if not format:
format = request.GET.get('format', '')
def _render(request, data, format=None):
if format:
if format == "json":
return _render_json(data)
else:
"""Just default to XML"""
return _render_xml(data)
else:
"""Render the data to Geckoboard based on the format request parameter."""
format = request.POST.get('format', '')
if not format:
format = request.GET.get('format', '')
if format == '2':
return _render_json(data)
else:
Expand Down
32 changes: 30 additions & 2 deletions django_geckoboard/tests/test_decorators.py
Expand Up @@ -49,21 +49,41 @@ def test_wrong_api_key(self):
self.assertTrue(isinstance(resp, HttpResponseForbidden), resp)
self.assertEqual('Geckoboard API key incorrect', resp.content)

def test_xml_get(self):
def test_xml_parameter_get(self):
req = HttpRequest()
req.GET['format'] = '1'
resp = widget(lambda r: "test")(req)
self.assertEqual('<?xml version="1.0" ?><root>test</root>',
resp.content)
self.assertEqual(resp._headers['content-type'], ('Content-Type', 'application/xml'))

def test_json_get(self):
def test_json_parameter_get(self):
req = HttpRequest()
req.GET['format'] = '2'
resp = widget(lambda r: "test")(req)
self.assertEqual('"test"', resp.content)
self.assertEqual(resp._headers['content-type'], ('Content-Type', 'application/json'))

def test_json_get(self):
req = HttpRequest()
resp = widget(format="json")(lambda r: "test")(req)
self.assertEqual('"test"', resp.content)
self.assertEqual(resp._headers['content-type'], ('Content-Type', 'application/json'))

def test_xml_get(self):
req = HttpRequest()
resp = widget(format="xml")(lambda r: "test")(req)
self.assertEqual('<?xml version="1.0" ?><root>test</root>',
resp.content)
self.assertEqual(resp._headers['content-type'], ('Content-Type', 'application/xml'))

def test_wrong_format(self):
req = HttpRequest()
resp = widget(format="csv")(lambda r: "test")(req)
self.assertEqual('<?xml version="1.0" ?><root>test</root>',
resp.content)
self.assertEqual(resp._headers['content-type'], ('Content-Type', 'application/xml'))

def test_xml_post(self):
req = HttpRequest()
req.POST['format'] = '1'
Expand Down Expand Up @@ -151,6 +171,14 @@ def test_single_value_and_parameter(self):
json = '{"item": [{"value": 10}], "absolute": "true"}'
self.assertEqual(json, resp.content)

def test_single_value_and_parameter_with_format(self):
# reset POST
del self.request.POST['format']
widget = number_widget(absolute='true', format="json")(lambda r: [10])
resp = widget(self.request)
json = '{"item": [{"value": 10}], "absolute": "true"}'
self.assertEqual(json, resp.content)

def test_single_value_as_dictionary(self):
widget = number_widget(lambda r: [{'value': 10}])
resp = widget(self.request)
Expand Down

0 comments on commit 2763a71

Please sign in to comment.