aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/blogpost/models.py31
-rw-r--r--examples/blogpost/urls.py31
-rw-r--r--examples/modelresourceexample/models.py14
-rw-r--r--examples/modelresourceexample/urls.py13
-rw-r--r--examples/modelresourceexample/views.py16
-rw-r--r--examples/objectstore/views.py64
-rw-r--r--examples/pygments_api/views.py66
-rw-r--r--examples/resourceexample/views.py25
-rw-r--r--examples/sandbox/views.py4
9 files changed, 148 insertions, 116 deletions
diff --git a/examples/blogpost/models.py b/examples/blogpost/models.py
index 3489c596..c4925a15 100644
--- a/examples/blogpost/models.py
+++ b/examples/blogpost/models.py
@@ -21,26 +21,10 @@ class BlogPost(models.Model):
created = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(editable=False, default='')
- class Meta:
- ordering = ('created',)
-
- @models.permalink
- def get_absolute_url(self):
- return ('blog-post', (), {'key': self.key})
-
- @property
- @models.permalink
- def comments_url(self):
- """Link to a resource which lists all comments for this blog post."""
- return ('comments', (), {'blogpost': self.key})
-
- def __unicode__(self):
- return self.title
-
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(self.__class__, self).save(*args, **kwargs)
- for obj in self.__class__.objects.order_by('-pk')[MAX_POSTS:]:
+ for obj in self.__class__.objects.order_by('-created')[MAX_POSTS:]:
obj.delete()
@@ -51,16 +35,3 @@ class Comment(models.Model):
rating = models.IntegerField(blank=True, null=True, choices=RATING_CHOICES, help_text='How did you rate this post?')
created = models.DateTimeField(auto_now_add=True)
- class Meta:
- ordering = ('created',)
-
- @models.permalink
- def get_absolute_url(self):
- return ('comment', (), {'blogpost': self.blogpost.key, 'id': self.id})
-
- @property
- @models.permalink
- def blogpost_url(self):
- """Link to the blog post resource which this comment corresponds to."""
- return ('blog-post', (), {'key': self.blogpost.key})
-
diff --git a/examples/blogpost/urls.py b/examples/blogpost/urls.py
index 1306b0d7..130363b1 100644
--- a/examples/blogpost/urls.py
+++ b/examples/blogpost/urls.py
@@ -1,9 +1,28 @@
from django.conf.urls.defaults import patterns, url
-from blogpost.views import BlogPosts, BlogPostInstance, Comments, CommentInstance
+from django.core.urlresolvers import reverse
+
+from djangorestframework.views import ListOrCreateModelView, InstanceModelView
+from djangorestframework.resources import ModelResource
+
+from blogpost.models import BlogPost, Comment
+
+class BlogPostResource(ModelResource):
+ model = BlogPost
+ fields = ('created', 'title', 'slug', 'content', 'url', 'comments')
+ ordering = ('-created',)
+
+ def comments(self, instance):
+ return reverse('comments', kwargs={'blogpost': instance.key})
+
+class CommentResource(ModelResource):
+ model = Comment
+ fields = ('username', 'comment', 'created', 'rating', 'url', 'blogpost')
+ ordering = ('-created',)
+
urlpatterns = patterns('',
- url(r'^$', BlogPosts.as_view(), name='blog-posts'),
- url(r'^(?P<key>[^/]+)/$', BlogPostInstance.as_view(), name='blog-post'),
- url(r'^(?P<blogpost>[^/]+)/comments/$', Comments.as_view(), name='comments'),
- url(r'^(?P<blogpost>[^/]+)/comments/(?P<id>[^/]+)/$', CommentInstance.as_view(), name='comment'),
-)
+ url(r'^$', ListOrCreateModelView.as_view(resource=BlogPostResource), name='blog-posts-root'),
+ url(r'^(?P<key>[^/]+)/$', InstanceModelView.as_view(resource=BlogPostResource)),
+ url(r'^(?P<blogpost>[^/]+)/comments/$', ListOrCreateModelView.as_view(resource=CommentResource), name='comments'),
+ url(r'^(?P<blogpost>[^/]+)/comments/(?P<id>[^/]+)/$', InstanceModelView.as_view(resource=CommentResource)),
+) \ No newline at end of file
diff --git a/examples/modelresourceexample/models.py b/examples/modelresourceexample/models.py
index 16047524..ff0179c8 100644
--- a/examples/modelresourceexample/models.py
+++ b/examples/modelresourceexample/models.py
@@ -7,17 +7,13 @@ class MyModel(models.Model):
bar = models.IntegerField(help_text='Must be an integer.')
baz = models.CharField(max_length=32, help_text='Free text. Max length 32 chars.')
created = models.DateTimeField(auto_now_add=True)
-
- class Meta:
- ordering = ('created',)
def save(self, *args, **kwargs):
- """For the purposes of the sandbox, limit the maximum number of stored models."""
+ """
+ For the purposes of the sandbox limit the maximum number of stored models.
+ """
super(MyModel, self).save(*args, **kwargs)
while MyModel.objects.all().count() > MAX_INSTANCES:
- MyModel.objects.all()[0].delete()
-
- @models.permalink
- def get_absolute_url(self):
- return ('my-model-resource', (self.pk,))
+ MyModel.objects.all().order_by('-created')[0].delete()
+
diff --git a/examples/modelresourceexample/urls.py b/examples/modelresourceexample/urls.py
index 53d950cd..5860c807 100644
--- a/examples/modelresourceexample/urls.py
+++ b/examples/modelresourceexample/urls.py
@@ -1,7 +1,14 @@
from django.conf.urls.defaults import patterns, url
-from modelresourceexample.views import MyModelRootResource, MyModelResource
+from djangorestframework.views import ListOrCreateModelView, InstanceModelView
+from djangorestframework.resources import ModelResource
+from modelresourceexample.models import MyModel
+
+class MyModelResource(ModelResource):
+ model = MyModel
+ fields = ('foo', 'bar', 'baz', 'url')
+ ordering = ('created',)
urlpatterns = patterns('modelresourceexample.views',
- url(r'^$', MyModelRootResource.as_view(), name='my-model-root-resource'),
- url(r'^([0-9]+)/$', MyModelResource.as_view(), name='my-model-resource'),
+ url(r'^$', ListOrCreateModelView.as_view(resource=MyModelResource), name='model-resource-root'),
+ url(r'^([0-9]+)/$', InstanceModelView.as_view(resource=MyModelResource)),
)
diff --git a/examples/modelresourceexample/views.py b/examples/modelresourceexample/views.py
index 5495a293..e69de29b 100644
--- a/examples/modelresourceexample/views.py
+++ b/examples/modelresourceexample/views.py
@@ -1,16 +0,0 @@
-from djangorestframework.modelresource import InstanceModelResource, ListOrCreateModelResource
-from modelresourceexample.models import MyModel
-
-FIELDS = ('foo', 'bar', 'baz', 'absolute_url')
-
-class MyModelRootResource(ListOrCreateModelResource):
- """A create/list resource for MyModel.
- Available for both authenticated and anonymous access for the purposes of the sandbox."""
- model = MyModel
- fields = FIELDS
-
-class MyModelResource(InstanceModelResource):
- """A read/update/delete resource for MyModel.
- Available for both authenticated and anonymous access for the purposes of the sandbox."""
- model = MyModel
- fields = FIELDS
diff --git a/examples/objectstore/views.py b/examples/objectstore/views.py
index 2e353e08..076e5941 100644
--- a/examples/objectstore/views.py
+++ b/examples/objectstore/views.py
@@ -1,7 +1,7 @@
from django.conf import settings
from django.core.urlresolvers import reverse
-from djangorestframework.resource import Resource
+from djangorestframework.views import BaseView
from djangorestframework.response import Response
from djangorestframework import status
@@ -15,55 +15,69 @@ MAX_FILES = 10
def remove_oldest_files(dir, max_files):
- """Remove the oldest files in a directory 'dir', leaving at most 'max_files' remaining.
- We use this to limit the number of resources in the sandbox."""
+ """
+ Remove the oldest files in a directory 'dir', leaving at most 'max_files' remaining.
+ We use this to limit the number of resources in the sandbox.
+ """
filepaths = [os.path.join(dir, file) for file in os.listdir(dir) if not file.startswith('.')]
ctime_sorted_paths = [item[0] for item in sorted([(path, os.path.getctime(path)) for path in filepaths],
key=operator.itemgetter(1), reverse=True)]
[os.remove(path) for path in ctime_sorted_paths[max_files:]]
-class ObjectStoreRoot(Resource):
- """Root of the Object Store API.
- Allows the client to get a complete list of all the stored objects, or to create a new stored object."""
- allowed_methods = anon_allowed_methods = ('GET', 'POST')
+class ObjectStoreRoot(BaseView):
+ """
+ Root of the Object Store API.
+ Allows the client to get a complete list of all the stored objects, or to create a new stored object.
+ """
- def get(self, request, auth):
- """Return a list of all the stored object URLs. (Ordered by creation time, newest first)"""
+ def get(self, request):
+ """
+ Return a list of all the stored object URLs. (Ordered by creation time, newest first)
+ """
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 [reverse('stored-object', kwargs={'key':key}) for key in ctime_sorted_basenames]
- def post(self, request, auth, content):
- """Create a new stored object, with a unique key."""
+ def post(self, request):
+ """
+ Create a new stored object, with a unique key.
+ """
key = str(uuid.uuid1())
pathname = os.path.join(OBJECT_STORE_DIR, key)
- pickle.dump(content, open(pathname, 'wb'))
+ pickle.dump(self.CONTENT, open(pathname, 'wb'))
remove_oldest_files(OBJECT_STORE_DIR, MAX_FILES)
- return Response(status.HTTP_201_CREATED, content, {'Location': reverse('stored-object', kwargs={'key':key})})
+ return Response(status.HTTP_201_CREATED, self.CONTENT, {'Location': reverse('stored-object', kwargs={'key':key})})
-class StoredObject(Resource):
- """Represents a stored object.
- The object may be any picklable content."""
- allowed_methods = anon_allowed_methods = ('GET', 'PUT', 'DELETE')
+class StoredObject(BaseView):
+ """
+ Represents a stored object.
+ The object may be any picklable content.
+ """
- def get(self, request, auth, key):
- """Return a stored object, by unpickling the contents of a locally stored file."""
+ def get(self, request, key):
+ """
+ Return a stored object, by unpickling the contents of a locally stored file.
+ """
pathname = os.path.join(OBJECT_STORE_DIR, key)
if not os.path.exists(pathname):
return Response(status.HTTP_404_NOT_FOUND)
return pickle.load(open(pathname, 'rb'))
- def put(self, request, auth, content, key):
- """Update/create a stored object, by pickling the request content to a locally stored file."""
+ def put(self, request, key):
+ """
+ Update/create a stored object, by pickling the request content to a locally stored file.
+ """
pathname = os.path.join(OBJECT_STORE_DIR, key)
- pickle.dump(content, open(pathname, 'wb'))
- return content
+ pickle.dump(self.CONTENT, open(pathname, 'wb'))
+ return self.CONTENT
- def delete(self, request, auth, key):
- """Delete a stored object, by removing it's pickled file."""
+ def delete(self, request):
+ """
+ Delete a stored object, by removing it's pickled file.
+ """
pathname = os.path.join(OBJECT_STORE_DIR, key)
if not os.path.exists(pathname):
return Response(status.HTTP_404_NOT_FOUND)
diff --git a/examples/pygments_api/views.py b/examples/pygments_api/views.py
index 253b0907..e6bfae48 100644
--- a/examples/pygments_api/views.py
+++ b/examples/pygments_api/views.py
@@ -2,9 +2,10 @@ from __future__ import with_statement # for python 2.5
from django.conf import settings
from django.core.urlresolvers import reverse
-from djangorestframework.resource import Resource
+from djangorestframework.resources import FormResource
from djangorestframework.response import Response
from djangorestframework.renderers import BaseRenderer
+from djangorestframework.views import BaseView
from djangorestframework import status
from pygments.formatters import HtmlFormatter
@@ -17,39 +18,60 @@ import os
import uuid
import operator
-# We need somewhere to store the code that we highlight
+# We need somewhere to store the code snippets that we highlight
HIGHLIGHTED_CODE_DIR = os.path.join(settings.MEDIA_ROOT, 'pygments')
MAX_FILES = 10
+
def list_dir_sorted_by_ctime(dir):
- """Return a list of files sorted by creation time"""
+ """
+ Return a list of files sorted by creation time
+ """
filepaths = [os.path.join(dir, file) for file in os.listdir(dir) if not file.startswith('.')]
- return [item[0] for item in sorted([(path, os.path.getctime(path)) for path in filepaths],
- key=operator.itemgetter(1), reverse=False)]
+ return [item[0] for item in sorted( [(path, os.path.getctime(path)) for path in filepaths],
+ key=operator.itemgetter(1), reverse=False) ]
+
def remove_oldest_files(dir, max_files):
- """Remove the oldest files in a directory 'dir', leaving at most 'max_files' remaining.
- We use this to limit the number of resources in the sandbox."""
+ """
+ Remove the oldest files in a directory 'dir', leaving at most 'max_files' remaining.
+ We use this to limit the number of resources in the sandbox.
+ """
[os.remove(path) for path in list_dir_sorted_by_ctime(dir)[max_files:]]
class HTMLRenderer(BaseRenderer):
- """Basic renderer which just returns the content without any further serialization."""
+ """
+ Basic renderer which just returns the content without any further serialization.
+ """
media_type = 'text/html'
-class PygmentsRoot(Resource):
- """This example demonstrates a simple RESTful Web API aound the awesome pygments library.
- This top level resource is used to create highlighted code snippets, and to list all the existing code snippets."""
+
+class PygmentsFormResource(FormResource):
+ """
+ """
form = PygmentsForm
+
+class PygmentsRoot(BaseView):
+ """
+ This example demonstrates a simple RESTful Web API aound the awesome pygments library.
+ This top level resource is used to create highlighted code snippets, and to list all the existing code snippets.
+ """
+ resource = PygmentsFormResource
+
def get(self, request):
- """Return a list of all currently existing snippets."""
+ """
+ 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 [reverse('pygments-instance', args=[unique_id]) for unique_id in unique_ids]
def post(self, request):
- """Create a new highlighed snippet and return it's location.
- For the purposes of the sandbox example, also ensure we delete the oldest snippets if we have > MAX_FILES."""
+ """
+ Create a new highlighed snippet and return it's location.
+ For the purposes of the sandbox example, also ensure we delete the oldest snippets if we have > MAX_FILES.
+ """
unique_id = str(uuid.uuid1())
pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id)
@@ -66,20 +88,26 @@ class PygmentsRoot(Resource):
return Response(status.HTTP_201_CREATED, headers={'Location': reverse('pygments-instance', args=[unique_id])})
-class PygmentsInstance(Resource):
- """Simply return the stored highlighted HTML file with the correct mime type.
- This Resource only renders HTML and uses a standard HTML renderer rather than the renderers.DocumentingHTMLRenderer class."""
+class PygmentsInstance(BaseView):
+ """
+ Simply return the stored highlighted HTML file with the correct mime type.
+ This Resource only renders HTML and uses a standard HTML renderer rather than the renderers.DocumentingHTMLRenderer class.
+ """
renderers = (HTMLRenderer,)
def get(self, request, unique_id):
- """Return the highlighted snippet."""
+ """
+ Return the highlighted snippet.
+ """
pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id)
if not os.path.exists(pathname):
return Response(status.HTTP_404_NOT_FOUND)
return open(pathname, 'r').read()
def delete(self, request, unique_id):
- """Delete the highlighted snippet."""
+ """
+ Delete the highlighted snippet.
+ """
pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id)
if not os.path.exists(pathname):
return Response(status.HTTP_404_NOT_FOUND)
diff --git a/examples/resourceexample/views.py b/examples/resourceexample/views.py
index 911fd467..70d96891 100644
--- a/examples/resourceexample/views.py
+++ b/examples/resourceexample/views.py
@@ -1,20 +1,33 @@
from django.core.urlresolvers import reverse
-from djangorestframework.resource import Resource
+from djangorestframework.views import BaseView
+from djangorestframework.resources import FormResource
from djangorestframework.response import Response
from djangorestframework import status
from resourceexample.forms import MyForm
-class ExampleResource(Resource):
- """A basic read-only resource that points to 3 other resources."""
+class MyFormValidation(FormResource):
+ """
+ A resource which applies form validation on the input.
+ """
+ form = MyForm
+
+
+class ExampleResource(BaseView):
+ """
+ A basic read-only resource that points to 3 other resources.
+ """
def get(self, request):
return {"Some other resources": [reverse('another-example-resource', kwargs={'num':num}) for num in range(3)]}
-class AnotherExampleResource(Resource):
- """A basic GET-able/POST-able resource."""
- form = MyForm # Optional form validation on input (Applies in this case the POST method, but can also apply to PUT)
+
+class AnotherExampleResource(BaseView):
+ """
+ A basic GET-able/POST-able resource.
+ """
+ resource = MyFormValidation
def get(self, request, num):
"""Handle GET requests"""
diff --git a/examples/sandbox/views.py b/examples/sandbox/views.py
index 78b722ca..d5b59284 100644
--- a/examples/sandbox/views.py
+++ b/examples/sandbox/views.py
@@ -27,8 +27,8 @@ class Sandbox(BaseView):
def get(self, request):
return [{'name': 'Simple Resource example', 'url': reverse('example-resource')},
- {'name': 'Simple ModelResource example', 'url': reverse('my-model-root-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')}]
+ {'name': 'Blog posts API', 'url': reverse('blog-posts-root')}]