aboutsummaryrefslogtreecommitdiffstats
path: root/examples/objectstore
diff options
context:
space:
mode:
Diffstat (limited to 'examples/objectstore')
-rw-r--r--examples/objectstore/__init__.py0
-rw-r--r--examples/objectstore/models.py3
-rw-r--r--examples/objectstore/tests.py23
-rw-r--r--examples/objectstore/urls.py6
-rw-r--r--examples/objectstore/views.py54
5 files changed, 86 insertions, 0 deletions
diff --git a/examples/objectstore/__init__.py b/examples/objectstore/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/examples/objectstore/__init__.py
diff --git a/examples/objectstore/models.py b/examples/objectstore/models.py
new file mode 100644
index 00000000..71a83623
--- /dev/null
+++ b/examples/objectstore/models.py
@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.
diff --git a/examples/objectstore/tests.py b/examples/objectstore/tests.py
new file mode 100644
index 00000000..2247054b
--- /dev/null
+++ b/examples/objectstore/tests.py
@@ -0,0 +1,23 @@
+"""
+This file demonstrates two different styles of tests (one doctest and one
+unittest). These will both pass when you run "manage.py test".
+
+Replace these with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+class SimpleTest(TestCase):
+ def test_basic_addition(self):
+ """
+ Tests that 1 + 1 always equals 2.
+ """
+ self.failUnlessEqual(1 + 1, 2)
+
+__test__ = {"doctest": """
+Another way to test that 1 + 1 is equal to 2.
+
+>>> 1 + 1 == 2
+True
+"""}
+
diff --git a/examples/objectstore/urls.py b/examples/objectstore/urls.py
new file mode 100644
index 00000000..c04e731e
--- /dev/null
+++ b/examples/objectstore/urls.py
@@ -0,0 +1,6 @@
+from django.conf.urls.defaults import patterns
+
+urlpatterns = patterns('objectstore.views',
+ (r'^$', 'ObjectStoreRoot'),
+ (r'^(?P<key>[A-Za-z0-9_-]{1,64})/$', 'StoredObject'),
+)
diff --git a/examples/objectstore/views.py b/examples/objectstore/views.py
new file mode 100644
index 00000000..16c7f8e9
--- /dev/null
+++ b/examples/objectstore/views.py
@@ -0,0 +1,54 @@
+from django.conf import settings
+
+from flywheel.resource import Resource
+from flywheel.response import Response, status
+
+import pickle
+import os
+import uuid
+
+OBJECT_STORE_DIR = os.path.join(settings.MEDIA_ROOT, 'objectstore')
+
+
+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 = ('GET', 'POST')
+
+ def get(self, request):
+ """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]
+
+ def post(self, request, content):
+ """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'))
+ 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."""
+ allowed_methods = ('GET', 'PUT', 'DELETE')
+
+ 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, content, 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
+
+ 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):
+ return Response(status.HTTP_404_NOT_FOUND)
+ os.remove(pathname)