Created
April 25, 2012 22:55
-
-
Save zen4ever/2494169 to your computer and use it in GitHub Desktop.
Decorator for using form with django-piston and JSON-encoded POST body
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.utils import simplejson as json | |
from piston.decorator import decorator | |
def validate_json(form_class, class_method=True): | |
@decorator | |
def wrap(f, *args, **kwargs): | |
if class_method: | |
request = args[1] | |
else: | |
request = args[0] | |
form = form_class(json.loads(request.raw_post_data)) | |
if form.is_valid(): | |
setattr(request, 'form', form) | |
return f(*args, **kwargs) | |
else: | |
return {'errors': form.errors} | |
return wrap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment