aboutsummaryrefslogtreecommitdiffstats
path: root/examples/objectstore/views.py
diff options
context:
space:
mode:
authorTom Christie2012-02-23 08:58:10 +0000
committerTom Christie2012-02-23 08:58:10 +0000
commit2b59df004a5bb7449aa4c07277ac846c330a79f7 (patch)
tree3a5e20948d3cd0ea1ca4d25ff35d970646722a30 /examples/objectstore/views.py
parent8e0b9e55ecb0733369918d4562ba38ba505cdfe8 (diff)
downloaddjango-rest-framework-2b59df004a5bb7449aa4c07277ac846c330a79f7.tar.bz2
reverse takes request as a kwarg for compatibility with django's reverse
Diffstat (limited to 'examples/objectstore/views.py')
-rw-r--r--examples/objectstore/views.py51
1 files changed, 35 insertions, 16 deletions
diff --git a/examples/objectstore/views.py b/examples/objectstore/views.py
index dd711482..880bd3fc 100644
--- a/examples/objectstore/views.py
+++ b/examples/objectstore/views.py
@@ -28,6 +28,20 @@ def remove_oldest_files(dir, max_files):
[os.remove(path) for path in ctime_sorted_paths[max_files:]]
+def get_filename(key):
+ """
+ Given a stored object's key returns the file's path.
+ """
+ return os.path.join(OBJECT_STORE_DIR, key)
+
+
+def get_file_url(key, request):
+ """
+ Given a stored object's key returns the URL for the object.
+ """
+ return reverse('stored-object', kwargs={'key': key}, request=request)
+
+
class ObjectStoreRoot(View):
"""
Root of the Object Store API.
@@ -38,20 +52,24 @@ class ObjectStoreRoot(View):
"""
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('.')]
+ 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', request, kwargs={'key':key}) for key in ctime_sorted_basenames]
+ return [get_file_url(key, request) for key in ctime_sorted_basenames]
def post(self, request):
"""
Create a new stored object, with a unique key.
"""
key = str(uuid.uuid1())
- pathname = os.path.join(OBJECT_STORE_DIR, key)
- pickle.dump(self.CONTENT, open(pathname, 'wb'))
+ filename = get_filename(key)
+ pickle.dump(self.CONTENT, open(filename, 'wb'))
+
remove_oldest_files(OBJECT_STORE_DIR, MAX_FILES)
- return Response(status.HTTP_201_CREATED, self.CONTENT, {'Location': reverse('stored-object', request, kwargs={'key':key})})
+ url = get_file_url(key, request)
+ return Response(status.HTTP_201_CREATED, self.CONTENT, {'Location': url})
class StoredObject(View):
@@ -59,29 +77,30 @@ class StoredObject(View):
Represents a stored object.
The object may be any picklable content.
"""
-
def get(self, request, key):
"""
- Return a stored object, by unpickling the contents of a locally stored file.
+ Return a stored object, by unpickling the contents of a locally
+ stored file.
"""
- pathname = os.path.join(OBJECT_STORE_DIR, key)
- if not os.path.exists(pathname):
+ filename = get_filename(key)
+ if not os.path.exists(filename):
return Response(status.HTTP_404_NOT_FOUND)
- return pickle.load(open(pathname, 'rb'))
+ return pickle.load(open(filename, 'rb'))
def put(self, request, key):
"""
- Update/create a stored object, by pickling the request content to a locally stored file.
+ Update/create a stored object, by pickling the request content to a
+ locally stored file.
"""
- pathname = os.path.join(OBJECT_STORE_DIR, key)
- pickle.dump(self.CONTENT, open(pathname, 'wb'))
+ filename = get_filename(key)
+ pickle.dump(self.CONTENT, open(filename, 'wb'))
return self.CONTENT
def delete(self, request, key):
"""
Delete a stored object, by removing it's pickled file.
"""
- pathname = os.path.join(OBJECT_STORE_DIR, key)
- if not os.path.exists(pathname):
+ filename = get_filename(key)
+ if not os.path.exists(filename):
return Response(status.HTTP_404_NOT_FOUND)
- os.remove(pathname)
+ os.remove(filename)