2011-01-24 02:08:16 +03:00
|
|
|
from django.conf import settings
|
|
|
|
|
2011-01-30 21:30:39 +03:00
|
|
|
from djangorestframework.resource import Resource
|
|
|
|
from djangorestframework.response import Response, status
|
2011-01-24 02:08:16 +03:00
|
|
|
|
|
|
|
import pickle
|
|
|
|
import os
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
OBJECT_STORE_DIR = os.path.join(settings.MEDIA_ROOT, 'objectstore')
|
2011-01-30 19:51:06 +03:00
|
|
|
MAX_FILES = 20
|
|
|
|
|
|
|
|
|
|
|
|
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)]
|
|
|
|
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:]]
|
2011-01-24 02:08:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
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."""
|
2011-01-26 23:31:47 +03:00
|
|
|
allowed_methods = anon_allowed_methods = ('GET', 'POST')
|
2011-01-24 02:08:16 +03:00
|
|
|
|
2011-01-26 23:31:47 +03:00
|
|
|
def get(self, request, auth):
|
2011-01-24 02:08:16 +03:00
|
|
|
"""Return a list of all the stored object URLs."""
|
|
|
|
keys = sorted(os.listdir(OBJECT_STORE_DIR))
|
|
|
|
return [self.reverse(StoredObject, key=key) for key in keys]
|
|
|
|
|
2011-01-26 23:31:47 +03:00
|
|
|
def post(self, request, auth, content):
|
2011-01-24 02:08:16 +03:00
|
|
|
"""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'))
|
2011-01-30 19:51:06 +03:00
|
|
|
remove_oldest_files(OBJECT_STORE_DIR, MAX_FILES)
|
2011-01-24 02:08:16 +03:00
|
|
|
return Response(status.HTTP_201_CREATED, content, {'Location': self.reverse(StoredObject, key=key)})
|
|
|
|
|
|
|
|
|
|
|
|
class StoredObject(Resource):
|
|
|
|
"""Represents a stored object.
|
|
|
|
The object may be any picklable content."""
|
2011-01-26 23:31:47 +03:00
|
|
|
allowed_methods = anon_allowed_methods = ('GET', 'PUT', 'DELETE')
|
2011-01-24 02:08:16 +03:00
|
|
|
|
2011-01-26 23:31:47 +03:00
|
|
|
def get(self, request, auth, key):
|
2011-01-24 02:08:16 +03:00
|
|
|
"""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'))
|
|
|
|
|
2011-01-26 23:31:47 +03:00
|
|
|
def put(self, request, auth, content, key):
|
2011-01-24 02:08:16 +03:00
|
|
|
"""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
|
|
|
|
|
2011-01-26 23:31:47 +03:00
|
|
|
def delete(self, request, auth, key):
|
2011-01-24 02:08:16 +03:00
|
|
|
"""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)
|
|
|
|
os.remove(pathname)
|