aboutsummaryrefslogtreecommitdiffstats
path: root/examples/objectstore
diff options
context:
space:
mode:
authorTom Christie2011-03-22 22:48:59 +0000
committerTom Christie2011-03-22 22:48:59 +0000
commit3acedec771bc2ca2afcbcc99a529ac1b309929f0 (patch)
tree81fe63fb8ac5344d78f037444286de078b11f7d1 /examples/objectstore
parentb4a076c8226223bb650605f4d04609f1f59b5913 (diff)
downloaddjango-rest-framework-3acedec771bc2ca2afcbcc99a529ac1b309929f0.tar.bz2
Fix some examples
Diffstat (limited to 'examples/objectstore')
-rw-r--r--examples/objectstore/views.py14
1 files changed, 8 insertions, 6 deletions
diff --git a/examples/objectstore/views.py b/examples/objectstore/views.py
index 02b469db..2e353e08 100644
--- a/examples/objectstore/views.py
+++ b/examples/objectstore/views.py
@@ -11,16 +11,16 @@ import uuid
import operator
OBJECT_STORE_DIR = os.path.join(settings.MEDIA_ROOT, 'objectstore')
-MAX_FILES = 20
+MAX_FILES = 10
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)]
+ filepaths = [os.path.join(dir, file) for file in os.listdir(dir) if not file.startswith('.')]
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_file:]]
+ [os.remove(path) for path in ctime_sorted_paths[max_files:]]
class ObjectStoreRoot(Resource):
@@ -29,9 +29,11 @@ class ObjectStoreRoot(Resource):
allowed_methods = anon_allowed_methods = ('GET', 'POST')
def get(self, request, auth):
- """Return a list of all the stored object URLs."""
- keys = sorted(os.listdir(OBJECT_STORE_DIR))
- return [reverse('stored-object', kwargs={'key':key}) for key in keys]
+ """Return a list of all the stored object URLs. (Ordered by creation time, newest first)"""
+ filepaths = [os.path.join(OBJECT_STORE_DIR, file) for file in os.listdir(OBJECT_STORE_DIR) if not file.startswith('.')]
+ ctime_sorted_basenames = [item[0] for item in sorted([(os.path.basename(path), os.path.getctime(path)) for path in filepaths],
+ key=operator.itemgetter(1), reverse=True)]
+ return [reverse('stored-object', kwargs={'key':key}) for key in ctime_sorted_basenames]
def post(self, request, auth, content):
"""Create a new stored object, with a unique key."""