bernardopires / django-tenant-schemas

Tenant support for Django using PostgreSQL schemas.
https://django-tenant-schemas.readthedocs.org/en/latest/
MIT License
1.45k stars 424 forks source link

File storage while in development #614

Open durdenk opened 4 years ago

durdenk commented 4 years ago

Hi, When in development environment, I change DEFAULT_FILE_STORAGE to suggested value. I get errors reaching added files since there is no nginx to configure when developing and using django's development server. Am I missing something?

federico-zb commented 4 years ago

Implement your own FileSystemStorage class:

class MyTenantFileSystemStorage(TenantFileSystemStorage):
    def url(self, name):
        if settings.DEBUG:
            if self.base_url is None:
                raise ValueError("This file is not accessible via a URL.")
            url = filepath_to_uri(name)
            if url is not None:
                url = url.lstrip('/')
            return urljoin(self.base_url, os.path.join(connection.tenant.domain_url, url))
        else:
            return super().url(name)

And change DEFAULT_FILE_STORAGE in settings.py.

durdenk commented 4 years ago

Works like a charm, thanks a lot. I think we can close this issue.

Final code :


import os
from urllib.parse import urljoin

from django.conf import settings
from django.db import connection
from django.utils.encoding import filepath_to_uri
from tenant_schemas.storage import TenantFileSystemStorage

class MyTenantFileSystemStorage(TenantFileSystemStorage):
    def url(self, name):
        print ('DEBUG>',settings.DEBUG)
        if settings.DEBUG:
            if self.base_url is None:
                raise ValueError("This file is not accessible via a URL.")
            url = filepath_to_uri(name)
            if url is not None:
                url = url.lstrip('/')
            print (urljoin(self.base_url, os.path.join(connection.tenant.domain_url, url)))
            return urljoin(self.base_url, os.path.join(connection.tenant.domain_url, url))
        else:
            return super().url(name)