diff options
| author | Tom Christie | 2012-10-30 14:32:31 +0000 |
|---|---|---|
| committer | Tom Christie | 2012-10-30 14:32:31 +0000 |
| commit | 9b30dab4f772f67a626e176dc4fae0a3ef9c2c81 (patch) | |
| tree | ca138abf4792f58ffa28684f784f201ee1eef6d7 /examples/objectstore/views.py | |
| parent | 7e5b1501b5cede61a9391fb1a751d2ebcdb37031 (diff) | |
| parent | 4e7805cb24d73e7f706318b5e5a27e3f9ba39d14 (diff) | |
| download | django-rest-framework-9b30dab4f772f67a626e176dc4fae0a3ef9c2c81.tar.bz2 | |
Merge branch 'restframework2' into rest-framework-2-merge2.0.0
Conflicts:
.gitignore
.travis.yml
AUTHORS
README.rst
djangorestframework/mixins.py
djangorestframework/renderers.py
djangorestframework/resources.py
djangorestframework/serializer.py
djangorestframework/templates/djangorestframework/base.html
djangorestframework/templates/djangorestframework/login.html
djangorestframework/templatetags/add_query_param.py
djangorestframework/tests/accept.py
djangorestframework/tests/authentication.py
djangorestframework/tests/content.py
djangorestframework/tests/reverse.py
djangorestframework/tests/serializer.py
djangorestframework/views.py
docs/examples.rst
docs/examples/blogpost.rst
docs/examples/modelviews.rst
docs/examples/objectstore.rst
docs/examples/permissions.rst
docs/examples/pygments.rst
docs/examples/views.rst
docs/howto/alternativeframeworks.rst
docs/howto/mixin.rst
docs/howto/reverse.rst
docs/howto/usingurllib2.rst
docs/index.rst
docs/topics/release-notes.md
examples/sandbox/views.py
rest_framework/__init__.py
rest_framework/compat.py
rest_framework/utils/breadcrumbs.py
setup.py
Diffstat (limited to 'examples/objectstore/views.py')
| -rw-r--r-- | examples/objectstore/views.py | 106 |
1 files changed, 0 insertions, 106 deletions
diff --git a/examples/objectstore/views.py b/examples/objectstore/views.py deleted file mode 100644 index 880bd3fc..00000000 --- a/examples/objectstore/views.py +++ /dev/null @@ -1,106 +0,0 @@ -from django.conf import settings - -from djangorestframework.reverse import reverse -from djangorestframework.views import View -from djangorestframework.response import Response -from djangorestframework import status - -import pickle -import os -import uuid -import operator - -OBJECT_STORE_DIR = os.path.join(settings.MEDIA_ROOT, 'objectstore') -MAX_FILES = 10 - -if not os.path.exists(OBJECT_STORE_DIR): - os.makedirs(OBJECT_STORE_DIR) - - -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) 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:]] - - -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. - Allows the client to get a complete list of all the stored objects, or to create a new stored object. - """ - - 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 [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()) - filename = get_filename(key) - pickle.dump(self.CONTENT, open(filename, 'wb')) - - remove_oldest_files(OBJECT_STORE_DIR, MAX_FILES) - url = get_file_url(key, request) - return Response(status.HTTP_201_CREATED, self.CONTENT, {'Location': url}) - - -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. - """ - filename = get_filename(key) - if not os.path.exists(filename): - return Response(status.HTTP_404_NOT_FOUND) - 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. - """ - 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. - """ - filename = get_filename(key) - if not os.path.exists(filename): - return Response(status.HTTP_404_NOT_FOUND) - os.remove(filename) |
