diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/blogpost/models.py | 34 | ||||
| -rw-r--r-- | examples/blogpost/tests.py | 1 | ||||
| -rw-r--r-- | examples/blogpost/urls.py | 37 | ||||
| -rw-r--r-- | examples/blogpost/views.py | 32 | ||||
| -rw-r--r-- | examples/mixin/urls.py | 11 | ||||
| -rw-r--r-- | examples/modelresourceexample/models.py | 14 | ||||
| -rw-r--r-- | examples/modelresourceexample/urls.py | 15 | ||||
| -rw-r--r-- | examples/modelresourceexample/views.py | 18 | ||||
| -rw-r--r-- | examples/objectstore/views.py | 64 | ||||
| -rw-r--r-- | examples/pygments_api/tests.py | 3 | ||||
| -rw-r--r-- | examples/pygments_api/views.py | 96 | ||||
| -rw-r--r-- | examples/resourceexample/views.py | 35 | ||||
| -rw-r--r-- | examples/sandbox/views.py | 17 | ||||
| -rw-r--r-- | examples/urls.py | 15 |
14 files changed, 195 insertions, 197 deletions
diff --git a/examples/blogpost/models.py b/examples/blogpost/models.py index 01a91e15..c4925a15 100644 --- a/examples/blogpost/models.py +++ b/examples/blogpost/models.py @@ -12,6 +12,8 @@ RATING_CHOICES = ((0, 'Awful'), (3, 'Good'), (4, 'Excellent')) +MAX_POSTS = 10 + class BlogPost(models.Model): key = models.CharField(primary_key=True, max_length=64, default=uuid_str, editable=False) title = models.CharField(max_length=128) @@ -19,28 +21,13 @@ 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')[10:]: + for obj in self.__class__.objects.order_by('-created')[MAX_POSTS:]: obj.delete() + class Comment(models.Model): blogpost = models.ForeignKey(BlogPost, editable=False, related_name='comments') username = models.CharField(max_length=128) @@ -48,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/tests.py b/examples/blogpost/tests.py index d4084e72..9b9a682f 100644 --- a/examples/blogpost/tests.py +++ b/examples/blogpost/tests.py @@ -1,6 +1,7 @@ """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 diff --git a/examples/blogpost/urls.py b/examples/blogpost/urls.py index 1306b0d7..c677b8fa 100644 --- a/examples/blogpost/urls.py +++ b/examples/blogpost/urls.py @@ -1,9 +1,36 @@ 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): + """ + A Blog Post has a *title* and *content*, and can be associated with zero or more comments. + """ + 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): + """ + A Comment is associated with a given Blog Post and has a *username* and *comment*, and optionally a *rating*. + """ + 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)), ) diff --git a/examples/blogpost/views.py b/examples/blogpost/views.py deleted file mode 100644 index 59a3fb9f..00000000 --- a/examples/blogpost/views.py +++ /dev/null @@ -1,32 +0,0 @@ -from djangorestframework.modelresource import ModelResource, RootModelResource - -from blogpost import models - -BLOG_POST_FIELDS = ('created', 'title', 'slug', 'content', 'absolute_url', 'comment_url', 'comments_url') -COMMENT_FIELDS = ('username', 'comment', 'created', 'rating', 'absolute_url', 'blogpost_url') -MAX_POSTS = 10 - -class BlogPosts(RootModelResource): - """A resource with which lists all existing blog posts and creates new blog posts.""" - anon_allowed_methods = allowed_methods = ('GET', 'POST',) - model = models.BlogPost - fields = BLOG_POST_FIELDS - -class BlogPostInstance(ModelResource): - """A resource which represents a single blog post.""" - anon_allowed_methods = allowed_methods = ('GET', 'PUT', 'DELETE') - model = models.BlogPost - fields = BLOG_POST_FIELDS - -class Comments(RootModelResource): - """A resource which lists all existing comments for a given blog post, and creates new blog comments for a given blog post.""" - anon_allowed_methods = allowed_methods = ('GET', 'POST',) - model = models.Comment - fields = COMMENT_FIELDS - -class CommentInstance(ModelResource): - """A resource which represents a single comment.""" - anon_allowed_methods = allowed_methods = ('GET', 'PUT', 'DELETE') - model = models.Comment - fields = COMMENT_FIELDS - diff --git a/examples/mixin/urls.py b/examples/mixin/urls.py index 05009284..1d25f6c7 100644 --- a/examples/mixin/urls.py +++ b/examples/mixin/urls.py @@ -1,20 +1,21 @@ from djangorestframework.compat import View # Use Django 1.3's django.views.generic.View, or fall back to a clone of that if Django < 1.3 -from djangorestframework.emitters import EmitterMixin, DEFAULT_EMITTERS +from djangorestframework.mixins import ResponseMixin +from djangorestframework.renderers import DEFAULT_RENDERERS from djangorestframework.response import Response from django.conf.urls.defaults import patterns, url from django.core.urlresolvers import reverse -class ExampleView(EmitterMixin, View): +class ExampleView(ResponseMixin, View): """An example view using Django 1.3's class based views. - Uses djangorestframework's EmitterMixin to provide support for multiple output formats.""" - emitters = DEFAULT_EMITTERS + Uses djangorestframework's RendererMixin to provide support for multiple output formats.""" + renderers = DEFAULT_RENDERERS def get(self, request): response = Response(200, {'description': 'Some example content', 'url': reverse('mixin-view')}) - return self.emit(response) + return self.render(response) urlpatterns = patterns('', 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..bb71ddd3 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 -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'), +class MyModelResource(ModelResource): + model = MyModel + fields = ('foo', 'bar', 'baz', 'url') + ordering = ('created',) + +urlpatterns = patterns('', + 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 e912c019..e69de29b 100644 --- a/examples/modelresourceexample/views.py +++ b/examples/modelresourceexample/views.py @@ -1,18 +0,0 @@ -from djangorestframework.modelresource import ModelResource, RootModelResource -from modelresourceexample.models import MyModel - -FIELDS = ('foo', 'bar', 'baz', 'absolute_url') - -class MyModelRootResource(RootModelResource): - """A create/list resource for MyModel. - Available for both authenticated and anonymous access for the purposes of the sandbox.""" - model = MyModel - allowed_methods = anon_allowed_methods = ('GET', 'POST') - fields = FIELDS - -class MyModelResource(ModelResource): - """A read/update/delete resource for MyModel. - Available for both authenticated and anonymous access for the purposes of the sandbox.""" - model = MyModel - allowed_methods = anon_allowed_methods = ('GET', 'PUT', 'DELETE') - fields = FIELDS diff --git a/examples/objectstore/views.py b/examples/objectstore/views.py index 2e353e08..19999aa9 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 View 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(View): + """ + 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(View): + """ + 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/tests.py b/examples/pygments_api/tests.py index 3bdc2ec5..6eb69da5 100644 --- a/examples/pygments_api/tests.py +++ b/examples/pygments_api/tests.py @@ -1,10 +1,13 @@ from django.test import TestCase
from django.utils import simplejson as json
+
from djangorestframework.compat import RequestFactory
+
from pygments_api import views
import tempfile, shutil
+
class TestPygmentsExample(TestCase):
def setUp(self):
diff --git a/examples/pygments_api/views.py b/examples/pygments_api/views.py index 377761b1..76647107 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.emitters import BaseEmitter +from djangorestframework.renderers import BaseRenderer +from djangorestframework.views import View from djangorestframework import status from pygments.formatters import HtmlFormatter @@ -17,73 +18,98 @@ 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 HTMLEmitter(BaseEmitter): - """Basic emitter which just returns the content without any further serialization.""" +class HTMLRenderer(BaseRenderer): + """ + 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 - allowed_methods = anon_allowed_methods = ('GET', 'POST',) - def get(self, request, auth): - """Return a list of all currently existing snippets.""" + +class PygmentsRoot(View): + """ + 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. + """ 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, auth, content): - """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.""" + 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. + """ unique_id = str(uuid.uuid1()) pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id) - lexer = get_lexer_by_name(content['lexer']) - linenos = 'table' if content['linenos'] else False - options = {'title': content['title']} if content['title'] else {} - formatter = HtmlFormatter(style=content['style'], linenos=linenos, full=True, **options) + lexer = get_lexer_by_name(self.CONTENT['lexer']) + linenos = 'table' if self.CONTENT['linenos'] else False + options = {'title': self.CONTENT['title']} if self.CONTENT['title'] else {} + formatter = HtmlFormatter(style=self.CONTENT['style'], linenos=linenos, full=True, **options) with open(pathname, 'w') as outfile: - highlight(content['code'], lexer, formatter, outfile) + highlight(self.CONTENT['code'], lexer, formatter, outfile) remove_oldest_files(HIGHLIGHTED_CODE_DIR, MAX_FILES) 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 emits HTML and uses a standard HTML emitter rather than the emitters.DocumentingHTMLEmitter class.""" - allowed_methods = anon_allowed_methods = ('GET',) - emitters = (HTMLEmitter,) +class PygmentsInstance(View): + """ + 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, auth, unique_id): - """Return the highlighted snippet.""" + def get(self, request, unique_id): + """ + Return the highlighted snippet. + """ pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id) if not os.path.exists(pathname): - return Resource(status.HTTP_404_NOT_FOUND) + return Response(status.HTTP_404_NOT_FOUND) return open(pathname, 'r').read() - def delete(self, request, auth, unique_id): - """Delete the highlighted snippet.""" + def delete(self, request, unique_id): + """ + Delete the highlighted snippet. + """ pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id) if not os.path.exists(pathname): - return Resource(status.HTTP_404_NOT_FOUND) + return Response(status.HTTP_404_NOT_FOUND) return os.remove(pathname) diff --git a/examples/resourceexample/views.py b/examples/resourceexample/views.py index 41d2e5c5..29651fbf 100644 --- a/examples/resourceexample/views.py +++ b/examples/resourceexample/views.py @@ -1,31 +1,42 @@ from django.core.urlresolvers import reverse -from djangorestframework.resource import Resource +from djangorestframework.views import View +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.""" - allowed_methods = anon_allowed_methods = ('GET',) +class MyFormValidation(FormResource): + """ + A resource which applies form validation on the input. + """ + form = MyForm - def get(self, request, auth): + +class ExampleResource(View): + """ + 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.""" - allowed_methods = anon_allowed_methods = ('GET', 'POST') - form = MyForm # Optional form validation on input (Applies in this case the POST method, but can also apply to PUT) - def get(self, request, auth, num): +class AnotherExampleResource(View): + """ + A basic GET-able/POST-able resource. + """ + resource = MyFormValidation + + def get(self, request, num): """Handle GET requests""" if int(num) > 2: return Response(status.HTTP_404_NOT_FOUND) return "GET request to AnotherExampleResource %s" % num - def post(self, request, auth, content, num): + def post(self, request, num): """Handle POST requests""" if int(num) > 2: return Response(status.HTTP_404_NOT_FOUND) - return "POST request to AnotherExampleResource %s, with content: %s" % (num, repr(content)) + return "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 561bdb1d..1c55c28f 100644 --- a/examples/sandbox/views.py +++ b/examples/sandbox/views.py @@ -1,35 +1,34 @@ """The root view for the examples provided with Django REST framework""" from django.core.urlresolvers import reverse -from djangorestframework.resource import Resource +from djangorestframework.views import View -class Sandbox(Resource): +class Sandbox(View): """This is the sandbox for the examples provided with [Django REST framework](http://django-rest-framework.org). These examples are provided to help you get a better idea of the 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... - bash: curl -X GET http://api.django-rest-framework.org/ # (Use default emitter) - bash: curl -X GET http://api.django-rest-framework.org/ -H 'Accept: text/plain' # (Use plaintext documentation emitter) + 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) 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 [EmitterMixin](http://django-rest-framework.org/library/emitters.html). + 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). 4. A generic object store API. 5. A code highlighting API. 6. A blog posts and comments API. Please feel free to browse, create, edit and delete the resources in these examples.""" - allowed_methods = anon_allowed_methods = ('GET',) - def get(self, request, auth): + 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')}] diff --git a/examples/urls.py b/examples/urls.py index 7cb5e7ce..cf4d4042 100644 --- a/examples/urls.py +++ b/examples/urls.py @@ -2,11 +2,8 @@ from django.conf.urls.defaults import patterns, include, url from django.conf import settings from sandbox.views import Sandbox -urlpatterns = patterns('djangorestframework.views', - (r'robots.txt', 'deny_robots'), - +urlpatterns = patterns('', (r'^$', Sandbox.as_view()), - (r'^resource-example/', include('resourceexample.urls')), (r'^model-resource-example/', include('modelresourceexample.urls')), (r'^mixin/', include('mixin.urls')), @@ -14,14 +11,6 @@ urlpatterns = patterns('djangorestframework.views', (r'^pygments/', include('pygments_api.urls')), (r'^blog-post/', include('blogpost.urls')), - (r'^accounts/login/$', 'api_login'), - (r'^accounts/logout/$', 'api_logout'), + (r'^', include('djangorestframework.urls')), ) -# Only serve favicon in production because otherwise chrome users will pretty much -# permanantly have the django-rest-framework favicon whenever they navigate to -# 127.0.0.1:8000 or whatever, which gets annoying -if not settings.DEBUG: - urlpatterns += patterns('djangorestframework.views', - (r'favicon.ico', 'favicon'), - ) |
