diff options
| author | Tom Christie | 2011-06-02 12:58:10 +0100 |
|---|---|---|
| committer | Tom Christie | 2011-06-02 12:58:10 +0100 |
| commit | b50492853f537a2473bb0a9eea86c8b0ed6b8824 (patch) | |
| tree | d289d39aacf187a8a0696a4c1c863aabe1472c3a /examples/objectstore/views.py | |
| parent | 7ee9adbe5c03c29cd4a894dd476548f7fe73b5e4 (diff) | |
| parent | fc1640de75511006e89f033c9270ec91a9f1e4d4 (diff) | |
| download | django-rest-framework-b50492853f537a2473bb0a9eea86c8b0ed6b8824.tar.bz2 | |
pull in -dev as 0.2.0
Diffstat (limited to 'examples/objectstore/views.py')
| -rw-r--r-- | examples/objectstore/views.py | 64 |
1 files changed, 39 insertions, 25 deletions
diff --git a/examples/objectstore/views.py b/examples/objectstore/views.py index 2e353e08..19999aa9 100644 --- a/examples/objectstore/views.py +++ b/examples/objectstore/views.py @@ -1,7 +1,7 @@ from django.conf import settings from django.core.urlresolvers import reverse -from djangorestframework.resource import Resource +from djangorestframework.views import View from djangorestframework.response import Response from djangorestframework import status @@ -15,55 +15,69 @@ 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.""" + """ + 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) 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_files:]] -class ObjectStoreRoot(Resource): - """Root of the Object Store API. - Allows the client to get a complete list of all the stored objects, or to create a new stored object.""" - allowed_methods = anon_allowed_methods = ('GET', 'POST') +class ObjectStoreRoot(View): + """ + Root of the Object Store API. + Allows the client to get a complete list of all the stored objects, or to create a new stored object. + """ - def get(self, request, auth): - """Return a list of all the stored object URLs. (Ordered by creation time, newest first)""" + def get(self, request): + """ + 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.""" + 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(content, open(pathname, 'wb')) + pickle.dump(self.CONTENT, open(pathname, 'wb')) remove_oldest_files(OBJECT_STORE_DIR, MAX_FILES) - return Response(status.HTTP_201_CREATED, content, {'Location': reverse('stored-object', kwargs={'key':key})}) + return Response(status.HTTP_201_CREATED, self.CONTENT, {'Location': reverse('stored-object', kwargs={'key':key})}) -class StoredObject(Resource): - """Represents a stored object. - The object may be any picklable content.""" - allowed_methods = anon_allowed_methods = ('GET', 'PUT', 'DELETE') +class StoredObject(View): + """ + Represents a stored object. + The object may be any picklable content. + """ - def get(self, request, auth, key): - """Return a stored object, by unpickling the contents of a locally stored file.""" + def get(self, request, key): + """ + 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): return Response(status.HTTP_404_NOT_FOUND) return pickle.load(open(pathname, 'rb')) - def put(self, request, auth, content, key): - """Update/create a stored object, by pickling the request content to a locally stored file.""" + def put(self, request, key): + """ + 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(content, open(pathname, 'wb')) - return content + pickle.dump(self.CONTENT, open(pathname, 'wb')) + return self.CONTENT - def delete(self, request, auth, key): - """Delete a stored object, by removing it's pickled file.""" + def delete(self, request): + """ + Delete a stored object, by removing it's pickled file. + """ pathname = os.path.join(OBJECT_STORE_DIR, key) if not os.path.exists(pathname): return Response(status.HTTP_404_NOT_FOUND) |
