aboutsummaryrefslogtreecommitdiffstats
path: root/examples/pygments_api
diff options
context:
space:
mode:
Diffstat (limited to 'examples/pygments_api')
-rw-r--r--examples/pygments_api/models.py1
-rw-r--r--examples/pygments_api/tests.py42
-rw-r--r--examples/pygments_api/views.py13
3 files changed, 51 insertions, 5 deletions
diff --git a/examples/pygments_api/models.py b/examples/pygments_api/models.py
new file mode 100644
index 00000000..402a813e
--- /dev/null
+++ b/examples/pygments_api/models.py
@@ -0,0 +1 @@
+#We need models.py otherwise the test framework complains (http://code.djangoproject.com/ticket/7198)
diff --git a/examples/pygments_api/tests.py b/examples/pygments_api/tests.py
new file mode 100644
index 00000000..76f1afba
--- /dev/null
+++ b/examples/pygments_api/tests.py
@@ -0,0 +1,42 @@
+from django.test import TestCase
+from djangorestframework.compat import RequestFactory
+from pygments_api import views
+import os, tempfile, shutil, time, json
+
+class TestPygmentsExample(TestCase):
+
+ def setUp(self):
+ 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:
+ pass
+
+ def test_get_to_root(self):
+ '''Just do a get on the base url'''
+ request = self.factory.get('/pygments')
+ view = views.PygmentsRoot.as_view()
+ response = view(request)
+ self.assertEqual(response.status_code, 200)
+
+ def test_snippets_datetime_sorted(self):
+ '''Pygments examples should be datetime sorted'''
+ locations = []
+ for snippet in 'abcdefghijk':
+ form_data = {'code': '%s' % snippet, 'style':'friendly', 'lexer':'python'}
+ request = self.factory.post('/pygments', data=form_data)
+ view = views.PygmentsRoot.as_view()
+ response = view(request)
+ locations.append(response.items()[2][1])
+ request = self.factory.get('/pygments')
+ view = views.PygmentsRoot.as_view()
+ response = view(request)
+ response_locations = json.loads(response.content)
+ self.assertEquals(locations, response_locations)
+
+
+ \ No newline at end of file
diff --git a/examples/pygments_api/views.py b/examples/pygments_api/views.py
index ac8b4cfb..91c6045b 100644
--- a/examples/pygments_api/views.py
+++ b/examples/pygments_api/views.py
@@ -21,13 +21,15 @@ import operator
HIGHLIGHTED_CODE_DIR = os.path.join(settings.MEDIA_ROOT, 'pygments')
MAX_FILES = 20
+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)]
+ return [item[0] for item in sorted([(path, os.path.getctime(path)) for path in filepaths],
+ key=operator.itemgetter(1), reverse=True)]
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."""
- filepaths = [os.path.join(dir, file) for file in os.listdir(dir)]
- 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:]]
+ [os.remove(path) for path in list_dir_sorted_by_ctime(dir)[max_files:]]
class HTMLEmitter(BaseEmitter):
@@ -43,7 +45,8 @@ class PygmentsRoot(Resource):
def get(self, request, auth):
"""Return a list of all currently existing snippets."""
- unique_ids = sorted(os.listdir(HIGHLIGHTED_CODE_DIR))
+ unique_ids = [os.path.split(f)[1] for f in list_dir_sorted_by_ctime(HIGHLIGHTED_CODE_DIR)]
+ unique_ids.reverse()
return [reverse('pygments-instance', args=[unique_id]) for unique_id in unique_ids]
def post(self, request, auth, content):