aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/methods.py
diff options
context:
space:
mode:
Diffstat (limited to 'djangorestframework/methods.py')
-rw-r--r--djangorestframework/methods.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/djangorestframework/methods.py b/djangorestframework/methods.py
new file mode 100644
index 00000000..06a96643
--- /dev/null
+++ b/djangorestframework/methods.py
@@ -0,0 +1,35 @@
+"""Mixin classes that provide a determine_method(request) function to determine the HTTP
+method that a given request should be treated as. We use this more generic behaviour to
+allow for overloaded methods in POST forms.
+
+See Richardson & Ruby's RESTful Web Services for justification.
+"""
+
+class MethodMixin(object):
+ """Base class for all MethodMixin classes, which simply defines the interface they provide."""
+ def determine_method(self, request):
+ """Simply return GET, POST etc... as appropriate."""
+ raise NotImplementedError()
+
+
+class StandardMethodMixin(MethodMixin):
+ """Provide for standard HTTP behaviour, with no overloaded POST."""
+
+ def determine_method(self, request):
+ """Simply return GET, POST etc... as appropriate."""
+ return request.method.upper()
+
+
+class OverloadedPOSTMethodMixin(MethodMixin):
+ """Provide for overloaded POST behaviour."""
+
+ """The name to use for the method override field in the POST form."""
+ FORM_PARAM_METHOD = '_method'
+
+ def determine_method(self, request):
+ """Simply return GET, POST etc... as appropriate, allowing for POST overloading
+ by setting a form field with the requested method name."""
+ method = request.method.upper()
+ if method == 'POST' and self.FORM_PARAM_METHOD and request.POST.has_key(self.FORM_PARAM_METHOD):
+ method = request.POST[self.FORM_PARAM_METHOD].upper()
+ return method \ No newline at end of file