aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortom christie tom@tomchristie.com2010-12-30 23:30:09 +0000
committertom christie tom@tomchristie.com2010-12-30 23:30:09 +0000
commitc10a95de0869348ccf0f54d4f44f5b52b98e58f0 (patch)
tree31161ca9184fc508ace1e09d45a003f7bd7ffa9e
parentc56e48f52e26a81d7a9f81fd74b0ea46d5434a90 (diff)
downloaddjango-rest-framework-c10a95de0869348ccf0f54d4f44f5b52b98e58f0.tar.bz2
Add parsers, form validation, etc...
-rw-r--r--src/rest/utils.py39
-rw-r--r--src/testapp/forms.py7
2 files changed, 46 insertions, 0 deletions
diff --git a/src/rest/utils.py b/src/rest/utils.py
new file mode 100644
index 00000000..fb655bd8
--- /dev/null
+++ b/src/rest/utils.py
@@ -0,0 +1,39 @@
+# From piston...
+
+def coerce_put_post(request):
+ """
+ Django doesn't particularly understand REST.
+ In case we send data over PUT, Django won't
+ actually look at the data and load it. We need
+ to twist its arm here.
+
+ The try/except abominiation here is due to a bug
+ in mod_python. This should fix it.
+ """
+ if request.method != 'PUT':
+ return
+
+ # Bug fix: if _load_post_and_files has already been called, for
+ # example by middleware accessing request.POST, the below code to
+ # pretend the request is a POST instead of a PUT will be too late
+ # to make a difference. Also calling _load_post_and_files will result
+ # in the following exception:
+ # AttributeError: You cannot set the upload handlers after the upload has been processed.
+ # The fix is to check for the presence of the _post field which is set
+ # the first time _load_post_and_files is called (both by wsgi.py and
+ # modpython.py). If it's set, the request has to be 'reset' to redo
+ # the query value parsing in POST mode.
+ if hasattr(request, '_post'):
+ del request._post
+ del request._files
+
+ try:
+ request.method = "POST"
+ request._load_post_and_files()
+ request.method = "PUT"
+ except AttributeError:
+ request.META['REQUEST_METHOD'] = 'POST'
+ request._load_post_and_files()
+ request.META['REQUEST_METHOD'] = 'PUT'
+
+ request.PUT = request.POST
diff --git a/src/testapp/forms.py b/src/testapp/forms.py
new file mode 100644
index 00000000..b86d8a5f
--- /dev/null
+++ b/src/testapp/forms.py
@@ -0,0 +1,7 @@
+from django import forms
+
+class ExampleForm(forms.Form):
+ title = forms.CharField(max_length=100)
+ message = forms.CharField()
+ sender = forms.EmailField()
+ valid = forms.BooleanField(required=False)