Project

General

Profile

0001-Add-tenant-based-storage-handler-fixes-5501.patch

Benjamin Dauvergne, 15 September 2014 09:29 AM

Download (4.58 KB)

View differences:

Subject: [PATCH] Add tenant based storage handler (fixes #5501)

To use it, add this to your settings.py:

  DEFAULT_FILE_STORAGE = 'entrouvert.djommon.multitenant.storage.TenantFileSystemStorage'
 entrouvert/djommon/multitenant/storage.py |   24 ++++++++++++++++++++
 entrouvert/djommon/multitenant/tests.py   |   34 ++++++++++++++++++++++++++---
 2 files changed, 55 insertions(+), 3 deletions(-)
 create mode 100644 entrouvert/djommon/multitenant/storage.py
entrouvert/djommon/multitenant/storage.py
import os
from django.conf import settings
from django.core.exceptions import SuspiciousOperation
from django.utils._os import safe_join
from django.db import connection
from django.core.files.storage import FileSystemStorage
__all__ = ('TenantFileSystemStorage', )
class TenantFileSystemStorage(FileSystemStorage):
'''Lookup files first in $TENANT_BASE/<tenant.schema>/media/ then in default location'''
def path(self, name):
if connection.tenant:
location = safe_join(settings.TENANT_BASE, connection.tenant.schema_name, 'media')
else:
location = self.location
try:
path = safe_join(location, name)
except ValueError:
raise SuspiciousOperation("Attempted access to '%s' denied." % name)
return os.path.normpath(path)
entrouvert/djommon/multitenant/tests.py
import shutil
import os
import json
import StringIO
from django.conf.urls import patterns
from django.test import TestCase, Client
......
def template(request, *args, **kwargs):
return TemplateResponse(request, 'tenant.html')
def upload(request):
from django.core.files.storage import default_storage
default_storage.save('upload', request.FILES['upload'])
return HttpResponse('')
def download(request):
from django.core.files.storage import default_storage
return HttpResponse(default_storage.open('upload').read())
urlpatterns = patterns('',
('^json_key/$', json_key),
('^python_key/$', python_key),
('^template/$', template),
('^upload/$', upload),
('^download/$', download),
)
@override_settings(
......
),
TEMPLATE_LOADERS = (
'entrouvert.djommon.multitenant.template_loader.FilesystemLoader',
)
),
DEFAULT_FILE_STORAGE = 'entrouvert.djommon.multitenant.storage.TenantFileSystemStorage',
)
class SimpleTest(TestCase):
TENANTS = ['tenant1', 'tenant2']
......
tenant_html = os.path.join(templates_dir, 'tenant.html')
with file(tenant_html, 'w') as f:
print >>f, tenant + ' template',
media_dir = os.path.join(tenant_dir, 'media')
os.mkdir(media_dir)
def tearDown(self):
shutil.rmtree(self.tenant_base, ignore_errors=True)
......
domain_url=tenant)) for tenant in self.TENANTS)
self.assertEquals(l1, l2)
def test_storage(self):
from django.core.files.base import ContentFile
with self.tenant_settings():
for tenant in self.TENANTS:
c = Client(HTTP_HOST=tenant)
uploaded_file_path = os.path.join(self.tenant_base, tenant, 'media', 'upload')
self.assertFalse(os.path.exists(uploaded_file_path), uploaded_file_path)
response = c.post('/upload/', {'upload': ContentFile(tenant + ' upload', name='upload.txt')})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, '')
self.assertTrue(os.path.exists(uploaded_file_path))
self.assertEqual(file(uploaded_file_path).read(), tenant + ' upload')
response = c.get('/download/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, tenant + ' upload')