tests.py: the response needed json deserialization. I guess this is the default serialization for responses.

views.py: Implements #28 Pygments examples should be datetime sorted
This commit is contained in:
markotibold 2011-03-09 10:39:45 +01:00
parent f88d8193a8
commit f7f001542a
2 changed files with 11 additions and 7 deletions

View File

@ -1,7 +1,7 @@
from django.test import TestCase
from djangorestframework.compat import RequestFactory
from pygments_api import views
import os, tempfile, shutil
import os, tempfile, shutil, time, json
class TestPygmentsExample(TestCase):
@ -35,7 +35,8 @@ class TestPygmentsExample(TestCase):
request = self.factory.get('/pygments')
view = views.PygmentsRoot.as_view()
response = view(request)
self.assertEquals(locations, response.content)
response_locations = json.loads(response.content)
self.assertEquals(locations, response_locations)

View File

@ -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):