aboutsummaryrefslogtreecommitdiffstats
path: root/examples/pygments_api
diff options
context:
space:
mode:
Diffstat (limited to 'examples/pygments_api')
-rw-r--r--examples/pygments_api/forms.py3
-rw-r--r--examples/pygments_api/tests.py7
-rw-r--r--examples/pygments_api/views.py23
3 files changed, 17 insertions, 16 deletions
diff --git a/examples/pygments_api/forms.py b/examples/pygments_api/forms.py
index 30a59a84..cc147740 100644
--- a/examples/pygments_api/forms.py
+++ b/examples/pygments_api/forms.py
@@ -6,6 +6,7 @@ from pygments.styles import get_all_styles
LEXER_CHOICES = sorted([(item[1][0], item[0]) for item in get_all_lexers()])
STYLE_CHOICES = sorted((item, item) for item in list(get_all_styles()))
+
class PygmentsForm(forms.Form):
"""A simple form with some of the most important pygments settings.
The code to be highlighted can be specified either in a text field, or by URL.
@@ -24,5 +25,3 @@ class PygmentsForm(forms.Form):
initial='python')
style = forms.ChoiceField(choices=STYLE_CHOICES,
initial='friendly')
-
-
diff --git a/examples/pygments_api/tests.py b/examples/pygments_api/tests.py
index 24726647..b728c3c2 100644
--- a/examples/pygments_api/tests.py
+++ b/examples/pygments_api/tests.py
@@ -14,13 +14,13 @@ class TestPygmentsExample(TestCase):
self.factory = RequestFactory()
self.temp_dir = tempfile.mkdtemp()
views.HIGHLIGHTED_CODE_DIR = self.temp_dir
-
+
def tearDown(self):
try:
shutil.rmtree(self.temp_dir)
except Exception:
pass
-
+
def test_get_to_root(self):
'''Just do a get on the base url'''
request = self.factory.get('/pygments')
@@ -44,6 +44,3 @@ class TestPygmentsExample(TestCase):
response = view(request)
response_locations = json.loads(response.content)
self.assertEquals(locations, response_locations)
-
-
-
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()
-