From 762a52edde09297e87c640797219c9bb8255d50a Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 25 Apr 2011 04:50:28 +0100 Subject: Fix some compat issues with json/simplejson --- examples/blogpost/tests.py | 13 +++++-------- examples/pygments_api/tests.py | 8 ++++---- 2 files changed, 9 insertions(+), 12 deletions(-) (limited to 'examples') diff --git a/examples/blogpost/tests.py b/examples/blogpost/tests.py index dfb4d5f5..14b0914d 100644 --- a/examples/blogpost/tests.py +++ b/examples/blogpost/tests.py @@ -1,13 +1,15 @@ """Test a range of REST API usage of the example application. """ -from django.test import TestCase from django.core.urlresolvers import reverse +from django.test import TestCase +from django.utils import simplejson as json + +from djangorestframework.compat import RequestFactory + from blogpost import views, models import blogpost -#import json -#from rest.utils import xml2dict, dict2xml class AcceptHeaderTests(TestCase): """Test correct behaviour of the Accept header as specified by RFC 2616: @@ -164,11 +166,6 @@ class AllowedMethodsTests(TestCase): #above testcases need to probably moved to the core -from djangorestframework.compat import RequestFactory -try: - import json -except ImportError: - import simplejson as json class TestRotation(TestCase): """For the example the maximum amount of Blogposts is capped off at views.MAX_POSTS. diff --git a/examples/pygments_api/tests.py b/examples/pygments_api/tests.py index 017823b9..a8f085cf 100644 --- a/examples/pygments_api/tests.py +++ b/examples/pygments_api/tests.py @@ -1,12 +1,12 @@ 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 -try: - import json -except ImportError: - import simplejson as json + class TestPygmentsExample(TestCase): -- cgit v1.2.3 From 028851bfa1ee44b8e92808b18d32278d4a473cc8 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 27 Apr 2011 18:07:28 +0100 Subject: Fix up tests and examples after refactoring --- examples/blogpost/views.py | 4 ---- examples/mixin/urls.py | 5 +++-- examples/modelresourceexample/views.py | 2 -- examples/pygments_api/views.py | 24 +++++++++++------------- examples/resourceexample/views.py | 10 ++++------ examples/sandbox/views.py | 3 +-- 6 files changed, 19 insertions(+), 29 deletions(-) (limited to 'examples') diff --git a/examples/blogpost/views.py b/examples/blogpost/views.py index 59a3fb9f..9e07aa8a 100644 --- a/examples/blogpost/views.py +++ b/examples/blogpost/views.py @@ -8,25 +8,21 @@ 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..96b630e3 100644 --- a/examples/mixin/urls.py +++ b/examples/mixin/urls.py @@ -1,12 +1,13 @@ 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.emitters import DEFAULT_EMITTERS 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 diff --git a/examples/modelresourceexample/views.py b/examples/modelresourceexample/views.py index e912c019..07f50b65 100644 --- a/examples/modelresourceexample/views.py +++ b/examples/modelresourceexample/views.py @@ -7,12 +7,10 @@ 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/pygments_api/views.py b/examples/pygments_api/views.py index 6fb9217a..f1a89702 100644 --- a/examples/pygments_api/views.py +++ b/examples/pygments_api/views.py @@ -41,26 +41,25 @@ 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.""" form = PygmentsForm - allowed_methods = anon_allowed_methods = ('GET', 'POST',) - def get(self, request, auth): + 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): + 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) @@ -70,20 +69,19 @@ class PygmentsRoot(Resource): 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,) - def get(self, request, auth, unique_id): + 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): + 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..911fd467 100644 --- a/examples/resourceexample/views.py +++ b/examples/resourceexample/views.py @@ -8,24 +8,22 @@ 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',) - def get(self, request, auth): + 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): + 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..5b84e8e4 100644 --- a/examples/sandbox/views.py +++ b/examples/sandbox/views.py @@ -24,9 +24,8 @@ class Sandbox(Resource): 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 Mixin-only example', 'url': reverse('mixin-view')}, -- cgit v1.2.3 From 5a59f339c1757767b136de33faa5b67a972141a1 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 27 Apr 2011 18:44:21 +0100 Subject: Urg. Fixing broken merge --- examples/blogpost/tests.py | 6 ------ examples/pygments_api/tests.py | 6 ------ 2 files changed, 12 deletions(-) (limited to 'examples') diff --git a/examples/blogpost/tests.py b/examples/blogpost/tests.py index 494478d8..9b9a682f 100644 --- a/examples/blogpost/tests.py +++ b/examples/blogpost/tests.py @@ -3,10 +3,7 @@ from django.core.urlresolvers import reverse from django.test import TestCase -<<<<<<< local -======= from django.core.urlresolvers import reverse ->>>>>>> other from django.utils import simplejson as json from djangorestframework.compat import RequestFactory @@ -170,10 +167,7 @@ class AllowedMethodsTests(TestCase): #above testcases need to probably moved to the core -<<<<<<< local -======= ->>>>>>> other class TestRotation(TestCase): """For the example the maximum amount of Blogposts is capped off at views.MAX_POSTS. diff --git a/examples/pygments_api/tests.py b/examples/pygments_api/tests.py index 766defc3..6eb69da5 100644 --- a/examples/pygments_api/tests.py +++ b/examples/pygments_api/tests.py @@ -1,18 +1,12 @@ from django.test import TestCase from django.utils import simplejson as json -<<<<<<< local -======= ->>>>>>> other from djangorestframework.compat import RequestFactory from pygments_api import views import tempfile, shutil -<<<<<<< local -======= ->>>>>>> other class TestPygmentsExample(TestCase): -- cgit v1.2.3 From 5921e5c84e13cafe90061629262f12dfe742c07a Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 27 Apr 2011 18:53:54 +0100 Subject: Fix up ModelResource issues --- examples/blogpost/views.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/blogpost/views.py b/examples/blogpost/views.py index 9e07aa8a..e47f4a5b 100644 --- a/examples/blogpost/views.py +++ b/examples/blogpost/views.py @@ -1,4 +1,4 @@ -from djangorestframework.modelresource import ModelResource, RootModelResource +from djangorestframework.modelresource import InstanceModelResource, RootModelResource from blogpost import models @@ -11,7 +11,7 @@ class BlogPosts(RootModelResource): model = models.BlogPost fields = BLOG_POST_FIELDS -class BlogPostInstance(ModelResource): +class BlogPostInstance(InstanceModelResource): """A resource which represents a single blog post.""" model = models.BlogPost fields = BLOG_POST_FIELDS @@ -21,7 +21,7 @@ class Comments(RootModelResource): model = models.Comment fields = COMMENT_FIELDS -class CommentInstance(ModelResource): +class CommentInstance(InstanceModelResource): """A resource which represents a single comment.""" model = models.Comment fields = COMMENT_FIELDS -- cgit v1.2.3 From 93aa065fa92f64472a3ee80564020a81776be742 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 28 Apr 2011 19:54:30 +0100 Subject: emitters -> renderers --- examples/mixin/urls.py | 6 +++--- examples/pygments_api/views.py | 10 +++++----- examples/sandbox/views.py | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) (limited to 'examples') diff --git a/examples/mixin/urls.py b/examples/mixin/urls.py index 96b630e3..f4300f41 100644 --- a/examples/mixin/urls.py +++ b/examples/mixin/urls.py @@ -1,6 +1,6 @@ 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.mixins import ResponseMixin -from djangorestframework.emitters import DEFAULT_EMITTERS +from djangorestframework.renderers import DEFAULT_RENDERERS from djangorestframework.response import Response from django.conf.urls.defaults import patterns, url @@ -9,8 +9,8 @@ from django.core.urlresolvers import reverse 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', diff --git a/examples/pygments_api/views.py b/examples/pygments_api/views.py index 4e6d1230..278e8250 100644 --- a/examples/pygments_api/views.py +++ b/examples/pygments_api/views.py @@ -4,7 +4,7 @@ from django.core.urlresolvers import reverse from djangorestframework.resource import Resource from djangorestframework.response import Response -from djangorestframework.emitters import BaseEmitter +from djangorestframework.renderers import BaseRenderer from djangorestframework import status from pygments.formatters import HtmlFormatter @@ -32,8 +32,8 @@ def remove_oldest_files(dir, max_files): [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' @@ -68,8 +68,8 @@ class PygmentsRoot(Resource): 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.""" - emitters = (HTMLEmitter,) + This Resource only emits 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.""" diff --git a/examples/sandbox/views.py b/examples/sandbox/views.py index 5b84e8e4..04e4da41 100644 --- a/examples/sandbox/views.py +++ b/examples/sandbox/views.py @@ -11,14 +11,14 @@ class Sandbox(Resource): 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. -- cgit v1.2.3 From 8756664e064a18afc4713d921c318cd968f18433 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 2 May 2011 19:49:12 +0100 Subject: emitters -> renderers --- examples/blogpost/models.py | 5 ++++- examples/blogpost/views.py | 7 +++---- examples/mixin/urls.py | 2 +- examples/pygments_api/views.py | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) (limited to 'examples') diff --git a/examples/blogpost/models.py b/examples/blogpost/models.py index 01a91e15..3489c596 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) @@ -38,9 +40,10 @@ class BlogPost(models.Model): 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('-pk')[MAX_POSTS:]: obj.delete() + class Comment(models.Model): blogpost = models.ForeignKey(BlogPost, editable=False, related_name='comments') username = models.CharField(max_length=128) diff --git a/examples/blogpost/views.py b/examples/blogpost/views.py index e47f4a5b..c4b54f73 100644 --- a/examples/blogpost/views.py +++ b/examples/blogpost/views.py @@ -1,12 +1,11 @@ -from djangorestframework.modelresource import InstanceModelResource, RootModelResource +from djangorestframework.modelresource import InstanceModelResource, ListOrCreateModelResource 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): +class BlogPosts(ListOrCreateModelResource): """A resource with which lists all existing blog posts and creates new blog posts.""" model = models.BlogPost fields = BLOG_POST_FIELDS @@ -16,7 +15,7 @@ class BlogPostInstance(InstanceModelResource): model = models.BlogPost fields = BLOG_POST_FIELDS -class Comments(RootModelResource): +class Comments(ListOrCreateModelResource): """A resource which lists all existing comments for a given blog post, and creates new blog comments for a given blog post.""" model = models.Comment fields = COMMENT_FIELDS diff --git a/examples/mixin/urls.py b/examples/mixin/urls.py index f4300f41..1d25f6c7 100644 --- a/examples/mixin/urls.py +++ b/examples/mixin/urls.py @@ -15,7 +15,7 @@ class ExampleView(ResponseMixin, View): 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/pygments_api/views.py b/examples/pygments_api/views.py index 278e8250..253b0907 100644 --- a/examples/pygments_api/views.py +++ b/examples/pygments_api/views.py @@ -68,7 +68,7 @@ class PygmentsRoot(Resource): 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 renderer rather than the renderers.DocumentingHTMLRenderer class.""" + 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): -- cgit v1.2.3 From d373b3a067796b8e181be9368fa24e89c572c45e Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 4 May 2011 09:21:17 +0100 Subject: Decouple views and resources --- examples/modelresourceexample/views.py | 6 +++--- examples/urls.py | 15 ++------------- 2 files changed, 5 insertions(+), 16 deletions(-) (limited to 'examples') diff --git a/examples/modelresourceexample/views.py b/examples/modelresourceexample/views.py index 07f50b65..5495a293 100644 --- a/examples/modelresourceexample/views.py +++ b/examples/modelresourceexample/views.py @@ -1,15 +1,15 @@ -from djangorestframework.modelresource import ModelResource, RootModelResource +from djangorestframework.modelresource import InstanceModelResource, ListOrCreateModelResource from modelresourceexample.models import MyModel FIELDS = ('foo', 'bar', 'baz', 'absolute_url') -class MyModelRootResource(RootModelResource): +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(ModelResource): +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 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'), - ) -- cgit v1.2.3 From 15f9e7c56699d31043782045a9fe47c354f612cb Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 12 May 2011 12:55:13 +0100 Subject: refactoring resource specfic stuff into ResourceMixin - validators now defunct --- examples/sandbox/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/sandbox/views.py b/examples/sandbox/views.py index 04e4da41..78b722ca 100644 --- a/examples/sandbox/views.py +++ b/examples/sandbox/views.py @@ -1,10 +1,10 @@ """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 BaseView -class Sandbox(Resource): +class Sandbox(BaseView): """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. -- cgit v1.2.3 From 1e04790d505a1174f9e3c4481288982f9e7fd6c0 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 16 May 2011 14:11:36 +0100 Subject: Fixing some of the last blocking issues --- examples/blogpost/models.py | 31 +--------------- examples/blogpost/urls.py | 31 +++++++++++++--- examples/modelresourceexample/models.py | 14 +++---- examples/modelresourceexample/urls.py | 13 +++++-- examples/modelresourceexample/views.py | 16 -------- examples/objectstore/views.py | 64 +++++++++++++++++++------------- examples/pygments_api/views.py | 66 +++++++++++++++++++++++---------- examples/resourceexample/views.py | 25 ++++++++++--- examples/sandbox/views.py | 4 +- 9 files changed, 148 insertions(+), 116 deletions(-) (limited to 'examples') 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[^/]+)/$', BlogPostInstance.as_view(), name='blog-post'), - url(r'^(?P[^/]+)/comments/$', Comments.as_view(), name='comments'), - url(r'^(?P[^/]+)/comments/(?P[^/]+)/$', CommentInstance.as_view(), name='comment'), -) + url(r'^$', ListOrCreateModelView.as_view(resource=BlogPostResource), name='blog-posts-root'), + url(r'^(?P[^/]+)/$', InstanceModelView.as_view(resource=BlogPostResource)), + url(r'^(?P[^/]+)/comments/$', ListOrCreateModelView.as_view(resource=CommentResource), name='comments'), + url(r'^(?P[^/]+)/comments/(?P[^/]+)/$', 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')}] -- cgit v1.2.3 From 75cc1eb2a85c2e8ef872566c0b798c6526a23cb3 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 16 May 2011 15:57:10 +0100 Subject: blogpost/views no longer needed --- examples/blogpost/views.py | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 examples/blogpost/views.py (limited to 'examples') diff --git a/examples/blogpost/views.py b/examples/blogpost/views.py deleted file mode 100644 index c4b54f73..00000000 --- a/examples/blogpost/views.py +++ /dev/null @@ -1,27 +0,0 @@ -from djangorestframework.modelresource import InstanceModelResource, ListOrCreateModelResource - -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') - -class BlogPosts(ListOrCreateModelResource): - """A resource with which lists all existing blog posts and creates new blog posts.""" - model = models.BlogPost - fields = BLOG_POST_FIELDS - -class BlogPostInstance(InstanceModelResource): - """A resource which represents a single blog post.""" - model = models.BlogPost - fields = BLOG_POST_FIELDS - -class Comments(ListOrCreateModelResource): - """A resource which lists all existing comments for a given blog post, and creates new blog comments for a given blog post.""" - model = models.Comment - fields = COMMENT_FIELDS - -class CommentInstance(InstanceModelResource): - """A resource which represents a single comment.""" - model = models.Comment - fields = COMMENT_FIELDS - -- cgit v1.2.3 From 8c3280f9c0d73c4e2536f1d757ad457b4a8f1de7 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 19 May 2011 08:36:55 +0100 Subject: data flattening needs to go into resource --- examples/modelresourceexample/urls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/modelresourceexample/urls.py b/examples/modelresourceexample/urls.py index 5860c807..bb71ddd3 100644 --- a/examples/modelresourceexample/urls.py +++ b/examples/modelresourceexample/urls.py @@ -8,7 +8,7 @@ class MyModelResource(ModelResource): fields = ('foo', 'bar', 'baz', 'url') ordering = ('created',) -urlpatterns = patterns('modelresourceexample.views', +urlpatterns = patterns('', url(r'^$', ListOrCreateModelView.as_view(resource=MyModelResource), name='model-resource-root'), url(r'^([0-9]+)/$', InstanceModelView.as_view(resource=MyModelResource)), ) -- cgit v1.2.3 From c53175914752502629141556f3e001e9d2e9f7fa Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 23 May 2011 17:07:31 +0100 Subject: name and description --- examples/blogpost/urls.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/blogpost/urls.py b/examples/blogpost/urls.py index 130363b1..c677b8fa 100644 --- a/examples/blogpost/urls.py +++ b/examples/blogpost/urls.py @@ -6,7 +6,11 @@ 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',) @@ -14,7 +18,11 @@ class BlogPostResource(ModelResource): 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',) @@ -25,4 +33,4 @@ urlpatterns = patterns('', url(r'^(?P[^/]+)/$', InstanceModelView.as_view(resource=BlogPostResource)), url(r'^(?P[^/]+)/comments/$', ListOrCreateModelView.as_view(resource=CommentResource), name='comments'), url(r'^(?P[^/]+)/comments/(?P[^/]+)/$', InstanceModelView.as_view(resource=CommentResource)), -) \ No newline at end of file +) -- cgit v1.2.3 From 370274f5640d55ef71422f7a2440710a43ff900e Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 24 May 2011 10:27:24 +0100 Subject: Allow views to return HttpResponses. Add initial() hook method --- examples/objectstore/views.py | 6 +++--- examples/pygments_api/views.py | 6 +++--- examples/resourceexample/views.py | 6 +++--- examples/sandbox/views.py | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) (limited to 'examples') diff --git a/examples/objectstore/views.py b/examples/objectstore/views.py index 076e5941..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.views import BaseView +from djangorestframework.views import View from djangorestframework.response import Response from djangorestframework import status @@ -25,7 +25,7 @@ def remove_oldest_files(dir, max_files): [os.remove(path) for path in ctime_sorted_paths[max_files:]] -class ObjectStoreRoot(BaseView): +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. @@ -51,7 +51,7 @@ class ObjectStoreRoot(BaseView): return Response(status.HTTP_201_CREATED, self.CONTENT, {'Location': reverse('stored-object', kwargs={'key':key})}) -class StoredObject(BaseView): +class StoredObject(View): """ Represents a stored object. The object may be any picklable content. diff --git a/examples/pygments_api/views.py b/examples/pygments_api/views.py index e6bfae48..76647107 100644 --- a/examples/pygments_api/views.py +++ b/examples/pygments_api/views.py @@ -5,7 +5,7 @@ from django.core.urlresolvers import reverse from djangorestframework.resources import FormResource from djangorestframework.response import Response from djangorestframework.renderers import BaseRenderer -from djangorestframework.views import BaseView +from djangorestframework.views import View from djangorestframework import status from pygments.formatters import HtmlFormatter @@ -53,7 +53,7 @@ class PygmentsFormResource(FormResource): form = PygmentsForm -class PygmentsRoot(BaseView): +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. @@ -88,7 +88,7 @@ class PygmentsRoot(BaseView): return Response(status.HTTP_201_CREATED, headers={'Location': reverse('pygments-instance', args=[unique_id])}) -class PygmentsInstance(BaseView): +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. diff --git a/examples/resourceexample/views.py b/examples/resourceexample/views.py index 70d96891..29651fbf 100644 --- a/examples/resourceexample/views.py +++ b/examples/resourceexample/views.py @@ -1,6 +1,6 @@ from django.core.urlresolvers import reverse -from djangorestframework.views import BaseView +from djangorestframework.views import View from djangorestframework.resources import FormResource from djangorestframework.response import Response from djangorestframework import status @@ -14,7 +14,7 @@ class MyFormValidation(FormResource): form = MyForm -class ExampleResource(BaseView): +class ExampleResource(View): """ A basic read-only resource that points to 3 other resources. """ @@ -23,7 +23,7 @@ class ExampleResource(BaseView): return {"Some other resources": [reverse('another-example-resource', kwargs={'num':num}) for num in range(3)]} -class AnotherExampleResource(BaseView): +class AnotherExampleResource(View): """ A basic GET-able/POST-able resource. """ diff --git a/examples/sandbox/views.py b/examples/sandbox/views.py index d5b59284..1c55c28f 100644 --- a/examples/sandbox/views.py +++ b/examples/sandbox/views.py @@ -1,10 +1,10 @@ """The root view for the examples provided with Django REST framework""" from django.core.urlresolvers import reverse -from djangorestframework.views import BaseView +from djangorestframework.views import View -class Sandbox(BaseView): +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. -- cgit v1.2.3