mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-03-19 17:34:13 +03:00
Fix some examples
This commit is contained in:
parent
b4a076c822
commit
3acedec771
|
@ -8,8 +8,8 @@ env
|
|||
docs/build
|
||||
html
|
||||
htmlcov
|
||||
examples/media/pygments/*
|
||||
examples/media/objectstore/*
|
||||
examples/media/pygments/[A-Za-z0-9]*
|
||||
examples/media/objectstore/[A-Za-z0-9]*
|
||||
build/*
|
||||
dist/*
|
||||
xmlrunner/*
|
||||
|
|
1
examples/media/objectstore/.keep
Normal file
1
examples/media/objectstore/.keep
Normal file
|
@ -0,0 +1 @@
|
|||
Force media/objectstore directory to created
|
1
examples/media/pygments/.keep
Normal file
1
examples/media/pygments/.keep
Normal file
|
@ -0,0 +1 @@
|
|||
Force media/pygments directory to created
|
|
@ -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."""
|
||||
|
|
|
@ -19,11 +19,11 @@ import operator
|
|||
|
||||
# We need somewhere to store the code that we highlight
|
||||
HIGHLIGHTED_CODE_DIR = os.path.join(settings.MEDIA_ROOT, 'pygments')
|
||||
MAX_FILES = 20
|
||||
MAX_FILES = 10
|
||||
|
||||
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)]
|
||||
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=True)]
|
||||
def remove_oldest_files(dir, max_files):
|
||||
|
@ -46,7 +46,6 @@ class PygmentsRoot(Resource):
|
|||
def get(self, request, auth):
|
||||
"""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)]
|
||||
unique_ids.reverse()
|
||||
return [reverse('pygments-instance', args=[unique_id]) for unique_id in unique_ids]
|
||||
|
||||
def post(self, request, auth, content):
|
||||
|
|
Loading…
Reference in New Issue
Block a user