<commit_before>import os

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db import connection
from django.utils.functional import cached_property

from django_tenants import utils
from django_tenants.files.storage import TenantFileSystemStorage


class TenantStaticFilesStorage(TenantFileSystemStorage):
    """
    Implementation that extends core Django's StaticFilesStorage for multi-tenant setups.

    The defaults for ``location`` and ``base_url`` are ``STATIC_ROOT`` and ``STATIC_URL``.

    """
    def _clear_cached_properties(self, setting, **kwargs):
        """Reset setting based property values."""
        super()._clear_cached_properties(settings, **kwargs)

        if setting == 'MULTITENANT_RELATIVE_STATIC_ROOT':
            self.__dict__.pop('relative_static_root', None)

    @cached_property
    def relative_static_root(self):
        try:
            return os.path.join(settings.STATIC_ROOT, settings.MULTITENANT_RELATIVE_STATIC_ROOT)
        except AttributeError:
            # MULTITENANT_RELATIVE_STATIC_ROOT is an optional setting. Use the default of just appending
            # the tenant schema_name to STATIC_ROOT if no configuration value is provided
            try:
                return settings.STATIC_ROOT
            except AttributeError:
                raise ImproperlyConfigured("You're using the staticfiles app "
                                           "without having set the STATIC_ROOT "
                                           "setting to a filesystem path.")

    @property
    def relative_static_url(self):
        url = settings.STATIC_URL
        if not url.endswith('/'):
            url += '/'

        try:
            if settings.REWRITE_STATIC_URLS is True:
                try:
                    multitenant_relative_url = settings.MULTITENANT_RELATIVE_STATIC_ROOT
                except AttributeError:
                    # MULTITENANT_RELATIVE_STATIC_ROOT is an optional setting. Use the default of just appending
                    # the tenant schema_name to STATIC_ROOT if no configuration value is provided
                    multitenant_relative_url = "%s"

                multitenant_relative_url = multitenant_relative_url % connection.schema_name
                return "{}{}".format(url, multitenant_relative_url)

        except AttributeError:
            # REWRITE_STATIC_URLS not set - ignore
            pass

        return url

    @property  # Not cached like in parent class
    def base_location(self):
        return utils.parse_tenant_config_path(self.relative_static_root)

    @property  # Not cached like in parent class
    def base_url(self):
        url = self.relative_static_url

        if not url.endswith('/'):
            url += '/'

        return url<commit_msg>Cache relative static URL.<commit_after>import os

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db import connection
from django.utils.functional import cached_property

from django_tenants import utils
from django_tenants.files.storage import TenantFileSystemStorage


class TenantStaticFilesStorage(TenantFileSystemStorage):
    """
    Implementation that extends core Django's StaticFilesStorage for multi-tenant setups.

    The defaults for ``location`` and ``base_url`` are ``STATIC_ROOT`` and ``STATIC_URL``.

    """
    def _clear_cached_properties(self, setting, **kwargs):
        """Reset setting based property values."""
        super()._clear_cached_properties(settings, **kwargs)

        if setting == 'MULTITENANT_RELATIVE_STATIC_ROOT':
            self.__dict__.pop('relative_static_root', None)

    @cached_property
    def relative_static_root(self):
        try:
            return os.path.join(settings.STATIC_ROOT, settings.MULTITENANT_RELATIVE_STATIC_ROOT)
        except AttributeError:
            # MULTITENANT_RELATIVE_STATIC_ROOT is an optional setting. Use the default of just appending
            # the tenant schema_name to STATIC_ROOT if no configuration value is provided
            try:
                return settings.STATIC_ROOT
            except AttributeError:
                raise ImproperlyConfigured("You're using the staticfiles app "
                                           "without having set the STATIC_ROOT "
                                           "setting to a filesystem path.")

    @cached_property
    def relative_static_url(self):
        url = settings.STATIC_URL
        if not url.endswith('/'):
            url += '/'

        try:
            if settings.REWRITE_STATIC_URLS is True:
                try:
                    multitenant_relative_url = settings.MULTITENANT_RELATIVE_STATIC_ROOT
                except AttributeError:
                    # MULTITENANT_RELATIVE_STATIC_ROOT is an optional setting. Use the default of just appending
                    # the tenant schema_name to STATIC_ROOT if no configuration value is provided
                    multitenant_relative_url = "%s"

                return "{}{}".format(url, multitenant_relative_url)

        except AttributeError:
            # REWRITE_STATIC_URLS not set - ignore
            pass

        return url

    @property  # Not cached like in parent class
    def base_location(self):
        return utils.parse_tenant_config_path(self.relative_static_root)

    @property  # Not cached like in parent class
    def base_url(self):
        url = self.relative_static_url % connection.schema_name

        if not url.endswith('/'):
            url += '/'

        return url
{'fraction_non_alphanumeric': 0.06381461675579322, 'fraction_numerical': 0.0, 'mean_word_length': 2.3760529482551145, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3295724', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1382, 'n_words': 402}