multitenant: redo storage backend by overriding location property (#63725)
gitea-wip/hobo/pipeline/head There was a failure building this commit Details
gitea/hobo/pipeline/head Something is wrong with the build of this commit Details

This commit is contained in:
Frédéric Péters 2022-04-08 14:41:26 +02:00
parent 1f0d890048
commit 46b334579f
1 changed files with 6 additions and 9 deletions

View File

@ -2,6 +2,7 @@ import os
from django.conf import settings
from django.core.exceptions import SuspiciousOperation
from django.core.files import File
from django.core.files.storage import FileSystemStorage
from django.db import connection
from django.utils._os import safe_join
@ -18,15 +19,11 @@ __all__ = ('TenantFileSystemStorage',)
class TenantFileSystemStorage(FileSystemStorage, TenantStorageMixin):
'''Lookup files first in $TENANT_BASE/<tenant.schema>/media/ then in default location'''
'''Lookup files in $TENANT_BASE/<tenant.schema>/media/'''
def path(self, name):
@property
def location(self):
if connection.tenant:
location = safe_join(settings.TENANT_BASE, connection.tenant.domain_url, 'media')
return safe_join(settings.TENANT_BASE, connection.tenant.domain_url, '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)
return os.path.abspath(self.base_location)