Skip to content

Commit

Permalink
Added:
Browse files Browse the repository at this point in the history
 * from ajaxutils import json
 * tests
 * documentation

Bumped version number to 0.2
  • Loading branch information
vad committed Aug 21, 2012
1 parent 64370a5 commit a65c8c4
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 9 deletions.
72 changes: 72 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,75 @@ This will return a ``405: Method not allowed`` response with the following JSON
}

You can of course set ``require_GET=True`` for GET requests.

You can also use this alternative syntax:

@ajax(require="GET")
def my_cool_view(request):
return {
'hello': 'world!'
}


Custom Status Codes
-------------------
What if you don't want to return an HTTP 200? Do you want to return a 404? Write::

from django.http import Http404

@ajax()
def my_cool_view(request):
raise Http404

This returns::

{
'status': 'error',
'error': 'Not found',
}

Or maybe a 400 - Bad Request::

from django.http import HttpResponseBadRequest

@ajax()
def my_cool_view(request):
return HttpResponseBadRequest('My error message')

This returns::

{
'status': 'error',
'error': 'My error message',
}

and the HTTP response has status code 400.

Another syntax, more Flask-ish::

@ajax()
def my_cool_view(request):
return {
"i'm a": 'teapot'
}, 418


From infinity import json
-------------------------

Tired of writing infinite import statements to choose the best json module? Let ajaxutils do it for you::

from ajaxutils import json

At the moment, ajaxutils prefers simplejson over the stdlib json. No other json module is used. In the future we will probably provide support to ujson using a Django setting.

Changelog
=========

v0.2
----

* Moved JsonResponse to ajaxutils.http
* Added Custom Status Codes handling
* Added documentation for @ajax(require=METHOD)
* Added "from ajaxutils import json"
4 changes: 4 additions & 0 deletions ajaxutils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
try:
import simplejson as json
except ImportError:
import json
7 changes: 2 additions & 5 deletions ajaxutils/http.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
try:
import simplejson as json
except ImportError:
import json

from django.http import HttpResponse

from ajaxutils import json


def handler(obj):
if hasattr(obj, 'isoformat'):
Expand Down
10 changes: 8 additions & 2 deletions ajaxutils_tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import json

from django.test import TestCase
from django.contrib.auth.models import User

from ajaxutils import json


class TestAjaxDecorator(TestCase):
def setUp(self):
Expand Down Expand Up @@ -63,6 +63,12 @@ def test_logged_failure(self):

json.loads(response.content)

def test_catch_http_404_exception(self):
response = self.client.get('/raise/404/')
self.assertEqual(response.status_code, 404)

json.loads(response.content)

def test_custom_status_code(self):
response = self.client.get('/custom/712/')
self.assertEqual(response.status_code, 712)
Expand Down
4 changes: 3 additions & 1 deletion ajaxutils_tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.conf.urls.defaults import patterns, url

from .views import simple, simple_with_custom_status_code
from .views import simple, simple_with_custom_status_code, raise_404

from ajaxutils.decorators import ajax

Expand All @@ -13,6 +13,8 @@
url(r'^simple_post/$', ajax(require="POST")(simple)),
url(r'^logged/$', ajax(login_required=True)(simple)),

url(r'^raise/404/$', ajax()(raise_404)),

url(
r'^custom/(?P<status_code>\d+)/$',
ajax()(simple_with_custom_status_code)
Expand Down
6 changes: 6 additions & 0 deletions ajaxutils_tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@ def simple(request):
return {'status': 'success'}


def raise_404(request):
from django.http import Http404

raise Http404


def simple_with_custom_status_code(request, status_code):
return {'status': 'success'}, int(status_code)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

setup(
name='django-ajaxutils',
version="0.1.2",
version="0.2",
description='Ajax requests for Ponies',
url='http://github.com/ahref/django-ajaxutils',
packages=['ajaxutils'],
Expand Down

0 comments on commit a65c8c4

Please sign in to comment.