2011-01-24 02:08:16 +03:00
|
|
|
from django.conf import settings
|
2011-02-19 13:26:27 +03:00
|
|
|
from django.core.urlresolvers import reverse
|
2011-01-24 02:08:16 +03:00
|
|
|
|
2011-05-24 13:27:24 +04:00
|
|
|
from djangorestframework.views import View
|
2011-02-19 13:47:26 +03:00
|
|
|
from djangorestframework.response import Response
|
|
|
|
from djangorestframework import status
|
2011-01-24 02:08:16 +03:00
|
|
|
|
|
|
|
import pickle
|
|
|
|
import os
|
|
|
|
import uuid
|
2011-02-07 01:28:03 +03:00
|
|
|
import operator
|
2011-01-24 02:08:16 +03:00
|
|
|
|
|
|
|
OBJECT_STORE_DIR = os.path.join(settings.MEDIA_ROOT, 'objectstore')
|
2011-03-23 01:48:59 +03:00
|
|
|
MAX_FILES = 10
|
2011-01-30 19:51:06 +03:00
|
|
|
|
2011-12-30 19:33:08 +04:00
|
|
|
if not os.path.exists(OBJECT_STORE_DIR):
|
|
|
|
os.makedirs(OBJECT_STORE_DIR)
|
|
|
|
|
2011-01-30 19:51:06 +03:00
|
|
|
|
|
|
|
def remove_oldest_files(dir, max_files):
|
2011-05-16 17:11:36 +04:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
2011-03-23 01:48:59 +03:00
|
|
|
filepaths = [os.path.join(dir, file) for file in os.listdir(dir) if not file.startswith('.')]
|
2011-01-30 19:51:06 +03:00
|
|
|
ctime_sorted_paths = [item[0] for item in sorted([(path, os.path.getctime(path)) for path in filepaths],
|
|
|
|
key=operator.itemgetter(1), reverse=True)]
|
2011-03-23 01:48:59 +03:00
|
|
|
[os.remove(path) for path in ctime_sorted_paths[max_files:]]
|
2011-01-24 02:08:16 +03:00
|
|
|
|
|
|
|
|
2011-05-24 13:27:24 +04:00
|
|
|
class ObjectStoreRoot(View):
|
2011-05-16 17:11:36 +04:00
|
|
|
"""
|
|
|
|
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-24 02:08:16 +03:00
|
|
|
|
2011-05-16 17:11:36 +04:00
|
|
|
def get(self, request):
|
|
|
|
"""
|
|
|
|
Return a list of all the stored object URLs. (Ordered by creation time, newest first)
|
|
|
|
"""
|
2011-03-23 01:48:59 +03:00
|
|
|
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]
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2011-05-16 17:11:36 +04:00
|
|
|
def post(self, request):
|
|
|
|
"""
|
|
|
|
Create a new stored object, with a unique key.
|
|
|
|
"""
|
2011-01-24 02:08:16 +03:00
|
|
|
key = str(uuid.uuid1())
|
|
|
|
pathname = os.path.join(OBJECT_STORE_DIR, key)
|
2011-05-16 17:11:36 +04:00
|
|
|
pickle.dump(self.CONTENT, open(pathname, 'wb'))
|
2011-01-30 19:51:06 +03:00
|
|
|
remove_oldest_files(OBJECT_STORE_DIR, MAX_FILES)
|
2011-05-16 17:11:36 +04:00
|
|
|
return Response(status.HTTP_201_CREATED, self.CONTENT, {'Location': reverse('stored-object', kwargs={'key':key})})
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
|
2011-05-24 13:27:24 +04:00
|
|
|
class StoredObject(View):
|
2011-05-16 17:11:36 +04:00
|
|
|
"""
|
|
|
|
Represents a stored object.
|
|
|
|
The object may be any picklable content.
|
|
|
|
"""
|
2011-01-24 02:08:16 +03:00
|
|
|
|
2011-05-16 17:11:36 +04:00
|
|
|
def get(self, request, key):
|
|
|
|
"""
|
|
|
|
Return a stored object, by unpickling the contents of a locally stored file.
|
|
|
|
"""
|
2011-01-24 02:08:16 +03:00
|
|
|
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-05-16 17:11:36 +04:00
|
|
|
def put(self, request, key):
|
|
|
|
"""
|
|
|
|
Update/create a stored object, by pickling the request content to a locally stored file.
|
|
|
|
"""
|
2011-01-24 02:08:16 +03:00
|
|
|
pathname = os.path.join(OBJECT_STORE_DIR, key)
|
2011-05-16 17:11:36 +04:00
|
|
|
pickle.dump(self.CONTENT, open(pathname, 'wb'))
|
|
|
|
return self.CONTENT
|
2011-01-24 02:08:16 +03:00
|
|
|
|
2011-05-16 17:11:36 +04:00
|
|
|
def delete(self, request):
|
|
|
|
"""
|
|
|
|
Delete a stored object, by removing it's pickled file.
|
|
|
|
"""
|
2011-01-24 02:08:16 +03:00
|
|
|
pathname = os.path.join(OBJECT_STORE_DIR, key)
|
|
|
|
if not os.path.exists(pathname):
|
|
|
|
return Response(status.HTTP_404_NOT_FOUND)
|
|
|
|
os.remove(pathname)
|