diff options
| author | markotibold | 2011-03-09 10:39:45 +0100 |
|---|---|---|
| committer | markotibold | 2011-03-09 10:39:45 +0100 |
| commit | f7f001542a06f1fa816953779684f6b62d6e3908 (patch) | |
| tree | 1194861a8961cdaee35ceccf854bab1bccba29c2 /examples/pygments_api/views.py | |
| parent | f88d8193a8757ce00b91ba81377216559977b519 (diff) | |
| download | django-rest-framework-f7f001542a06f1fa816953779684f6b62d6e3908.tar.bz2 | |
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
Diffstat (limited to 'examples/pygments_api/views.py')
| -rw-r--r-- | examples/pygments_api/views.py | 13 |
1 files changed, 8 insertions, 5 deletions
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): |
