aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorTom Christie2012-02-21 20:12:14 +0000
committerTom Christie2012-02-21 20:12:14 +0000
commitaf9e4f69d732cc643d6ec7ae13d4a19ac0332d44 (patch)
tree9d3fb9a8aebc520716e7f83075ef83e8102b5952 /examples
parent21fcd3a90631e96e3fa210dd526abab9571ad6e1 (diff)
downloaddjango-rest-framework-af9e4f69d732cc643d6ec7ae13d4a19ac0332d44.tar.bz2
Merging master into develop
Diffstat (limited to 'examples')
-rw-r--r--examples/blogpost/resources.py6
-rw-r--r--examples/blogpost/tests.py3
-rw-r--r--examples/mixin/urls.py4
-rw-r--r--examples/objectstore/views.py8
-rw-r--r--examples/permissionsexample/views.py6
-rw-r--r--examples/pygments_api/views.py15
-rw-r--r--examples/resourceexample/views.py14
-rw-r--r--examples/sandbox/views.py65
8 files changed, 75 insertions, 46 deletions
diff --git a/examples/blogpost/resources.py b/examples/blogpost/resources.py
index 5a3c1ce2..d4e0594d 100644
--- a/examples/blogpost/resources.py
+++ b/examples/blogpost/resources.py
@@ -1,5 +1,5 @@
-from django.core.urlresolvers import reverse
from djangorestframework.resources import ModelResource
+from djangorestframework.utils import reverse
from blogpost.models import BlogPost, Comment
@@ -12,7 +12,7 @@ class BlogPostResource(ModelResource):
ordering = ('-created',)
def comments(self, instance):
- return reverse('comments', kwargs={'blogpost': instance.key})
+ return reverse('comments', request, kwargs={'blogpost': instance.key})
class CommentResource(ModelResource):
@@ -24,4 +24,4 @@ class CommentResource(ModelResource):
ordering = ('-created',)
def blogpost(self, instance):
- return reverse('blog-post', kwargs={'key': instance.blogpost.key})
+ return reverse('blog-post', request, kwargs={'key': instance.blogpost.key})
diff --git a/examples/blogpost/tests.py b/examples/blogpost/tests.py
index 5aa4f89f..9f72e686 100644
--- a/examples/blogpost/tests.py
+++ b/examples/blogpost/tests.py
@@ -1,12 +1,11 @@
"""Test a range of REST API usage of the example application.
"""
-from django.core.urlresolvers import reverse
from django.test import TestCase
-from django.core.urlresolvers import reverse
from django.utils import simplejson as json
from djangorestframework.compat import RequestFactory
+from djangorestframework.utils import reverse
from djangorestframework.views import InstanceModelView, ListOrCreateModelView
from blogpost import models, urls
diff --git a/examples/mixin/urls.py b/examples/mixin/urls.py
index 58cf370c..c899467b 100644
--- a/examples/mixin/urls.py
+++ b/examples/mixin/urls.py
@@ -2,9 +2,9 @@ from djangorestframework.compat import View # Use Django 1.3's django.views.gen
from djangorestframework.mixins import ResponseMixin
from djangorestframework.renderers import DEFAULT_RENDERERS
from djangorestframework.response import Response
+from djangorestframework.utils import reverse
from django.conf.urls.defaults import patterns, url
-from django.core.urlresolvers import reverse
class ExampleView(ResponseMixin, View):
@@ -14,7 +14,7 @@ class ExampleView(ResponseMixin, View):
def get(self, request):
response = Response({'description': 'Some example content',
- 'url': reverse('mixin-view')}, status=200)
+ 'url': reverse('mixin-view', request)}, status=200)
self.response = self.prepare_response(response)
return self.response
diff --git a/examples/objectstore/views.py b/examples/objectstore/views.py
index ae545394..a8bc249a 100644
--- a/examples/objectstore/views.py
+++ b/examples/objectstore/views.py
@@ -1,6 +1,6 @@
from django.conf import settings
-from django.core.urlresolvers import reverse
+from djangorestframework.utils import reverse
from djangorestframework.views import View
from djangorestframework.response import Response
from djangorestframework import status
@@ -41,7 +41,7 @@ class ObjectStoreRoot(View):
filepaths = [os.path.join(OBJECT_STORE_DIR, file) for file in os.listdir(OBJECT_STORE_DIR) if not file.startswith('.')]
ctime_sorted_basenames = [item[0] for item in sorted([(os.path.basename(path), os.path.getctime(path)) for path in filepaths],
key=operator.itemgetter(1), reverse=True)]
- return Response([reverse('stored-object', kwargs={'key':key}) for key in ctime_sorted_basenames])
+ return Response([reverse('stored-object', request, kwargs={'key':key}) for key in ctime_sorted_basenames])
def post(self, request):
"""
@@ -51,8 +51,8 @@ class ObjectStoreRoot(View):
pathname = os.path.join(OBJECT_STORE_DIR, key)
pickle.dump(self.CONTENT, open(pathname, 'wb'))
remove_oldest_files(OBJECT_STORE_DIR, MAX_FILES)
- self.headers['Location'] = reverse('stored-object', kwargs={'key':key})
- return Response(self.CONTENT, status=status.HTTP_201_CREATED)
+ url = reverse('stored-object', request, kwargs={'key':key})
+ return Response(self.CONTENT, status.HTTP_201_CREATED, {'Location': url})
class StoredObject(View):
diff --git a/examples/permissionsexample/views.py b/examples/permissionsexample/views.py
index bcf6619c..0bc31b27 100644
--- a/examples/permissionsexample/views.py
+++ b/examples/permissionsexample/views.py
@@ -1,7 +1,7 @@
from djangorestframework.views import View
from djangorestframework.response import Response
from djangorestframework.permissions import PerUserThrottling, IsAuthenticated
-from django.core.urlresolvers import reverse
+from djangorestframework.utils import reverse
class PermissionsExampleView(View):
@@ -13,11 +13,11 @@ class PermissionsExampleView(View):
return Response([
{
'name': 'Throttling Example',
- 'url': reverse('throttled-resource')
+ 'url': reverse('throttled-resource', request)
},
{
'name': 'Logged in example',
- 'url': reverse('loggedin-resource')
+ 'url': reverse('loggedin-resource', request)
},
])
diff --git a/examples/pygments_api/views.py b/examples/pygments_api/views.py
index 852b6730..bca3dac6 100644
--- a/examples/pygments_api/views.py
+++ b/examples/pygments_api/views.py
@@ -1,10 +1,10 @@
from __future__ import with_statement # for python 2.5
from django.conf import settings
-from django.core.urlresolvers import reverse
from djangorestframework.resources import FormResource
from djangorestframework.response import Response
from djangorestframework.renderers import BaseRenderer
+from djangorestframework.utils import reverse
from djangorestframework.views import View
from djangorestframework import status
@@ -61,7 +61,7 @@ class PygmentsRoot(View):
Return a list of all currently existing snippets.
"""
unique_ids = [os.path.split(f)[1] for f in list_dir_sorted_by_ctime(HIGHLIGHTED_CODE_DIR)]
- return Response([reverse('pygments-instance', args=[unique_id]) for unique_id in unique_ids])
+ return Response([reverse('pygments-instance', request, args=[unique_id]) for unique_id in unique_ids])
def post(self, request):
"""
@@ -81,8 +81,8 @@ class PygmentsRoot(View):
remove_oldest_files(HIGHLIGHTED_CODE_DIR, MAX_FILES)
- self.headers['Location'] = reverse('pygments-instance', args=[unique_id])
- return Response(status=status.HTTP_201_CREATED)
+ location = reverse('pygments-instance', request, args=[unique_id])
+ return Response(status=status.HTTP_201_CREATED, headers={'Location': location})
class PygmentsInstance(View):
@@ -98,7 +98,7 @@ class PygmentsInstance(View):
"""
pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id)
if not os.path.exists(pathname):
- return Response(status.HTTP_404_NOT_FOUND)
+ return Response(status=status.HTTP_404_NOT_FOUND)
return Response(open(pathname, 'r').read())
def delete(self, request, unique_id):
@@ -107,6 +107,7 @@ class PygmentsInstance(View):
"""
pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id)
if not os.path.exists(pathname):
- return Response(status.HTTP_404_NOT_FOUND)
- return Response(os.remove(pathname))
+ return Response(status=status.HTTP_404_NOT_FOUND)
+ os.remove(pathname)
+ return Response()
diff --git a/examples/resourceexample/views.py b/examples/resourceexample/views.py
index 44c4176a..f2a7a08a 100644
--- a/examples/resourceexample/views.py
+++ b/examples/resourceexample/views.py
@@ -1,5 +1,4 @@
-from django.core.urlresolvers import reverse
-
+from djangorestframework.utils import reverse
from djangorestframework.views import View
from djangorestframework.response import Response
from djangorestframework import status
@@ -14,9 +13,12 @@ class ExampleView(View):
def get(self, request):
"""
- Handle GET requests, returning a list of URLs pointing to 3 other views.
+ Handle GET requests, returning a list of URLs pointing to
+ three other views.
"""
- return Response({"Some other resources": [reverse('another-example', kwargs={'num':num}) for num in range(3)]})
+ urls = [reverse('another-example', request, kwargs={'num': num})
+ for num in range(3)]
+ return Response({"Some other resources": urls})
class AnotherExampleView(View):
@@ -32,7 +34,7 @@ class AnotherExampleView(View):
Returns a simple string indicating which view the GET request was for.
"""
if int(num) > 2:
- return Response(status.HTTP_404_NOT_FOUND)
+ return Response(status=status.HTTP_404_NOT_FOUND)
return Response("GET request to AnotherExampleResource %s" % num)
def post(self, request, num):
@@ -41,5 +43,5 @@ class AnotherExampleView(View):
Returns a simple string indicating what content was supplied.
"""
if int(num) > 2:
- return Response(status.HTTP_404_NOT_FOUND)
+ return Response(status=status.HTTP_404_NOT_FOUND)
return Response("POST request to AnotherExampleResource %s, with content: %s" % (num, repr(self.CONTENT)))
diff --git a/examples/sandbox/views.py b/examples/sandbox/views.py
index 49b59b40..34216ad2 100644
--- a/examples/sandbox/views.py
+++ b/examples/sandbox/views.py
@@ -1,40 +1,67 @@
"""The root view for the examples provided with Django REST framework"""
-from django.core.urlresolvers import reverse
+from djangorestframework.utils import reverse
from djangorestframework.views import View
from djangorestframework.response import Response
class Sandbox(View):
- """This is the sandbox for the examples provided with [Django REST framework](http://django-rest-framework.org).
+ """
+ This is the sandbox for the examples provided with
+ [Django REST framework][1].
- These examples are provided to help you get a better idea of some of the features of RESTful APIs created using the framework.
+ These examples are provided to help you get a better idea of some of the
+ features of RESTful APIs created using the framework.
- All the example APIs allow anonymous access, and can be navigated either through the browser or from the command line...
+ All the example APIs allow anonymous access, and can be navigated either
+ through the browser or from the command line.
- bash: curl -X GET http://api.django-rest-framework.org/ # (Use default renderer)
- bash: curl -X GET http://api.django-rest-framework.org/ -H 'Accept: text/plain' # (Use plaintext documentation renderer)
+ For example, to get the default representation using curl:
+
+ bash: curl -X GET http://rest.ep.io/
+
+ Or, to get the plaintext documentation represention:
+
+ bash: curl -X GET http://rest.ep.io/ -H 'Accept: text/plain'
The examples provided:
- 1. A basic example using the [Resource](http://django-rest-framework.org/library/resource.html) class.
- 2. A basic example using the [ModelResource](http://django-rest-framework.org/library/modelresource.html) class.
- 3. An basic example using Django 1.3's [class based views](http://docs.djangoproject.com/en/dev/topics/class-based-views/) and djangorestframework's [RendererMixin](http://django-rest-framework.org/library/renderers.html).
+ 1. A basic example using the [Resource][2] class.
+ 2. A basic example using the [ModelResource][3] class.
+ 3. An basic example using Django 1.3's [class based views][4] and
+ djangorestframework's [RendererMixin][5].
4. A generic object store API.
5. A code highlighting API.
6. A blog posts and comments API.
7. A basic example using permissions.
8. A basic example using enhanced request.
- Please feel free to browse, create, edit and delete the resources in these examples."""
+ Please feel free to browse, create, edit and delete the resources in
+ these examples.
+
+ [1]: http://django-rest-framework.org
+ [2]: http://django-rest-framework.org/library/resource.html
+ [3]: http://django-rest-framework.org/library/modelresource.html
+ [4]: http://docs.djangoproject.com/en/dev/topics/class-based-views/
+ [5]: http://django-rest-framework.org/library/renderers.html
+ """
def get(self, request):
- return Response([{'name': 'Simple Resource example', 'url': reverse('example-resource')},
- {'name': 'Simple ModelResource example', 'url': reverse('model-resource-root')},
- {'name': 'Simple Mixin-only example', 'url': reverse('mixin-view')},
- {'name': 'Object store API', 'url': reverse('object-store-root')},
- {'name': 'Code highlighting API', 'url': reverse('pygments-root')},
- {'name': 'Blog posts API', 'url': reverse('blog-posts-root')},
- {'name': 'Permissions example', 'url': reverse('permissions-example')},
- {'name': 'Simple request mixin example', 'url': reverse('request-example')}
- ])
+ return Response([
+ {'name': 'Simple Resource example',
+ 'url': reverse('example-resource', request)},
+ {'name': 'Simple ModelResource example',
+ 'url': reverse('model-resource-root', request)},
+ {'name': 'Simple Mixin-only example',
+ 'url': reverse('mixin-view', request)},
+ {'name': 'Object store API'
+ 'url': reverse('object-store-root', request)},
+ {'name': 'Code highlighting API',
+ 'url': reverse('pygments-root', request)},
+ {'name': 'Blog posts API',
+ 'url': reverse('blog-posts-root', request)},
+ {'name': 'Permissions example',
+ 'url': reverse('permissions-example', request)},
+ {'name': 'Simple request mixin example',
+ 'url': reverse('request-example', request)}
+ ])