From 2cdff1b01e3aca6c56cef433e786e3ae75362739 Mon Sep 17 00:00:00 2001 From: Sébastien Piquemal Date: Tue, 7 Feb 2012 16:52:15 +0200 Subject: modified examples, somethin' still broken, can't find what --- examples/pygments_api/views.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'examples/pygments_api/views.py') diff --git a/examples/pygments_api/views.py b/examples/pygments_api/views.py index ffea60ae..44dd2caa 100644 --- a/examples/pygments_api/views.py +++ b/examples/pygments_api/views.py @@ -61,7 +61,7 @@ class PygmentsRoot(View): Return a list of all currently existing snippets. """ unique_ids = [os.path.split(f)[1] for f in list_dir_sorted_by_ctime(HIGHLIGHTED_CODE_DIR)] - return [reverse('pygments-instance', args=[unique_id]) for unique_id in unique_ids] + return Response([reverse('pygments-instance', args=[unique_id]) for unique_id in unique_ids]) def post(self, request): """ @@ -98,7 +98,7 @@ class PygmentsInstance(View): pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id) if not os.path.exists(pathname): return Response(status.HTTP_404_NOT_FOUND) - return open(pathname, 'r').read() + return Response(open(pathname, 'r').read()) def delete(self, request, unique_id): """ @@ -107,5 +107,5 @@ class PygmentsInstance(View): pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id) if not os.path.exists(pathname): return Response(status.HTTP_404_NOT_FOUND) - return os.remove(pathname) + return Response(os.remove(pathname)) -- cgit v1.2.3 From b33579a7a18c2cbc6e3789d4a7dc78c82fb0fe80 Mon Sep 17 00:00:00 2001 From: Sébastien Piquemal Date: Fri, 10 Feb 2012 11:05:20 +0200 Subject: attempt at fixing the examples --- examples/pygments_api/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'examples/pygments_api/views.py') diff --git a/examples/pygments_api/views.py b/examples/pygments_api/views.py index 44dd2caa..d59a52c0 100644 --- a/examples/pygments_api/views.py +++ b/examples/pygments_api/views.py @@ -81,7 +81,8 @@ class PygmentsRoot(View): remove_oldest_files(HIGHLIGHTED_CODE_DIR, MAX_FILES) - return Response(status.HTTP_201_CREATED, headers={'Location': reverse('pygments-instance', args=[unique_id])}) + self.headers['Location'] = reverse('pygments-instance', args=[unique_id]) + return Response(status.HTTP_201_CREATED) class PygmentsInstance(View): -- cgit v1.2.3 From 821844bb11e5262fb0dfc2fecf2add8fe18d3210 Mon Sep 17 00:00:00 2001 From: Sébastien Piquemal Date: Tue, 14 Feb 2012 10:05:28 +0200 Subject: fixed examples, corrected small bugs in the process --- examples/pygments_api/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples/pygments_api/views.py') diff --git a/examples/pygments_api/views.py b/examples/pygments_api/views.py index d59a52c0..852b6730 100644 --- a/examples/pygments_api/views.py +++ b/examples/pygments_api/views.py @@ -82,7 +82,7 @@ class PygmentsRoot(View): remove_oldest_files(HIGHLIGHTED_CODE_DIR, MAX_FILES) self.headers['Location'] = reverse('pygments-instance', args=[unique_id]) - return Response(status.HTTP_201_CREATED) + return Response(status=status.HTTP_201_CREATED) class PygmentsInstance(View): @@ -90,7 +90,7 @@ 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,) + renderer_classes = (HTMLRenderer,) def get(self, request, unique_id): """ -- cgit v1.2.3 From af9e4f69d732cc643d6ec7ae13d4a19ac0332d44 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 21 Feb 2012 20:12:14 +0000 Subject: Merging master into develop --- examples/pygments_api/views.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'examples/pygments_api/views.py') diff --git a/examples/pygments_api/views.py b/examples/pygments_api/views.py index 852b6730..bca3dac6 100644 --- a/examples/pygments_api/views.py +++ b/examples/pygments_api/views.py @@ -1,10 +1,10 @@ from __future__ import with_statement # for python 2.5 from django.conf import settings -from django.core.urlresolvers import reverse from djangorestframework.resources import FormResource from djangorestframework.response import Response from djangorestframework.renderers import BaseRenderer +from djangorestframework.utils import reverse from djangorestframework.views import View from djangorestframework import status @@ -61,7 +61,7 @@ class PygmentsRoot(View): Return a list of all currently existing snippets. """ unique_ids = [os.path.split(f)[1] for f in list_dir_sorted_by_ctime(HIGHLIGHTED_CODE_DIR)] - return Response([reverse('pygments-instance', args=[unique_id]) for unique_id in unique_ids]) + return Response([reverse('pygments-instance', request, args=[unique_id]) for unique_id in unique_ids]) def post(self, request): """ @@ -81,8 +81,8 @@ class PygmentsRoot(View): remove_oldest_files(HIGHLIGHTED_CODE_DIR, MAX_FILES) - self.headers['Location'] = reverse('pygments-instance', args=[unique_id]) - return Response(status=status.HTTP_201_CREATED) + location = reverse('pygments-instance', request, args=[unique_id]) + return Response(status=status.HTTP_201_CREATED, headers={'Location': location}) class PygmentsInstance(View): @@ -98,7 +98,7 @@ class PygmentsInstance(View): """ pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id) if not os.path.exists(pathname): - return Response(status.HTTP_404_NOT_FOUND) + return Response(status=status.HTTP_404_NOT_FOUND) return Response(open(pathname, 'r').read()) def delete(self, request, unique_id): @@ -107,6 +107,7 @@ class PygmentsInstance(View): """ pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id) if not os.path.exists(pathname): - return Response(status.HTTP_404_NOT_FOUND) - return Response(os.remove(pathname)) + return Response(status=status.HTTP_404_NOT_FOUND) + os.remove(pathname) + return Response() -- cgit v1.2.3 From 5fd4c639d7c64572dd07dc31dcd627bed9469b05 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 21 Feb 2012 20:57:36 +0000 Subject: Merge master into develop --- examples/pygments_api/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/pygments_api/views.py') diff --git a/examples/pygments_api/views.py b/examples/pygments_api/views.py index bca3dac6..75d36fea 100644 --- a/examples/pygments_api/views.py +++ b/examples/pygments_api/views.py @@ -4,7 +4,7 @@ from django.conf import settings from djangorestframework.resources import FormResource from djangorestframework.response import Response from djangorestframework.renderers import BaseRenderer -from djangorestframework.utils import reverse +from djangorestframework.reverse import reverse from djangorestframework.views import View from djangorestframework import status -- cgit v1.2.3 From 1cde31c86d9423e9b7a7409c2ef2ba7c0500e47f Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Sat, 25 Feb 2012 18:45:17 +0000 Subject: Massive merge --- examples/pygments_api/views.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'examples/pygments_api/views.py') diff --git a/examples/pygments_api/views.py b/examples/pygments_api/views.py index 75d36fea..a3812ef4 100644 --- a/examples/pygments_api/views.py +++ b/examples/pygments_api/views.py @@ -1,7 +1,6 @@ from __future__ import with_statement # for python 2.5 from django.conf import settings -from djangorestframework.resources import FormResource from djangorestframework.response import Response from djangorestframework.renderers import BaseRenderer from djangorestframework.reverse import reverse @@ -30,9 +29,13 @@ def list_dir_sorted_by_ctime(dir): """ 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) ] + filepaths = [os.path.join(dir, file) + for file in os.listdir(dir) + if not file.startswith('.')] + ctimes = [(path, os.path.getctime(path)) for path in filepaths] + ctimes = sorted(ctimes, key=operator.itemgetter(1), reverse=False) + return [filepath for filepath, ctime in ctimes] + def remove_oldest_files(dir, max_files): """ @@ -60,8 +63,11 @@ class PygmentsRoot(View): """ Return a list of all currently existing snippets. """ - unique_ids = [os.path.split(f)[1] for f in list_dir_sorted_by_ctime(HIGHLIGHTED_CODE_DIR)] - return Response([reverse('pygments-instance', request, args=[unique_id]) for unique_id in unique_ids]) + unique_ids = [os.path.split(f)[1] + for f in list_dir_sorted_by_ctime(HIGHLIGHTED_CODE_DIR)] + urls = [reverse('pygments-instance', args=[unique_id], request=request) + for unique_id in unique_ids] + return Response(urls) def post(self, request): """ @@ -81,7 +87,7 @@ class PygmentsRoot(View): remove_oldest_files(HIGHLIGHTED_CODE_DIR, MAX_FILES) - location = reverse('pygments-instance', request, args=[unique_id]) + location = reverse('pygments-instance', args=[unique_id], request=request) return Response(status=status.HTTP_201_CREATED, headers={'Location': location}) @@ -90,7 +96,7 @@ 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. """ - renderer_classes = (HTMLRenderer,) + renderers = (HTMLRenderer, ) def get(self, request, unique_id): """ @@ -110,4 +116,3 @@ class PygmentsInstance(View): return Response(status=status.HTTP_404_NOT_FOUND) os.remove(pathname) return Response() - -- cgit v1.2.3 From eea2aa04378d27d79e7aba12ce95c697148bd57e Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 29 Aug 2012 19:54:38 +0100 Subject: Remove examples (to be moved to a seperate project) --- examples/pygments_api/views.py | 118 ----------------------------------------- 1 file changed, 118 deletions(-) delete mode 100644 examples/pygments_api/views.py (limited to 'examples/pygments_api/views.py') diff --git a/examples/pygments_api/views.py b/examples/pygments_api/views.py deleted file mode 100644 index a3812ef4..00000000 --- a/examples/pygments_api/views.py +++ /dev/null @@ -1,118 +0,0 @@ -from __future__ import with_statement # for python 2.5 -from django.conf import settings - -from djangorestframework.response import Response -from djangorestframework.renderers import BaseRenderer -from djangorestframework.reverse import reverse -from djangorestframework.views import View -from djangorestframework import status - -from pygments.formatters import HtmlFormatter -from pygments.lexers import get_lexer_by_name -from pygments import highlight - -from forms import PygmentsForm - -import os -import uuid -import operator - -# We need somewhere to store the code snippets that we highlight -HIGHLIGHTED_CODE_DIR = os.path.join(settings.MEDIA_ROOT, 'pygments') -MAX_FILES = 10 - -if not os.path.exists(HIGHLIGHTED_CODE_DIR): - os.makedirs(HIGHLIGHTED_CODE_DIR) - - -def list_dir_sorted_by_ctime(dir): - """ - 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('.')] - ctimes = [(path, os.path.getctime(path)) for path in filepaths] - ctimes = sorted(ctimes, key=operator.itemgetter(1), reverse=False) - return [filepath for filepath, ctime in ctimes] - - -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. - """ - [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. - """ - media_type = 'text/html' - - -class PygmentsRoot(View): - """ - This example demonstrates a simple RESTful Web API around 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 - - 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)] - urls = [reverse('pygments-instance', args=[unique_id], request=request) - for unique_id in unique_ids] - return Response(urls) - - 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(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(self.CONTENT['code'], lexer, formatter, outfile) - - remove_oldest_files(HIGHLIGHTED_CODE_DIR, MAX_FILES) - - location = reverse('pygments-instance', args=[unique_id], request=request) - return Response(status=status.HTTP_201_CREATED, headers={'Location': location}) - - -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, unique_id): - """ - Return the highlighted snippet. - """ - pathname = os.path.join(HIGHLIGHTED_CODE_DIR, unique_id) - if not os.path.exists(pathname): - return Response(status=status.HTTP_404_NOT_FOUND) - return Response(open(pathname, 'r').read()) - - 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 Response(status=status.HTTP_404_NOT_FOUND) - os.remove(pathname) - return Response() -- cgit v1.2.3