Skip to content

Commit

Permalink
replaced @Ajax(require='GET') with @Ajax(methods=['GET']). require= i…
Browse files Browse the repository at this point in the history
…s now 'pending-deprecated'
  • Loading branch information
vad committed Aug 23, 2012
1 parent 1039e2d commit 179d290
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ You can of course set ``require_GET=True`` for GET requests.

You can also use this alternative syntax::

@ajax(require="GET")
@ajax(methods=["GET", "POST"])
def my_cool_view(request):
return {
'hello': 'world!'
Expand Down
19 changes: 13 additions & 6 deletions ajaxutils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
syntactic sugar for Ajax requests in django
"""
from functools import wraps
import warnings

from django.http import Http404

Expand All @@ -17,19 +18,25 @@ def my_ajax_view(request):
return {'count': 42}
"""
def __init__(self, login_required=False, require_GET=False,
require_POST=False, require=None):
require_POST=False, require=None, methods=None):

self.login_required = login_required

# check request method
method = None
if require_GET:
method = "GET"
methods = ["GET"]
if require_POST:
method = "POST"
methods = ["POST"]
if require:
method = require
self.method = method
warnings.warn(
"'require' argument will be removed. Use 'methods' instead",
PendingDeprecationWarning,
)
methods = [require.upper()]
if methods:
methods = [method.upper() for method in methods]
self.methods = methods

def __call__(self, fn):
@wraps(fn)
Expand All @@ -45,7 +52,7 @@ def wrapper(request, *args, **kwargs):
status=401
)

if self.method and self.method != request.method:
if self.methods and request.method not in self.methods:
return JsonResponse(
{
'status': 'error',
Expand Down
18 changes: 18 additions & 0 deletions ajaxutils_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,21 @@ def test_custom_status_code(self):
self.assertEqual(response.status_code, 712)

json.loads(response.content)

def test_methods_argument_allows_given_methods(self):
response = self.client.get('/simple_get_and_post/')
self.assertEqual(response.status_code, 200)

response = self.client.post('/simple_get_and_post/')
self.assertEqual(response.status_code, 200)

json.loads(response.content)

def test_methods_argument_denies_blocked_methods(self):
response = self.client.delete('/simple_get_and_post/')
self.assertEqual(response.status_code, 405)

response = self.client.put('/simple_get_and_post/')
self.assertEqual(response.status_code, 405)

json.loads(response.content)
1 change: 1 addition & 0 deletions ajaxutils_tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
urlpatterns = patterns('ajaxutils_tests.views',
url(r'^simple/$', ajax()(simple)),
url(r'^simple_bool_get/$', ajax(require_GET=True)(simple)),
url(r'^simple_get_and_post/$', ajax(methods=["get", "post"])(simple)),
url(r'^simple_get/$', ajax(require="GET")(simple)),
url(r'^simple_bool_post/$', ajax(require_POST=True)(simple)),
url(r'^simple_post/$', ajax(require="POST")(simple)),
Expand Down

0 comments on commit 179d290

Please sign in to comment.