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/pygments_api/tests.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'examples/pygments_api') 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/pygments_api/views.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'examples/pygments_api') 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) -- 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/pygments_api/tests.py | 6 ------ 1 file changed, 6 deletions(-) (limited to 'examples/pygments_api') 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 93aa065fa92f64472a3ee80564020a81776be742 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 28 Apr 2011 19:54:30 +0100 Subject: emitters -> renderers --- examples/pygments_api/views.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'examples/pygments_api') 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.""" -- 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/pygments_api/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/pygments_api') 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 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/pygments_api/views.py | 66 ++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 19 deletions(-) (limited to 'examples/pygments_api') 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) -- 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/pygments_api/views.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'examples/pygments_api') 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. -- cgit v1.2.3