1
|
import os
|
2
|
|
3
|
from django.conf import settings
|
4
|
from django.core.exceptions import SuspiciousOperation
|
5
|
from django.utils._os import safe_join
|
6
|
|
7
|
from django.db import connection
|
8
|
|
9
|
from django.core.files.storage import FileSystemStorage
|
10
|
|
11
|
__all__ = ('TenantFileSystemStorage', )
|
12
|
|
13
|
class TenantFileSystemStorage(FileSystemStorage):
|
14
|
'''Lookup files first in $TENANT_BASE/<tenant.schema>/media/ then in default location'''
|
15
|
def path(self, name):
|
16
|
if connection.tenant:
|
17
|
location = safe_join(settings.TENANT_BASE, connection.tenant.domain_url, 'media')
|
18
|
else:
|
19
|
location = self.location
|
20
|
try:
|
21
|
path = safe_join(location, name)
|
22
|
except ValueError:
|
23
|
raise SuspiciousOperation("Attempted access to '%s' denied." % name)
|
24
|
return os.path.normpath(path)
|