aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/content.py
diff options
context:
space:
mode:
authortom christie tom@tomchristie.com2011-02-04 21:52:21 +0000
committertom christie tom@tomchristie.com2011-02-04 21:52:21 +0000
commitfcd7f414c407c8dab7c0badbfa16476826dad3ae (patch)
tree4d532d354e5f63b0ab31212d2dfc26d937c2720d /djangorestframework/content.py
parenteebcdc4dc0e3b34fa76eca638c469b1e79240844 (diff)
downloaddjango-rest-framework-fcd7f414c407c8dab7c0badbfa16476826dad3ae.tar.bz2
Huge stack of refactoring getting stuff into Mixin classes, and loads of tests. Kickass.
Diffstat (limited to 'djangorestframework/content.py')
-rw-r--r--djangorestframework/content.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/djangorestframework/content.py b/djangorestframework/content.py
new file mode 100644
index 00000000..94b908f5
--- /dev/null
+++ b/djangorestframework/content.py
@@ -0,0 +1,55 @@
+"""Mixin classes that provide a determine_content(request) method to return the content type and content of a request.
+We use this more generic behaviour to allow for overloaded content in POST forms.
+"""
+
+class ContentMixin(object):
+ """Base class for all ContentMixin classes, which simply defines the interface they provide."""
+
+ def determine_content(self, request):
+ """If the request contains content return a tuple of (content_type, content) otherwise return None.
+ Note that content_type may be None if it is unset.
+ Must be overridden to be implemented."""
+ raise NotImplementedError()
+
+
+class StandardContentMixin(ContentMixin):
+ """Standard HTTP request content behaviour.
+ See RFC 2616 sec 4.3 - http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3"""
+
+ def determine_content(self, request):
+ """If the request contains content return a tuple of (content_type, content) otherwise return None.
+ Note that content_type may be None if it is unset."""
+
+ if not request.META.get('CONTENT_LENGTH', None) and not request.META.get('TRANSFER_ENCODING', None):
+ return None
+ return (request.META.get('CONTENT_TYPE', None), request.raw_post_data)
+
+
+class OverloadedContentMixin(ContentMixin):
+ """HTTP request content behaviour that also allows arbitrary content to be tunneled in form data."""
+
+ """The name to use for the content override field in the POST form."""
+ FORM_PARAM_CONTENT = '_content'
+
+ """The name to use for the content-type override field in the POST form."""
+ FORM_PARAM_CONTENTTYPE = '_contenttype'
+
+ def determine_content(self, request):
+ """If the request contains content return a tuple of (content_type, content) otherwise return None.
+ Note that content_type may be None if it is unset."""
+ if not request.META.get('CONTENT_LENGTH', None) and not request.META.get('TRANSFER_ENCODING', None):
+ return None
+
+ content_type = request.META.get('CONTENT_TYPE', None)
+
+ if (request.method == 'POST' and self.FORM_PARAM_CONTENT and
+ request.POST.get(self.FORM_PARAM_CONTENT, None) is not None):
+
+ # Set content type if form contains a none empty FORM_PARAM_CONTENTTYPE field
+ content_type = None
+ if self.FORM_PARAM_CONTENTTYPE and request.POST.get(self.FORM_PARAM_CONTENTTYPE, None):
+ content_type = request.POST.get(self.FORM_PARAM_CONTENTTYPE, None)
+
+ return (content_type, request.POST[self.FORM_PARAM_CONTENT])
+
+ return (content_type, request.raw_post_data) \ No newline at end of file