Permalink
Browse files

Made it possible to override the task that stores the file remotely

  • Loading branch information...
1 parent 9399c45 commit 357a07c57ca5a6779d1925ec72ea16ef2fada27f @codekoala committed Oct 28, 2010
Showing with 43 additions and 30 deletions.
  1. +3 −0 .gitignore
  2. +39 −29 queued_storage/backend.py
  3. +1 −1 setup.py
View
@@ -0,0 +1,3 @@
+build/
+dist/
+django_queued_storage.egg-info/
View
@@ -1,4 +1,4 @@
-import urllib
+import urllib
from django.core.cache import cache
from django.core.files.storage import get_storage_class, Storage
@@ -8,69 +8,79 @@
QUEUED_REMOTE_STORAGE_CACHE_KEY_PREFIX = 'queued_remote_storage_'
class QueuedRemoteStorage(Storage):
- def __init__(self, local, remote, cache_prefix=QUEUED_REMOTE_STORAGE_CACHE_KEY_PREFIX):
+
+ def __init__(self, local, remote, cache_prefix=QUEUED_REMOTE_STORAGE_CACHE_KEY_PREFIX, task=None):
self.local_class = local
self.local = get_storage_class(self.local_class)()
self.remote_class = remote
self.remote = get_storage_class(self.remote_class)()
self.cache_prefix = cache_prefix
+ # allow users to override the task that uploads the image to the remote
+ # server
+ self.task = task or SaveToRemoteTask
+
def get_storage(self, name):
cache_result = cache.get(self.get_cache_key(name))
+
if cache_result:
- return self.remote
- elif cache_result is None:
- if self.remote.exists(name):
- cache.set(self.get_cache_key(name), True)
- return self.remote
+ return self.remote
+ elif cache_result is None and self.remote.exists(name):
+ cache.set(self.get_cache_key(name), True)
+ return self.remote
+
return self.local
def get_cache_key(self, name):
return '%s%s' % (self.cache_prefix, urllib.quote(name))
-
+
def using_local(self, name):
return self.get_storage(name) is self.local
-
+
def using_remote(self, name):
return self.get_storage(name) is self.remote
-
+
def open(self, name, **kwargs):
- return self.local.open(name, **kwargs)
-
+ return self.local.open(name, **kwargs)
+
def save(self, name, content):
cache.set(self.get_cache_key(name), False)
name = self.local.save(name, content)
- SaveToRemoteTask.delay(name, self.local_class, self.remote_class, self.get_cache_key(name))
+
+ self.task.delay(name, self.local_class, self.remote_class, self.get_cache_key(name))
+
return name
-
+
def get_valid_name(self, name):
- return self.get_storage(name).get_valid_name(name)
-
+ return self.get_storage(name).get_valid_name(name)
+
def get_available_name(self, name):
- return self.get_storage(name).get_available_name(name)
-
+ return self.get_storage(name).get_available_name(name)
+
def path(self, name):
return self.get_storage(name).path(name)
-
+
def delete(self, name):
return self.get_storage(name).delete(name)
-
+
def exists(self, name):
return self.get_storage(name).exists(name)
-
+
def listdir(self, name):
return self.get_storage(name).listdir(name)
-
+
def size(self, name):
return self.get_storage(name).size(name)
-
+
def url(self, name):
return self.get_storage(name).url(name)
-
-
+
class FileSystemAndS3Backend(QueuedRemoteStorage):
+
def __init__(self, cache_prefix=QUEUED_REMOTE_STORAGE_CACHE_KEY_PREFIX):
- super(FileSystemAndS3Backend, self).__init__(local='django.core.files.storage.FileSystemStorage',
- remote='storages.backends.s3boto.S3BotoStorage', cache_prefix=cache_prefix)
-
-
+ super(FileSystemAndS3Backend, self).__init__(
+ local='django.core.files.storage.FileSystemStorage',
+ remote='storages.backends.s3boto.S3BotoStorage',
+ cache_prefix=cache_prefix
+ )
+
View
@@ -2,7 +2,7 @@
setup(
name='django-queued-storage',
- version='0.1',
+ version='0.2',
description='Provides a proxy for django file storage, that allows you to upload files locally and eventually serve them remotely',
author='Sean Brant',
author_email='brant.sean@gmail.com',

0 comments on commit 357a07c

Please sign in to comment.