aboutsummaryrefslogtreecommitdiffstats
path: root/src/rest
diff options
context:
space:
mode:
authortom christie tom@tomchristie.com2010-12-30 01:02:11 +0000
committertom christie tom@tomchristie.com2010-12-30 01:02:11 +0000
commit8a12f89aaacfc0839d6ab1e62b4b5046930517ba (patch)
tree3f0b4fd095d93c5b003832b5e19ecf7f670924ab /src/rest
parent650111dc8c0800e5b7d4c878c1d454657b68efca (diff)
downloaddjango-rest-framework-8a12f89aaacfc0839d6ab1e62b4b5046930517ba.tar.bz2
Added tests
Diffstat (limited to 'src/rest')
-rw-r--r--src/rest/resource.py70
-rw-r--r--src/rest/templates/emitter.html11
-rw-r--r--src/rest/templates/emitter.txt4
-rw-r--r--src/rest/templates/emitter.xhtml3
4 files changed, 70 insertions, 18 deletions
diff --git a/src/rest/resource.py b/src/rest/resource.py
index 7e4a6cd5..18421a19 100644
--- a/src/rest/resource.py
+++ b/src/rest/resource.py
@@ -4,6 +4,14 @@ from rest import emitters, parsers
class Resource(object):
+ class HTTPException(Exception):
+ def __init__(self, status, content, headers):
+ self.status = status
+ self.content = content
+ self.headers = headers
+
+ allowed_methods = ('GET',)
+
callmap = { 'GET': 'read', 'POST': 'create',
'PUT': 'update', 'DELETE': 'delete' }
@@ -17,6 +25,7 @@ class Resource(object):
'application/xml': parsers.XMLParser,
'application/x-www-form-urlencoded': parsers.FormParser }
+
def __new__(cls, request, *args, **kwargs):
self = object.__new__(cls)
self.__init__()
@@ -28,7 +37,6 @@ class Resource(object):
def _determine_parser(self, request):
"""Return the appropriate parser for the input, given the client's 'Content-Type' header,
and the content types that this Resource knows how to parse."""
- print request.META
return self.parsers.values()[0]
# TODO: Raise 415 Unsupported media type
@@ -79,25 +87,40 @@ class Resource(object):
(accept_mimetype == mimetype)):
return (mimetype, emitter)
- # TODO: Raise 406, Not Acceptable
+ raise self.HTTPException(406, {'status': 'Not Acceptable',
+ 'accepts': ','.join(item[0] for item in self.emitters)}, {})
+
def _handle_request(self, request, *args, **kwargs):
- meth = request.method
-
- # Parse the HTTP Request content
- if meth in ('PUT', 'POST'):
- parser = self._determine_parser(request)
- data = parser(self, request).parse(request.raw_post_data)
+ method = request.method
- if meth == "POST":
- (status, ret, headers) = self.handle_post(data, request.META, *args, **kwargs)
- else:
- (status, ret, headers) = self.handle_get(request.META, *args, **kwargs)
+ try:
+ if not method in self.allowed_methods:
+ raise self.HTTPException(405, {'status': 'Method Not Allowed'}, {})
+
+ # Parse the HTTP Request content
+ func = getattr(self, self.callmap.get(method, ''))
+
+ if method in ('PUT', 'POST'):
+ parser = self._determine_parser(request)
+ data = parser(self, request).parse(request.raw_post_data)
+ (status, ret, headers) = func(data, request.META, *args, **kwargs)
+
+ else:
+ (status, ret, headers) = func(request.META, *args, **kwargs)
+ except self.HTTPException, exc:
+ (status, ret, headers) = (exc.status, exc.content, exc.headers)
+ headers['Allow'] = ', '.join(self.allowed_methods)
+
# Serialize the HTTP Response content
- mimetype, emitter = self._determine_emitter(request)
+ try:
+ mimetype, emitter = self._determine_emitter(request)
+ except self.HTTPException, exc:
+ (status, ret, headers) = (exc.status, exc.content, exc.headers)
+ mimetype, emitter = self.emitters[0]
+
content = emitter(self, status, headers).emit(ret)
- print mimetype, emitter, content
# Build the HTTP Response
resp = HttpResponse(content, mimetype=mimetype, status=status)
@@ -106,8 +129,19 @@ class Resource(object):
return resp
- def handle_get(self):
- raise NotImplementedError(self.handle_get)
+ def _not_implemented(self, operation):
+ resource_name = self.__class__.__name__
+ return (500, {'status': 'Internal Server Error',
+ 'detail': '%s %s operation is permitted but has not been implemented' % (resource_name, operation)}, {})
+
+ def read(self, headers={}, *args, **kwargs):
+ return self._not_implemented('read')
+
+ def create(self, data=None, headers={}, *args, **kwargs):
+ return self._not_implemented('create')
+
+ def update(self, data=None, headers={}, *args, **kwargs):
+ return self._not_implemented('update')
- def handle_post(self):
- raise NotImplementedError(self.handle_post) \ No newline at end of file
+ def delete(self, headers={}, *args, **kwargs):
+ return self._not_implemented('delete')
diff --git a/src/rest/templates/emitter.html b/src/rest/templates/emitter.html
new file mode 100644
index 00000000..4c843aa3
--- /dev/null
+++ b/src/rest/templates/emitter.html
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <body>
+ <h1>{{ resource_name }}</h1>
+ <p>{{ resource_doc }}</p>
+ <pre>
+{% include 'emitter.txt' %} </pre>
+ </body>
+</html> \ No newline at end of file
diff --git a/src/rest/templates/emitter.txt b/src/rest/templates/emitter.txt
new file mode 100644
index 00000000..3bf094c6
--- /dev/null
+++ b/src/rest/templates/emitter.txt
@@ -0,0 +1,4 @@
+{% autoescape off %}HTTP Status {{ status }}
+{% for key, val in headers.items %}{{ key }}: {{ val }}
+{% endfor %}
+{{ content }}{% endautoescape %} \ No newline at end of file
diff --git a/src/rest/templates/emitter.xhtml b/src/rest/templates/emitter.xhtml
new file mode 100644
index 00000000..d9fb3ce9
--- /dev/null
+++ b/src/rest/templates/emitter.xhtml
@@ -0,0 +1,3 @@
+HTML:
+
+{{ content }} \ No newline at end of file